summaryrefslogtreecommitdiff
path: root/build-js.sh
diff options
context:
space:
mode:
authorRasmus <rasmus@notion.se>2016-09-02 13:41:08 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-09-02 13:41:08 -0700
commitf872f2d456921df1c2b256b9c448031371d8a713 (patch)
tree5e88c767aed7b2c2474aaae06cd8cd53d958a74f /build-js.sh
parent1651ac798181107f8547b58c4c129c977f2b11e8 (diff)
downloadbinaryen-f872f2d456921df1c2b256b9c448031371d8a713.tar.gz
binaryen-f872f2d456921df1c2b256b9c448031371d8a713.tar.bz2
binaryen-f872f2d456921df1c2b256b9c448031371d8a713.zip
Improvements to build-js.sh and JS API (#679)
* Adds command-line argument to build-js.sh for specifying the location of Emscripten. Also spreads out program arguments on separate lines * Exposes WasmBinaryWriter and BufferWithRandomAccess in the JS API to allow writing WASM modules and access the produced bytes via the JS API * Updates bin/binaryen.js and bin/wasm.js from changes to build-js.sh * Adds exclude patterns to .gitignore for some files generated by build-js.sh and .DS_Store for macOS * Changes build-js.sh to use EMSCRIPTEN env var instead of a command-line argument to provide the path to emscripten * Improvements to JS builds - Adds -g flag to build-js.sh that will build unoptimized and unmangled versions of binaryen.js and wasm.js (output has a "-g.js" suffix to allow co-existence with regular optimized builds). - Enables closure compiler for non-debug builds - Adds browser test for s-expression parser + WASM code gen (requires wasm to be enabled in the browser.) - Adds iterator interface to BufferWithRandomAccess (when Symbol.iterator is available) - Adds toArrayBuffer to BufferWithRandomAccess (when TypedArray is available) - Adds compileWast(sourceText :string) :ArrayBuffer to the module, parsing & compiling s-expression code to a WASM module - Changes the way binaryen.js is exported to allow usage in CommonJS, AMD and UMD envionments. * Expose "Binaryen" global in a better way to work with a.js generated by check.py * Fix to binaryen.js to only export a global variable when running the test (a.js) while inside a module (avoids polluting global in e.g. nodejs). Also fixes a spelling mistake. * Better "no WASM detected" message in test/binaryen.js/browser.html * Small change to error message in build-js.sh where $EMSCRIPTEN does not point to a directory * Changes emcc args in build-js.sh after investingating a large number of argument combinations. Also adds a browser benchmark. The result of emcc arguments and the effect on performance is summarized in this doc: https://gist.github.com/rsms/e33c61a25a31c08260161a087be03169 * Enable inferring emscripten path by looking in PATH when EMSCRIPTEN is not set in env
Diffstat (limited to 'build-js.sh')
-rwxr-xr-xbuild-js.sh116
1 files changed, 113 insertions, 3 deletions
diff --git a/build-js.sh b/build-js.sh
index 71ff29660..d4951402f 100755
--- a/build-js.sh
+++ b/build-js.sh
@@ -3,12 +3,122 @@
# to run this, as the builds are bundled in the repo in bin/. Running this is
# useful if you are a developer and want to update those builds.
#
+# Usage: build-js.sh
+# Usage: EMSCRIPTEN=path/to/emscripten build-js.sh # explicit emscripten dir
+#
+# Emscripten's em++ and tools/webidl_binder.py will be accessed through the
+# env var EMSCRIPTEN, e.g. ${EMSCRIPTEN}/em++
+#
+# You can get emscripten from
+# http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html
+#
+set -e
+
+if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "-help" ]; then
+ echo "usage: $0 [-g]" >&2
+ echo " -g produce debug build" >&2
+ echo ""
+ echo "If EMSCRIPTEN is set in the envionment, emscripten will be loaded"
+ echo "from that directory. Otherwise the location of emscripten is resolved"
+ echo "through PATH."
+ exit 1
+fi
+
+if [ -z $EMSCRIPTEN ]; then
+ if (which emcc >/dev/null); then
+ # Found emcc in PATH -- set EMSCRIPTEN (we need this to access webidl_binder.py)
+ EMSCRIPTEN=$(dirname "$(which emcc)")
+ else
+ echo "$0: EMSCRIPTEN environment variable is not set and emcc was not found in PATH" >&2
+ exit 1
+ fi
+elif [ ! -d "$EMSCRIPTEN" ]; then
+ echo "$0: \"$EMSCRIPTEN\" (\$EMSCRIPTEN) is not a directory" >&2
+ exit 1
+fi
+
+EMCC_ARGS="-std=c++11 --memory-init-file 0"
+EMCC_ARGS="$EMCC_ARGS -s ALLOW_MEMORY_GROWTH=1"
+EMCC_ARGS="$EMCC_ARGS -s DEMANGLE_SUPPORT=1"
+OUT_FILE_SUFFIX=
+
+if [ "$1" == "-g" ]; then
+ EMCC_ARGS="$EMCC_ARGS -O0"
+ EMCC_ARGS="$EMCC_ARGS --llvm-opts 0 --llvm-lto 0"
+ EMCC_ARGS="$EMCC_ARGS -profiling"
+ OUT_FILE_SUFFIX=-g
+else
+ EMCC_ARGS="$EMCC_ARGS -Oz"
+ EMCC_ARGS="$EMCC_ARGS --llvm-lto 1"
+ EMCC_ARGS="$EMCC_ARGS -s ELIMINATE_DUPLICATE_FUNCTIONS=1"
+ # Why these settings?
+ # See https://gist.github.com/rsms/e33c61a25a31c08260161a087be03169
+fi
+
echo "building wasm.js"
-em++ -std=c++11 src/wasm-js.cpp src/passes/pass.cpp src/passes/Print.cpp src/emscripten-optimizer/parser.cpp src/emscripten-optimizer/simple_ast.cpp src/emscripten-optimizer/optimizer-shared.cpp src/support/colors.cpp src/support/safe_integer.cpp src/support/bits.cpp src/support/threads.cpp src/asmjs/asm_v_wasm.cpp src/asmjs/shared-constants.cpp src/wasm.cpp -Isrc/ -o bin/wasm.js -s MODULARIZE=1 -s 'EXPORT_NAME="WasmJS"' --memory-init-file 0 -Oz -s ALLOW_MEMORY_GROWTH=1 -profiling -s DEMANGLE_SUPPORT=1 #-DWASM_JS_DEBUG -DWASM_INTERPRETER_DEBUG=2
+"$EMSCRIPTEN/em++" \
+ $EMCC_ARGS \
+ src/wasm-js.cpp \
+ src/passes/pass.cpp \
+ src/passes/Print.cpp \
+ src/emscripten-optimizer/parser.cpp \
+ src/emscripten-optimizer/simple_ast.cpp \
+ src/emscripten-optimizer/optimizer-shared.cpp \
+ src/support/colors.cpp \
+ src/support/safe_integer.cpp \
+ src/support/bits.cpp \
+ src/support/threads.cpp \
+ src/asmjs/asm_v_wasm.cpp \
+ src/asmjs/shared-constants.cpp \
+ src/wasm.cpp \
+ -Isrc/ \
+ -o bin/wasm${OUT_FILE_SUFFIX}.js \
+ -s MODULARIZE=1 \
+ -s 'EXPORT_NAME="WasmJS"'
+ #-DWASM_JS_DEBUG
+ #-DWASM_INTERPRETER_DEBUG=2
echo "building binaryen.js"
-python ~/Dev/emscripten/tools/webidl_binder.py src/js/binaryen.idl glue
-em++ -std=c++11 src/binaryen-js.cpp src/passes/pass.cpp src/passes/MergeBlocks.cpp src/passes/Print.cpp src/passes/RemoveUnusedBrs.cpp src/passes/RemoveUnusedNames.cpp src/passes/PostEmscripten.cpp src/passes/SimplifyLocals.cpp src/passes/ReorderLocals.cpp src/passes/Vacuum.cpp src/passes/DuplicateFunctionElimination.cpp src/passes/CoalesceLocals.cpp src/emscripten-optimizer/parser.cpp src/emscripten-optimizer/simple_ast.cpp src/emscripten-optimizer/optimizer-shared.cpp src/support/colors.cpp src/support/safe_integer.cpp src/support/bits.cpp src/support/threads.cpp src/asmjs/asm_v_wasm.cpp src/asmjs/shared-constants.cpp src/wasm.cpp -Isrc/ -o bin/binaryen.js -s MODULARIZE=1 -s 'EXPORT_NAME="Binaryen"' --memory-init-file 0 -Oz -s ALLOW_MEMORY_GROWTH=1 -profiling -s DEMANGLE_SUPPORT=1 -s INVOKE_RUN=0 --post-js glue.js
+if [ "$1" != "-g" ]; then
+ EMCC_ARGS="$EMCC_ARGS --closure 1"
+fi
+
+python "$EMSCRIPTEN/tools/webidl_binder.py" src/js/binaryen.idl glue
+
+"$EMSCRIPTEN/em++" \
+ $EMCC_ARGS \
+ -std=c++11 \
+ src/binaryen-js.cpp \
+ src/passes/pass.cpp \
+ src/passes/MergeBlocks.cpp \
+ src/passes/Print.cpp \
+ src/passes/RemoveUnusedBrs.cpp \
+ src/passes/RemoveUnusedNames.cpp \
+ src/passes/PostEmscripten.cpp \
+ src/passes/SimplifyLocals.cpp \
+ src/passes/ReorderLocals.cpp \
+ src/passes/Vacuum.cpp \
+ src/passes/DuplicateFunctionElimination.cpp \
+ src/passes/CoalesceLocals.cpp \
+ src/emscripten-optimizer/parser.cpp \
+ src/emscripten-optimizer/simple_ast.cpp \
+ src/emscripten-optimizer/optimizer-shared.cpp \
+ src/support/colors.cpp \
+ src/support/safe_integer.cpp \
+ src/support/bits.cpp \
+ src/support/threads.cpp \
+ src/asmjs/asm_v_wasm.cpp \
+ src/asmjs/shared-constants.cpp \
+ src/wasm.cpp \
+ -Isrc/ \
+ -o bin/binaryen${OUT_FILE_SUFFIX}.js \
+ -s 'EXPORT_NAME="Binaryen"' \
+ --memory-init-file 0 \
+ -s INVOKE_RUN=0 \
+ --pre-js src/js/binaryen.js-pre.js \
+ --post-js glue.js \
+ --post-js src/js/binaryen.js-extended.js \
+ --post-js src/js/binaryen.js-post.js