| 1 | #!/usr/bin/env bash |
| 2 | # Build the core crate for wazero. No wasm-bindgen, no wasm-pack: the Go host |
| 3 | # provides no JS glue, so the module must be plain wasm32 with a C ABI. |
| 4 | set -euo pipefail |
| 5 | cd "$(dirname "$0")/.." |
| 6 | # Lint before building: disallowed-methods (clippy.toml) enforces the |
| 7 | # Doc-construction rule documented in core/src/lib.rs — Doc::new() and |
| 8 | # Doc::with_client_id() both draw entropy this build has no safe source |
| 9 | # for. A doc comment alone has been wrong, or gotten around, more than |
| 10 | # once; this is the actual enforcement. |
| 11 | if [[ "${1:-}" != "--build-only" && "${1:-}" != "--canonical-build" && "${1:-}" != "--native-build" ]]; then |
| 12 | cargo clippy --manifest-path core/Cargo.toml --all-targets -- -D warnings -D clippy::disallowed_methods |
| 13 | fi |
| 14 | # Rust/Cargo crate identities affect code ordering even after path remapping. |
| 15 | # Build on Linux amd64 at fixed source/toolchain paths on every host that can. |
| 16 | if [[ "${1:-}" == "--canonical-build" ]]; then |
| 17 | [[ "$(uname -s)/$(uname -m)" == "Linux/x86_64" && "$(pwd)" == /work \ |
| 18 | && "$CARGO_HOME" == /usr/local/cargo && "$RUSTUP_HOME" == /usr/local/rustup ]] \ |
| 19 | || { echo "build-wasm: invalid canonical build environment" >&2; exit 1; } |
| 20 | export CARGO_TARGET_DIR=/work/core/target RUSTUP_TOOLCHAIN=1.98.0 |
| 21 | elif [[ "${1:-}" == "--native-build" ]]; then |
| 22 | : # this checkout, this host's toolchain: built below, not reproducible |
| 23 | else |
| 24 | # Cargo's crate identities (-C metadata) also depend on the host |
| 25 | # architecture, so no build on a Linux arm64 host matches the committed |
| 26 | # amd64 bytes, and the amd64 image crashes under qemu there. Build |
| 27 | # natively instead, for the tests that read the artifact. That build |
| 28 | # writes no stamp; the gate checks freshness there by the input stamp |
| 29 | # alone, and on Linux amd64 by the stamp and the bytes. |
| 30 | if [[ "$(uname -s)" == Linux && "$(uname -m)" != x86_64 ]]; then |
| 31 | echo "build-wasm: Linux/$(uname -m) builds natively; the result is not the canonical amd64 build" >&2 |
| 32 | exec ./scripts/build-wasm.sh --native-build |
| 33 | fi |
| 34 | # Hash the inputs before building, so an edit made while cargo runs |
| 35 | # cannot be stamped as built (scripts/wasm-stamp.sh says what is hashed). |
| 36 | inputs=$(./scripts/wasm-stamp.sh hash slides_core) |
| 37 | out="core/target/wasm32-unknown-unknown/release/slides_core.wasm" |
| 38 | mkdir -p "$(dirname "$out")" |
| 39 | temporary=$(mktemp "${out}.XXXXXX") |
| 40 | trap 'rm -f "$temporary"' EXIT |
| 41 | if [[ "$(uname -s)/$(uname -m)" == "Linux/x86_64" ]] && command -v bwrap >/dev/null; then |
| 42 | cargo_home=$(cd "${CARGO_HOME:-$HOME/.cargo}" && pwd) |
| 43 | rustup_home=$(cd "${RUSTUP_HOME:-$HOME/.rustup}" && pwd) |
| 44 | mkdir -p core/target/canonical-linux-amd64 |
| 45 | bwrap --tmpfs / \ |
| 46 | --ro-bind /usr /usr --tmpfs /usr/local \ |
| 47 | --ro-bind /bin /bin --ro-bind /lib /lib --ro-bind /lib64 /lib64 \ |
| 48 | --ro-bind /etc /etc --bind /tmp /tmp --proc /proc --dev /dev \ |
| 49 | --ro-bind "$(pwd)" /work \ |
| 50 | --bind "$(pwd)/core/target/canonical-linux-amd64" /work/core/target \ |
| 51 | --bind "$cargo_home" /usr/local/cargo --ro-bind "$rustup_home" /usr/local/rustup \ |
| 52 | --chdir /work --setenv CARGO_HOME /usr/local/cargo --setenv RUSTUP_HOME /usr/local/rustup \ |
| 53 | --setenv PATH /usr/local/cargo/bin:/usr/local/bin:/usr/bin:/bin \ |
| 54 | bash -ec './scripts/build-wasm.sh --canonical-build >&2; cat core/target/wasm32-unknown-unknown/release/slides_core.wasm' > "$temporary" |
| 55 | else |
| 56 | command -v docker >/dev/null || { echo "build-wasm: install Docker, or bubblewrap on Linux amd64" >&2; exit 1; } |
| 57 | target_key=$(printf %s "$(pwd)" | shasum -a 256 | cut -c1-16) |
| 58 | docker run --rm --platform linux/amd64 \ |
| 59 | --mount "type=bind,source=$(pwd),target=/work,readonly" \ |
| 60 | --mount "type=volume,source=oficina-slides-core-wasm-target-${target_key},target=/work/core/target" \ |
| 61 | --mount type=volume,source=oficina-slides-core-wasm-cargo,target=/usr/local/cargo \ |
| 62 | --mount type=volume,source=oficina-slides-core-wasm-rustup,target=/usr/local/rustup \ |
| 63 | --workdir /work \ |
| 64 | rust@sha256:8aea3a845c48caac4091a8d0e6494d213061f8f014dc2f5a5f1a88a1a8c9f301 \ |
| 65 | bash -ec 'rustup target add wasm32-unknown-unknown >&2; ./scripts/build-wasm.sh --canonical-build >&2; cat core/target/wasm32-unknown-unknown/release/slides_core.wasm' > "$temporary" |
| 66 | fi |
| 67 | mv "$temporary" "$out" |
| 68 | # The stamp goes beside the committed copy. Any host's gate checks it: |
| 69 | # the inputs must still hash to what was built, and the committed wasm |
| 70 | # must be this build, so a forgotten cp fails too. |
| 71 | ./scripts/wasm-stamp.sh write slides_core "$inputs" |
| 72 | echo "build-wasm: built $out ($(wc -c < "$out") bytes) and wrote internal/slides/core/testdata/slides_core.wasm.stamp" >&2 |
| 73 | echo "build-wasm: to ship it, cp $out internal/slides/core/testdata/slides_core.wasm and commit both" >&2 |
| 74 | exit 0 |
| 75 | fi |
| 76 | # --lib only: the wasm module shipped to wazero is the library crate. Native |
| 77 | # tooling under src/bin (e.g. emit_update, used from the host side of |
| 78 | # wire-gate.sh) is never part of that artifact and does not define the |
| 79 | # __getrandom_v03_custom symbol itself, so building it for wasm32 fails to |
| 80 | # link -- it was never meant to run there. |
| 81 | # |
| 82 | # --remap-path-prefix (x2): rustc embeds the absolute source path of every |
| 83 | # panic site (Location::caller()) into the binary. Left alone, that means |
| 84 | # the registry path of every dependency (which contains $HOME, so it |
| 85 | # leaks a username) and this worktree's own absolute path end up baked |
| 86 | # into slides_core.wasm -- so the artifact only reproduces byte-for-byte on |
| 87 | # the machine, and under the $HOME, that built it. The gate's `cmp` |
| 88 | # against the committed copy then fails everywhere else and misreports |
| 89 | # a stale artifact instead of a non-reproducible build. Remapping both |
| 90 | # roots to fixed placeholders makes the build's output independent of |
| 91 | # where it ran. |
| 92 | # |
| 93 | # Setting RUSTFLAGS here does not merge with .cargo/config.toml's |
| 94 | # target.wasm32-unknown-unknown.rustflags -- an env RUSTFLAGS replaces a |
| 95 | # config-file rustflags for the same target outright, it does not append |
| 96 | # to it. That config entry (--cfg getrandom_backend="custom") is what |
| 97 | # routes getrandom to our no-JS-import backend on wasm32; dropping it |
| 98 | # breaks the build (getrandom falls back to a backend wasm32-unknown- |
| 99 | # unknown doesn't support) rather than just losing the cfg silently. So |
| 100 | # it has to be repeated here alongside the remap flags. |
| 101 | registry_root="${CARGO_HOME:-$HOME/.cargo}/registry/src" |
| 102 | workspace_root="$(pwd)" |
| 103 | # Cargo also puts local path dependency identities into mangled debug names. |
| 104 | # Remove that optional symbol section so identical code reproduces across worktrees. |
| 105 | export RUSTFLAGS="-C strip=symbols --cfg getrandom_backend=\"custom\" --remap-path-prefix=${registry_root}=/cargo-registry-src --remap-path-prefix=${workspace_root}=/slides-core" |
| 106 | cargo build --release --manifest-path core/Cargo.toml --target wasm32-unknown-unknown --locked --lib |
| 107 | out="core/target/wasm32-unknown-unknown/release/slides_core.wasm" |
| 108 | [ -f "$out" ] || { echo "build-wasm: no artifact at $out" >&2; exit 1; } |
| 109 | echo "$out ($(wc -c < "$out") bytes)" |
| 110 | |