|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import tempfile |
| 9 | +from concurrent.futures import ThreadPoolExecutor |
| 10 | + |
| 11 | +ROOT_DIR = os.path.abspath(os.path.join(__file__, '..', '..')) |
| 12 | + |
| 13 | +def install_and_rebuild(args, install_args): |
| 14 | + out_dir = os.path.abspath(args.out_dir) |
| 15 | + node_bin = os.path.join(out_dir, 'node') |
| 16 | + if args.is_win: |
| 17 | + node_bin += '.exe' |
| 18 | + |
| 19 | + if os.path.isabs(args.node_gyp): |
| 20 | + node_gyp = args.node_gyp |
| 21 | + else: |
| 22 | + node_gyp = os.path.join(ROOT_DIR, args.node_gyp) |
| 23 | + |
| 24 | + # Create a temporary directory for node headers. |
| 25 | + with tempfile.TemporaryDirectory() as headers_dir: |
| 26 | + # Run install.py to install headers. |
| 27 | + print('Generating headers') |
| 28 | + subprocess.check_call([ |
| 29 | + sys.executable, |
| 30 | + os.path.join(ROOT_DIR, 'tools/install.py'), |
| 31 | + 'install', |
| 32 | + '--silent', |
| 33 | + '--headers-only', |
| 34 | + '--prefix', '/', |
| 35 | + '--dest-dir', headers_dir, |
| 36 | + ] + install_args) |
| 37 | + |
| 38 | + # Copy node.lib. |
| 39 | + if args.is_win: |
| 40 | + node_lib_dir = os.path.join(headers_dir, 'Release') |
| 41 | + os.makedirs(node_lib_dir) |
| 42 | + shutil.copy2(os.path.join(args.out_dir, 'node.lib'), |
| 43 | + os.path.join(node_lib_dir, 'node.lib')) |
| 44 | + |
| 45 | + def rebuild_addon(test_dir): |
| 46 | + print('Building addon in', test_dir) |
| 47 | + try: |
| 48 | + process = subprocess.Popen([ |
| 49 | + node_bin, |
| 50 | + node_gyp, |
| 51 | + 'rebuild', |
| 52 | + '--directory', test_dir, |
| 53 | + '--nodedir', headers_dir, |
| 54 | + '--python', sys.executable, |
| 55 | + '--loglevel', args.loglevel, |
| 56 | + ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 57 | + |
| 58 | + # We buffer the output and print it out once the process is done in order |
| 59 | + # to avoid interleaved output from multiple builds running at once. |
| 60 | + return_code = process.wait() |
| 61 | + stdout, stderr = process.communicate() |
| 62 | + if return_code != 0: |
| 63 | + print(f'Failed to build addon in {test_dir}:') |
| 64 | + if stdout: |
| 65 | + print(stdout.decode()) |
| 66 | + if stderr: |
| 67 | + print(stderr.decode()) |
| 68 | + |
| 69 | + except Exception as e: |
| 70 | + print(f'Unexpected error when building addon in {test_dir}. Error: {e}') |
| 71 | + |
| 72 | + test_dirs = [] |
| 73 | + skip_tests = args.skip_tests.split(',') |
| 74 | + only_tests = args.only_tests.split(',') if args.only_tests else None |
| 75 | + for child_dir in os.listdir(args.target): |
| 76 | + full_path = os.path.join(args.target, child_dir) |
| 77 | + if not os.path.isdir(full_path): |
| 78 | + continue |
| 79 | + if 'binding.gyp' not in os.listdir(full_path): |
| 80 | + continue |
| 81 | + if child_dir in skip_tests: |
| 82 | + continue |
| 83 | + if only_tests and child_dir not in only_tests: |
| 84 | + continue |
| 85 | + test_dirs.append(full_path) |
| 86 | + |
| 87 | + with ThreadPoolExecutor() as executor: |
| 88 | + executor.map(rebuild_addon, test_dirs) |
| 89 | + |
| 90 | +def main(): |
| 91 | + if sys.platform == 'cygwin': |
| 92 | + raise RuntimeError('This script does not support running with cygwin python.') |
| 93 | + |
| 94 | + parser = argparse.ArgumentParser( |
| 95 | + description='Install headers and rebuild child directories') |
| 96 | + parser.add_argument('target', help='target directory to build addons') |
| 97 | + parser.add_argument('--out-dir', help='path to the output directory', |
| 98 | + default='out/Release') |
| 99 | + parser.add_argument('--loglevel', help='loglevel of node-gyp', |
| 100 | + default='silent') |
| 101 | + parser.add_argument('--skip-tests', help='skip building tests', |
| 102 | + default='') |
| 103 | + parser.add_argument('--only-tests', help='only build tests in the list', |
| 104 | + default='') |
| 105 | + parser.add_argument('--node-gyp', help='path to node-gyp script', |
| 106 | + default='deps/npm/node_modules/node-gyp/bin/node-gyp.js') |
| 107 | + parser.add_argument('--is-win', help='build for Windows target', |
| 108 | + action='store_true', default=(sys.platform == 'win32')) |
| 109 | + args, unknown_args = parser.parse_known_args() |
| 110 | + |
| 111 | + install_and_rebuild(args, unknown_args) |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + main() |
0 commit comments