forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpatch_host_makefiles.py
More file actions
194 lines (157 loc) Β· 7.12 KB
/
patch_host_makefiles.py
File metadata and controls
194 lines (157 loc) Β· 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python3
"""
Patch GYP-generated host makefiles to use correct architecture (arm64 instead of wasm32).
This script is necessary because when Node.js is configured with --dest-cpu=wasm32,
GYP applies the V8_TARGET_ARCH_WASM32 flag to both target and host builds.
Host builds (torque, mksnapshot, etc.) should use the native architecture (arm64)
to avoid including WASI-specific stubs that are incomplete for the host.
"""
import os
import re
import sys
def patch_makefile(filepath):
"""Replace V8_TARGET_ARCH_WASM32 with V8_TARGET_ARCH_ARM64 in a makefile."""
with open(filepath, 'r') as f:
content = f.read()
original_content = content
# Replace the WASM32 arch flag with ARM64
content = content.replace("'-DV8_TARGET_ARCH_WASM32'", "'-DV8_TARGET_ARCH_ARM64'")
# Remove the GLIBCXX ABI flag since macOS uses libc++, not libstdc++
content = content.replace("'-D_GLIBCXX_USE_CXX11_ABI=1' \\\n", "")
# Use C++2b for spaceship operator and concepts support
content = content.replace("-std=c++20", "-std=c++2b -fconcepts")
# Add the 64_BIT flag if ARM64 is defined but 64_BIT is not
# We need to handle both DEFS_Debug and DEFS_Release sections
if "'-DV8_TARGET_ARCH_ARM64'" in content:
# Replace all occurrences of ARM64 with ARM64 + 64_BIT, but avoid double-adding
lines = content.split('\n')
new_lines = []
for i, line in enumerate(lines):
new_lines.append(line)
# If this line has ARM64 and the next line doesn't have 64_BIT, add it
if "'-DV8_TARGET_ARCH_ARM64'" in line and (i+1 >= len(lines) or "'-DV8_TARGET_ARCH_64_BIT'" not in lines[i+1]):
# Add the 64_BIT flag on the next line
indent = '\t' if line.startswith('\t') else ''
new_lines.append(f"{indent}'-DV8_TARGET_ARCH_64_BIT' \\")
content = '\n'.join(new_lines)
if content != original_content:
with open(filepath, 'w') as f:
f.write(content)
return True
return False
def patch_root_makefile():
"""Patch the root Makefile to use native ar for host builds and wasm-ld for target."""
makefile_path = 'out/Makefile'
if not os.path.exists(makefile_path):
return False
with open(makefile_path, 'r') as f:
content = f.read()
original_content = content
# Find and replace cmd_alink to conditionally use ar or wasm-ld based on TOOLSET
old_alink = (
"cmd_alink = rm -f $@ && /opt/wasi-sdk/bin/wasm-ld -static -o $@ $(filter %.o,$^)"
)
new_alink = (
"cmd_alink = rm -f $@; "
"if [ '$(TOOLSET)' = 'host' ]; then "
"$(AR.host) rcs $@ $(filter %.o,$^); "
"else "
"/opt/wasi-sdk/bin/wasm-ld -static -o $@ $(filter %.o,$^); "
"fi"
)
if old_alink in content:
content = content.replace(old_alink, new_alink)
if content != original_content:
with open(makefile_path, 'w') as f:
f.write(content)
return True
return False
def patch_torque_makefile():
"""Patch torque.host.mk specifically to add necessary libraries and frameworks."""
torque_mk = 'out/tools/v8_gypfiles/torque.host.mk'
if not os.path.exists(torque_mk):
return False
with open(torque_mk, 'r') as f:
content = f.read()
original_content = content
# Add C++ library to LIBS if not present
if 'LIBS := ' in content and 'LIBS := -lc++' not in content:
content = re.sub(
r'^LIBS\s*:=\s*$',
'LIBS := -lc++',
content,
flags=re.MULTILINE
)
# Add CoreFoundation framework for macOS time zone support
if 'LDFLAGS_Release :=' in content and '-framework CoreFoundation' not in content:
content = re.sub(
r'(LDFLAGS_Release\s*:=\s*.*?)(?=\\?$)',
r'\1 \\\n\t-framework CoreFoundation',
content,
flags=re.MULTILINE
)
# Also add CoreFoundation to Debug builds if they exist
if 'LDFLAGS_Debug :=' in content and '-framework CoreFoundation' not in content:
content = re.sub(
r'(LDFLAGS_Debug\s*:=\s*.*?)(?=\\?$)',
r'\1 \\\n\t-framework CoreFoundation',
content,
flags=re.MULTILINE
)
# Add system libraries for proper symbol resolution
if 'LIBS := -lc++' in content and '-framework CoreFoundation' not in content:
content = content.replace('LIBS := -lc++', 'LIBS := -lc++ -framework CoreFoundation')
# Add system libraries for V8 and C++ standard library symbols
if 'LIBS := -lc++ -framework CoreFoundation' in content:
# Add system libraries that V8 and C++ standard library might need
if '-framework SystemConfiguration' not in content:
content = content.replace('LIBS := -lc++ -framework CoreFoundation',
'LIBS := -lc++ -framework CoreFoundation -framework SystemConfiguration')
# Add essential system libraries for threading and memory management
if '-lpthread' not in content and '-lSystem' not in content:
content = content.replace('LIBS := -lc++ -framework CoreFoundation -framework SystemConfiguration',
'LIBS := -lc++ -framework CoreFoundation -framework SystemConfiguration -lpthread -lSystem')
# Add system libraries to LDFLAGS as well
if 'LDFLAGS_Release :=' in content:
# Add SystemConfiguration framework for networking and system calls
if '-framework SystemConfiguration' not in content:
content = re.sub(
r'(LDFLAGS_Release\s*:=\s*.*?)(?=\\?$)',
r'\1 \\\n\t-framework SystemConfiguration \\\n\t-lpthread \\\n\t-lSystem',
content,
flags=re.MULTILINE
)
if 'LDFLAGS_Debug :=' in content:
if '-framework SystemConfiguration' not in content:
content = re.sub(
r'(LDFLAGS_Debug\s*:=\s*.*?)(?=\\?$)',
r'\1 \\\n\t-framework SystemConfiguration \\\n\t-lpthread \\\n\t-lSystem',
content,
flags=re.MULTILINE
)
if content != original_content:
with open(torque_mk, 'w') as f:
f.write(content)
return True
return False
def main():
makefile_dir = 'out/tools/v8_gypfiles'
if not os.path.isdir(makefile_dir):
print(f"Error: {makefile_dir} directory not found")
sys.exit(1)
patched_count = 0
for filename in os.listdir(makefile_dir):
if filename.endswith('.host.mk'):
filepath = os.path.join(makefile_dir, filename)
if patch_makefile(filepath):
patched_count += 1
print(f"β
Patched {filename}")
print(f"\nβ
Successfully patched {patched_count} host makefile(s)")
# Also patch the root Makefile for alink command
if patch_root_makefile():
print("β
Patched root Makefile alink command")
# Patch torque.host.mk specifically to add C++ library and CoreFoundation
if patch_torque_makefile():
print("β
Configured torque.host.mk with C++ library and CoreFoundation")
if __name__ == '__main__':
main()