tree-sitter/script/build-wasm
Matthew Krupcale ee9a3c0ebb lib: remove utf8proc dependency (#436)
* Remove dependency on utf8proc

This removes the only external dependency on utf8proc for UTF-8 decoding. It does so by implementing its own UTF-8 decoder. This decoder is both faster and has a simpler API.

 * .gitmodules: remove utf8proc submodule
 * docs/section-2-using-parsers.md: remove requirement for utf8proc submodule
 * docs/section-6-contributing.md: likewise
 * lib/Cargo.toml: remove utf8proc subdirectory package include
 * lib/README.md: remove utf8proc subdirectory description
 * lib/binding_rust/build.rs: remove utf8proc compiler include directory
 * lib/src/lexer.c: remove utf8proc dependencies and types
 * lib/src/lib.c: remove utf8proc dependency
 * lib/src/unicode.h: define types for Unicode decoders
 * lib/src/utf16.{c,h}: implement more readable UTF-16 decoder
 * lib/src/utf8.{c,h}: implement fast UTF-8 decoder
 * lib/utf8proc: remove utf8proc submodule directory
 * script/build-lib: remove utf8proc compiler include directory
 * script/build-wasm: likewise

* Optimize ts_lexer__get_lookahead.

Try to favor non-failure code path and assign lookahead values directly to lexer

 * lib/src/lexer.c: optimize for non-failure code path

* Fix some compiler errors

 * lib/src/lexer.c: cast from signed to unsigned for decode_next result
 * lib/src/utf16.c: fix non-constant initializers for older compilers

* Remove some missed remnants of utf8proc

 * docs/section-2-using-parsers.md: only two include paths necessary now
 * lib/src/lib.c: no need to define UTF8PROC_STATIC

* Use ICU's utf8 and utf16 decoding routines

* Remove unnecessary casts when calling icu macros

* Check buffer length before attempting to decode a unicode character

* Use new unicode function when parsing Queries

Co-Authored-By: Matthew Krupcale <mkrupcale@matthewkrupcale.com>

* Mark libicu files as vendored for GitHub's stats
2019-10-14 11:18:39 -07:00

123 lines
3 KiB
Bash
Executable file

#!/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
exports=$(cat ${web_dir}/exports.json)
emscripten_flags="-O3"
minify_js=1
force_docker=0
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
export EMCC_FORCE_STDLIBS=libc++
emcc=emcc
elif which docker > /dev/null; then
emcc="docker run \
--rm \
-v $(pwd):/src:Z \
-u $(id -u) \
-e EMCC_FORCE_STDLIBS=libc++ \
trzeci/emscripten-slim \
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 "EXPORTED_FUNCTIONS=${exports}" \
$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 \
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