# Maintainer: fauxpark <fauxpark@gmail.com>

_realname=gcc
_target=avr

pkgbase=mingw-w64-${_target}-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_target}-${_realname}
pkgver=15.1.0
pkgrel=2
pkgdesc='GNU compiler collection for AVR 8-bit and 32-bit microcontrollers (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}-gmp"
         "${MINGW_PACKAGE_PREFIX}-isl"
         "${MINGW_PACKAGE_PREFIX}-mpc"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-cc")
options=(!strip)
source=(https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}/${_realname}-${pkgver}.tar.xz{,.sig})
sha256sums=('e2b09ec21660f01fecffb715e0120265216943f038d0e48a9868713e54f06cea'
            'SKIP')
validpgpkeys=(F3691687D867B81B51CE07D9BBE43771487328A9  # bpiotrowski@archlinux.org
              86CFFCA918CF3AF47147588051E8B148A9999C34  # evangelos@foutrelis.com
              13975A70E63C361C73AE69EF6EEB81F8981C74C7  # richard.guenther@gmail.com
              D3A93CAD751C2AF4F8C7AD516C35B99309B5FA62) # Jakub Jelinek <jakub@redhat.com>

build() {
    mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

    if [[ ${CC} == clang ]]; then
        CFLAGS+=" -Wno-int-conversion"
    fi

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

    ../${_realname}-${pkgver}/configure \
        --prefix=${MINGW_PREFIX} \
        --target=${_target} \
        --enable-languages=c,c++ \
        --disable-nls \
        --disable-libssp \
        --disable-shared \
        --disable-threads \
        --disable-libgomp \
        --disable-libada \
        --with-dwarf2 \
        --enable-mingw-wildcard \
        "${_extra_config[@]}"

    make
}

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

    make DESTDIR="$pkgdir" install

    cd "${pkgdir}"${MINGW_PREFIX}

    # strip debug symbols from libraries
    find lib -type f -name "*.a" -exec ${MINGW_PREFIX}/bin/${_target}-strip --strip-debug '{}' \;

    # info and man7 files conflict with native gcc
    rm -rf share/info share/man/man7
}
