Skip to content

Commit 815dcae

Browse files
committed
build: separate host and target Rust artifacts in cross builds
Capture the Rust target and host triples during configure for bundled Temporal cross builds. Use explicit Cargo targets and separate output directories so host and target archives do not share a build rule. Fixes: nodejs#66897 Refs: nodejs#66803 Assisted-by: Codex Signed-off-by: inoway46 <[email protected]>
1 parent e0619bb commit 815dcae

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎BUILDING.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,19 @@ a warning is printed and Temporal support is disabled.
10671067

10681068
Passing both options to `configure.py` is an error.
10691069

1070+
For non-Windows cross builds with bundled Temporal, set the Rust target when
1071+
configuring:
1072+
1073+
```bash
1074+
CARGO_BUILD_TARGET=riscv64gc-unknown-linux-gnu ./configure \
1075+
--cross-compiling --dest-os=linux --dest-cpu=riscv64
1076+
```
1077+
1078+
Configure saves this target and the host triple reported by `rustc -vV`.
1079+
Install the target's Rust standard library and configure the cross linker
1080+
separately. Re-run configure when changing targets. Cargo configuration-file
1081+
targets and custom JSON targets are not supported by this detection.
1082+
10701083
## Building Node.js with external core modules
10711084

10721085
It is possible to specify one or more JavaScript text files to be bundled in

‎configure.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,27 @@ def get_rustc_version(rustc):
16201620
warn(f'Could not recognize `rustc`: {rustc_ret}')
16211621
return '0.0'
16221622

1623+
def get_rust_cross_targets(default_target):
1624+
rust_target = os.environ.get('CARGO_BUILD_TARGET', '') or default_target
1625+
if not rust_target:
1626+
error('Cross compilation with bundled Temporal requires CARGO_BUILD_TARGET')
1627+
rust_host_target = ''
1628+
rustc = os.environ.get('RUSTC', 'rustc')
1629+
try:
1630+
version = subprocess.check_output(shlex.split(rustc) + ['-vV'],
1631+
stderr=subprocess.PIPE, text=True)
1632+
except (OSError, subprocess.CalledProcessError):
1633+
version = ''
1634+
match = re.search(r'^host: (\S+)$', version, re.MULTILINE)
1635+
if match:
1636+
rust_host_target = match.group(1)
1637+
if not rust_host_target:
1638+
error('Cannot determine the Rust host triple from rustc -vV')
1639+
for triple in (rust_host_target, rust_target):
1640+
if not re.fullmatch(r'[A-Za-z0-9_]+(?:-[A-Za-z0-9_]+)+', triple):
1641+
error('Rust targets must be target triples, not paths to target JSON files')
1642+
return rust_host_target, rust_target
1643+
16231644
# Note: Apple clang self-reports as clang 4.2.0 and gcc 4.2.1. It passes
16241645
# the version check more by accident than anything else but a more rigorous
16251646
# check involves checking the build number against an allowlist. I'm not
@@ -1957,6 +1978,14 @@ def configure_node(o):
19571978
os.environ['GYP_CROSSCOMPILE'] = "1"
19581979

19591980
o['variables']['want_separate_host_toolset'] = int(cross_compiling)
1981+
o['variables']['cargo_cross_compiling'] = 0
1982+
if (flavor != 'win' and cross_compiling and
1983+
not options.v8_disable_temporal_support and not options.shared_temporal_capi):
1984+
# Only bundled Temporal cross builds need explicit per-toolset Rust targets.
1985+
rust_host_target, rust_target = get_rust_cross_targets(o['variables']['cargo_rust_target'])
1986+
o['variables']['cargo_cross_compiling'] = 1
1987+
o['variables']['cargo_rust_target_target'] = rust_target
1988+
o['variables']['cargo_rust_target_host'] = rust_host_target
19601989

19611990
if target_arch == 'arm64':
19621991
o['variables']['arm_fpu'] = options.arm_fpu or 'neon'

‎deps/crates/crates.gyp‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
'zoneinfo64_dir': 'zoneinfo64-v0_3',
77
'temporal_zoneinfo64_data_output': '<(SHARED_INTERMEDIATE_DIR)/src/builtins/builtins-temporal-zoneinfo64-data.cc',
88
'cargo_rust_target%': '',
9+
'cargo_cross_compiling%': 0,
10+
'cargo_rust_target_host%': '',
11+
'cargo_rust_target_target%': '',
912
},
1013
'conditions': [
1114
['build_type == "Release"', {
1215
'variables': {
1316
'cargo_build_flags': ['--release'],
17+
'cargo_profile': 'release',
1418
},
1519
'conditions': [
16-
['cargo_rust_target!=""', {
20+
['cargo_rust_target!="" and cargo_cross_compiling==0', {
1721
'variables': {
1822
'cargo_build_flags': ['--target', '<(cargo_rust_target)'],
1923
}
@@ -31,6 +35,7 @@
3135
}, {
3236
'variables': {
3337
'cargo_build_flags': [],
38+
'cargo_profile': 'debug',
3439
},
3540
'conditions': [
3641
['OS=="win"', {
@@ -50,6 +55,17 @@
5055
'target_name': 'node_crates',
5156
'type': 'none',
5257
'toolsets': ['host', 'target'],
58+
'variables': {
59+
'cargo_output_dir': '<(SHARED_INTERMEDIATE_DIR)',
60+
'cargo_cross_target_flags': [],
61+
'conditions': [
62+
['cargo_cross_compiling==1', {
63+
'cargo_output_dir': '<(SHARED_INTERMEDIATE_DIR)/cargo/<(_toolset)',
64+
'cargo_cross_target_flags': ['--target', '<(cargo_rust_target_<(_toolset))'],
65+
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/cargo/<(_toolset)/<(cargo_rust_target_<(_toolset))/<(cargo_profile)/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
66+
}],
67+
],
68+
},
5369
'hard_dependency': 1,
5470
'sources': [
5571
'Cargo.toml',
@@ -103,10 +119,11 @@
103119
'action': [
104120
'<(cargo)',
105121
'rustc',
122+
'<@(cargo_cross_target_flags)',
106123
'<@(cargo_build_flags)',
107124
'--frozen',
108125
'--target-dir',
109-
'<(SHARED_INTERMEDIATE_DIR)'
126+
'<(cargo_output_dir)'
110127
],
111128
}
112129
],

0 commit comments

Comments
 (0)