65816-llvm-mos/runtime/build.sh
Scott Duensing 18ef7e1fa6 Checkpoint
2026-05-01 17:22:55 -05:00

51 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
# Build the entire W65816 runtime — assemble *.s, compile *.c.
# Run after editing anything under runtime/src/.
set -euo pipefail
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
LLVM_MC="$PROJECT_ROOT/tools/llvm-mos-build/bin/llvm-mc"
CLANG="$PROJECT_ROOT/tools/llvm-mos-build/bin/clang"
[ -x "$LLVM_MC" ] || { echo "llvm-mc not found at $LLVM_MC" >&2; exit 1; }
[ -x "$CLANG" ] || { echo "clang not found at $CLANG" >&2; exit 1; }
SRC="$PROJECT_ROOT/runtime/src"
OUT="$PROJECT_ROOT/runtime"
asm() {
local s="$1"
local o="$OUT/$(basename "${s%.s}").o"
echo " AS $(basename "$s")"
"$LLVM_MC" -arch=w65816 -filetype=obj "$s" -o "$o"
}
cc() {
local c="$1"
local o="$OUT/$(basename "${c%.c}").o"
local extra=("${@:2}")
echo " CC $(basename "$c")"
"$CLANG" -target w65816 -O2 -ffunction-sections \
"${extra[@]}" \
-I"$PROJECT_ROOT/runtime/include" \
-c "$c" -o "$o"
}
asm "$SRC/crt0.s"
asm "$SRC/libgcc.s"
cc "$SRC/libc.c"
cc "$SRC/strtol.c"
cc "$SRC/snprintf.c"
cc "$SRC/qsort.c"
cc "$SRC/extras.c"
cc "$SRC/strtok.c"
cc "$SRC/math.c"
cc "$SRC/softFloat.c"
# softDouble.c builds at -O1 instead of -O2: __muldf3's 64x64 -> 128
# mul + inlined alignment shifts overflows the greedy allocator on
# the single-A target ("ran out of registers during register
# allocation"). -O1 produces correct + smaller code than the
# previous -O2 + -regalloc=fast workaround.
cc "$SRC/softDouble.c" -O1
echo "runtime built: $(ls -1 "$OUT"/*.o | wc -l) objects"