#!/bin/sh
# C++ companion of ci/zigcc — see that file for why this exists.
if [ -z "$ZIGCC_TARGET" ]; then
    echo "zigcxx: ZIGCC_TARGET is not set (e.g. x86_64-linux-musl)" >&2
    exit 1
fi

kept=
skip_next=0
for arg in "$@"; do
    if [ "$skip_next" = 1 ]; then
        skip_next=0
        continue
    fi
    case "$arg" in
        --target=*) ;;
        -target | --target) skip_next=1 ;;
        # rustc passes these for aarch64-linux-gnu; zig's linker driver
        # rejects them (lld handles the errata workarounds itself).
        -Wl,--fix-cortex-a53-843419 | -Wl,--fix-cortex-a53-835769) ;;
        *)
            kept="$kept
$arg"
            ;;
    esac
done

# shellcheck disable=SC2046
exec zig c++ -target "$ZIGCC_TARGET" $(printf '%s' "$kept" | tail -n +2)
