Skip to content

Commit 79c9149

Browse files
StefanStojanovicrichardlau
authored andcommitted
build,win: fix Temporal build
PR-URL: #61806 Fixes: #57127 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 9cae819 commit 79c9149

File tree

3 files changed

+133
-18
lines changed

3 files changed

+133
-18
lines changed

configure.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,14 @@ def configure_node(o):
17801780
o['variables']['target_arch'] = target_arch
17811781
o['variables']['node_byteorder'] = sys.byteorder
17821782

1783+
# On Windows, cargo may default to the GNU target (e.g. x86_64-pc-windows-gnu)
1784+
# but Node.js requires MSVC-compatible libraries. Set explicit Rust target
1785+
# triple for the target architecture.
1786+
o['variables']['cargo_rust_target'] = ''
1787+
if flavor == 'win':
1788+
o['variables']['cargo_rust_target'] = \
1789+
'aarch64-pc-windows-msvc' if target_arch == 'arm64' else 'x86_64-pc-windows-msvc'
1790+
17831791
# Allow overriding the compiler - needed by embedders.
17841792
if options.use_clang:
17851793
o['variables']['clang'] = 1

deps/crates/cargo_build.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
3+
"""Invoke cargo with the correct Rust target on Windows cross-compilation.
4+
5+
Works around three issues: GYP's MSVS generator mangles executable names in
6+
host-toolset actions (breaking direct cargo calls), GYP cannot set different
7+
variable values per toolset, and cargo's output directory layout uses Rust
8+
target triples while MSBuild expects platform names (x64/arm64). This script
9+
reads MSBuild's $(Platform) to select the right Rust target and copies the
10+
built library to a path MSBuild can resolve per-project.
11+
"""
12+
13+
import os
14+
import shutil
15+
import subprocess
16+
import sys
17+
18+
19+
def main():
20+
# Arguments: <platform> <output_dir> [cargo_args...]
21+
if len(sys.argv) < 3:
22+
print('Usage: cargo_build.py <platform> <output_dir> [cargo_args...]', file=sys.stderr)
23+
sys.exit(1)
24+
25+
platform = sys.argv[1] # x64 or arm64
26+
output_dir = sys.argv[2] # SHARED_INTERMEDIATE_DIR
27+
cargo_args = sys.argv[3:]
28+
build_profile = 'release' if '--release' in cargo_args else 'debug'
29+
30+
cargo = os.environ.get('CARGO', 'cargo')
31+
if not os.path.isabs(cargo) and shutil.which(cargo) is None:
32+
home = os.environ.get('USERPROFILE', '')
33+
cargo_home = os.path.join(home, '.cargo', 'bin', 'cargo.exe')
34+
if os.path.isfile(cargo_home):
35+
cargo = cargo_home
36+
37+
rust_target_map = {
38+
'x64': 'x86_64-pc-windows-msvc',
39+
'arm64': 'aarch64-pc-windows-msvc',
40+
}
41+
rust_target = rust_target_map.get(platform)
42+
if rust_target is None:
43+
print(f'Unsupported platform: {platform}', file=sys.stderr)
44+
sys.exit(1)
45+
46+
cmd = [cargo, 'rustc', '--target', rust_target, '--target-dir', output_dir] + cargo_args
47+
ret = subprocess.call(cmd)
48+
if ret != 0:
49+
sys.exit(ret)
50+
51+
# Copy output to the platform-specific directory that MSBuild expects.
52+
src = os.path.join(output_dir, rust_target, build_profile, 'node_crates.lib')
53+
dst_dir = os.path.join(output_dir, platform, build_profile)
54+
os.makedirs(dst_dir, exist_ok=True)
55+
dst = os.path.join(dst_dir, 'node_crates.lib')
56+
shutil.copy2(src, dst)
57+
58+
59+
if __name__ == '__main__':
60+
main()

deps/crates/crates.gyp

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,46 @@
22
'variables': {
33
'cargo%': 'cargo',
44
'cargo_vendor_dir': './vendor',
5+
'cargo_rust_target%': '',
56
},
67
'conditions': [
78
['build_type == "Release"', {
89
'variables': {
910
'cargo_build_flags': ['--release'],
10-
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/release/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
1111
},
12+
'conditions': [
13+
['cargo_rust_target!=""', {
14+
'variables': {
15+
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/$(Platform)/release/node_crates.lib',
16+
},
17+
}, {
18+
'variables': {
19+
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/release/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
20+
},
21+
}],
22+
],
1223
}, {
1324
'variables': {
1425
'cargo_build_flags': [],
15-
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/debug/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
1626
},
27+
'conditions': [
28+
['cargo_rust_target!=""', {
29+
'variables': {
30+
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/$(Platform)/debug/node_crates.lib',
31+
},
32+
}, {
33+
'variables': {
34+
'node_crates_libpath': '<(SHARED_INTERMEDIATE_DIR)/debug/<(STATIC_LIB_PREFIX)node_crates<(STATIC_LIB_SUFFIX)',
35+
},
36+
}],
37+
],
1738
}]
1839
],
1940
'targets': [
2041
{
2142
'target_name': 'node_crates',
2243
'type': 'none',
44+
'toolsets': ['host', 'target'],
2345
'hard_dependency': 1,
2446
'sources': [
2547
'Cargo.toml',
@@ -39,29 +61,54 @@
3961
}],
4062
],
4163
},
42-
'actions': [
43-
{
44-
'action_name': 'cargo_build',
45-
'inputs': [
46-
'<@(_sources)'
47-
],
48-
'outputs': [
49-
'<(node_crates_libpath)'
64+
'conditions': [
65+
['cargo_rust_target!=""', {
66+
'actions': [
67+
{
68+
'action_name': 'cargo_build',
69+
'inputs': [
70+
'<@(_sources)'
71+
],
72+
'outputs': [
73+
'<(node_crates_libpath)'
74+
],
75+
'action': [
76+
'<(python)',
77+
'cargo_build.py',
78+
'$(Platform)',
79+
'<(SHARED_INTERMEDIATE_DIR)',
80+
'<@(cargo_build_flags)',
81+
'--frozen',
82+
],
83+
}
5084
],
51-
'action': [
52-
'<(cargo)',
53-
'rustc',
54-
'<@(cargo_build_flags)',
55-
'--frozen',
56-
'--target-dir',
57-
'<(SHARED_INTERMEDIATE_DIR)'
85+
}, {
86+
'actions': [
87+
{
88+
'action_name': 'cargo_build',
89+
'inputs': [
90+
'<@(_sources)'
91+
],
92+
'outputs': [
93+
'<(node_crates_libpath)'
94+
],
95+
'action': [
96+
'<(cargo)',
97+
'rustc',
98+
'<@(cargo_build_flags)',
99+
'--frozen',
100+
'--target-dir',
101+
'<(SHARED_INTERMEDIATE_DIR)'
102+
],
103+
}
58104
],
59-
}
105+
}],
60106
],
61107
},
62108
{
63109
'target_name': 'temporal_capi',
64110
'type': 'none',
111+
'toolsets': ['host', 'target'],
65112
'sources': [],
66113
'dependencies': [
67114
'node_crates',

0 commit comments

Comments
 (0)