-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
emsdk-cc
456 lines (353 loc) · 11.9 KB
/
emsdk-cc
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env python3
import sys
import os
from pathlib import Path
def dbg(*argv, **kw):
kw.setdefault("file", sys.stderr)
return print(*argv, **kw)
SDKROOT = Path(os.environ.get("SDKROOT", "/opt/python-wasm-sdk"))
EMSDK = Path(os.environ.get("EMSDK", "/opt/python-wasm-sdk/emsdk"))
PREFIX = Path(os.environ.get("PREFIX", "/opt/python-wasm-sdk/devices/emsdk/usr"))
sys.argv.pop(0)
# point to emscripten cc
if not sys.argv[0].endswith(".py"):
EXEC = f"{EMSDK}/upstream/emscripten/emcc.py"
else:
EXEC = sys.argv.pop(0)
args = sys.argv
def env(k, default):
if default is false:
default = "false"
if default is true:
default = "true"
v = os.environ.get(k, default)
if v == "false":
return False
if v == "true":
return True
return v.strip()
def arglist(*argv):
al = " ".join(argv)
al = al.replace("\n", " ")
while al.find(" ") >= 0:
al = al.replace(" ", " ")
return al.strip().split(" ")
# -Wwarn-absolute-paths
# --valid-abspath ${SDKROOT}
# COMMON="-Wno-unsupported-floating-point-opt"
COMMON = arglist(
"""
-Wno-limited-postlink-optimizations
-Wno-unused-command-line-argument
-Wno-unreachable-code-fallthrough
-Wno-unused-function
""",
os.environ.get("PYDK_CFLAGS", ""),
)
false = False
true = True
PY_MODULE = IS_SHARED = false
SHARED_TARGET = SHARED = ""
MVP = env("MVP", true)
if env("EMMAKEN_JUST_CONFIGURE", false):
CONFIGURE = True
elif env("CONFIG_SITE", false):
CONFIGURE = True
else:
CONFIGURE = env("CONFIGURE", false)
if MVP:
# turn of wasm ex (https://github.com/emscripten-core/emscripten/pull/20536)
# -fno-wasm-exceptions -sEMSCRIPTEN_LONGJMP=0
# -mcpu=generic would activate those https://reviews.llvm.org/D125728
# https://github.com/emscripten-core/emscripten/pull/17689
# -fPIC not allowed with -mno-mutable-globals
# -mno-sign-ext not allowed with pthread
# WASMOPTS="-fno-wasm-exceptions -sSUPPORT_LONGJMP=emscripten"
# CPU="-mnontrapping-fptoint -mno-reference-types -mno-sign-ext -m32"
# bulk and main/side
# https://github.com/emscripten-core/emscripten/issues/22161
# -mno-bulk-memory
CPU = arglist(
"""
-m32
-D_FILE_OFFSET_BITS=64
-sSUPPORT_LONGJMP=emscripten
-mno-bulk-memory
-mnontrapping-fptoint
-mno-reference-types
-mno-sign-ext
-mno-extended-const
-mno-atomics
-mno-tail-call
-mno-fp16
-mno-multivalue
-mno-relaxed-simd
-mno-simd128
-mno-multimemory
-mno-exception-handling
"""
)
else:
CPU = arglist("-D_FILE_OFFSET_BITS=64 -mcpu=bleeding-edge -m64")
# try to keep 32 but maybe with 64 iface (bigint)
WASM_EXTRA = env("WASM_EXTRA", "") + " " + env("WASM_OPTS", "")
# the only sane default for now
COPTS = env("COPTS", "-O2 -g3")
SIZEOPT = '-Os' in COPTS
MAIN_MODULE = LINKING = STATIC = False
EXE = False
HTML = False
MODE = ""
AOUT = ""
SKIP = False
COMPILE = False
USE_RAWFS = True
CMAKE = False
RUSTC = False
NINJA = env("NINJA", false)
out = []
# fix rust calling
for argc, arg in enumerate(sys.argv):
# clean up rustc way of passing args.
if arg in ("-l", "-L", "-I"):
sys.argv[argc] += sys.argv[argc + 1]
sys.argv[argc + 1] = ""
RUSTC = True
while "" in sys.argv:
sys.argv.remove("")
for argc, arg in enumerate(sys.argv):
if arg.startswith("CMakeFiles/") or arg.startswith("@CMakeFiles/"):
CMAKE = True
if arg.startswith("--preload-file") or arg.startswith("--embed-file"):
USE_RAWFS = False
if arg.find("MAIN_MODULE") > 0:
MAIN_MODULE = True
if arg == '-static':
STATIC = True
if arg == "-sENVIRONMENT=web":
EXE = False
HTML = True
for argc, arg in enumerate(sys.argv):
if arg in ("-v", "--version"):
SKIP = True
break
# THEY ARE NOT SAFE TO CHANGE !
if arg in ("-O0", "-O1", "-O2", "-O3", "-Os", "-Oz"):
continue
if arg in ("-g0", "-g1", "-g2", "-g3", "-g4"):
continue
if not MAIN_MODULE:
# https://github.com/emscripten-core/emscripten/issues/22742
# https://github.com/hoodmane/emscripten/commit/34144634026c91a73bd3e1db85627132d3a37a6d
if arg == "-lc":
continue
# only html may not exit runtime.
if not HTML and arg.find("EXIT_RUNTIME") > 0:
continue
if arg.lower() in ("-fpic", "-latomic"):
continue
if arg in ("-Wl,--as-needed", "-Wl,--eh-frame-hdr", "-Wl,-znoexecstack", "-Wl,-znow", "-Wl,-zrelro", "-Wl,-zrelro,-znow"):
continue
if arg in ("-lgcc", "-lgcc_s", "-fallow-argument-mismatch"):
continue
if arg == "-pthread":
if MVP:
continue
# FAILSAFE
# that is for some very bad known setup.py behaviour regarding cross compiling and some old codebases.
# should not be needed ..
if arg.startswith("-I/"):
if arg.startswith("-I/usr/"):
continue
if arg.startswith("-L/"):
if arg.startswith("-L/usr/"):
continue
if arg.find("ASSERTIONS") > 0:
continue
# rustc has an habit of "-l" "c" instead of "-lc"
if arg.startswith("-l"):
if len(arg) > 2:
LINKING = True
# prevent duplicate lib when linking
if arg in out:
continue
elif arg in ("-o", "-c"):
MODE = arg
MODE_POS = argc
if arg == "-c":
COMPILE = True
# TODO maybe add node runner for a .cjs
elif arg == "-o":
out_pos = argc + 1
if IS_SHARED:
SHARED_TARGET = sys.argv[out_pos]
elif not AOUT:
AOUT = sys.argv[out_pos]
elif not STATIC:
if arg.endswith(".so") and arg.startswith("/usr/lib"):
arg = f"-l{arg.rsplit('/',1)[-1][3:-3]}"
if arg in ('-lportmidi','-lporttime'):
continue
elif arg.endswith(".so") or arg == "-shared" or arg.find("SIDE_MODULE") > 0:
IS_SHARED = True
if arg == "-shared":
pass
elif arg.endswith(".so"):
if arg.find("wasm32-emscripten.so") > 0 or arg.find("abi3.so") > 0:
PY_MODULE = true
SHARED_TARGET = arg
out.append(arg)
SHARED = f"-shared -sASSERTIONS=0 -sSIDE_MODULE=1 -L{os.environ['PREFIX']}/lib"
continue
# duplicates can happen on cmake/rustc but they are expected to be here for a reason so skip them
if not (CMAKE or NINJA or RUSTC):
# prevent duplicates objects/archives files on cmdline when linking
if LINKING or MODE == "-o":
if arg.endswith(".a") or arg.endswith(".o"):
if arg in out:
continue
# fix sysroot is not default to PIC
arg = arg.replace("/lib/wasm32-emscripten/lib","/lib/wasm32-emscripten/pic/lib")
out.append(arg)
os.environ.pop("_EMCC_CCACHE", "")
# if $MVP
# then
# if $WASM_PURE
# then
# SOTMP=$(mktemp).so
# mv $SHARED_TARGET $SOTMP
# # --memory64-lowering --signext-lowering
# $SDKROOT/emsdk/upstream/bin/wasm-emscripten-finalize -mvp $SOTMP -o $SHARED_TARGET
# [ -f $SHARED_TARGET.map ] && rm $SHARED_TARGET.map
# rm $SOTMP
# fi
# fi
final = [EXEC]
# a.out test misses fpic
if CONFIGURE and len(out) == 1:
SKIP = out[0] == "conftest.c"
sys.argv.insert(0, "-fPIC")
sys.argv.insert(0, "-sASSERTIONS=0")
sys.argv.extend(CPU)
if SKIP:
final.extend(sys.argv)
else:
if AOUT:
if CMAKE or CONFIGURE:
EXE = False
# should not happen
elif AOUT.endswith(".o") and "-c" not in out:
final.append("-c")
EXE = False
MAIN_MODULE = False
elif AOUT.endswith(".html"):
MAIN_MODULE = True
EXE = False
HTML = True
# emscripten aware build
elif AOUT.endswith(".cjs") or AOUT.endswith(".js"):
MAIN_MODULE = True
EXE = True
# a.out case
elif "-c" not in out:
EXE = True
else:
EXE = False
# potentially executable running via js
# TODO: handle js runtimes other than shipped node.
# TODO: full node path
if (EXE and USE_RAWFS) and not HTML:
if AOUT.endswith(".cjs") or AOUT.endswith(".js"):
def make_exe(*argv, **kw):
global AOUT, CONFIGURE
if os.path.isfile(AOUT) and not CONFIGURE:
try:
with open(AOUT, "r") as file:
bin = file.read()
with open(AOUT, "w") as file:
file.write("#!/usr/bin/env node\n")
file.write(bin)
os.chmod(AOUT, 0o766)
except Exception as e:
dbg("ERROR: 292", e)
# the build system is old and exe has no suffix from cmake or configure
else:
def make_exe(*argv, **kw):
global AOUT, CONFIGURE
if os.path.isfile(AOUT) and os.path.isfile(AOUT + ".wasm") and not CONFIGURE:
os.rename(AOUT, AOUT + ".cjs")
try:
with open(AOUT, "w") as file:
file.write("#!/usr/bin/env bash\n")
file.write('node $0.cjs "$@"\n')
except Exception as e:
dbg("ERROR: 306", e)
os.rename(AOUT + ".cjs", AOUT)
return
try:
os.chmod(AOUT, 0o766)
except Exception as e:
dbg("ERROR: 312", e)
if SIZEOPT:
final.append("-sENVIRONMENT=node")
# error: explicitly setting EXIT_RUNTIME not compatible with STANDALONE_WASM.
# EXIT_RUNTIME will always be True for programs (with a main function) and False for reactors (not main function).
# final.append("-sEXIT_RUNTIME")
if USE_RAWFS:
# final.append("-sASYNCIFY")
final.append("-sNODERAWFS")
__import__("atexit").register(make_exe)
elif HTML:
if SIZEOPT and ("-sENVIRONMENT=web" not in out):
final.append("-sENVIRONMENT=web")
# maybe not pass all WASM opts when -c + -o but always PIC and opt level
final.extend(arglist("-fPIC", SHARED, COPTS))
# prevent use of extended instr
final.extend(CPU)
if IS_SHARED or LINKING:
# , "-gsource-map --source-map-base /"))
final.append(f"-L{PREFIX}/lib")
final.extend(arglist(WASM_EXTRA, env("LDFLAGS", "")))
else:
final.append(f"-I{PREFIX}/include")
if MAIN_MODULE:
# TODO: module level
# -gsource-map --source-map-base /
pass
final.extend(arglist(WASM_EXTRA, "-D__PYDK__=1"))
final.extend(out)
final.extend(COMMON)
sys.path.insert(0, str(Path(EXEC).parent))
sys.argv.clear()
EMCC_TRACE = env("EMCC_TRACE", false)
if EMCC_TRACE:
DEBUG_PATTERN = env("DEBUG_PATTERN", "main")
def dump():
dbg(
f"""
{COMMON=}
{CPU=}
{out=}
{LINKING=}
{PY_MODULE=} {SHARED_TARGET=}
{MODE=} {EXE=}
{final=}
{' '.join(sys.argv)}
"""
)
while len(final):
arg = final.pop(0)
# add debug filters here.
sys.argv.append(arg)
# for debugging configure, cmake has its own very detailed log.
if os.path.isfile("conftest.c"):
__import__("shutil").copy("conftest.c", SDKROOT / "emcc.c")
if DEBUG_PATTERN not in (False, True):
with open("conftest.c", "r") as file:
if file.read().find(DEBUG_PATTERN) > 0:
dump()
if DEBUG_PATTERN == "*":
dump()
else:
sys.argv.extend(final)
exec(open(EXEC, "r").read(), globals(), globals())