# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Simon Zeni <simon@bl4ckb0ne.ca>
pkgname=mingw-w64-gcc-base
pkgver=13.2.0
pkgrel=1
_islver=0.26
pkgdesc="Cross GCC for the MinGW-w64 cross-compiler (bootstrap)"
url="https://gcc.gnu.org/"
arch="x86_64 x86"
license="GPL-3.0-or-later"
depends="
	!mingw-w64-gcc
	mingw-w64-binutils
	mingw-w64-headers
	mingw-w64-headers-bootstrap
	"
makedepends="
	gmp-dev
	isl-dev
	mpc1-dev
	mpfr-dev
	zlib-dev
	"
source="$pkgname-$pkgver.tar.xz::https://ftp.gnu.org/gnu/gcc/gcc-$pkgver/gcc-$pkgver.tar.xz"
builddir="$srcdir/gcc-$pkgver"

case "$CARCH" in
x86_64)
	_targets="x86_64-w64-mingw32 i686-w64-mingw32"
	depends="
		$depends
		!i686-mingw-w64-gcc
		i686-mingw-w64-binutils
		i686-mingw-w64-headers
		"
	;;
x86)
	_targets="i686-w64-mingw32"
	;;
esac

build() {
	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100431
	export CFLAGS="$CFLAGS -O2 -Wno-error=format-security"
	export CXXFLAGS="$CXXFLAGS -O2 -Wno-error=format-security"

	local target
	for target in $_targets; do
		msg "building target: $target"
		(
		local bld="$srcdir"/bld-$target
		mkdir -p "$bld"
		cd "$bld"
		"$builddir"/configure \
			--prefix=/usr \
			--program-prefix=$target- \
			--target=$target \
			--disable-dw2-exceptions \
			--disable-multilib \
			--disable-ns \
			--enable-checking=release \
			--enable-languages=c,lto \
			--enable-static \
			--enable-version-specific-runtime-libs \
			--with-bugurl="https://gitlab.alpinelinux.org/alpine/aports/-/issues" \
			--with-system-zlib
		make all-gcc
		)
	done
}

package() {
	local target
	for target in $_targets; do
		make -C "$srcdir"/bld-$target DESTDIR="$pkgdir" install-gcc
	done

	rm -rf "$pkgdir"/usr/share/man
	rm -rf "$pkgdir"/usr/share/info
	rm -rf "$pkgdir"/usr/share/locale
}

sha512sums="
d99e4826a70db04504467e349e9fbaedaa5870766cda7c5cab50cdebedc4be755ebca5b789e1232a34a20be1a0b60097de9280efe47bdb71c73251e30b0862a2  mingw-w64-gcc-base-13.2.0.tar.xz
"
