-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-wasm
executable file
·124 lines (104 loc) · 3.35 KB
/
build-wasm
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
#!/usr/bin/env bash
usage() {
cat <<EOF
USAGE
$0 [--help] [--debug] [--docker]
SUMMARY
Compile the Tree-sitter WASM library. This will create two files in the
\`lib/binding_web\` directory: \`tree-sitter.js\` and \`tree-sitter.wasm\`.
REQUIREMENTS
You must have either the \`emcc\` command or the \`docker\` command
on your PATH for this to work.
OPTIONS
--help: Display this message.
--debug: Compile the library more quickly, with fewer optimizations
and more runtime assertions.
--docker: Run emscripten using docker, even if \`emcc\` is installed.
By default, \`emcc\` will be run directly when available.
EOF
}
set -e
web_dir=lib/binding_web
emscripten_flags="-O3"
minify_js=1
force_docker=0
emscripen_version=$(cat "$(dirname "$0")"/../cli/emscripten-version)
while [[ $# > 0 ]]; do
case "$1" in
--debug)
minify_js=0
emscripten_flags="-s ASSERTIONS=1 -s SAFE_HEAP=1 -O0"
;;
--help)
usage
exit 0
;;
--docker)
force_docker=1
;;
*)
usage
echo "Unrecognized argument '$1'"
exit 1
;;
esac
shift
done
emcc=
if which emcc > /dev/null && [[ "$force_docker" == "0" ]]; then
emcc=emcc
elif which docker > /dev/null; then
emcc="docker run \
--rm \
-v $(pwd):/src:Z \
-u $(id -u) \
emscripten/emsdk:$emscripen_version \
emcc"
else
echo 'You must have either `docker` or `emcc` on your PATH to run this script'
exit 1
fi
mkdir -p target/scratch
# Use emscripten to generate `tree-sitter.js` and `tree-sitter.wasm`
# in the `target/scratch` directory
$emcc \
-s WASM=1 \
-s TOTAL_MEMORY=33554432 \
-s ALLOW_MEMORY_GROWTH=1 \
-s MAIN_MODULE=2 \
-s NO_FILESYSTEM=1 \
-s NODEJS_CATCH_EXIT=0 \
-s NODEJS_CATCH_REJECTION=0 \
-s EXPORTED_FUNCTIONS=@${web_dir}/exports.json \
$emscripten_flags \
-std=c99 \
-D 'fprintf(...)=' \
-D NDEBUG= \
-I lib/src \
-I lib/include \
--js-library ${web_dir}/imports.js \
--pre-js ${web_dir}/prefix.js \
--post-js ${web_dir}/binding.js \
--post-js ${web_dir}/suffix.js \
lib/src/lib.c \
${web_dir}/binding.c \
-o target/scratch/tree-sitter.js
# Use terser to write a minified version of `tree-sitter.js` into
# the `lib/binding_web` directory.
if [[ "$minify_js" == "1" ]]; then
if [ ! -d ${web_dir}/node_modules/terser ]; then
(
cd ${web_dir}
npm install
)
fi
${web_dir}/node_modules/.bin/terser \
--compress \
--mangle \
--keep-classnames \
-- target/scratch/tree-sitter.js \
> $web_dir/tree-sitter.js
else
cp target/scratch/tree-sitter.js $web_dir/tree-sitter.js
fi
mv target/scratch/tree-sitter.wasm $web_dir/tree-sitter.wasm