-
Notifications
You must be signed in to change notification settings - Fork 36
/
build-binaryen.sh
executable file
·86 lines (69 loc) · 2.74 KB
/
build-binaryen.sh
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
#!/bin/bash
SRC=$(dirname $0)
BUILD="$1"
BINARYEN_SRC="$2"
if [ "$BINARYEN_SRC" == "" ]; then
BINARYEN_SRC=$(pwd)/upstream/binaryen
fi
if [ "$BUILD" == "" ]; then
BUILD=$(pwd)/build
fi
SRC=$(realpath "$SRC")
BUILD=$(realpath "$BUILD")
BINARYEN_BUILD=$BUILD/binaryen
# If we don't have a copy of binaryen, make one
if [ ! -d $BINARYEN_SRC/ ]; then
git clone --depth 1 https://github.com/WebAssembly/binaryen.git "$BINARYEN_SRC/"
pushd $BINARYEN_SRC/
# This is the last tested commit of binaryen.
# Feel free to try with a newer version
COMMIT=8ab8e40d15a4d9f28ced76d28232f9e791f161d3
git fetch origin $COMMIT
git reset --hard $COMMIT
git submodule init
git submodule update
popd
fi
if [ ! -d $BINARYEN_BUILD/ ]; then
LDFLAGS="\
-s ALLOW_MEMORY_GROWTH=1 \
-s EXPORTED_FUNCTIONS=_main,_free,_malloc \
-s EXPORTED_RUNTIME_METHODS=FS,PROXYFS,ERRNO_CODES,allocateUTF8 \
-lproxyfs.js \
--js-library=$SRC/emlib/fsroot.js \
" emcmake cmake -G Ninja \
-S $BINARYEN_SRC/ \
-B $BINARYEN_BUILD/ \
-DCMAKE_BUILD_TYPE=Release
# Binaryen likes to build single files, but that uses base64 and is less compressible.
# Make sure we build a separate wasm file
sed -i -E 's/-s\s*SINGLE_FILE(=[^ ]*)?//g' $BINARYEN_BUILD/build.ninja
# Binaryen likes to build with -flto, which is great.
# However, LTO generates objects file with LLVM-IR bitcode rather than WebAssembly.
# The patching mechanism to generate binaryen-box only understands wasm object files.
# Because of that, we need to disable LTO.
sed -i -E 's/-flto//g' $BINARYEN_BUILD/build.ninja
# Binaryen builds with NODERAWFS, which is not compatible with browser workflows.
# Disable it.
sed -i -E 's/-s\s*NODERAWFS(\s*=\s*[^ ]*)?//g' $BINARYEN_BUILD/build.ninja
# Make sure we build js modules (.mjs).
# The patch-ninja.sh script assumes that.
sed -i -E 's/\.js/.mjs/g' $BINARYEN_BUILD/build.ninja
# The mjs patching is over zealous, and patches some source JS files rather than just output files.
# Undo that.
sed -i -E 's/\.mjs-/.js-/g' $BINARYEN_BUILD/build.ninja
sed -i -E 's/(pre|post|proxyfs|fsroot)\.mjs/\1.js/g' $BINARYEN_BUILD/build.ninja
# Patch the build script to add the "binaryen-box" target.
# This new target bundles many executables in one, reducing the total size.
pushd $SRC
TMP_FILE=$(mktemp)
./patch-ninja.sh \
$BINARYEN_BUILD/build.ninja \
binaryen-box \
$BUILD/tooling \
wasm-as wasm-ctor-eval wasm-emscripten-finalize wasm-metadce wasm-opt wasm-shell \
> $TMP_FILE
cat $TMP_FILE >> $BINARYEN_BUILD/build.ninja
popd
fi
cmake --build $BINARYEN_BUILD/ -- binaryen-box