1From 999d918bc8fefec1752243743a47c0ce5380bcec Mon Sep 17 00:00:00 2001
2From: Ivan Trubach <mr.trubach@icloud.com>
3Date: Wed, 17 Jul 2024 10:16:02 +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 | 4 ++++
25 tools/v8_gypfiles/v8.gyp | 4 ++++
26 4 files changed, 23 insertions(+)
27
28diff --git a/common.gypi b/common.gypi
29index 154bbf2a0d..54d2afe3b3 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 f7e3310723..f7c7acdf4f 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@@ -2276,6 +2282,14 @@ if flavor == 'win' and python.lower().endswith('.exe'):
58 # will fail to run python scripts.
59 gyp_args += ['-Dpython=' + python]
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 9617596760..439c76aca6 100644
74--- a/node.gyp
75+++ b/node.gyp
76@@ -703,6 +703,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@@ -722,6 +723,7 @@
85 '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc',
86 ],
87 'action': [
88+ '<@(emulator)',
89 '<@(_inputs)',
90 '<@(_outputs)',
91 ],
92@@ -1010,6 +1012,7 @@
93 '<(SHARED_INTERMEDIATE_DIR)/node_javascript.cc',
94 ],
95 'action': [
96+ '<@(emulator)',
97 '<(node_js2c_exec)',
98 '<@(_outputs)',
99 'lib',
100@@ -1477,6 +1480,7 @@
101 '<(PRODUCT_DIR)/<(node_core_target_name).def',
102 ],
103 'action': [
104+ '<@(emulator)',
105 '<(PRODUCT_DIR)/gen_node_def.exe',
106 '<@(_inputs)',
107 '<@(_outputs)',
108diff --git a/tools/v8_gypfiles/v8.gyp b/tools/v8_gypfiles/v8.gyp
109index d65a5c268e..5cd6c36b86 100644
110--- a/tools/v8_gypfiles/v8.gyp
111+++ b/tools/v8_gypfiles/v8.gyp
112@@ -112,6 +112,7 @@
113 '<@(torque_outputs_inc)',
114 ],
115 'action': [
116+ '<@(emulator)',
117 '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)torque<(EXECUTABLE_SUFFIX)',
118 '-o', '<(SHARED_INTERMEDIATE_DIR)/torque-generated',
119 '-v8-root', '<(V8_ROOT)',
120@@ -232,6 +233,7 @@
121 'action': [
122 '<(python)',
123 '<(V8_ROOT)/tools/run.py',
124+ '<@(emulator)',
125 '<@(_inputs)',
126 '<@(_outputs)',
127 ],
128@@ -453,6 +455,7 @@
129 }],
130 ],
131 'action': [
132+ '<@(emulator)',
133 '>@(_inputs)',
134 '>@(mksnapshot_flags)',
135 ],
136@@ -1842,6 +1845,7 @@
137 'action': [
138 '<(python)',
139 '<(V8_ROOT)/tools/run.py',
140+ '<@(emulator)',
141 '<@(_inputs)',
142 '<@(_outputs)',
143 ],
144--
1452.44.1
146