blob: 814cd04afcb5e20d55828cbcb2c57d3194bb9af1 [file] [log] [blame]
James Kuszmaul27da8142019-07-21 16:13:55 -07001#!/bin/bash
2# Originally sourced from https://github.com/ribrdb/rules_emscripten
3set -euo pipefail
4EM_CONFIG="LLVM_ROOT='$(pwd -P)/external/emscripten_clang';"
5EM_CONFIG+="EMSCRIPTEN_NATIVE_OPTIMIZER='external/emscripten_clang/optimizer';"
6EM_CONFIG+="BINARYEN_ROOT='external/emscripten_clang/binaryen';"
7EM_CONFIG+="NODE_JS='$(pwd -P)/external/nodejs/bin/node';"
8EM_CONFIG+="EMSCRIPTEN_ROOT='external/emscripten_toolchain';"
9EM_CONFIG+="SPIDERMONKEY_ENGINE='';"
10EM_CONFIG+="V8_ENGINE='';"
11EM_CONFIG+="TEMP_DIR='tmp';"
12EM_CONFIG+="COMPILER_ENGINE=NODE_JS;"
13EM_CONFIG+="JS_ENGINES=[NODE_JS];"
14export EM_CONFIG
15
16export EM_EXCLUSIVE_CACHE_ACCESS=1
17export EMCC_SKIP_SANITY_CHECK=1
18# export EMCC_DEBUG=2
19export EMCC_WASM_BACKEND=0
20export EMMAKEN_NO_SDK=1
21
22mkdir -p "tmp/emscripten_cache"
23export EM_CACHE="$(pwd -P)/tmp/emscripten_cache"
24export EMCC_TEMP_DIR="$(pwd -P)/tmp"
25
26# Prepare the cache content so emscripten doesn't try to rebuild it all the time
27cache_source=tools/cpp/emscripten/emscripten_cache
James Kuszmaul9a05bfd2019-08-03 17:03:38 -070028# 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.
30if [ -d bazel-out/host/bin/tools/cpp/emscripten/emscripten_cache ]; then
31 cache_source=bazel-out/host/bin/tools/cpp/emscripten/emscripten_cache
James Kuszmaul27da8142019-07-21 16:13:55 -070032elif [ -d external/rules_emscripten/toolchain/emscripten_cache ]; then
33 cache_source=external/rules_emscripten/toolchain/emscripten_cache
34fi
35(
36 cd tmp/emscripten_cache;
37 for n in "../../$cache_source"/*;do
James Kuszmaul9a05bfd2019-08-03 17:03:38 -070038 ln -f -s "$n"
James Kuszmaul27da8142019-07-21 16:13:55 -070039 done
40)
41
42argv=("$@")
43tarfile=
44# Find the -o option, and strip the .tar from it.
45for (( 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
56done
57python external/emscripten_toolchain/emcc.py "${argv[@]}"
58# Now create the tarfile
59shopt -s extglob
60if [ "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 )
67fi