Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/bin/compile.py
1390 views
1
#!/usr/bin/env python-native
2
import os, subprocess, sys
3
4
RUN = "wasi-js"
5
6
SCRIPT_DIR = r"""SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"""
7
8
9
def run(args):
10
# print(' '.join(args)) -- don't enable except for debugging, since would obviously break scripts trying to parse output
11
a = subprocess.run(args, capture_output=True, check=False)
12
sys.stdout.write(a.stdout.decode('utf-8'))
13
sys.stderr.write(a.stderr.decode('utf-8'))
14
if a.returncode:
15
sys.exit(1)
16
17
18
def build(compiler):
19
args = [
20
"zig",
21
compiler,
22
"-target",
23
"wasm32-wasi",
24
"-D_WASI_EMULATED_SIGNAL",
25
"-D_WASI_EMULATED_PROCESS_CLOCKS",
26
]
27
prev = None
28
make_exe = False
29
wasm_file = '"${BASH_SOURCE[0]}.wasm"'
30
for x in sys.argv[1:]:
31
if prev == '-o' and '-c' not in sys.argv:
32
args.append(x + '.wasm')
33
wasm_file = x + '.wasm'
34
make_exe = x
35
else:
36
args.append(x)
37
prev = x
38
if not make_exe and '-c' not in sys.argv:
39
# E.g., zcc foo.c produces a.out always.
40
make_exe = "a.out"
41
args.append("-o")
42
args.append("a.out.wasm")
43
wasm_file = 'a.out.wasm'
44
run(args)
45
if make_exe:
46
file = open(make_exe, 'w')
47
file.write('#!/usr/bin/env bash\n' + SCRIPT_DIR + '\n' + RUN +
48
' "${SCRIPT_DIR}/' + wasm_file + '" "$@"')
49
file.close()
50
run(["chmod", "+x", make_exe])
51
52