tree-sitter/script/build-wasm

87 lines
2.2 KiB
Text
Raw Normal View History

2019-04-23 14:29:46 -07:00
#!/usr/bin/env bash
2019-04-26 19:13:32 -07:00
set -e
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
cat <<EOF
USAGE
$0 [--debug]
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 the \`docker\` command on your PATH for this to work.
OPTIONS
--debug: Compile the library more quickly, with fewer optimizations and more runtime assertions.
EOF
exit 0
fi
web_dir=lib/binding_web
exports=$(cat ${web_dir}/exports.json)
2019-04-23 14:29:46 -07:00
args="-Os"
2019-04-23 14:46:46 -07:00
minify=1
2019-04-23 14:29:46 -07:00
if [[ "$1" == "--debug" ]]; then
2019-04-23 14:46:46 -07:00
minify=0
2019-05-01 11:29:35 -07:00
args="-s ASSERTIONS=1 -s SAFE_HEAP=1 -Os"
2019-04-23 14:29:46 -07:00
fi
mkdir -p target/scratch
2019-04-23 14:29:46 -07:00
2019-04-25 17:27:24 -07:00
docker run \
--rm \
-v $(pwd):/src \
-u $(id -u) \
2019-04-27 18:06:34 -07:00
-e EMCC_FORCE_STDLIBS=libc++ \
2019-04-25 17:27:24 -07:00
trzeci/emscripten-slim \
\
emcc \
2019-04-23 14:29:46 -07:00
-s WASM=1 \
-s TOTAL_MEMORY=33554432 \
2019-04-23 14:29:46 -07:00
-s ALLOW_MEMORY_GROWTH \
2019-05-01 11:29:35 -07:00
-s MAIN_MODULE=2 \
-s NO_FILESYSTEM=1 \
-s "EXPORTED_FUNCTIONS=${exports}" \
2019-04-23 14:29:46 -07:00
$args \
-std=c99 \
-D 'fprintf(...)=' \
-I lib/src \
-I lib/include \
-I lib/utf8proc \
--js-library ${web_dir}/imports.js \
--pre-js ${web_dir}/prefix.js \
--post-js ${web_dir}/binding.js \
2019-04-23 14:29:46 -07:00
lib/src/lib.c \
${web_dir}/binding.c \
2019-04-26 19:13:32 -07:00
-o target/scratch/tree-sitter.js
2019-04-23 14:46:46 -07:00
if [[ "$minify" == "1" ]]; then
if [ ! -d ${web_dir}/node_modules/terser ]; then
2019-04-23 14:46:46 -07:00
(
cd ${web_dir}
2019-04-23 14:46:46 -07:00
npm install
)
fi
${web_dir}/node_modules/.bin/terser \
2019-04-23 14:46:46 -07:00
--compress \
--mangle \
--keep-fnames \
--keep-classnames \
-- target/scratch/tree-sitter.js \
> $web_dir/tree-sitter.js
2019-04-23 14:46:46 -07:00
else
cp target/scratch/tree-sitter.js $web_dir/tree-sitter.js
2019-04-23 14:46:46 -07:00
fi
2019-04-26 19:13:32 -07:00
mv target/scratch/tree-sitter.wasm $web_dir/tree-sitter.wasm