Skip to content

Commit ad439a6

Browse files
committed
build: add rust target for macOS cross compiles
When we build the macOS pkg, we build Node.js twice (on arm64): - Once for arm64 (native) - Once for x64, using a combination of Rosetta 2 and compiler flags before combining both into a universal binary. For the x64 case, pass target flag to `rustc` so that the binary is built for the correct target architecture. Signed-off-by: Richard Lau <richard.lau@ibm.com>
1 parent 186995e commit ad439a6

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

configure.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,23 @@ def host_arch_cc():
17251725
return rtn
17261726

17271727

1728+
def is_rosetta2():
1729+
if flavor != 'mac':
1730+
return False
1731+
try:
1732+
proc = subprocess.Popen(['sysctl', '-n', 'sysctl.proc_translated'],
1733+
stdin=subprocess.PIPE, stderr=subprocess.PIPE,
1734+
stdout=subprocess.PIPE)
1735+
except OSError:
1736+
warn('sysctl failed')
1737+
return False
1738+
with proc:
1739+
out = to_utf8(proc.communicate()[0]).strip()
1740+
warn(f'proc_translated: {out}')
1741+
# return out == '1'
1742+
return True
1743+
1744+
17281745
def host_arch_win():
17291746
"""Host architecture check using environ vars (better way to do this?)"""
17301747

@@ -1855,6 +1872,8 @@ def configure_node(o):
18551872
if flavor == 'win':
18561873
o['variables']['cargo_rust_target'] = \
18571874
'aarch64-pc-windows-msvc' if target_arch == 'arm64' else 'x86_64-pc-windows-msvc'
1875+
if target_arch == 'x64' and is_rosetta2():
1876+
o['variables']['cargo_rust_target'] = 'x86_64-apple-darwin'
18581877

18591878
# Allow overriding the compiler - needed by embedders.
18601879
if options.use_clang:

deps/crates/crates.gyp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
},
1212
'conditions': [
1313
['cargo_rust_target!=""', {
14+
'variables': {
15+
'cargo_build_flags': ['--target', '<(cargo_rust_target)'],
16+
}
17+
}],
18+
['OS=="win"', {
1419
'variables': {
1520
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/$(Platform)/release/node_crates.lib',
1621
},
1722
}, {
1823
'variables': {
19-
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/release/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
24+
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/<(cargo_rust_target)/release/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
2025
},
2126
}],
2227
],
@@ -26,12 +31,17 @@
2631
},
2732
'conditions': [
2833
['cargo_rust_target!=""', {
34+
'variables': {
35+
'cargo_build_flags': ['--target', '$(cargo_rust_target)'],
36+
}
37+
}],
38+
['OS=="win"', {
2939
'variables': {
3040
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/$(Platform)/debug/node_crates.lib',
3141
},
3242
}, {
3343
'variables': {
34-
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/debug/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
44+
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/<(cargo_rust_target)/debug/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
3545
},
3646
}],
3747
],
@@ -62,7 +72,7 @@
6272
],
6373
},
6474
'conditions': [
65-
['cargo_rust_target!=""', {
75+
['OS=="win"', {
6676
'actions': [
6777
{
6878
'action_name': 'cargo_build',

0 commit comments

Comments
 (0)