#!/usr/bin/env bash # Build the core crate for wazero. No wasm-bindgen, no wasm-pack: the Go host # provides no JS glue, so the module must be plain wasm32 with a C ABI. set -euo pipefail cd "$(dirname "$0")/.." # Lint before building: disallowed-methods (clippy.toml) enforces the # Doc-construction rule documented in core/src/lib.rs — Doc::new() and # Doc::with_client_id() both draw entropy this build has no safe source # for. A doc comment alone has been wrong, or gotten around, more than # once; this is the actual enforcement. if [[ "${1:-}" != "--build-only" && "${1:-}" != "--canonical-build" ]]; then cargo clippy --manifest-path core/Cargo.toml --all-targets -- -D warnings -D clippy::disallowed_methods fi # Rust/Cargo crate identities affect code ordering even after path remapping. # Build on Linux amd64 at fixed source/toolchain paths on every host. if [[ "${1:-}" == "--canonical-build" ]]; then [[ "$(uname -s)/$(uname -m)" == "Linux/x86_64" && "$(pwd)" == /work \ && "$CARGO_HOME" == /usr/local/cargo && "$RUSTUP_HOME" == /usr/local/rustup ]] \ || { echo "build-wasm: invalid canonical build environment" >&2; exit 1; } export CARGO_TARGET_DIR=/work/core/target RUSTUP_TOOLCHAIN=1.98.0 else out="core/target/wasm32-unknown-unknown/release/docs_core.wasm" mkdir -p "$(dirname "$out")" temporary=$(mktemp "${out}.XXXXXX") trap 'rm -f "$temporary"' EXIT if [[ "$(uname -s)/$(uname -m)" == "Linux/x86_64" ]] && command -v bwrap >/dev/null; then cargo_home=$(cd "${CARGO_HOME:-$HOME/.cargo}" && pwd) rustup_home=$(cd "${RUSTUP_HOME:-$HOME/.rustup}" && pwd) mkdir -p core/target/canonical-linux-amd64 bwrap --tmpfs / \ --ro-bind /usr /usr --tmpfs /usr/local \ --ro-bind /bin /bin --ro-bind /lib /lib --ro-bind /lib64 /lib64 \ --ro-bind /etc /etc --bind /tmp /tmp --proc /proc --dev /dev \ --ro-bind "$(pwd)" /work \ --bind "$(pwd)/core/target/canonical-linux-amd64" /work/core/target \ --bind "$cargo_home" /usr/local/cargo --ro-bind "$rustup_home" /usr/local/rustup \ --chdir /work --setenv CARGO_HOME /usr/local/cargo --setenv RUSTUP_HOME /usr/local/rustup \ --setenv PATH /usr/local/cargo/bin:/usr/local/bin:/usr/bin:/bin \ bash -ec './scripts/build-wasm.sh --canonical-build >&2; cat core/target/wasm32-unknown-unknown/release/docs_core.wasm' > "$temporary" else command -v docker >/dev/null || { echo "build-wasm: install Docker, or bubblewrap on Linux amd64" >&2; exit 1; } target_key=$(printf %s "$(pwd)" | shasum -a 256 | cut -c1-16) docker run --rm --platform linux/amd64 \ --mount "type=bind,source=$(pwd),target=/work,readonly" \ --mount "type=volume,source=oficina-docs-wasm-target-${target_key},target=/work/core/target" \ --mount type=volume,source=oficina-docs-wasm-cargo,target=/usr/local/cargo \ --mount type=volume,source=oficina-docs-wasm-rustup,target=/usr/local/rustup \ --workdir /work \ rust@sha256:8aea3a845c48caac4091a8d0e6494d213061f8f014dc2f5a5f1a88a1a8c9f301 \ bash -ec 'rustup target add wasm32-unknown-unknown >&2; ./scripts/build-wasm.sh --canonical-build >&2; cat core/target/wasm32-unknown-unknown/release/docs_core.wasm' > "$temporary" fi mv "$temporary" "$out" exit 0 fi # --lib only: the wasm module shipped to wazero is the library crate. Native # tooling under src/bin (e.g. emit_update, used from the host side of # wire-gate.sh) is never part of that artifact and does not define the # __getrandom_v03_custom symbol itself, so building it for wasm32 fails to # link -- it was never meant to run there. # # --remap-path-prefix (x2): rustc embeds the absolute source path of every # panic site (Location::caller()) into the binary. Left alone, that means # the registry path of every dependency (which contains $HOME, so it # leaks a username) and this worktree's own absolute path end up baked # into docs_core.wasm -- so the artifact only reproduces byte-for-byte on # the machine, and under the $HOME, that built it. The gate's `cmp` # against the committed copy then fails everywhere else and misreports # a stale artifact instead of a non-reproducible build. Remapping both # roots to fixed placeholders makes the build's output independent of # where it ran. # # Setting RUSTFLAGS here does not merge with .cargo/config.toml's # target.wasm32-unknown-unknown.rustflags -- an env RUSTFLAGS replaces a # config-file rustflags for the same target outright, it does not append # to it. That config entry (--cfg getrandom_backend="custom") is what # routes getrandom to our no-JS-import backend on wasm32; dropping it # breaks the build (getrandom falls back to a backend wasm32-unknown- # unknown doesn't support) rather than just losing the cfg silently. So # it has to be repeated here alongside the remap flags. registry_root="${CARGO_HOME:-$HOME/.cargo}/registry/src" workspace_root="$(pwd)" # Cargo also puts local path dependency identities into mangled debug names. # Remove that optional symbol section so identical code reproduces across worktrees. export RUSTFLAGS="-C strip=symbols --cfg getrandom_backend=\"custom\" --remap-path-prefix=${registry_root}=/cargo-registry-src --remap-path-prefix=${workspace_root}=/docs-core" cargo build --release --manifest-path core/Cargo.toml --target wasm32-unknown-unknown --locked --lib out="core/target/wasm32-unknown-unknown/release/docs_core.wasm" [ -f "$out" ] || { echo "build-wasm: no artifact at $out" >&2; exit 1; } echo "$out ($(wc -c < "$out") bytes)"