|
| 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 | +# Run install.py to install headers. |
| 14 | +def generate_headers(headers_dir, install_args): |
| 15 | + print('Generating headers') |
| 16 | + subprocess.check_call([ |
| 17 | + sys.executable, |
| 18 | + os.path.join(ROOT_DIR, 'tools/install.py'), |
| 19 | + 'install', |
| 20 | + '--silent', |
| 21 | + '--headers-only', |
| 22 | + '--prefix', '/', |
| 23 | + '--dest-dir', headers_dir, |
| 24 | + ] + install_args) |
| 25 | + |
| 26 | +# Rebuild addons in parallel. |
| 27 | +def rebuild_addons(args): |
| 28 | + headers_dir = os.path.abspath(args.headers_dir) |
| 29 | + out_dir = os.path.abspath(args.out_dir) |
| 30 | + node_bin = os.path.join(out_dir, 'node') |
| 31 | + if args.is_win: |
| 32 | + node_bin += '.exe' |
| 33 | + |
| 34 | + if os.path.isabs(args.node_gyp): |
| 35 | + node_gyp = args.node_gyp |
| 36 | + else: |
| 37 | + node_gyp = os.path.join(ROOT_DIR, args.node_gyp) |
| 38 | + |
| 39 | + # Copy node.lib. |
| 40 | + if args.is_win: |
| 41 | + node_lib_dir = os.path.join(headers_dir, 'Release') |
| 42 | + os.makedirs(node_lib_dir) |
| 43 | + shutil.copy2(os.path.join(args.out_dir, 'node.lib'), |
| 44 | + os.path.join(node_lib_dir, 'node.lib')) |
| 45 | + |
| 46 | + def node_gyp_rebuild(test_dir): |
| 47 | + print('Building addon in', test_dir) |
| 48 | + try: |
| 49 | + process = subprocess.Popen([ |
| 50 | + node_bin, |
| 51 | + node_gyp, |
| 52 | + 'rebuild', |
| 53 | + '--directory', test_dir, |
| 54 | + '--nodedir', headers_dir, |
| 55 | + '--python', sys.executable, |
| 56 | + '--loglevel', args.loglevel, |
| 57 | + ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 58 | + |
| 59 | + # We buffer the output and print it out once the process is done in order |
| 60 | + # to avoid interleaved output from multiple builds running at once. |
| 61 | + return_code = process.wait() |
| 62 | + stdout, stderr = process.communicate() |
| 63 | + if return_code != 0: |
| 64 | + print(f'Failed to build addon in {test_dir}:') |
| 65 | + if stdout: |
| 66 | + print(stdout.decode()) |
| 67 | + if stderr: |
| 68 | + print(stderr.decode()) |
| 69 | + |
| 70 | + except Exception as e: |
| 71 | + print(f'Unexpected error when building addon in {test_dir}. Error: {e}') |
| 72 | + |
| 73 | + test_dirs = [] |
| 74 | + skip_tests = args.skip_tests.split(',') |
| 75 | + only_tests = args.only_tests.split(',') if args.only_tests else None |
| 76 | + for child_dir in os.listdir(args.target): |
| 77 | + full_path = os.path.join(args.target, child_dir) |
| 78 | + if not os.path.isdir(full_path): |
| 79 | + continue |
| 80 | + if 'binding.gyp' not in os.listdir(full_path): |
| 81 | + continue |
| 82 | + if child_dir in skip_tests: |
| 83 | + continue |
| 84 | + if only_tests and child_dir not in only_tests: |
| 85 | + continue |
| 86 | + test_dirs.append(full_path) |
| 87 | + |
| 88 | + with ThreadPoolExecutor() as executor: |
| 89 | + executor.map(node_gyp_rebuild, test_dirs) |
| 90 | + |
| 91 | +def get_default_out_dir(args): |
| 92 | + default_out_dir = os.path.join('out', 'Release') |
| 93 | + if not args.is_win: |
| 94 | + # POSIX platforms only have one out dir. |
| 95 | + return default_out_dir |
| 96 | + # On Windows depending on the args of GYP and configure script, the out dir |
| 97 | + # could be 'out/Release' or just 'Release'. |
| 98 | + if os.path.exists(default_out_dir): |
| 99 | + return default_out_dir |
| 100 | + if os.path.exists('Release'): |
| 101 | + return 'Release' |
| 102 | + raise RuntimeError('Can not find out dir, did you run configure?') |
| 103 | + |
| 104 | +def main(): |
| 105 | + if sys.platform == 'cygwin': |
| 106 | + raise RuntimeError('This script does not support running with cygwin python.') |
| 107 | + |
| 108 | + parser = argparse.ArgumentParser( |
| 109 | + description='Install headers and rebuild child directories') |
| 110 | + parser.add_argument('target', help='target directory to build addons') |
| 111 | + parser.add_argument('--headers-dir', |
| 112 | + help='path to node headers directory, if not specified ' |
| 113 | + 'new headers will be generated for building', |
| 114 | + default=None) |
| 115 | + parser.add_argument('--out-dir', help='path to the output directory', |
| 116 | + default=None) |
| 117 | + parser.add_argument('--loglevel', help='loglevel of node-gyp', |
| 118 | + default='silent') |
| 119 | + parser.add_argument('--skip-tests', help='skip building tests', |
| 120 | + default='') |
| 121 | + parser.add_argument('--only-tests', help='only build tests in the list', |
| 122 | + default='') |
| 123 | + parser.add_argument('--node-gyp', help='path to node-gyp script', |
| 124 | + default='deps/npm/node_modules/node-gyp/bin/node-gyp.js') |
| 125 | + parser.add_argument('--is-win', help='build for Windows target', |
| 126 | + action='store_true', default=(sys.platform == 'win32')) |
| 127 | + args, unknown_args = parser.parse_known_args() |
| 128 | + |
| 129 | + if not args.out_dir: |
| 130 | + args.out_dir = get_default_out_dir(args) |
| 131 | + |
| 132 | + if args.headers_dir: |
| 133 | + rebuild_addons(args) |
| 134 | + else: |
| 135 | + # When --headers-dir is not specified, generate headers into a temp dir and |
| 136 | + # build with the new headers. |
| 137 | + try: |
| 138 | + args.headers_dir = tempfile.mkdtemp() |
| 139 | + generate_headers(args.headers_dir, unknown_args) |
| 140 | + rebuild_addons(args) |
| 141 | + finally: |
| 142 | + shutil.rmtree(args.headers_dir) |
| 143 | + |
| 144 | +if __name__ == '__main__': |
| 145 | + main() |
0 commit comments