#!/usr/bin/env bash # SPDX-License-Identifier: MIT # Build the wasm, bind it for the web, optimize it for speed, and report the sizes (play-gb's). # Output: web/pkg/play_2600.js + play_2600_bg.wasm. # Usage: scripts/build-wasm.sh (needs wasm-bindgen 0.2.128 and wasm-opt on PATH) # BOARD_ANY=1 builds from a board checkout other than the pinned commit (for trying a change; # never for a deploy). set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) cd "$ROOT" export PATH="$HOME/.cargo/bin:$HOME/.local/binaryen/bin:$PATH" # The board comes by path; this is its pin. A path dependency cannot pin a commit, so check it. BOARD_DIR=../a2600-rust-fast BOARD_PIN=ac4b2ed if [ -z "${BOARD_ANY:-}" ]; then head=$(git -C "$BOARD_DIR" rev-parse --short=7 HEAD) [ "$head" = "$BOARD_PIN" ] || { echo "$BOARD_DIR is at $head, not the pinned $BOARD_PIN (BOARD_ANY=1 to build anyway)"; exit 1; } git -C "$BOARD_DIR" diff --quiet HEAD -- . || { echo "$BOARD_DIR has uncommitted changes (BOARD_ANY=1 to build anyway)"; exit 1; } fi # 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} $CARGO build --release --target wasm32-unknown-unknown -q 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_2600.wasm cp web/pkg/play_2600_bg.wasm target/play_2600_bg.unopt.wasm wasm-opt -O3 --enable-bulk-memory --enable-nontrapping-float-to-int --enable-sign-ext \ -o web/pkg/play_2600_bg.wasm target/play_2600_bg.unopt.wasm printf 'wasm size: %d bytes unoptimized, %d bytes after wasm-opt -O3\n' \ "$(stat -c %s target/play_2600_bg.unopt.wasm)" "$(stat -c %s web/pkg/play_2600_bg.wasm)"