Something went wrong. Try again.
Monorepo for PackTools - tooling for Minecraft Modpacks!
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374#!/usr/bin/env bash# packutils' clang-side gates, which `zig c++` cannot run: formatting, clang-tidy, and the tests# under ASan+UBSan and with line coverage. Needs `nix develop` (PACKLUAU_CLANGXX, the sanitizer# headers and LLVM's tools come from the root flake's packluau environment, which packutils# shares; clang-tools 22). AI-authored (Claude).# Usage: scripts/clang.sh fmt | fmt-check | tidy | asan | coverageset -euo pipefailcd "$(dirname "$0")/..": "${PACKLUAU_CLANGXX:?run inside nix develop}"mapfile -t own < <(find src tests -name '*.cpp' -o -name '*.hpp' -o -name '*.c' | sort)own+=(include/packutils.h)strict=(-std=c++23 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Werror)includes=(-Iinclude -Isrc)sanitizer_includes=(-isystem "${PACKLUAU_SANITIZER_INCLUDE:?run inside nix develop}")
# Compiles every source into $1 with flags $2..; prints the object paths.objects_for() { local out=$1 shift mkdir -p "$out" objects=() for source in src/*.cpp; do local object object="$out/$(basename "$source" .cpp).o" "$PACKLUAU_CLANGXX" "${strict[@]}" "${includes[@]}" "$@" -c "$source" -o "$object" objects+=("$object") done}
case "${1:-}" in fmt) clang-format -i "${own[@]}" ;; fmt-check) clang-format --dry-run -Werror "${own[@]}" ;; tidy) mapfile -t units < <(find src tests -name '*.cpp' | sort) clang-tidy --quiet "${units[@]}" -- "${strict[@]}" "${includes[@]}" ;; asan) out=.zig-cache/packutils-asan flags=(-O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=undefined "${sanitizer_includes[@]}") objects_for "$out" "${flags[@]}" "$PACKLUAU_CLANGXX" "${strict[@]}" "${includes[@]}" "${flags[@]}" tests/engine_test.cpp \ "${objects[@]}" -o "$out/engine_test" "$out/engine_test" ;; coverage) # Line coverage of the engine's rules (links, records, the mirror plan, JSON, the ABI) under # its tests. The OS layer is left out: fs_windows.cpp cannot run here (Wine runs it, `zig build # test-windows -fwine`), and fs_posix.cpp's uncovered lines are OS faults (EINTR, a failing # fsync or rename) that need fault injection; its paths that do run, run under ASan. : "${PACKLUAU_LLVM_BIN:?run inside nix develop}" threshold=85 out=.zig-cache/packutils-coverage rm -rf "$out" # -O1, not -O0: the Nix clang wrapper sets _FORTIFY_SOURCE, which warns without optimization. flags=(-O1 -g -fprofile-instr-generate -fcoverage-mapping) objects_for "$out" "${flags[@]}" "$PACKLUAU_CLANGXX" "${strict[@]}" "${includes[@]}" "${flags[@]}" tests/engine_test.cpp \ "${objects[@]}" -o "$out/engine_test" LLVM_PROFILE_FILE="$out/engine.profraw" "$out/engine_test" "$PACKLUAU_LLVM_BIN/llvm-profdata" merge -sparse "$out/engine.profraw" -o "$out/merged.profdata" mapfile -t measured < <(find src -name '*.cpp' ! -name 'fs_windows.cpp' ! -name 'fs_posix.cpp' | sort) "$PACKLUAU_LLVM_BIN/llvm-cov" report "$out/engine_test" \ -instr-profile="$out/merged.profdata" "${measured[@]}" | tee "$out/report.txt" percent=$(awk '/^TOTAL/ { gsub("%", "", $10); print $10 }' "$out/report.txt") echo "engine line coverage: $percent% (threshold $threshold%)" awk -v p="$percent" -v t="$threshold" 'BEGIN { exit !(p >= t) }' ;; *) echo "usage: scripts/clang.sh fmt | fmt-check | tidy | asan | coverage" >&2 exit 2 ;;esac