-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.py
executable file
·121 lines (98 loc) · 2.82 KB
/
build.py
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
#!/usr/bin/env python3
import glob
import os
import re
import subprocess
import random
import shutil
import string
import sys
RELEASE = len(sys.argv) > 1 and sys.argv[1] == 'release'
clang = 'clang'
wasmld = 'wasm-ld'
try:
subprocess.run(['clang-10', '-v'], stderr=subprocess.DEVNULL)
clang = 'clang-10'
except FileNotFoundError:
pass
try:
subprocess.run(['wasm-ld-10', '-v'], stdout=subprocess.DEVNULL)
wasmld = 'wasm-ld-10'
except FileNotFoundError:
pass
[os.remove(f) for f in glob.iglob('build/dist/*', recursive=True)]
for ext in ['*.o', '*.wasm', '*.wat']:
[os.remove(f) for f in glob.iglob('build/**/' + ext, recursive=True)]
os.makedirs('build', exist_ok=True)
os.chdir('build')
flags = [
'--target=wasm32',
'-c', '-O2', '-flto',
'-nostdlib',
'-I', '../src/fakestdlib',
'-Wall', '-Wno-builtin-requires-header',
]
print('Compiling .wat files:')
for watfile in glob.glob('../src/**/*.wat', recursive=True):
print('- ' + watfile)
subprocess.run(['wat2wasm', watfile])
print('Compiling .c files:')
ofiles = []
for cfile in glob.glob('../src/**/*.c', recursive=True):
safename = re.sub(r'[^a-zA-Z0-9]', '_', cfile[0:-2])
ofile = safename + '.o'
print('- ' + cfile)
subprocess.run([clang] + flags + ['-o', ofile, cfile])
ofiles.append(ofile)
print('Linking...')
subprocess.run([
wasmld,
'--no-entry',
'--export-all', '--no-gc-sections',
'--allow-undefined',
'--import-memory',
'--lto-O2',
'-o', 'regex.wasm',
] + ofiles)
# Optimize output WASM file
if RELEASE:
print('Optimizing WASM...')
subprocess.run([
'wasm-opt', 'regex.wasm',
'-o', 'regex.wasm',
'-O2', # general perf optimizations
'--memory-packing', # remove unnecessary and extremely large .bss segment
'--zero-filled-memory',
])
#
# Output the dist folder for upload
#
print('Building dist folder...')
os.chdir('..')
os.makedirs('build/dist', exist_ok=True)
buildId = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8)) # so beautiful. so pythonic.
root = 'src/index.html'
assets = [
'src/normalize.css',
'build/regex.wasm',
'build/sys.wasm',
]
rootContents = open(root).read()
def addId(filename, id):
parts = filename.split('.')
parts.insert(-1, buildId)
return '.'.join(parts)
for asset in assets:
basename = os.path.basename(asset)
newFilename = addId(basename, buildId)
shutil.copy(asset, 'build/dist/{}'.format(newFilename))
rootContents = rootContents.replace(basename, newFilename)
with open('build/dist/index.html', 'w') as f:
f.write(rootContents)
# Produce a WAT version of the code for inspection.
if not RELEASE:
os.chdir('build')
print('Producing .wat files...')
subprocess.run(['wasm2wat', 'regex.wasm', '-o', 'regex.wat'])
os.chdir('..')
print('Done!')