-
Notifications
You must be signed in to change notification settings - Fork 30.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build,tools: make addons tests work with GN
- Loading branch information
Showing
10 changed files
with
477 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import os | ||
import shutil | ||
import subprocess | ||
import sys | ||
import tempfile | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
ROOT_DIR = os.path.abspath(os.path.join(__file__, '..', '..')) | ||
|
||
def install_and_rebuild(args, install_args): | ||
out_dir = os.path.abspath(args.out_dir) | ||
node_bin = os.path.join(out_dir, 'node') | ||
if args.is_win: | ||
node_bin += '.exe' | ||
|
||
if os.path.isabs(args.node_gyp): | ||
node_gyp = args.node_gyp | ||
else: | ||
node_gyp = os.path.join(ROOT_DIR, args.node_gyp) | ||
|
||
# Create a temporary directory for node headers. | ||
with tempfile.TemporaryDirectory() as headers_dir: | ||
# Run install.py to install headers. | ||
print('Generating headers') | ||
subprocess.check_call([ | ||
sys.executable, | ||
os.path.join(ROOT_DIR, 'tools/install.py'), | ||
'install', | ||
'--silent', | ||
'--headers-only', | ||
'--prefix', '/', | ||
'--dest-dir', headers_dir, | ||
] + install_args) | ||
|
||
# Copy node.lib. | ||
if args.is_win: | ||
node_lib_dir = os.path.join(headers_dir, 'Release') | ||
os.makedirs(node_lib_dir) | ||
shutil.copy2(os.path.join(args.out_dir, 'node.lib'), | ||
os.path.join(node_lib_dir, 'node.lib')) | ||
|
||
def rebuild_addon(test_dir): | ||
print('Building addon in', test_dir) | ||
try: | ||
process = subprocess.Popen([ | ||
node_bin, | ||
node_gyp, | ||
'rebuild', | ||
'--directory', test_dir, | ||
'--nodedir', headers_dir, | ||
'--python', sys.executable, | ||
'--loglevel', args.loglevel, | ||
], stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
|
||
# We buffer the output and print it out once the process is done in order | ||
# to avoid interleaved output from multiple builds running at once. | ||
return_code = process.wait() | ||
stdout, stderr = process.communicate() | ||
if return_code != 0: | ||
print(f'Failed to build addon in {test_dir}:') | ||
if stdout: | ||
print(stdout.decode()) | ||
if stderr: | ||
print(stderr.decode()) | ||
|
||
except Exception as e: | ||
print(f'Unexpected error when building addon in {test_dir}. Error: {e}') | ||
|
||
test_dirs = [] | ||
skip_tests = args.skip_tests.split(',') | ||
only_tests = args.only_tests.split(',') if args.only_tests else None | ||
for child_dir in os.listdir(args.target): | ||
full_path = os.path.join(args.target, child_dir) | ||
if not os.path.isdir(full_path): | ||
continue | ||
if 'binding.gyp' not in os.listdir(full_path): | ||
continue | ||
if child_dir in skip_tests: | ||
continue | ||
if only_tests and child_dir not in only_tests: | ||
continue | ||
test_dirs.append(full_path) | ||
|
||
with ThreadPoolExecutor() as executor: | ||
executor.map(rebuild_addon, test_dirs) | ||
|
||
def main(): | ||
if sys.platform == 'cygwin': | ||
raise RuntimeError('This script does not support running with cygwin python.') | ||
|
||
parser = argparse.ArgumentParser( | ||
description='Install headers and rebuild child directories') | ||
parser.add_argument('target', help='target directory to build addons') | ||
parser.add_argument('--out-dir', help='path to the output directory', | ||
default='out/Release') | ||
parser.add_argument('--loglevel', help='loglevel of node-gyp', | ||
default='silent') | ||
parser.add_argument('--skip-tests', help='skip building tests', | ||
default='') | ||
parser.add_argument('--only-tests', help='only build tests in the list', | ||
default='') | ||
parser.add_argument('--node-gyp', help='path to node-gyp script', | ||
default='deps/npm/node_modules/node-gyp/bin/node-gyp.js') | ||
parser.add_argument('--is-win', help='build for Windows target', | ||
action='store_true', default=(sys.platform == 'win32')) | ||
args, unknown_args = parser.parse_known_args() | ||
|
||
install_and_rebuild(args, unknown_args) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import lzma | ||
import os | ||
import shutil | ||
import sys | ||
import subprocess | ||
import tarfile | ||
import tempfile | ||
|
||
def strip_tarball_suffix(filename): | ||
suffixs = ['-headers.tar.gz', '-headers.tar.xz'] | ||
for suffix in suffixs: | ||
if filename.endswith(suffix): | ||
return filename[:-len(suffix)] | ||
raise ValueError('The tarball name must ends with -headers.tar.gz/xz.') | ||
|
||
def run_install_py(flags, temp_dir): | ||
install_py = os.path.join(os.path.dirname(__file__), 'install.py') | ||
subprocess.check_call([ | ||
sys.executable, | ||
install_py, | ||
'install', | ||
'--silent', | ||
'--headers-only', | ||
'--dest-dir', temp_dir, | ||
'--prefix', '/', | ||
] + flags) | ||
|
||
def create_tarball(temp_dir, tarball, compression_level): | ||
if tarball.endswith('.gz'): | ||
tar = tarfile.open(tarball, 'w:gz', compresslevel=int(compression_level)) | ||
elif tarball.endswith('.xz'): | ||
if compression_level.endswith('e'): | ||
preset = int(compression_level[:-1]) | lzma.PRESET_EXTREME | ||
else: | ||
preset = int(compression_level) | ||
tar = tarfile.open(tarball, 'w:xz', preset=preset) | ||
else: | ||
raise ValueError('Invalid tarball extension. Only .tar.gz and .tar.xz are supported.') | ||
with tar: | ||
tar.add(temp_dir, arcname=os.path.basename(temp_dir)) | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description='Install headers and create tarball') | ||
parser.add_argument('--target', | ||
required=True, | ||
help='Target path of the generated tarball') | ||
parser.add_argument('--compression-level', | ||
default='9', | ||
help='Compression level for gzip or xz (default: 9)') | ||
args, unknown_args = parser.parse_known_args() | ||
|
||
temp_dir = tempfile.mkdtemp() | ||
try: | ||
workspace = os.path.join(temp_dir, strip_tarball_suffix(args.target)) | ||
run_install_py(unknown_args, workspace) | ||
create_tarball(workspace, args.target, args.compression_level) | ||
finally: | ||
shutil.rmtree(temp_dir) | ||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.