#!/usr/bin/env bash # Build the page every view of WAI Quantum is served from: this crate for # WebAssembly, the same build translated to JavaScript, and the page around # them (wai/quantum-studio/build-views.mjs). Writes wai/wai-quantum/views/, # which the engine serves (wai_quantum::engine::views) and the repository keeps. # # bash build.sh [OUT_DIR] # # It needs Rust with the wasm32-unknown-unknown target and Node with # wai/quantum-studio's packages installed (npm ci there): binaryen, pinned in # its package.json, optimises the WebAssembly and translates it. The build is # the same bytes on every host with the same toolchain: paths are remapped out # of the binary and nothing in it names the time or the machine. set -euo pipefail here=$(cd "$(dirname "$0")" && pwd) root=$(cd "$here/../.." && pwd) studio="$root/wai/quantum-studio" out=${1:-"$root/wai/wai-quantum/views"} target=${CARGO_TARGET_DIR:-"$here/target"} # The standard library's own sources, where rust-src is installed, read as they do where it is not. sysroot=$(cd "$here" && rustc --print sysroot) commit=$(cd "$here" && 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 "$here" && cargo build --locked --release --target wasm32-unknown-unknown) wasm="$target/wasm32-unknown-unknown/release/wai_quantum_views.wasm" tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT features="--enable-bulk-memory --enable-sign-ext --enable-mutable-globals --enable-nontrapping-float-to-int --enable-multivalue --enable-reference-types" cd "$studio" # The WebAssembly the page carries: optimised for size, its custom sections dropped. npx --no-install wasm-opt $features -Oz --strip-debug --strip-producers "$wasm" -o "$tmp/views.wasm" # The same, lowered to what the translation takes, and translated to JavaScript. npx --no-install wasm-opt $features --llvm-memory-copy-fill-lowering --llvm-nontrapping-fptoint-lowering --signext-lowering "$tmp/views.wasm" -o "$tmp/lowered.wasm" npx --no-install wasm2js -Oz "$tmp/lowered.wasm" -o "$tmp/views.mjs" node "$studio/build-views.mjs" --wasm "$tmp/views.wasm" --js "$tmp/views.mjs" --out "$out"