Skip to content

Commit 2358017

Browse files
committedNov 29, 2023
build,tools: make addons tests work with GN
1 parent a3ee187 commit 2358017

File tree

9 files changed

+411
-321
lines changed

9 files changed

+411
-321
lines changed
 

‎Makefile

+5-8
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ config.gypi: configure configure.py src/node_version.h
187187

188188
.PHONY: install
189189
install: all ## Installs node into $PREFIX (default=/usr/local).
190-
$(PYTHON) tools/install.py $@ '$(DESTDIR)' '$(PREFIX)'
190+
$(PYTHON) tools/install.py $@ --dest-dir '$(DESTDIR)' --prefix '$(PREFIX)'
191191

192192
.PHONY: uninstall
193193
uninstall: ## Uninstalls node from $PREFIX (default=/usr/local).
194-
$(PYTHON) tools/install.py $@ '$(DESTDIR)' '$(PREFIX)'
194+
$(PYTHON) tools/install.py $@ --dest-dir '$(DESTDIR)' --prefix '$(PREFIX)'
195195

196196
.PHONY: clean
197197
.NOTPARALLEL: clean
@@ -388,15 +388,12 @@ ADDONS_BINDING_SOURCES := \
388388
$(filter-out test/addons/??_*/*.h, $(wildcard test/addons/*/*.h))
389389

390390
ADDONS_PREREQS := config.gypi \
391-
deps/npm/node_modules/node-gyp/package.json tools/build-addons.mjs \
391+
deps/npm/node_modules/node-gyp/package.json tools/build_addons.py \
392392
deps/uv/include/*.h deps/v8/include/*.h \
393393
src/node.h src/node_buffer.h src/node_object_wrap.h src/node_version.h
394394

395395
define run_build_addons
396-
env npm_config_loglevel=$(LOGLEVEL) npm_config_nodedir="$$PWD" \
397-
npm_config_python="$(PYTHON)" $(NODE) "$$PWD/tools/build-addons.mjs" \
398-
"$$PWD/deps/npm/node_modules/node-gyp/bin/node-gyp.js" \
399-
$1
396+
env $(PYTHON) "$$PWD/tools/build_addons.py" --loglevel=$(LOGLEVEL) $1
400397
touch $2
401398
endef
402399

@@ -1216,7 +1213,7 @@ $(TARBALL)-headers: release-only
12161213
--tag=$(TAG) \
12171214
--release-urlbase=$(RELEASE_URLBASE) \
12181215
$(CONFIG_FLAGS) $(BUILD_RELEASE_FLAGS)
1219-
HEADERS_ONLY=1 $(PYTHON) tools/install.py install '$(TARNAME)' '/'
1216+
$(PYTHON) tools/install.py install --headers-only --dest-dir '$(TARNAME)' --prefix '$(PREFIX)'
12201217
find $(TARNAME)/ -type l | xargs $(RM)
12211218
tar -cf $(TARNAME)-headers.tar $(TARNAME)
12221219
$(RM) -r $(TARNAME)

‎deps/openssl/unofficial.gni

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ template("openssl_gn_build") {
9393
configs += [ ":openssl_internal_config" ]
9494
public_configs = [ ":openssl_external_config" ]
9595

96+
if (is_posix) {
97+
configs -= [ "//build/config/gcc:symbol_visibility_hidden" ]
98+
configs += [ "//build/config/gcc:symbol_visibility_default" ]
99+
}
100+
96101
config_path_name = ""
97102
if (is_win) {
98103
if (target_cpu == "x86") {

‎deps/uv/unofficial.gni

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ template("uv_gn_build") {
6969
configs += [ ":uv_internal_config" ]
7070
public_configs = [ ":uv_external_config" ]
7171

72+
if (is_posix) {
73+
configs -= [ "//build/config/gcc:symbol_visibility_hidden" ]
74+
configs += [ "//build/config/gcc:symbol_visibility_default" ]
75+
}
76+
7277
if (is_win) {
7378
libs = [
7479
"advapi32.lib",

‎doc/contributing/collaborator-guide.md

-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ There are some other files that touch the build chain. Changes in the following
232232
files also qualify as affecting the `node` binary:
233233

234234
* `tools/*.py`
235-
* `tools/build-addons.mjs`
236235
* `*.gyp`
237236
* `*.gypi`
238237
* `configure`

‎tools/build-addons.mjs

-63
This file was deleted.

‎tools/build_addons.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)
Please sign in to comment.