#!/usr/bin/env bash # Build the npm package @transaction-science/wai-quantum: the quantum job service's # engine for WebAssembly (wai/wai-web with its quantum_engine feature), its JavaScript # bindings, and this folder's package.json and README, packed as one tarball: # # bash wai/wai-quantum-js/build.sh OUT_DIR # # Writes OUT_DIR/package/ (the package's files) and OUT_DIR/-.tgz, and # prints the tarball's SHA-256. It needs Rust with the wasm32-unknown-unknown target, # wasm-bindgen 0.2.127 (the version wai-web's lock file pins) and npm. Paths are # remapped out of the binary, and the build is refused if a local path is left in the # files it ships. set -euo pipefail if [ $# -ne 1 ]; then echo "usage: bash wai/wai-quantum-js/build.sh OUT_DIR" >&2 exit 2 fi here=$(cd "$(dirname "$0")" && pwd) root=$(cd "$here/../.." && pwd) web="$root/wai/wai-web" mkdir -p "$1" out=$(cd "$1" && pwd) sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1"; else shasum -a 256 "$1"; fi; } want=$(sed -n '/^name = "wasm-bindgen"$/{n;s/^version = "\(.*\)"$/\1/p;}' "$web/Cargo.lock") have=$(wasm-bindgen --version | cut -d' ' -f2) [ "$have" = "$want" ] || { echo "build.sh: wasm-bindgen is $have; wai-web's lock file pins $want" >&2; exit 1; } target=${CARGO_TARGET_DIR:-"$web/target"} sysroot=$(cd "$web" && rustc --print sysroot) commit=$(cd "$web" && rustc -vV | sed -n 's/^commit-hash: //p') export RUSTFLAGS="--remap-path-prefix=$root=/open-standards --remap-path-prefix=${CARGO_HOME:-$HOME/.cargo}=/cargo --remap-path-prefix=$sysroot/lib/rustlib/src/rust=/rustc/$commit" (cd "$web" && cargo build --locked --release --target wasm32-unknown-unknown --features quantum_engine) pkg="$out/package" rm -rf "$pkg" mkdir -p "$pkg" wasm-bindgen --target web --out-dir "$pkg" --out-name wai_quantum "$target/wasm32-unknown-unknown/release/wai_web.wasm" cp "$here/package.json" "$here/README.md" "$pkg/" cp "$root/wai/LICENSE" "$pkg/LICENSE" # Nothing the package ships names the machine that built it. for leak in "$HOME" /Users/ /home/ /Volumes/ /private/; do if grep -r -q -a -F "$leak" "$pkg"/wai_quantum*; then echo "build.sh: '$leak' is in the package's bindings or binary" >&2 exit 1 fi done rm -f "$out"/*.tgz npm pack "$pkg" --pack-destination "$out" --silent > /dev/null tgz=$(ls "$out"/*.tgz) echo "wasm: $(wc -c < "$pkg/wai_quantum_bg.wasm" | tr -d ' ') bytes" sha256 "$tgz"