65816-llvm-mos/scripts/common.sh
2026-09-29 19:30:56 -05:00

141 lines
5 KiB
Bash
Executable file

# Shared helpers for all install scripts. Sourced, not executed.
set -euo pipefail
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TOOLS_DIR="$PROJECT_ROOT/tools"
DOWNLOAD_CACHE="$PROJECT_ROOT/.cache/downloads"
# The llvm-mos clone our backend is stitched into, and its build tree.
# The clone is pinned to the commit in llvmMosCommit.txt so a fresh
# install builds exactly what we last tested; updateLlvmMos.sh moves the
# pin forward.
LLVM_SRC="$TOOLS_DIR/llvm-mos"
LLVM_BUILD="$TOOLS_DIR/llvm-mos-build"
LLVM_MOS_URL="https://github.com/llvm-mos/llvm-mos.git"
LLVM_MOS_COMMIT_FILE="$PROJECT_ROOT/llvmMosCommit.txt"
install -d "$TOOLS_DIR" "$DOWNLOAD_CACHE"
log() {
printf '\033[1;34m[llvm816]\033[0m %s\n' "$*" >&2
}
warn() {
printf '\033[1;33m[llvm816 WARN]\033[0m %s\n' "$*" >&2
}
die() {
printf '\033[1;31m[llvm816 FAIL]\033[0m %s\n' "$*" >&2
exit 1
}
# MAME's cfg / nvram (the IIgs battery RAM) go to a throwaway directory.
# Run from a directory with no mame.ini (tests/ubsan's runner cd's there),
# MAME would otherwise leave cfg/ and nvram/ in it, and a saved BRAM would
# carry settings from run to run. Sets MAME_STATE and MAME_STATE_ARGS
# (pass those to mame); the caller's EXIT trap removes "$MAME_STATE".
makeMameState() {
MAME_STATE=$(mktemp -d)
MAME_STATE_ARGS=(-cfg_directory "$MAME_STATE" -nvram_directory "$MAME_STATE")
}
# Download to cache unless already present. $1=url, $2=dest filename in cache.
fetchCached() {
local url="$1"
local name="$2"
local dest="$DOWNLOAD_CACHE/$name"
if [ -s "$dest" ]; then
log "cached: $name"
else
log "fetching: $url"
curl -fL --retry 3 --progress-bar -o "$dest.part" "$url"
mv "$dest.part" "$dest"
fi
printf '%s\n' "$dest"
}
needCmd() {
command -v "$1" >/dev/null 2>&1 || die "required command not found: $1"
}
haveCmd() {
command -v "$1" >/dev/null 2>&1
}
# Print the llvm-mos commit this tree is pinned to.
llvmMosPinnedCommit() {
local commit
[ -s "$LLVM_MOS_COMMIT_FILE" ] || die "missing pin file: $LLVM_MOS_COMMIT_FILE"
commit="$(tr -d '[:space:]' < "$LLVM_MOS_COMMIT_FILE")"
[[ "$commit" =~ ^[0-9a-f]{40}$ ]] || die "bad commit in $LLVM_MOS_COMMIT_FILE: '$commit'"
printf '%s\n' "$commit"
}
# Shallow-fetch $1 (a commit SHA) and check it out as local branch main.
# The clone must already have its backend unapplied (unapplyBackend).
checkoutLlvmMosCommit() {
local commit="$1"
git -C "$LLVM_SRC" fetch --depth=1 origin "$commit"
git -C "$LLVM_SRC" checkout -q -B main FETCH_HEAD
}
# Undo applyBackend.sh so the clone is a pristine upstream tree again:
# reverse our patches, then delete the symlinks into src/. Dies if the
# clone still has changes afterwards (someone edited it by hand).
unapplyBackend() {
local patch
local link
local target
log "reverting any applied patches"
for patch in "$PROJECT_ROOT"/patches/*.patch; do
[ -f "$patch" ] || continue
if git -C "$LLVM_SRC" apply --reverse --check "$patch" >/dev/null 2>&1; then
git -C "$LLVM_SRC" apply --reverse "$patch"
log " reverted: $(basename "$patch")"
fi
done
# Symlinks from applyBackend.sh are untracked files inside tracked
# directories; git leaves them alone, so remove them explicitly.
log "removing symlinks under $LLVM_SRC pointing into $PROJECT_ROOT/src"
find "$LLVM_SRC" -type l | while read -r link; do
target="$(readlink -f "$link" || true)"
case "$target" in
"$PROJECT_ROOT"/src/*) rm "$link" ;;
esac
done
if ! git -C "$LLVM_SRC" diff --quiet || ! git -C "$LLVM_SRC" diff --cached --quiet; then
die "llvm-mos checkout still has local changes after cleanup. Inspect with: git -C $LLVM_SRC status"
fi
}
# runGnoMameSmoke <omf-path> <marker1> [<marker2> ...]
#
# Launch an already-built GNO/ME OMF under real GS/OS 6.0.4 + GNO in
# headless MAME and assert that every <marker> matches. Each marker is
# a single `addr=hexValue` token in the runInGno.sh `--check` syntax
# (e.g. `0x025000=C0DE`). Multiple markers are passed positionally —
# the function does NOT split on commas, so a caller wanting two checks
# passes two separate args.
#
# Exit 0 on all-match, 1 on any miss. Mirrors tests/lua/runLuaTest.sh's
# pattern of "run program in emulator, then assert canned markers";
# scoped at GNO instead of bare-metal because the C++ smoke / cxxstdlib
# / cursor work needs a real OMF Loader path.
#
# Required prereqs (caller should pre-check or let this function die):
# tools/cadius/cadius
# tools/gsos/6.0.4 - System.Disk.po
# tools/gno/gnobase.po
runGnoMameSmoke() {
local omfPath="$1"
shift
[ -f "$omfPath" ] || die "runGnoMameSmoke: OMF not found: $omfPath"
[ $# -ge 1 ] || die "runGnoMameSmoke: at least one marker required"
local args=()
local m
for m in "$@"; do
args+=("$m")
done
bash "$PROJECT_ROOT/scripts/runInGno.sh" "$omfPath" --check "${args[@]}"
}