# Check version consistency.
MAJOR="${PKGVER%%.*}"
if [ "${PKGNAME/$MAJOR/}" == "$PKGNAME" ] ; then
	abdie "Either version or PKGNAME is wrong: ‘$PKGNAME’ with version ‘$PKGVER’."
fi

abinfo "Nutering fixinc.sh in gcc/Makefile ..."
sed -i 's@\./fixinc\.sh@-c true@' "$SRCDIR"/gcc/Makefile.in

abinfo 'Adjusting $CPPFLAGS ...'
sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" "$SRCDIR"/{libiberty,gcc}/configure

abinfo "Adjusting path for Go importing ..."
sed -i 's@%D@-L/usr/lib@g' "$SRCDIR"/gcc/go/lang-specs.h

# You will see this in "gcc -v" output.
# BASE-VER identifies the GCC version, please DO NOT EDIT.
# * DEV-PHASE are used as branding identification, you may alter the line.
abinfo "Appending AOSC versions to gcc -v ..."
echo 'AOSC OS, Core' > "$SRCDIR"/gcc/DEV-PHASE

abinfo "Stripping pre-defined configure options ..."
export AUTOTOOLS_DEF=""

abinfo "Sourcing more architecture-dependent flags ..."
source "$AB"/arch/${CROSS:-$ARCH}.sh

abinfo 'GCC only passes CPPFLAGS in some stages, appending other flags to $CPPFLAGS ...'
export CPPFLAGS="${CPPFLAGS} -O3 ${CFLAGS_COMMON_ARCH}"

if ab_match_arch loongarch64 || \
   ab_match_arch loongarch64_nosimd; then
    abinfo "Setting code model to medium ..."
    export CFLAGS="${CFLAGS} -mcmodel=medium"
    export CXXFLAGS="${CXXFLAGS} -mcmodel=medium"
fi
if ab_match_arch riscv64 ; then
	# -mno-omit-leaf-frame-pointer is only available to GCC 14 and up.
	# -mno-fence-tso is only available to GCC 15 and up.
	abinfo "Removing -mno-fence-tso from compiler flags ..."
	export CFLAGS="${CFLAGS/-mno-fence-tso/}"
	export CXXFLAGS="${CXXFLAGS/-mno-fence-tso/}"
fi

abinfo "Building libgccjit ..."
mkdir -pv "$SRCDIR"/jitbuild
cd "$SRCDIR"/jitbuild
"$SRCDIR"/configure ${AUTOTOOLS_DEF[@]} ${AUTOTOOLS_AFTER[@]} \
    --disable-bootstrap --enable-host-shared --enable-languages=jit \
    --build="${ARCH_TARGET[$ARCH]}" --host="${ARCH_TARGET[$ARCH]}" &&
    make
cd "$SRCDIR"
