#!/usr/bin/env bash # Run from the app's Cargo workspace. Produces a static, HTTPS-hostable folder. set -euo pipefail usage="usage: web-build.sh PACKAGE [--example NAME] [--out DIR] [--opt] [--dev] [--watch] [--no-html] [--names]" package=${1:?$usage} shift example= out=target/web opt=false dev=false watch=false html=true names=false while (($#)); do case "$1" in --example) example=${2:?--example needs a name}; shift 2 ;; --out) out=${2:?--out needs a directory}; shift 2 ;; --opt) opt=true; shift ;; --dev) dev=true; shift ;; --watch) watch=true; shift ;; --no-html) html=false; shift ;; --names) names=true; shift ;; -h|--help) echo "$usage"; exit 0 ;; *) echo "unknown option: $1" >&2; echo "$usage" >&2; exit 2 ;; esac done # Build with the toolchain's own cargo rather than a `cargo` wrapper on PATH, which may run # the build on another machine and leave no wasm here. Set CARGO to choose a cargo binary. cargo=${CARGO:-$(rustup which cargo 2>/dev/null || command -v cargo)} # Calling the toolchain's cargo directly skips rustup's proxy, so pin the toolchain for the # rustc it runs too: build scripts compile outside the repo, where rust-toolchain.toml can't reach. if [[ -z ${RUSTUP_TOOLCHAIN:-} ]] && toolchain=$(rustup show active-toolchain 2>/dev/null); then export RUSTUP_TOOLCHAIN=${toolchain%% *} fi # C and C++ dependencies (rune-luau's Luau) build for the web with WASI SDK's clang against WASI # libc, using WebAssembly exception handling. Set WASI_SDK_PATH to an unpacked WASI SDK 33 or # newer; rune-luau's build script links the SDK's libc and C++ runtime from it. if [[ -n ${WASI_SDK_PATH:-} ]]; then exe= [[ -f $WASI_SDK_PATH/bin/clang.exe ]] && exe=.exe export CC_wasm32_unknown_unknown="$WASI_SDK_PATH/bin/clang$exe" \ CXX_wasm32_unknown_unknown="$WASI_SDK_PATH/bin/clang++$exe" \ AR_wasm32_unknown_unknown="$WASI_SDK_PATH/bin/llvm-ar$exe" \ CXXSTDLIB_wasm32_unknown_unknown=c++ \ CFLAGS_wasm32_unknown_unknown=--target=wasm32-wasip1 \ LUAU_CXXFLAGS="--target=wasm32-wasip1 -fexceptions -fwasm-exceptions -mllvm -wasm-use-legacy-eh=false" fi meta=$("$cargo" metadata --format-version 1 --no-deps) selection=$(python3 -c ' import json,sys m=json.load(sys.stdin); p=next((p for p in m["packages"] if p["name"]==sys.argv[1]),None) if p is None: sys.exit("package is not a workspace member") targets=[t for t in p["targets"] if "example" in t["kind"] and t["name"]==sys.argv[2]] if sys.argv[2] else [t for t in p["targets"] if "cdylib" in t["crate_types"]] if not targets and not sys.argv[2]: targets=[t for t in p["targets"] if "bin" in t["kind"]] if len(targets)!=1: sys.exit("select one cdylib, binary, or --example target") t=targets[0] print(m["workspace_root"]); print(m["target_directory"]); print(t["name"]) print("example" if sys.argv[2] else "lib" if "cdylib" in t["crate_types"] else "bin") ' "$package" "$example" <<< "$meta") root=$(sed -n '1p' <<< "$selection") target=$(sed -n '2p' <<< "$selection") name=$(sed -n '3p' <<< "$selection") kind=$(sed -n '4p' <<< "$selection") if $dev; then # The debug profile without debug info, with dependencies at opt-level 1: an edit rebuilds # only this package, unoptimized, while Rune and the rest stay fast at run time. profile=web-dev build=(--profile web-dev --config 'profile.web-dev.inherits="dev"' --config 'profile.web-dev.debug=0' --config 'profile.web-dev.package."*".opt-level=1') # `"*"` covers only non-members, so name the workspace members this package depends on. while IFS= read -r member; do [[ -n $member ]] && build+=(--config "profile.web-dev.package.\"$member\".opt-level=1") done < <(python3 -c ' import json,sys m=json.load(sys.stdin); by={p["name"]:p for p in m["packages"]} todo=[sys.argv[1]]; seen=set() while todo: for d in by[todo.pop()]["dependencies"]: if d.get("path") and d["name"] in by and d["name"] not in seen and d["kind"] is None: seen.add(d["name"]); todo.append(d["name"]) print("\n".join(sorted(seen - {sys.argv[1]}))) ' "$package" <<< "$meta") opt=false else profile=release build=(--release) # Defaults for size; set any of these to override, e.g. CARGO_PROFILE_RELEASE_LTO=fat for a smaller, slower build. export CARGO_PROFILE_RELEASE_OPT_LEVEL=${CARGO_PROFILE_RELEASE_OPT_LEVEL:-s} \ CARGO_PROFILE_RELEASE_LTO=${CARGO_PROFILE_RELEASE_LTO:-thin} CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1 \ CARGO_PROFILE_RELEASE_DEBUG=0 CARGO_PROFILE_RELEASE_STRIP=debuginfo # Panic messages carry source paths; keep the builder's home folder out of a wasm meant for the public. # rustc applies the last matching prefix, so the narrower cargo home comes after the home folder. export RUSTFLAGS="${RUSTFLAGS:-} --remap-path-prefix=$HOME=/home --remap-path-prefix=${CARGO_HOME:-$HOME/.cargo}=/cargo" fi args=(--lib) artifact="$target/wasm32-unknown-unknown/$profile/${name//-/_}.wasm" if [[ $kind == example ]]; then args=(--example "$name"); artifact="$target/wasm32-unknown-unknown/$profile/examples/$name.wasm"; fi if [[ $kind == bin ]]; then args=(--bin "$name"); artifact="$target/wasm32-unknown-unknown/$profile/$name.wasm"; fi version=$(python3 -c 'import sys,tomllib; print(next(p["version"] for p in tomllib.load(open(sys.argv[1],"rb"))["package"] if p["name"]=="wasm-bindgen"))' "$root/Cargo.lock") if ! command -v wasm-bindgen >/dev/null || [[ $(wasm-bindgen --version) != "wasm-bindgen $version" ]]; then echo "Install the matching CLI: cargo install wasm-bindgen-cli --version $version --locked" >&2 exit 2 fi marker='' write_html() { local reload='' import="import init from './app.js';" load="await init();" if $watch; then import= reload='' load="const v = Date.now(); const { default: init } = await import('./app.js?v=' + v); await init({ module_or_path: './app_bg.wasm?v=' + v });" fi cat > "$out/index.html" < $marker