AtlatestRepositorysigil-geometry
sigil-geometry / tree / scriptscheck-targets
1
#!/usr/bin/env bash2
# Run sigil-geometry's test suite and the allocation check of every !3
# form on each target: bytecode (dev), native ELF (release) and native4
# wasm (web, run under Node).5
#6
# scripts/check-targets all three targets7
# TARGETS="release web" scripts/check-targets8
# SIGIL=/path/to/sigil scripts/check-targets9
#10
# How: every test/test-*.sgl except test-facade.sgl (which reads source11
# files, so it only means something on the host) is wrapped as a library12
# under harness/src/geometry-harness/suite/, and the harness app in13
# harness/ compiles them with the allocation check. Each target prints a14
# HARNESS-VERDICT line, which this script parses.15
#16
# A target passes only when its verdict is PASS, the test count equals17
# the number of (test ...) forms in the generated suite (every test ran),18
# and the allocation check count equals the entries in alloc.sgl (which19
# must cover every exported ! form). A target with no20
# verdict line is SETUP-FAILED, which is not a pass and not a test21
# failure: nothing was measured.22
#23
# The web target needs wasm-opt (binaryen) and node on PATH:24
# guix shell -m manifest.scm -- scripts/check-targets25
#26
# Exit status: 0 every target passed, 1 a target failed, 2 setup failed.28
set -euo pipefail30
ROOT=$(cd "$(dirname "$0")/.." && pwd)31
HARNESS="$ROOT/harness"32
SUITE="$HARNESS/src/geometry-harness/suite"33
SIGIL=${SIGIL:-sigil}34
TARGETS=${TARGETS:-"dev release web"}35
LOGS=$(mktemp -d "${TMPDIR:-/tmp}/geometry-check-targets.XXXXXX")37
echo "check-targets: sigil $("$SIGIL" --version 2>&1 | head -1)"38
echo "check-targets: logs in $LOGS"40
# --- generate the suite ------------------------------------------------42
rm -rf "$SUITE" "$SUITE.sgl"43
mkdir -p "$SUITE"45
names=()46
for test_file in "$ROOT"/test/test-*.sgl; do47
base=$(basename "$test_file" .sgl)48
[ "$base" = "test-facade" ] && continue49
names+=("$base")50
out="$SUITE/$base.sgl"51
# Split the file into its leading (import ...) form and everything52
# 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 = $058
opens = gsub(/\(/, "(", line); closes = gsub(/\)/, ")", line)59
depth += opens - closes60
imports = imports $0 "\n"61
if (depth == 0) { in_import = 0; done_import = 1 }62
next63
}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", name70
printf "(define-library (geometry-harness suite %s)\n", name71
printf " (import (sigil core)\n %s)\n", imports72
printf " (export %s-loaded)\n", name73
printf " (begin\n (define %s-loaded (quote %s))\n", name, name74
printf "%s))\n", body75
}' "$test_file" > "$out"76
# Guard the generated artifact: non-empty, the right library, and the77
# 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 ]; then82
echo "SETUP-FAILED: generating $out (tests: source $src_tests, generated $gen_tests)"83
exit 284
fi85
done87
expected_tests=$(cat "$SUITE"/*.sgl | grep -c '(test "' || true)89
# The allocation check must measure every exported ! form, and the90
# harness must report exactly as many checks as alloc.sgl lists.91
ALLOC_SRC="$HARNESS/src/geometry-harness/alloc.sgl"92
expected_alloc=$(grep -c '(cons "' "$ALLOC_SRC" || true)93
missing=""94
for module in vec mat3 mat4 quat camera ray; do95
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 '!$'); do98
grep -q -F "(cons \"$name\"" "$ALLOC_SRC" || grep -q -F "(cons \"$name (" "$ALLOC_SRC" \99
|| missing="$missing $name"100
done101
done102
if [ -n "$missing" ]; then103
echo "SETUP-FAILED: ! forms with no allocation check:$missing"104
exit 2105
fi106
echo "check-targets: allocation check covers every exported ! form ($expected_alloc checks)"108
{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)"; done113
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"120
echo "check-targets: generated ${#names[@]} suite modules, $expected_tests tests"122
# --- build and run each target -----------------------------------------124
cd "$HARNESS"125
"$SIGIL" deps install > "$LOGS/deps.log" 2>&1 || { echo "SETUP-FAILED: deps install (see $LOGS/deps.log)"; exit 2; }127
status=0128
summary=()130
for target in $TARGETS; do131
log="$LOGS/$target.log"132
case "$target" in133
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; do137
if ! command -v "$tool" > /dev/null; then138
echo "SETUP-FAILED: web needs $tool on PATH (guix shell -m manifest.scm)"139
exit 2140
fi141
done ;;142
*) echo "SETUP-FAILED: unknown target $target"; exit 2 ;;143
esac145
if ! "$SIGIL" build --config "$config" > "$LOGS/$target.build.log" 2>&1; then146
echo "SETUP-FAILED: $target build (see $LOGS/$target.build.log)"147
status=2; summary+=("$target SETUP-FAILED build"); continue148
fi150
set +e151
"${run[@]}" > "$log" 2>&1152
run_rc=$?153
set -e155
verdict=$(grep '^HARNESS-VERDICT: ' "$log" | tail -1 || true)156
if [ -z "$verdict" ]; then157
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"); continue160
fi162
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: /" || true170
# Presence: each target must have run the kind of code it claims to171
# 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 loads173
# dependency packages from their .sgb, so the geometry code there is174
# bytecode called from native code, and the allocation rows measure175
# that call bridge rather than the library. Allocation is gated only176
# where the geometry code is uniformly bytecode (dev) or native.177
case "$target" in178
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
esac183
# Control for the detector itself: the harness's own procedure is184
# bytecode on dev and native on the two native targets. A detector185
# that always answered one kind would fail here on one of them.186
if [ "$target" = dev ]; then hwant=bytecode; else hwant=native; fi187
if [ "$hcode" != "$hwant" ]; then188
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"); continue190
fi191
if [ -z "$code" ] || [ "$code" != "$want" ]; then192
echo "SETUP-FAILED: $target ran geometry code as '$code', expected $want"193
status=2; summary+=("$target SETUP-FAILED geometry-code=$code"); continue194
fi196
alloc_ok=yes197
if [ "$gate_alloc" = yes ] && [ "$bad" -ne 0 ]; then alloc_ok=no; fi198
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 ]; then204
summary+=("$target PASS tests=$tests/$expected_tests geometry-code=$code alloc-checked=$checked alloc-bad=$bad$note")205
else206
[ "$status" -eq 0 ] && status=1207
summary+=("$target FAIL tests=$tests/$expected_tests failed=$failed geometry-code=$code alloc-checked=$checked alloc-bad=$bad exit=$run_rc$note")208
fi209
done211
echo212
for line in "${summary[@]}"; do echo "check-targets: $line"; done213
exit "$status"