James Kuszmaul | 27da814 | 2019-07-21 16:13:55 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Originally sourced from https://github.com/ribrdb/rules_emscripten |
| 3 | set -euo pipefail |
| 4 | EM_CONFIG="LLVM_ROOT='$(pwd -P)/external/emscripten_clang';" |
| 5 | EM_CONFIG+="EMSCRIPTEN_NATIVE_OPTIMIZER='external/emscripten_clang/optimizer';" |
| 6 | EM_CONFIG+="BINARYEN_ROOT='external/emscripten_clang/binaryen';" |
| 7 | EM_CONFIG+="NODE_JS='$(pwd -P)/external/nodejs/bin/node';" |
| 8 | EM_CONFIG+="EMSCRIPTEN_ROOT='external/emscripten_toolchain';" |
| 9 | EM_CONFIG+="SPIDERMONKEY_ENGINE='';" |
| 10 | EM_CONFIG+="V8_ENGINE='';" |
| 11 | EM_CONFIG+="TEMP_DIR='tmp';" |
| 12 | EM_CONFIG+="COMPILER_ENGINE=NODE_JS;" |
| 13 | EM_CONFIG+="JS_ENGINES=[NODE_JS];" |
| 14 | export EM_CONFIG |
| 15 | |
| 16 | export EM_EXCLUSIVE_CACHE_ACCESS=1 |
| 17 | export EMCC_SKIP_SANITY_CHECK=1 |
| 18 | # export EMCC_DEBUG=2 |
| 19 | export EMCC_WASM_BACKEND=0 |
| 20 | export EMMAKEN_NO_SDK=1 |
| 21 | |
| 22 | mkdir -p "tmp/emscripten_cache" |
| 23 | export EM_CACHE="$(pwd -P)/tmp/emscripten_cache" |
| 24 | export EMCC_TEMP_DIR="$(pwd -P)/tmp" |
| 25 | |
| 26 | # Prepare the cache content so emscripten doesn't try to rebuild it all the time |
| 27 | cache_source=tools/cpp/emscripten/emscripten_cache |
James Kuszmaul | 9a05bfd | 2019-08-03 17:03:38 -0700 | [diff] [blame^] | 28 | # TODO(james): How do I avoid hardcoding this path? This is needed to make |
| 29 | # gencache.sh work properly and to have it put the files in the correct spot. |
| 30 | if [ -d bazel-out/host/bin/tools/cpp/emscripten/emscripten_cache ]; then |
| 31 | cache_source=bazel-out/host/bin/tools/cpp/emscripten/emscripten_cache |
James Kuszmaul | 27da814 | 2019-07-21 16:13:55 -0700 | [diff] [blame] | 32 | elif [ -d external/rules_emscripten/toolchain/emscripten_cache ]; then |
| 33 | cache_source=external/rules_emscripten/toolchain/emscripten_cache |
| 34 | fi |
| 35 | ( |
| 36 | cd tmp/emscripten_cache; |
| 37 | for n in "../../$cache_source"/*;do |
James Kuszmaul | 9a05bfd | 2019-08-03 17:03:38 -0700 | [diff] [blame^] | 38 | ln -f -s "$n" |
James Kuszmaul | 27da814 | 2019-07-21 16:13:55 -0700 | [diff] [blame] | 39 | done |
| 40 | ) |
| 41 | |
| 42 | argv=("$@") |
| 43 | tarfile= |
| 44 | # Find the -o option, and strip the .tar from it. |
| 45 | for (( i=0; i<$#; i++ )); do |
| 46 | if [[ "x${argv[i]}" == x-o ]]; then |
| 47 | arg=${argv[$((i+1))]} |
| 48 | if [[ "x$arg" == x*.tar ]];then |
| 49 | tarfile="$(cd $(dirname "$arg"); pwd -P)/$(basename "$arg")" |
| 50 | emfile="$(dirname "$arg")/$(basename $arg .tar)" |
| 51 | basearg="$(basename "$(basename "$(basename "$emfile" .js)" .html)" .wasm)" |
| 52 | baseout="$(dirname "$arg")/$basearg" |
| 53 | argv[$((i+1))]="$emfile" |
| 54 | fi |
| 55 | fi |
| 56 | done |
| 57 | python external/emscripten_toolchain/emcc.py "${argv[@]}" |
| 58 | # Now create the tarfile |
| 59 | shopt -s extglob |
| 60 | if [ "x$tarfile" != x ]; then |
| 61 | outdir="$(dirname "$baseout")" |
| 62 | outbase="$(basename "$baseout")" |
| 63 | ( |
| 64 | cd "$outdir"; |
| 65 | tar cf "$tarfile" "$outbase."?(html|js|wasm|mem|data|worker.js) |
| 66 | ) |
| 67 | fi |