AtlatestRepositorysigil-geometry

sigil-geometry / tree / scriptscheck-targets

1#!/usr/bin/env bash
2# Run sigil-geometry's test suite and the allocation check of every !
3# form on each target: bytecode (dev), native ELF (release) and native
4# wasm (web, run under Node).
5#
6# scripts/check-targets all three targets
7# TARGETS="release web" scripts/check-targets
8# SIGIL=/path/to/sigil scripts/check-targets
9#
10# How: every test/test-*.sgl except test-facade.sgl (which reads source
11# files, so it only means something on the host) is wrapped as a library
12# under harness/src/geometry-harness/suite/, and the harness app in
13# harness/ compiles them with the allocation check. Each target prints a
14# HARNESS-VERDICT line, which this script parses.
16# A target passes only when its verdict is PASS, the test count equals
17# the number of (test ...) forms in the generated suite (every test ran),
18# and the allocation check count equals the entries in alloc.sgl (which
19# must cover every exported ! form). A target with no
20# verdict line is SETUP-FAILED, which is not a pass and not a test
21# failure: nothing was measured.
23# The web target needs wasm-opt (binaryen) and node on PATH:
24# guix shell -m manifest.scm -- scripts/check-targets
26# Exit status: 0 every target passed, 1 a target failed, 2 setup failed.
28set -euo pipefail
30ROOT=$(cd "$(dirname "$0")/.." && pwd)
31HARNESS="$ROOT/harness"
32SUITE="$HARNESS/src/geometry-harness/suite"
33SIGIL=${SIGIL:-sigil}
34TARGETS=${TARGETS:-"dev release web"}
35LOGS=$(mktemp -d "${TMPDIR:-/tmp}/geometry-check-targets.XXXXXX")
37echo "check-targets: sigil $("$SIGIL" --version 2>&1 | head -1)"
38echo "check-targets: logs in $LOGS"
40# --- generate the suite ------------------------------------------------
42rm -rf "$SUITE" "$SUITE.sgl"
43mkdir -p "$SUITE"
45names=()
46for test_file in "$ROOT"/test/test-*.sgl; do
47 base=$(basename "$test_file" .sgl)
48 [ "$base" = "test-facade" ] && continue
49 names+=("$base")
50 out="$SUITE/$base.sgl"
51 # Split the file into its leading (import ...) form and everything
52 # else; the import specs go into the library's import clause.
53 awk -v name="$base" '
54 BEGIN { depth = 0; in_import = 0; done_import = 0 }
55 !done_import && !in_import && /^\(import/ { in_import = 1 }
56 in_import {
57 line = $0
58 opens = gsub(/\(/, "(", line); closes = gsub(/\)/, ")", line)
59 depth += opens - closes
60 imports = imports $0 "\n"
61 if (depth == 0) { in_import = 0; done_import = 1 }
62 next
63 }
64 { body = body " " $0 "\n" }
65 END {
66 if (!done_import) { print "no (import ...) form found" > "/dev/stderr"; exit 1 }
67 sub(/^\(import[ \t\n]*/, "", imports)
68 sub(/\)[ \t\n]*$/, "", imports)
69 printf ";; GENERATED from test/%s.sgl by scripts/check-targets. Do not edit.\n", name
70 printf "(define-library (geometry-harness suite %s)\n", name
71 printf " (import (sigil core)\n %s)\n", imports
72 printf " (export %s-loaded)\n", name
73 printf " (begin\n (define %s-loaded (quote %s))\n", name, name
74 printf "%s))\n", body
75 }' "$test_file" > "$out"
76 # Guard the generated artifact: non-empty, the right library, and the
77 # same number of (test ...) forms as its source.
78 src_tests=$(grep -c '(test "' "$test_file" || true)
79 gen_tests=$(grep -c '(test "' "$out" || true)
80 if ! grep -q "^(define-library (geometry-harness suite $base)" "$out" \
81 || [ "$src_tests" -ne "$gen_tests" ] || [ "$src_tests" -eq 0 ]; then
82 echo "SETUP-FAILED: generating $out (tests: source $src_tests, generated $gen_tests)"
83 exit 2
84 fi
85done
87expected_tests=$(cat "$SUITE"/*.sgl | grep -c '(test "' || true)
89# The allocation check must measure every exported ! form, and the
90# harness must report exactly as many checks as alloc.sgl lists.
91ALLOC_SRC="$HARNESS/src/geometry-harness/alloc.sgl"
92expected_alloc=$(grep -c '(cons "' "$ALLOC_SRC" || true)
93missing=""
94for module in vec mat3 mat4 quat camera ray; do
95 for name in $(awk '/^ \(export/{f=1; next} f && /^ \(begin/{exit} f' \
96 "$ROOT/src/sigil/geometry/$module.sgl" \
97 | sed 's/;;.*//' | tr -s ' ()' '\n\n\n' | grep -E '!$'); do
98 grep -q -F "(cons \"$name\"" "$ALLOC_SRC" || grep -q -F "(cons \"$name (" "$ALLOC_SRC" \
99 || missing="$missing $name"
100 done
101done
102if [ -n "$missing" ]; then
103 echo "SETUP-FAILED: ! forms with no allocation check:$missing"
104 exit 2
105fi
106echo "check-targets: allocation check covers every exported ! form ($expected_alloc checks)"
109 echo ";; GENERATED by scripts/check-targets. Do not edit."
110 echo "(define-library (geometry-harness suite)"
111 echo " (import (sigil core)"
112 for n in "${names[@]}"; do echo " (geometry-harness suite $n)"; done
113 echo " )"
114 echo " (export suite-modules)"
115 echo " (begin"
116 echo " (define suite-modules"
117 echo " (list $(for n in "${names[@]}"; do printf '%s-loaded ' "$n"; done)))))"
118} > "$SUITE.sgl"
120echo "check-targets: generated ${#names[@]} suite modules, $expected_tests tests"
122# --- build and run each target -----------------------------------------
124cd "$HARNESS"
125"$SIGIL" deps install > "$LOGS/deps.log" 2>&1 || { echo "SETUP-FAILED: deps install (see $LOGS/deps.log)"; exit 2; }
127status=0
128summary=()
130for target in $TARGETS; do
131 log="$LOGS/$target.log"
132 case "$target" in
133 dev) config=dev; run=(./build/dev/bin/geometry-harness) ;;
134 release) config=release; run=(./build/release/bin/geometry-harness) ;;
135 web) config=web; run=(node "$HARNESS/run-wasm.mjs" ./build/web/geometry-harness.wasm)
136 for tool in wasm-opt node; do
137 if ! command -v "$tool" > /dev/null; then
138 echo "SETUP-FAILED: web needs $tool on PATH (guix shell -m manifest.scm)"
139 exit 2
140 fi
141 done ;;
142 *) echo "SETUP-FAILED: unknown target $target"; exit 2 ;;
143 esac
145 if ! "$SIGIL" build --config "$config" > "$LOGS/$target.build.log" 2>&1; then
146 echo "SETUP-FAILED: $target build (see $LOGS/$target.build.log)"
147 status=2; summary+=("$target SETUP-FAILED build"); continue
148 fi
150 set +e
151 "${run[@]}" > "$log" 2>&1
152 run_rc=$?
153 set -e
155 verdict=$(grep '^HARNESS-VERDICT: ' "$log" | tail -1 || true)
156 if [ -z "$verdict" ]; then
157 echo "SETUP-FAILED: $target printed no verdict (exit $run_rc); last lines:"
158 tail -5 "$log" | sed 's/^/ /'
159 status=2; summary+=("$target SETUP-FAILED no-verdict"); continue
160 fi
162 field() { echo "$verdict" | sed -n "s/.* $1=\([0-9]*\).*/\1/p"; }
163 tests=$(field tests); failed=$(field failed); tests=${tests:--1}; failed=${failed:--1}
164 checked=$(field alloc-checked); bad=$(field alloc-bad); checked=${checked:--1}; bad=${bad:--1}
165 code=$(echo "$verdict" | sed -n 's/.* geometry-code=\([a-z]*\).*/\1/p')
166 hcode=$(echo "$verdict" | sed -n 's/.* harness-code=\([a-z]*\).*/\1/p')
168 grep -E '^(FAILED:|alloc: ALLOCATES)' "$log" | sed "s/^/ $target: /" || true
170 # Presence: each target must have run the kind of code it claims to
171 # measure. dev is all bytecode; web must run sigil-geometry natively.
172 # release reports what it got: on sigil 0.22.7 an ELF bundle loads
173 # dependency packages from their .sgb, so the geometry code there is
174 # bytecode called from native code, and the allocation rows measure
175 # that call bridge rather than the library. Allocation is gated only
176 # where the geometry code is uniformly bytecode (dev) or native.
177 case "$target" in
178 dev) want=bytecode; gate_alloc=yes ;;
179 web) want=native; gate_alloc=yes ;;
180 release) want="$code"
181 if [ "$code" = native ]; then gate_alloc=yes; else gate_alloc=no; fi ;;
182 esac
183 # Control for the detector itself: the harness's own procedure is
184 # bytecode on dev and native on the two native targets. A detector
185 # that always answered one kind would fail here on one of them.
186 if [ "$target" = dev ]; then hwant=bytecode; else hwant=native; fi
187 if [ "$hcode" != "$hwant" ]; then
188 echo "SETUP-FAILED: $target harness code read as '$hcode', expected $hwant (the detector is wrong)"
189 status=2; summary+=("$target SETUP-FAILED harness-code=$hcode"); continue
190 fi
191 if [ -z "$code" ] || [ "$code" != "$want" ]; then
192 echo "SETUP-FAILED: $target ran geometry code as '$code', expected $want"
193 status=2; summary+=("$target SETUP-FAILED geometry-code=$code"); continue
194 fi
196 alloc_ok=yes
197 if [ "$gate_alloc" = yes ] && [ "$bad" -ne 0 ]; then alloc_ok=no; fi
198 note=""
199 [ "$gate_alloc" = no ] && note=" (alloc not gated: geometry-code=$code, alloc-bad=$bad)"
201 if [[ "$verdict" == "HARNESS-VERDICT: PASS "* ]] && [ "$run_rc" -eq 0 ] \
202 && [ "$tests" -eq "$expected_tests" ] && [ "$failed" -eq 0 ] \
203 && [ "$checked" -eq "$expected_alloc" ] && [ "$alloc_ok" = yes ]; then
204 summary+=("$target PASS tests=$tests/$expected_tests geometry-code=$code alloc-checked=$checked alloc-bad=$bad$note")
205 else
206 [ "$status" -eq 0 ] && status=1
207 summary+=("$target FAIL tests=$tests/$expected_tests failed=$failed geometry-code=$code alloc-checked=$checked alloc-bad=$bad exit=$run_rc$note")
208 fi
209done
211echo
212for line in "${summary[@]}"; do echo "check-targets: $line"; done
213exit "$status"