oficina / docs Public

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

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 docs_core)
34 builder=scripts/build-wasm.sh
35 built=core/target/wasm32-unknown-unknown/release/docs_core.wasm
36 committed=internal/core/testdata/docs_core.wasm
37 # core/tests and clippy.toml are left out: neither changes the
38 # library's wasm. native/vendor/yrs is the yrs the crate patches in.
39 inputs=".cargo/config.toml rust-toolchain.toml $builder
40 core/Cargo.toml core/Cargo.lock core/src core/vendor
41 native/vendor/yrs-0.23.5"
42 ;;
43 *)
44 echo "wasm-stamp: unknown module '$1' (known: docs_core)" >&2
45 exit 2
46 ;;
47 esac
48 stamp="$committed.stamp"
49}
50
51if command -v sha256sum >/dev/null 2>&1; then
52 sha() { sha256sum | cut -d' ' -f1; }
53elif command -v shasum >/dev/null 2>&1; then
54 sha() { shasum -a 256 | cut -d' ' -f1; }
55else
56 echo "wasm-stamp: needs sha256sum or shasum" >&2
57 exit 1
58fi
59
60# Checked up front rather than inside listing: listing runs in a
61# pipeline, where an exit would end only the pipeline, not the script.
62preflight() {
63 git rev-parse --is-inside-work-tree >/dev/null 2>&1 \
64 || { echo "wasm-stamp: not a git checkout" >&2; exit 1; }
65 for input in $inputs; do
66 # A renamed input would otherwise drop out of the hash in silence.
67 [ -e "$input" ] || { echo "wasm-stamp: input $input does not exist; update scripts/wasm-stamp.sh" >&2; exit 1; }
68 done
69}
70
71listing() {
72 # shellcheck disable=SC2086 # $inputs is a word list on purpose
73 git -c core.quotepath=off ls-files --cached -- $inputs | LC_ALL=C sort -u |
74 while IFS= read -r path; do
75 [ -f "$path" ] || continue # deleted in the working tree
76 printf '%s %s\n' "$(sha < "$path")" "$path"
77 done
78}
79
80stamp_field() {
81 sed -n "s/^$1 //p" "$stamp"
82}
83
84command=${1:-}
85[ $# -ge 2 ] || { echo "usage: scripts/wasm-stamp.sh hash|list|write|check NAME" >&2; exit 2; }
86module "$2"
87preflight
88
89case "$command" in
90 hash)
91 listing | sha
92 ;;
93 list)
94 listing
95 ;;
96 write)
97 # The builder passes the hash it took before building, so an edit
98 # made while cargo ran cannot be stamped as built.
99 hash=${3:-$(listing | sha)}
100 [ -f "$built" ] || { echo "wasm-stamp: no build at $built" >&2; exit 1; }
101 {
102 echo "# Written by $builder; checked by scripts/wasm-stamp.sh check $2."
103 echo "inputs $hash"
104 echo "wasm $(sha < "$built")"
105 } > "$stamp"
106 ;;
107 check)
108 fix="run $builder, then cp $built $committed and commit it with $stamp"
109 [ -f "$stamp" ] || { echo "wasm-stamp: $stamp is missing; $fix" >&2; exit 1; }
110 if [ "$(listing | sha)" != "$(stamp_field inputs)" ]; then
111 echo "wasm-stamp: $committed: wasm is stale: inputs changed since it was built; run $builder" >&2
112 echo "wasm-stamp: then cp $built $committed and commit it with $stamp" >&2
113 exit 1
114 fi
115 if [ "$(sha < "$committed")" != "$(stamp_field wasm)" ]; then
116 echo "wasm-stamp: $committed is not the module $stamp was written for (built, but not copied?); $fix" >&2
117 exit 1
118 fi
119 echo "wasm-stamp: $committed was built from these inputs"
120 ;;
121 *)
122 echo "usage: scripts/wasm-stamp.sh hash|list|write|check NAME" >&2
123 exit 2
124 ;;
125esac
126