#!/usr/bin/env bash
# moon's C compiler/linker driver for debug builds: the vendored tinycc
# (third_party/tinycc, built by `just tcc` into _build/tcc).
# moon adds -DMOONBIT_NATIVE_NO_SYS_HEADER whenever the compiler is tcc; that
# mode compiles the system parts of the runtime out (it expects to be JIT-ed
# inside moon), so a standalone executable would not link. Drop it.
set -euo pipefail
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
tcc="$root/_build/tcc/bin/tcc"
args=()
for a in "$@"; do
  [ "$a" = "-DMOONBIT_NATIVE_NO_SYS_HEADER" ] || args+=("$a")
done
# tcc does not link libm by default; the font rasteriser needs it. Add it on link steps only.
link=1
for a in "${args[@]}"; do
  case "$a" in -c|-ar) link=0 ;; esac
done
if [ "$link" = 1 ]; then args+=(-lm); fi
exec "$tcc" "${args[@]}"
