#!/bin/sh # The input stamp beside each committed wasm module: a SHA-256 of every # file that decides the module's bytes, recorded when the module is built # and recomputed by the gate on every host. # # Why it exists: the gate used to prove freshness only by rebuilding and # comparing bytes, and that comparison only holds on Linux amd64. Cargo # hashes the host architecture into each crate's -C metadata, so an arm64 # build of the same sources differs by about a kilobyte. The input hash # does not depend on the host at all, so it holds everywhere; Linux amd64 # keeps the byte comparison as well. # # scripts/wasm-stamp.sh hash NAME print the input hash # scripts/wasm-stamp.sh list NAME print each input and its hash # scripts/wasm-stamp.sh write NAME [HASH] stamp the module just built # scripts/wasm-stamp.sh check NAME fail if the stamp is stale # # The hash is over git's tracked files only (working-tree contents), so an # untracked scratch file never makes a stamp unreproducible. Each line is # " ", sorted bytewise; the input # hash is the SHA-256 of those lines. No timestamps, modes or absolute # paths go in, so Linux and macOS agree. # # POSIX sh plus git and sha256sum or shasum, so it runs on the hosted # runner and on a Mac alike. set -eu cd "$(dirname "$0")/.." # module NAME: sets inputs, built (the build's output), committed (the # go:embed copy) and builder (the script that builds it). module() { case "$1" in # Tests and clippy.toml are left out of both: neither changes a # library's wasm. slides_core) builder=scripts/build-wasm.sh built=core/target/wasm32-unknown-unknown/release/slides_core.wasm committed=internal/slides/core/testdata/slides_core.wasm # core/pdf is a separate crate with its own module, below; core/vendor # is the fastrand the crate patches in. inputs=".cargo/config.toml rust-toolchain.toml $builder core/Cargo.toml core/Cargo.lock core/src core/vendor" ;; slides_pdf) builder=scripts/build-pdf-wasm.sh built=core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm committed=internal/slides/pdfcore/testdata/slides_pdf.wasm inputs=".cargo/config.toml rust-toolchain.toml $builder core/pdf/Cargo.toml core/pdf/Cargo.lock core/pdf/src" ;; *) echo "wasm-stamp: unknown module '$1' (known: slides_core, slides_pdf)" >&2 exit 2 ;; esac stamp="$committed.stamp" } if command -v sha256sum >/dev/null 2>&1; then sha() { sha256sum | cut -d' ' -f1; } elif command -v shasum >/dev/null 2>&1; then sha() { shasum -a 256 | cut -d' ' -f1; } else echo "wasm-stamp: needs sha256sum or shasum" >&2 exit 1 fi # Checked up front rather than inside listing: listing runs in a # pipeline, where an exit would end only the pipeline, not the script. preflight() { git rev-parse --is-inside-work-tree >/dev/null 2>&1 \ || { echo "wasm-stamp: not a git checkout" >&2; exit 1; } for input in $inputs; do # A renamed input would otherwise drop out of the hash in silence. [ -e "$input" ] || { echo "wasm-stamp: input $input does not exist; update scripts/wasm-stamp.sh" >&2; exit 1; } done } listing() { # shellcheck disable=SC2086 # $inputs is a word list on purpose git -c core.quotepath=off ls-files --cached -- $inputs | LC_ALL=C sort -u | while IFS= read -r path; do [ -f "$path" ] || continue # deleted in the working tree printf '%s %s\n' "$(sha < "$path")" "$path" done } stamp_field() { sed -n "s/^$1 //p" "$stamp" } command=${1:-} [ $# -ge 2 ] || { echo "usage: scripts/wasm-stamp.sh hash|list|write|check NAME" >&2; exit 2; } module "$2" preflight case "$command" in hash) listing | sha ;; list) listing ;; write) # The builder passes the hash it took before building, so an edit # made while cargo ran cannot be stamped as built. hash=${3:-$(listing | sha)} [ -f "$built" ] || { echo "wasm-stamp: no build at $built" >&2; exit 1; } { echo "# Written by $builder; checked by scripts/wasm-stamp.sh check $2." echo "inputs $hash" echo "wasm $(sha < "$built")" } > "$stamp" ;; check) fix="run $builder, then cp $built $committed and commit it with $stamp" [ -f "$stamp" ] || { echo "wasm-stamp: $stamp is missing; $fix" >&2; exit 1; } if [ "$(listing | sha)" != "$(stamp_field inputs)" ]; then echo "wasm-stamp: $committed: wasm is stale: inputs changed since it was built; run $builder" >&2 echo "wasm-stamp: then cp $built $committed and commit it with $stamp" >&2 exit 1 fi if [ "$(sha < "$committed")" != "$(stamp_field wasm)" ]; then echo "wasm-stamp: $committed is not the module $stamp was written for (built, but not copied?); $fix" >&2 exit 1 fi echo "wasm-stamp: $committed was built from these inputs" ;; *) echo "usage: scripts/wasm-stamp.sh hash|list|write|check NAME" >&2 exit 2 ;; esac