Files
static-php-cli/src/SPC/store/scripts/zig-cc.sh

79 lines
2.7 KiB
Bash
Raw Normal View History

2025-06-26 16:03:57 +07:00
#!/usr/bin/env bash
SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
2025-09-08 13:33:44 +07:00
BUILDROOT_ABS="$(realpath "$SCRIPT_DIR/../../../buildroot/include" 2>/dev/null || true)"
2025-06-26 16:03:57 +07:00
PARSED_ARGS=()
while [[ $# -gt 0 ]]; do
case "$1" in
-isystem)
shift
ARG="$1"
2025-07-02 09:26:11 +07:00
shift
ARG_ABS="$(realpath "$ARG" 2>/dev/null || true)"
[[ "$ARG_ABS" == "$BUILDROOT_ABS" ]] && PARSED_ARGS+=("-I$ARG") || PARSED_ARGS+=("-isystem" "$ARG")
2025-06-26 16:03:57 +07:00
;;
-isystem*)
ARG="${1#-isystem}"
shift
2025-07-02 09:26:11 +07:00
ARG_ABS="$(realpath "$ARG" 2>/dev/null || true)"
[[ "$ARG_ABS" == "$BUILDROOT_ABS" ]] && PARSED_ARGS+=("-I$ARG") || PARSED_ARGS+=("-isystem$ARG")
2025-06-26 16:03:57 +07:00
;;
-march=*|-mcpu=*)
2025-07-25 10:00:49 +07:00
OPT_NAME="${1%%=*}"
OPT_VALUE="${1#*=}"
# Skip armv8- flags entirely as Zig doesn't support them
if [[ "$OPT_VALUE" == armv8-* ]]; then
shift
continue
fi
# replace -march=x86-64 with -march=x86_64
2025-07-25 10:00:49 +07:00
OPT_VALUE="${OPT_VALUE//-/_}"
PARSED_ARGS+=("${OPT_NAME}=${OPT_VALUE}")
shift
;;
2025-06-26 16:03:57 +07:00
*)
PARSED_ARGS+=("$1")
shift
;;
esac
done
IS_LINK=1
NEED_PROFILE_RT=0 # https://codeberg.org/ziglang/zig/issues/32066
NEED_CRT=0 # https://codeberg.org/ziglang/zig/issues/32064
for _a in "${PARSED_ARGS[@]}"; do
case "$_a" in
-c|-S|-E|-M|-MM) IS_LINK=0 ;;
-fprofile-generate*|-fprofile-instr-generate*|-fcs-profile-generate*) NEED_PROFILE_RT=1 ;;
-shared) NEED_CRT=1 ;;
esac
done
[[ "$SPC_COMPILER_EXTRA" == *-fprofile-generate* || "$SPC_COMPILER_EXTRA" == *-fcs-profile-generate* ]] && NEED_PROFILE_RT=1
if [[ $IS_LINK -eq 1 && $NEED_PROFILE_RT -eq 1 && -f "$SCRIPT_DIR/lib/libclang_rt.profile.a" ]]; then
PARSED_ARGS+=("$SCRIPT_DIR/lib/libclang_rt.profile.a" "-Wl,-u,__llvm_profile_runtime")
fi
if [[ $IS_LINK -eq 1 && $NEED_CRT -eq 1 && -f "$SCRIPT_DIR/lib/clang_rt.crtbegin.o" && -f "$SCRIPT_DIR/lib/clang_rt.crtend.o" ]]; then
PARSED_ARGS+=("$SCRIPT_DIR/lib/clang_rt.crtbegin.o" "$SCRIPT_DIR/lib/clang_rt.crtend.o")
fi
2025-07-02 09:26:11 +07:00
[[ -n "$SPC_TARGET" ]] && TARGET="-target $SPC_TARGET" || TARGET=""
2025-07-01 16:21:57 +07:00
if [[ "$SPC_TARGET" =~ \.[0-9]+\.[0-9]+ ]]; then
output=$(zig cc $TARGET $SPC_COMPILER_EXTRA "${PARSED_ARGS[@]}" 2>&1)
status=$?
2025-07-01 14:12:51 +07:00
if [[ $status -eq 0 ]]; then
echo "$output"
exit 0
fi
2025-07-02 21:38:58 +07:00
if echo "$output" | grep -qE "version '.*' in target triple"; then
filtered_output=$(echo "$output" | grep -vE "version '.*' in target triple")
echo "$filtered_output"
exit 0
fi
2025-07-02 21:38:58 +07:00
fi
exec zig cc $TARGET $SPC_COMPILER_EXTRA "${PARSED_ARGS[@]}"