[source]
git = "https://gitlab.redox-os.org/redox-os/gcc"
branch = "redox-13.2.0"
shallow_clone = true
script = """
DYNAMIC_INIT
COOKBOOK_AUTORECONF=autoreconf2.69 autotools_recursive_regenerate -I"$(realpath ./config)"
LIBTOOL_BUILD_AUX="${COOKBOOK_LIBTOOL_DIR:-$COOKBOOK_HOST_SYSROOT}"/share/libtool/build-aux
cp -fpv "$LIBTOOL_BUILD_AUX"/{config.sub,config.guess,install-sh} libiberty/
"""

[build]
template = "custom"
dependencies = [
    "libgmp",
    "libmpfr",
    "mpc",
# TODO: this zlib get linked when boostrapping gcc
#    "zlib"
]
script = """
DYNAMIC_STATIC_INIT
CROSS_GNU_TARGET=${COOKBOOK_CROSS_GNU_TARGET:-$GNU_TARGET}

if [ "${COOKBOOK_HOST_SYSROOT}" = "/usr" ]; then
# not specifying --enable-shared as it will link shared deps
COOKBOOK_STAGE+="/usr"
COOKBOOK_CONFIGURE_FLAGS=(
    --prefix=""
    --host="${GNU_TARGET}"
    --program-prefix="${CROSS_GNU_TARGET}-"
    --with-native-system-header-dir="/include"
    --disable-hosted-libstdcxx
    --with-sysroot
    --with-build-sysroot="${COOKBOOK_CROSS_SYSROOT:-$COOKBOOK_SYSROOT}"
    --disable-bootstrap
)
else
export CFLAGS_FOR_TARGET="${CPPFLAGS}" CXXFLAGS_FOR_TARGET="${CPPFLAGS}" LDFLAGS_FOR_TARGET="${LDFLAGS}"
export CC_FOR_BUILD="$CC_WRAPPER gcc" CXX_FOR_BUILD="$CC_WRAPPER g++"
unset CFLAGS CPPFLAGS LDFLAGS
COOKBOOK_CONFIGURE_FLAGS+=(
    --with-sysroot=/
    --with-gmp="${COOKBOOK_SYSROOT}/usr"
    --with-mpfr="${COOKBOOK_SYSROOT}/usr"
    --with-mpc="${COOKBOOK_SYSROOT}/usr"
)
fi

if [ "${CROSS_GNU_TARGET}" = "riscv64gc-unknown-redox" ]; then
COOKBOOK_CONFIGURE_FLAGS+=(
    --with-arch=rv64gc
    --with-abi=lp64d
)
fi

if [ "${CROSS_GNU_TARGET}" != "i586-unknown-redox" ]; then
COOKBOOK_CONFIGURE_FLAGS+=(
    --enable-frame-pointer
)
fi

COOKBOOK_CONFIGURE_FLAGS+=(
    --target="${CROSS_GNU_TARGET}"
    --with-linker-hash-style=gnu
    --enable-languages=c,c++,lto
    --enable-initfini-array
    --disable-nls
    --disable-multilib
#   --with-system-zlib
    --enable-host-shared
    --enable-threads=posix
    --enable-libstdcxx-threads
    --with-bugurl="https://gitlab.redox-os.org/redox-os/gcc/-/issues"
)

"${COOKBOOK_CONFIGURE}" "${COOKBOOK_CONFIGURE_FLAGS[@]}"
"${COOKBOOK_MAKE}" -j "${COOKBOOK_MAKE_JOBS}" all-gcc
"${COOKBOOK_MAKE}" install-gcc DESTDIR="${COOKBOOK_STAGE}"

# requires relibc which is not available on bootstrapping
if [ "${COOKBOOK_HOST_SYSROOT}" != "/usr" ]; then

"${COOKBOOK_MAKE}" -j "${COOKBOOK_MAKE_JOBS}" all-target-libgcc all-target-libstdc++-v3
"${COOKBOOK_MAKE}" install-target-libgcc install-target-libstdc++-v3 DESTDIR="${COOKBOOK_STAGE}"
ln -s "gcc" "${COOKBOOK_STAGE}/usr/bin/cc"
# Avoid conflict with libgcc & libstdcxx
rm -f "${COOKBOOK_STAGE}"/usr/lib/libgcc_s.so* "${COOKBOOK_STAGE}"/usr/lib/libstdc++.so*
# Avoid rebuilding problem
rm -f ./gcc/libgcc_s.so*

fi
"""

[package]
dependencies = [
    "gnu-binutils"
]

[[optional-packages]]
name = "cxx"
dependencies = []
files = [
    "usr/bin/*c++",
    "usr/bin/*g++",
    "usr/include/c++/**",
    "usr/lib/*c++*",
    "usr/libexec/gcc/**/cc1plus",
    "usr/share/gcc-*/python/libstdcxx/**",
]
