#!/usr/bin/env bash
# Cross-compiler wrapper for build systems (configure, CMake, Meson): host gcc
# with the system's musl, start-up code and linker script.
#
#   CC=/path/to/sdk/koi-gcc CXX=/path/to/sdk/koi-g++ ./configure --host=x86_64-linux-musl
#
# Called as koi-g++ (a link) it compiles C++ with libstdc++.
set -euo pipefail

SDK=$(cd "$(dirname "$(readlink -f "$0")")" && pwd)
LIBC=$SDK/libc
case "$(basename "$0")" in
    *++) DRIVER=${KOI_HOST_CXX:-g++}; CPLUS=1 ;;
    *) DRIVER=${KOI_HOST_CC:-gcc}; CPLUS=0 ;;
esac

LINKING=1
for argument in "$@"; do
    case "$argument" in
        -c|-S|-E|-M|-MM|-fsyntax-only) LINKING=0 ;;
    esac
done
[ $# -eq 0 ] && LINKING=0

# The SDK's C++ library comes with the headers of one GCC (VERSION: libstdc++=X.Y.Z). A host
# GCC older than that cannot compile them (tried: GCC 12 and 13 fail in bits/alloc_traits.h on
# 14's headers; 14 and 16 build): koi-g++ refuses at once, saying why, rather than with errors
# deep in the headers. KOI_SDK_FORCE=1 tries anyway. C (koi-gcc) builds with older GCCs too.
# Only for GCC: another compiler's -dumpversion (KOI_HOST_CXX=clang++) is not comparable.
if [ "$CPLUS" = 1 ] && [ -z "${KOI_SDK_FORCE:-}" ] && [ -f "$SDK/VERSION" ] &&
   "$DRIVER" --version 2>/dev/null | head -1 | grep -q -e GCC -e 'g++'; then
    WANT=$(sed -n 's/^libstdc++=\([0-9]*\).*/\1/p' "$SDK/VERSION")
    HAVE=$("$DRIVER" -dumpversion 2>/dev/null | cut -d. -f1)
    if [ -n "$WANT" ] && [ -n "$HAVE" ] && [ "$HAVE" -lt "$WANT" ]; then
        echo "koi-g++: this machine's $DRIVER is GCC $HAVE; the SDK's C++ library needs GCC $WANT or newer to compile its headers. Install GCC $WANT+ (KOI_HOST_CXX=g++-$WANT), or build in the SDK's container (sdk/Containerfile). KOI_SDK_FORCE=1 tries anyway." >&2
        exit 1
    fi
fi

INCLUDE=(-nostdinc)
if [ "$CPLUS" = 1 ]; then
    # The C++ library's headers (sdk/libc/include/c++/VERSION). Without them, say what is missing
    # rather than an ls error (an SDK from before 1128, or a broken unpack).
    if [ ! -d "$LIBC/include/c++" ]; then
        echo "koi-g++: this SDK has no C++ library headers ($LIBC/include/c++ is missing)." >&2
        echo "koi-g++: get the current SDK from https://docs.koiayame.com/sdk/mizu-sdk.tar.gz and unpack it again." >&2
        exit 1
    fi
    VERSION=$(ls "$LIBC/include/c++" | head -1)
    INCLUDE+=(-nostdinc++ -isystem "$LIBC/include/c++/$VERSION"
              -isystem "$LIBC/include/c++/$VERSION/x86_64-linux-musl")
fi
INCLUDE+=(-isystem "$LIBC/include" -isystem "$($DRIVER -print-file-name=include)")

# Position-independent, no red zone, no stack protector.
FLAGS=(-fpie -mno-red-zone -fno-stack-protector -DKOI_HOSTED -D__KOI_DOS__)
# No build folder in the program (__FILE__, assert, debug info): the folder it is built in and
# the tree the SDK sits in both become ".". The later map wins where both match.
ROOT=$(dirname "$SDK")
FLAGS+=(-ffile-prefix-map="$PWD"=. -ffile-prefix-map="$ROOT"=.)

if [ "$LINKING" = 0 ]; then
    exec "$DRIVER" "${FLAGS[@]}" "${INCLUDE[@]}" "$@"
fi

LIBGCC=$LIBC/lib/libgcc.a
[ -f "$LIBGCC" ] || LIBGCC=$($DRIVER -print-libgcc-file-name)
# Unwinding (C++ exceptions): the SDK's, else the host's, else none for C.
EH=$LIBC/lib/libgcc_eh.a
[ -f "$EH" ] || EH=$($DRIVER -print-file-name=libgcc_eh.a)
EH_LIB=()
[ -f "$EH" ] && EH_LIB=("$EH")
# Pull in pthread symbols that Mesa and libstdc++ reference weakly.
PTHREAD=()
for name in pthread_mutexattr_init pthread_mutexattr_settype pthread_mutexattr_destroy \
            pthread_mutex_lock pthread_mutex_unlock pthread_once pthread_key_create \
            pthread_getspecific pthread_setspecific pthread_cond_wait pthread_cond_broadcast \
            pthread_cond_signal pthread_create pthread_join pthread_detach pthread_self; do
    PTHREAD+=(-Wl,-u,"$name")
done
# OSMesa parts SDL needs when linked with -lGL.
for argument in "$@"; do
    case "$argument" in
        -lGL|-lOSMesa)
            for name in OSMesaCreateContextAttribs OSMesaMakeCurrent OSMesaDestroyContext \
                        OSMesaGetProcAddress OSMesaPixelStore glFinish; do
                PTHREAD+=(-Wl,-u,"$name")
            done
            CPLUS_LIBS=1
            ;;
    esac
done
LIBS=()
[ "$CPLUS" = 1 ] || [ "${CPLUS_LIBS:-0}" = 1 ] &&
    LIBS+=("$LIBC/lib/libstdc++.a" "$LIBC/lib/libsupc++.a")
exec "$DRIVER" "${FLAGS[@]}" "${INCLUDE[@]}" \
    -nostdlib -pie -Wl,--no-dynamic-linker -Wl,-T,"$LIBC/lib/program-libc.ld" \
    -Wl,--eh-frame-hdr -Wl,--build-id=sha1 -Wl,-z,max-page-size=0x1000 \
    -Wl,-z,noexecstack "${PTHREAD[@]}" \
    -L"$LIBC/lib" "$LIBC/lib/koi_crt.o" "$@" \
    -L"$LIBC/lib" -Wl,--start-group "${LIBS[@]}" "$LIBC/lib/libc.a" \
    "${EH_LIB[@]}" "$LIBGCC" -Wl,--end-group
