# Maintainer: Carlos Antunes <cmantunes@gmail.com>
# Maintainer: fauxpark <fauxpark@gmail.com>
# Contributed by: nanodude

_realname=gcc
_target=arm-none-eabi

pkgbase=mingw-w64-${_target}-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_target}-${_realname}")
pkgver=13.3.0
pkgrel=1
pkgdesc='GNU Tools for ARM Embedded Processors - GCC (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://www.gnu.org/software/gcc/gcc.html'
msys2_references=(
  "cpe: cpe:/a:gnu:gcc"
)
license=('spdx:GPL-3.0-or-later')
groups=("${MINGW_PACKAGE_PREFIX}-${_target}-toolchain")
depends=(
    "${MINGW_PACKAGE_PREFIX}-${_target}-binutils"
    "${MINGW_PACKAGE_PREFIX}-${_target}-newlib"
    "${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-gmp"
    "${MINGW_PACKAGE_PREFIX}-isl"
    "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
    "${MINGW_PACKAGE_PREFIX}-mpc"
    "${MINGW_PACKAGE_PREFIX}-mpfr"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-zstd"
)
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-cc"
    "${MINGW_PACKAGE_PREFIX}-autotools"
)
options=('!emptydirs' '!strip')
source=("https://ftp.gnu.org/gnu/gcc/gcc-${pkgver}/gcc-${pkgver}.tar.xz")
sha256sums=('0845e9621c9543a13f484e94584a49ffc0129970e9914624235fc1d061a0c083')

_build_gcc() {
    declare -a _extra_config
    if [[ ${CC} == clang ]]; then
        _extra_config+=("--disable-win32-utf8-manifest")
    fi

    CFLAGS+=" -w"
    CXXFLAGS+=" -w"
    local _GCC_LDFLAGS="${LDFLAGS} -Wl,--disable-dynamicbase"
    ../${_realname}-${pkgver}/configure \
        --prefix=${MINGW_PREFIX} \
        --target=${_target} \
        --with-native-system-header-dir=${MINGW_PREFIX}/include \
        --libexecdir=${MINGW_PREFIX}/lib \
        --enable-languages="c,c++" \
        --enable-plugins \
        --disable-decimal-float \
        --disable-libffi \
        --disable-libgomp \
        --disable-libmudflap \
        --disable-libquadmath \
        --disable-libssp \
        --disable-libstdcxx-pch \
        --disable-nls \
        --disable-shared \
        --disable-threads \
        --disable-tls \
        --disable-libada \
        --with-gnu-as \
        --with-gnu-ld \
        --with-system-zlib \
        --with-newlib \
        --with-headers=${MINGW_PREFIX}/${_target}/include \
        --with-python-dir=share/gcc-${_target} \
        --with-gmp \
        --with-mpfr \
        --with-mpc \
        --with-isl \
        --with-libelf \
        --enable-gnu-indirect_function \
        --with-multilib-list=rmprofile \
        --with-host-libstdcxx="-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm" \
        --enable-linker-plugin-flags='LDFLAGS=-static-libstdc++\ -static-libgcc\ '"${LDFLAGS// /\\ }"'\ -Wl,--stack,12582912' \
        "${_extra_config[@]}" \
        LDFLAGS="${_GCC_LDFLAGS}"

    make INHIBIT_LIBC_CFLAGS='-DUSE_TM_CLONE_REGISTRY=0'
}

build() {
    mkdir build-${MSYSTEM} && cd build-${MSYSTEM}

    export CFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections'
    export CXXFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections'
    _build_gcc

    # Build libstdc++ without exceptions support (the 'nano' variant)
    mkdir "${srcdir}"/build-nano-${MSYSTEM} && cd "${srcdir}"/build-nano-${MSYSTEM}

    export CFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections -fno-exceptions'
    export CXXFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections -fno-exceptions'
    # Only C and C++ are required for the nano build
    _build_gcc
}

package() {
    cd ${srcdir}/build-${MSYSTEM}

    make DESTDIR="${pkgdir}" install

    cd ${srcdir}/build-nano-${MSYSTEM}

    mkdir ${pkgdir}/nano-install-${MSYSTEM}

    make DESTDIR="${pkgdir}/nano-install-${MSYSTEM}" install

    cd ${pkgdir}${MINGW_PREFIX}

    # we need only libstdc nano files
    multilibs=($(bin/${_target}-gcc -print-multi-lib 2>/dev/null))
    for multilib in "${multilibs[@]}"; do
        dir="${multilib%%;*}"
        from_dir=${pkgdir}/nano-install-${MSYSTEM}${MINGW_PREFIX}/${_target}/lib/"$dir"
        to_dir=${_target}/lib/"$dir"

        cp -f "$from_dir"/libstdc++.a "$to_dir"/libstdc++_nano.a
        cp -f "$from_dir"/libsupc++.a "$to_dir"/libsupc++_nano.a
    done
    rm -r ${pkgdir}/nano-install-${MSYSTEM}

    # strip target binaries
    find lib/gcc/${_target}/${pkgver} ${_target}/lib -type f -and \( -name \*.a -or -name \*.o \) -exec ${_target}-objcopy -R .comment -R .note -R .debug_info -R .debug_aranges -R .debug_pubnames -R .debug_pubtypes -R .debug_abbrev -R .debug_line -R .debug_str -R .debug_ranges -R .debug_loc '{}' \;

    # strip host binaries
    find bin/ lib/gcc/${_target}/${pkgver} -type f -and \( -executable \) -exec strip '{}' \;

    # Remove files that conflict with host gcc package
    rm -r share/man/man7
    rm -r share/info
}
