Skip to content

Commit 6555d9d

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. Refs: nodejs#66803 Assisted-by: Codex Signed-off-by: inoway46 <[email protected]>
1 parent e0619bb commit 6555d9d

3 files changed

Lines changed: 61 additions & 4 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,31 @@ def configure_node(o):
19571957
os.environ['GYP_CROSSCOMPILE'] = "1"
19581958

19591959
o['variables']['want_separate_host_toolset'] = int(cross_compiling)
1960+
o['variables']['cargo_cross_compiling'] = 0
1961+
if (flavor != 'win' and cross_compiling and
1962+
not options.v8_disable_temporal_support and not options.shared_temporal_capi):
1963+
# Only bundled Temporal cross builds need explicit per-toolset Rust targets.
1964+
rust_target = os.environ.get('CARGO_BUILD_TARGET', '') or o['variables']['cargo_rust_target']
1965+
if not rust_target:
1966+
error('Cross compilation with bundled Temporal requires CARGO_BUILD_TARGET')
1967+
rust_host_target = ''
1968+
rustc = os.environ.get('RUSTC', 'rustc')
1969+
try:
1970+
version = subprocess.check_output(shlex.split(rustc) + ['-vV'],
1971+
stderr=subprocess.PIPE, text=True)
1972+
except (OSError, subprocess.CalledProcessError):
1973+
version = ''
1974+
match = re.search(r'^host: (\S+)$', version, re.MULTILINE)
1975+
if match:
1976+
rust_host_target = match.group(1)
1977+
if not rust_host_target:
1978+
error('Cannot determine the Rust host triple from rustc -vV')
1979+
for triple in (rust_host_target, rust_target):
1980+
if not re.fullmatch(r'[A-Za-z0-9_]+(?:-[A-Za-z0-9_]+)+', triple):
1981+
error('Rust targets must be target triples, not paths to target JSON files')
1982+
o['variables']['cargo_cross_compiling'] = 1
1983+
o['variables']['cargo_rust_target_target'] = rust_target
1984+
o['variables']['cargo_rust_target_host'] = rust_host_target
19601985

19611986
if target_arch == 'arm64':
19621987
o['variables']['arm_fpu'] = options.arm_fpu or 'neon'

‎deps/crates/crates.gyp‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
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%': '',
12+
'node_shared_temporal_capi%': 'false',
913
},
1014
'conditions': [
1115
['build_type == "Release"', {
1216
'variables': {
1317
'cargo_build_flags': ['--release'],
18+
'cargo_profile': 'release',
1419
},
1520
'conditions': [
16-
['cargo_rust_target!=""', {
21+
['cargo_rust_target!="" and cargo_cross_compiling==0', {
1722
'variables': {
1823
'cargo_build_flags': ['--target', '<(cargo_rust_target)'],
1924
}
@@ -31,6 +36,7 @@
3136
}, {
3237
'variables': {
3338
'cargo_build_flags': [],
39+
'cargo_profile': 'debug',
3440
},
3541
'conditions': [
3642
['OS=="win"', {
@@ -50,6 +56,17 @@
5056
'target_name': 'node_crates',
5157
'type': 'none',
5258
'toolsets': ['host', 'target'],
59+
'variables': {
60+
'cargo_output_dir': '<(SHARED_INTERMEDIATE_DIR)',
61+
'cargo_cross_target_flags': [],
62+
'conditions': [
63+
['cargo_cross_compiling==1', {
64+
'cargo_output_dir': '<(SHARED_INTERMEDIATE_DIR)/cargo/<(_toolset)',
65+
'cargo_cross_target_flags': ['--target', '<(cargo_rust_target_<(_toolset))'],
66+
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/cargo/<(_toolset)/<(cargo_rust_target_<(_toolset))/<(cargo_profile)/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
67+
}],
68+
],
69+
},
5370
'hard_dependency': 1,
5471
'sources': [
5572
'Cargo.toml',
@@ -70,7 +87,7 @@
7087
],
7188
},
7289
'conditions': [
73-
['OS=="win"', {
90+
['OS=="win" and node_shared_temporal_capi=="false"', {
7491
'actions': [
7592
{
7693
'action_name': 'cargo_build',
@@ -90,7 +107,8 @@
90107
],
91108
}
92109
],
93-
}, {
110+
}],
111+
['OS!="win" and node_shared_temporal_capi=="false"', {
94112
'actions': [
95113
{
96114
'action_name': 'cargo_build',
@@ -103,10 +121,11 @@
103121
'action': [
104122
'<(cargo)',
105123
'rustc',
124+
'<@(cargo_cross_target_flags)',
106125
'<@(cargo_build_flags)',
107126
'--frozen',
108127
'--target-dir',
109-
'<(SHARED_INTERMEDIATE_DIR)'
128+
'<(cargo_output_dir)'
110129
],
111130
}
112131
],

0 commit comments

Comments
 (0)