#!/usr/bin/env bash # Build the wasm, bind it for the web, optimize it for speed, and report the sizes. # Output: web/pkg/play_pce.js + play_pce_bg.wasm. # Usage: scripts/build-wasm.sh (needs wasm-bindgen 0.2.128 and wasm-opt on PATH) # BOARD=../pcengine-rust-fast scripts/build-wasm.sh (the base board from another checkout) set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) cd "$ROOT" export PATH="$HOME/.cargo/bin:$HOME/.local/binaryen/bin:$PATH" # Use the rust-toolchain.toml toolchain's own cargo and rustc directly: a # snap-installed rustup's `rustup run` leaves the system rustc first on PATH, # and that rustc has no wasm32 target. TOOLCHAIN=${TOOLCHAIN:-"$HOME/.rustup/toolchains/1.93.1-x86_64-unknown-linux-gnu"} if [ -z "${CARGO:-}" ] && [ -x "$TOOLCHAIN/bin/cargo" ]; then CARGO="$TOOLCHAIN/bin/cargo" export RUSTC="$TOOLCHAIN/bin/rustc" fi CARGO=${CARGO:-cargo} # THE BASE BOARD, ONE PATH. Empty: the one Cargo.toml names (../pcengine-rust). Any # other checkout with the same library and API (the speed fork) replaces it for this # crate and for both add-on crates at once. See the README, "Switching the base board". # The site ships the speed fork (same crate, same API, lockstep-gated against pcengine-rust: # 275 of 275 cases); BOARD=../pcengine-rust builds the plain board for comparison. BOARD=${BOARD:-../pcengine-rust-fast} override=() if [ -n "$BOARD" ]; then scripts/stage-board.sh "$BOARD" override=(--config 'paths=["target/board"]') fi echo "${BOARD:-../pcengine-rust}" > target/board.path 2>/dev/null || { mkdir -p target; echo "${BOARD:-../pcengine-rust}" > target/board.path; } $CARGO build --release --target wasm32-unknown-unknown -q "${override[@]}" wasm-bindgen --version | grep -q 0.2.128 || { echo "wasm-bindgen CLI must be 0.2.128 (matches Cargo.toml)"; exit 1; } wasm-bindgen --target web --out-dir web/pkg --no-typescript \ target/wasm32-unknown-unknown/release/play_pce.wasm cp web/pkg/play_pce_bg.wasm target/play_pce_bg.unopt.wasm wasm-opt -O3 --enable-bulk-memory --enable-nontrapping-float-to-int --enable-sign-ext \ -o web/pkg/play_pce_bg.wasm target/play_pce_bg.unopt.wasm printf 'wasm size: %d bytes unoptimized, %d bytes after wasm-opt -O3\n' \ "$(stat -c %s target/play_pce_bg.unopt.wasm)" "$(stat -c %s web/pkg/play_pce_bg.wasm)"