#!/bin/bash
# Canonical 407-sysroot rebuild of the four Beebo performance LV2 plugins.
# Evidence: /srv/beebo/407_audit/SYSROOT_BUILD.md, REBUILD_ABI_CHECK.txt
#
# Rules honored:
#  - no host /usr/include and no host aarch64 libraries enter the build:
#    headers come from --sysroot + -isystem $SYS/usr/include (407's own
#    glibc-2.36/gcc-12 headers); link libs come from -L $SYS/... only.
#  - produces GLIBC_2.17-max binaries (2.27 in Space for expf/powf),
#    all under the 407 image floor of GLIBC_2.36.
set -e
SYS=/srv/beebo/407_audit/sysroot
TREE=${TREE:-/root/beebo-work/beebo-v2.0.1}
OUT=${OUT:-/srv/beebo/407_audit/rebuilt}
CC=aarch64-linux-gnu-gcc

mkdir -p "$OUT"
for p in performance_buffer performance_spectral performance_space performance_granular; do
    "$CC" -std=c11 -O2 -Wall -Wextra -fPIC -shared \
        --sysroot="$SYS" -isystem "$SYS/usr/include" \
        -L "$SYS/lib/aarch64-linux-gnu" -L "$SYS/usr/lib/aarch64-linux-gnu" \
        -I "$TREE/beebo/tests/lv2" -I "$TREE/beebo/lv2/$p.lv2" \
        "$TREE/beebo/lv2/$p.lv2/$p.c" -lm -o "$OUT/$p.so"
done
echo "Post-rebuild ABI: see /srv/beebo/407_audit/rebuilt/REBUILD_ABI_CHECK.txt"