File: //home/ubuntu/neovim/.deps/build/src/treesitter/script/build-wasm
#!/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
SRC_DIR=lib/src
EMSCRIPTEN_VERSION=$(< cli/loader/emscripten-version)
verbose=0
force_docker=0
emscripten_flags=(-O3 --minify 0)
while (($# > 0)); do
case "$1" in
--debug)
emscripten_flags=(-s ASSERTIONS=1 -s SAFE_HEAP=1 -O0 -g)
;;
--help)
usage
exit 0
;;
--docker)
force_docker=1
;;
-v|--verbose)
verbose=1
;;
*)
usage
printf "Unrecognized argument '%s'\n" "$1" >&2
exit 1
;;
esac
shift
done
if [[ $verbose == 1 ]]; then
emscripten_flags+=(-s VERBOSE=1 -v)
fi
emcc=
docker=
if [[ $force_docker == 0 ]] && command -v emcc > /dev/null; then
emcc=emcc
elif command -v docker > /dev/null; then
# detect which one to use
docker=docker
elif command -v podman > /dev/null; then
docker=podman
fi
if [[ -z $emcc ]] && [[ -n $docker ]]; then
if [[ $docker == podman ]]; then
export PODMAN_USERNS=keep-id
fi
emcc="$docker run \
--rm \
-v $PWD:/src:Z \
-u $UID \
emscripten/emsdk:$EMSCRIPTEN_VERSION \
emcc"
fi
if [[ -z $emcc ]]; then
if [[ $force_docker == 1 ]]; then
# shellcheck disable=SC2016
printf 'You must have `docker` or `podman` in your PATH to run this script with --docker\n' >&2
else
# shellcheck disable=SC2016
printf 'You must have either `docker`, `podman`, or `emcc` in your PATH to run this script\n' >&2
fi
exit 1
fi
mkdir -p target/scratch
runtime_methods=stringToUTF16,AsciiToString
# Remove quotes, add leading underscores, remove newlines, remove trailing comma.
exported_functions=$(
cat ${SRC_DIR}/wasm/stdlib-symbols.txt ${WEB_DIR}/exports.txt |
sed -e 's/"//g;s/^/_/g' | tr -d '\n' | sed -e 's/,$//'
)
# Use emscripten to generate `tree-sitter.js` and `tree-sitter.wasm`
# in the `target/scratch` directory
$emcc \
-s WASM=1 \
-s INITIAL_MEMORY=33554432 \
-s ALLOW_MEMORY_GROWTH=1 \
-s SUPPORT_BIG_ENDIAN=1 \
-s MAIN_MODULE=2 \
-s FILESYSTEM=0 \
-s NODEJS_CATCH_EXIT=0 \
-s NODEJS_CATCH_REJECTION=0 \
-s EXPORTED_FUNCTIONS="${exported_functions}" \
-s EXPORTED_RUNTIME_METHODS=$runtime_methods \
"${emscripten_flags[@]}" \
-fno-exceptions \
-std=c11 \
-D 'fprintf(...)=' \
-D NDEBUG= \
-I ${SRC_DIR} \
-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
mv target/scratch/tree-sitter.js ${WEB_DIR}/tree-sitter.js
mv target/scratch/tree-sitter.wasm ${WEB_DIR}/tree-sitter.wasm