#!/bin/sh
# A C compiler for Linux programs on Dusken: clang for x86_64-linux-musl (or MUSL_TARGET) against the musl built by
# `make linux` (MUSL_SYSROOT, default build/musl/sysroot), linking static position-independent executables with
# musl's start files and MUSL_LIBCC, if set (aarch64's quad-precision helpers). Used to build BusyBox, whose build
# expects a gcc-like driver.
SR=${MUSL_SYSROOT:-$(cd "$(dirname "$0")/.." && pwd)/build/musl/sysroot}
T=${MUSL_TARGET:-x86_64-linux-musl}
link=1
for a in "$@"; do case $a in -c|-E|-S|-M|-MM) link=0;; esac; done
if [ $link = 0 ]; then
    exec clang --target=$T -nostdinc -isystem "$SR/include" -fPIE "$@"
fi
exec clang --target=$T -fuse-ld=lld -static-pie -nostdlib -fPIE "$SR/lib/rcrt1.o" "$SR/lib/crti.o" \
    "$@" -L"$SR/lib" -lc $MUSL_LIBCC "$SR/lib/crtn.o"
