#!/usr/bin/env bash
# Phase 6.2 UBSan-min smoke probe build.
#
# Compiles ubsanProbe.c with:
#   -fsanitize=undefined -fsanitize-minimal-runtime
# so clang emits __ubsan_handle_*_minimal calls at every UB site.  The
# probe overrides the three recovering handlers it exercises with
# strong defs (see ubsanProbe.c); the remaining handlers are pulled
# in from runtime/ubsan.o at link time.
#
# Output: build/ubsanProbe.o
#
# Usage:
#   bash tests/ubsan/build.sh

set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
OUT="$SCRIPT_DIR/build"
CLANG="$PROJECT_ROOT/tools/llvm-mos-build/bin/clang"

mkdir -p "$OUT"

CFLAGS=(--target=w65816 -O2 -ffunction-sections
        -I "$PROJECT_ROOT/runtime/include"
        -fsanitize=undefined -fsanitize-minimal-runtime)

echo "  CC  ubsanProbe.c (-fsanitize=undefined -fsanitize-minimal-runtime)"
"$CLANG" "${CFLAGS[@]}" -c "$SCRIPT_DIR/ubsanProbe.c" \
    -o "$OUT/ubsanProbe.o"

echo ""
echo "UBSan probe built: $(ls -1 "$OUT"/*.o | wc -l) objects, $(du -sh "$OUT" | cut -f1) total"
