oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1#!/bin/sh
2# The input stamp beside each committed wasm module: a SHA-256 of every
3# file that decides the module's bytes, recorded when the module is built
4# and recomputed by the gate on every host.
5#
6# Why it exists: the gate used to prove freshness only by rebuilding and
7# comparing bytes, and that comparison only holds on Linux amd64. Cargo
8# hashes the host architecture into each crate's -C metadata, so an arm64
9# build of the same sources differs by about a kilobyte. The input hash
10# does not depend on the host at all, so it holds everywhere; Linux amd64
11# keeps the byte comparison as well.
12#
13# scripts/wasm-stamp.sh hash NAME print the input hash
14# scripts/wasm-stamp.sh list NAME print each input and its hash
15# scripts/wasm-stamp.sh write NAME [HASH] stamp the module just built
16# scripts/wasm-stamp.sh check NAME fail if the stamp is stale
17#
18# The hash is over git's tracked files only (working-tree contents), so an
19# untracked scratch file never makes a stamp unreproducible. Each line is
20# "<sha256 of contents> <repo-relative path>", sorted bytewise; the input
21# hash is the SHA-256 of those lines. No timestamps, modes or absolute
22# paths go in, so Linux and macOS agree.
23#
24# POSIX sh plus git and sha256sum or shasum, so it runs on the hosted
25# runner and on a Mac alike.
26set -eu
27cd "$(dirname "$0")/.."
28
29# module NAME: sets inputs, built (the build's output), committed (the
30# go:embed copy) and builder (the script that builds it).
31module() {
32 case "$1" in
33 # Tests and clippy.toml are left out of both: neither changes a
34 # library's wasm.
35 slides_core)
36 builder=scripts/build-wasm.sh
37 built=core/target/wasm32-unknown-unknown/release/slides_core.wasm
38 committed=internal/slides/core/testdata/slides_core.wasm
39 # core/pdf is a separate crate with its own module, below; core/vendor
40 # is the fastrand the crate patches in.
41 inputs=".cargo/config.toml rust-toolchain.toml $builder
42 core/Cargo.toml core/Cargo.lock core/src core/vendor"
43 ;;
44 slides_pdf)
45 builder=scripts/build-pdf-wasm.sh
46 built=core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm
47 committed=internal/slides/pdfcore/testdata/slides_pdf.wasm
48 inputs=".cargo/config.toml rust-toolchain.toml $builder
49 core/pdf/Cargo.toml core/pdf/Cargo.lock core/pdf/src"
50 ;;
51 *)
52 echo "wasm-stamp: unknown module '$1' (known: slides_core, slides_pdf)" >&2
53 exit 2
54 ;;
55 esac
56 stamp="$committed.stamp"
57}
58
59if command -v sha256sum >/dev/null 2>&1; then
60 sha() { sha256sum | cut -d' ' -f1; }
61elif command -v shasum >/dev/null 2>&1; then
62 sha() { shasum -a 256 | cut -d' ' -f1; }
63else
64 echo "wasm-stamp: needs sha256sum or shasum" >&2
65 exit 1
66fi
67
68# Checked up front rather than inside listing: listing runs in a
69# pipeline, where an exit would end only the pipeline, not the script.
70preflight() {
71 git rev-parse --is-inside-work-tree >/dev/null 2>&1 \
72 || { echo "wasm-stamp: not a git checkout" >&2; exit 1; }
73 for input in $inputs; do
74 # A renamed input would otherwise drop out of the hash in silence.
75 [ -e "$input" ] || { echo "wasm-stamp: input $input does not exist; update scripts/wasm-stamp.sh" >&2; exit 1; }
76 done
77}
78
79listing() {
80 # shellcheck disable=SC2086 # $inputs is a word list on purpose
81 git -c core.quotepath=off ls-files --cached -- $inputs | LC_ALL=C sort -u |
82 while IFS= read -r path; do
83 [ -f "$path" ] || continue # deleted in the working tree
84 printf '%s %s\n' "$(sha < "$path")" "$path"
85 done
86}
87
88stamp_field() {
89 sed -n "s/^$1 //p" "$stamp"
90}
91
92command=${1:-}
93[ $# -ge 2 ] || { echo "usage: scripts/wasm-stamp.sh hash|list|write|check NAME" >&2; exit 2; }
94module "$2"
95preflight
96
97case "$command" in
98 hash)
99 listing | sha
100 ;;
101 list)
102 listing
103 ;;
104 write)
105 # The builder passes the hash it took before building, so an edit
106 # made while cargo ran cannot be stamped as built.
107 hash=${3:-$(listing | sha)}
108 [ -f "$built" ] || { echo "wasm-stamp: no build at $built" >&2; exit 1; }
109 {
110 echo "# Written by $builder; checked by scripts/wasm-stamp.sh check $2."
111 echo "inputs $hash"
112 echo "wasm $(sha < "$built")"
113 } > "$stamp"
114 ;;
115 check)
116 fix="run $builder, then cp $built $committed and commit it with $stamp"
117 [ -f "$stamp" ] || { echo "wasm-stamp: $stamp is missing; $fix" >&2; exit 1; }
118 if [ "$(listing | sha)" != "$(stamp_field inputs)" ]; then
119 echo "wasm-stamp: $committed: wasm is stale: inputs changed since it was built; run $builder" >&2
120 echo "wasm-stamp: then cp $built $committed and commit it with $stamp" >&2
121 exit 1
122 fi
123 if [ "$(sha < "$committed")" != "$(stamp_field wasm)" ]; then
124 echo "wasm-stamp: $committed is not the module $stamp was written for (built, but not copied?); $fix" >&2
125 exit 1
126 fi
127 echo "wasm-stamp: $committed was built from these inputs"
128 ;;
129 *)
130 echo "usage: scripts/wasm-stamp.sh hash|list|write|check NAME" >&2
131 exit 2
132 ;;
133esac
134