1From 4b83f714c821d6d4d2306673ee3a87907cfec80e Mon Sep 17 00:00:00 2001
2From: Ivan Trubach <mr.trubach@icloud.com>
3Date: Fri, 19 Jul 2024 10:45:13 +0300
4Subject: [PATCH] build: support setting an emulator from configure script
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9V8’s JIT infrastructure requires binaries such as mksnapshot to be run
10during the build. However, these binaries must have the same bit-width
11as the host platform (e.g. a x86_64 build platform targeting ARMv6 needs
12to produce a 32-bit binary).
13
14To work around this issue, allow building the binaries for the host
15platform and running them on the build platform with an emulator.
16
17Based on Buildroot’s nodejs-src 0001-add-qemu-wrapper-support.patch.
18https://gitlab.com/buildroot.org/buildroot/-/blob/c1d5eada4d4db9eeaa1c44dd1dea95a67c8a70ca/package/nodejs/nodejs-src/0001-add-qemu-wrapper-support.patch
19
20Upstream: https://github.com/nodejs/node/pull/53899
21---
22 common.gypi | 1 +
23 configure.py | 14 ++++++++++++++
24 node.gyp | 3 +++
25 tools/v8_gypfiles/v8.gyp | 4 ++++
26 4 files changed, 22 insertions(+)
27
28diff --git a/common.gypi b/common.gypi
29index ec92c9df4c..6474495ab6 100644
30--- a/common.gypi
31+++ b/common.gypi
32@@ -13,6 +13,7 @@
33 'enable_pgo_generate%': '0',
34 'enable_pgo_use%': '0',
35 'python%': 'python',
36+ 'emulator%': [],
37
38 'node_shared%': 'false',
39 'force_dynamic_crt%': 0,
40diff --git a/configure.py b/configure.py
41index 82916748fd..10dc0becbb 100755
42--- a/configure.py
43+++ b/configure.py
44@@ -112,6 +112,12 @@ parser.add_argument('--dest-cpu',
45 choices=valid_arch,
46 help=f"CPU architecture to build for ({', '.join(valid_arch)})")
47
48+parser.add_argument('--emulator',
49+ action='store',
50+ dest='emulator',
51+ default=None,
52+ help='emulator command that can run executables built for the target system')
53+
54 parser.add_argument('--cross-compiling',
55 action='store_true',
56 dest='cross_compiling',
57@@ -2160,6 +2166,14 @@ write('config.mk', do_not_edit + config_str)
58 gyp_args = ['--no-parallel', '-Dconfiguring_node=1']
59 gyp_args += ['-Dbuild_type=' + config['BUILDTYPE']]
60
61+if options.emulator is not None:
62+ if not options.cross_compiling:
63+ # Note that emulator is a list so we have to quote the variable.
64+ gyp_args += ['-Demulator=' + shlex.quote(options.emulator)]
65+ else:
66+ # TODO: perhaps use emulator for tests?
67+ warn('The `--emulator` option has no effect when cross-compiling.')
68+
69 if options.use_ninja:
70 gyp_args += ['-f', 'ninja-' + flavor]
71 elif flavor == 'win' and sys.platform != 'msys':
72diff --git a/node.gyp b/node.gyp
73index 08cb3f38e8..515b305933 100644
74--- a/node.gyp
75+++ b/node.gyp
76@@ -332,6 +332,7 @@
77 '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc',
78 ],
79 'action': [
80+ '<@(emulator)',
81 '<(node_mksnapshot_exec)',
82 '--build-snapshot',
83 '<(node_snapshot_main)',
84@@ -351,6 +352,7 @@
85 '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc',
86 ],
87 'action': [
88+ '<@(emulator)',
89 '<@(_inputs)',
90 '<@(_outputs)',
91 ],
92@@ -1520,6 +1522,7 @@
93 '<(PRODUCT_DIR)/<(node_core_target_name).def',
94 ],
95 'action': [
96+ '<@(emulator)',
97 '<(PRODUCT_DIR)/gen_node_def.exe',
98 '<@(_inputs)',
99 '<@(_outputs)',
100diff --git a/tools/v8_gypfiles/v8.gyp b/tools/v8_gypfiles/v8.gyp
101index ba8b161f0f..d5c90dad50 100644
102--- a/tools/v8_gypfiles/v8.gyp
103+++ b/tools/v8_gypfiles/v8.gyp
104@@ -99,6 +99,7 @@
105 '<@(torque_outputs_inc)',
106 ],
107 'action': [
108+ '<@(emulator)',
109 '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)torque<(EXECUTABLE_SUFFIX)',
110 '-o', '<(SHARED_INTERMEDIATE_DIR)/torque-generated',
111 '-v8-root', '<(V8_ROOT)',
112@@ -219,6 +220,7 @@
113 'action': [
114 '<(python)',
115 '<(V8_ROOT)/tools/run.py',
116+ '<@(emulator)',
117 '<@(_inputs)',
118 '<@(_outputs)',
119 ],
120@@ -442,6 +444,7 @@
121 }],
122 ],
123 'action': [
124+ '<@(emulator)',
125 '>@(_inputs)',
126 '>@(mksnapshot_flags)',
127 ],
128@@ -1577,6 +1580,7 @@
129 'action': [
130 '<(python)',
131 '<(V8_ROOT)/tools/run.py',
132+ '<@(emulator)',
133 '<@(_inputs)',
134 '<@(_outputs)',
135 ],
136--
1372.44.1
138