# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Simon Zeni <simon@bl4ckb0ne.ca>
pkgname=mingw-w64-crt
pkgver=11.0.1
pkgrel=0
pkgdesc="MinGW-w64 CRT for Windows"
options="!check !strip"
url="https://mingw-w64.sourceforge.net/"
arch="x86_64 x86"
license="custom"
makedepends="
	mingw-w64-gcc-base
	mingw-w64-binutils
	mingw-w64-headers
	mingw-w64-headers-bootstrap
	"
source="https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v$pkgver.tar.bz2"
builddir="$srcdir/mingw-w64-v$pkgver/mingw-w64-crt"

case "$CARCH" in
x86_64)
	_targets="x86_64-w64-mingw32 i686-w64-mingw32"
	makedepends="$makedepends i686-mingw-w64-binutils i686-mingw-w64-headers"
	subpackages="$subpackages i686-$pkgname:x32"
	;;
x86)
	_targets="i686-w64-mingw32"
	;;
esac

build() {
	export CFLAGS="$CFLAGS -O2"
	export CXXFLAGS="$CXXFLAGS -O2"
	export CPPFLAGS="$CPPFLAGS -O2"

	local target
	for target in $_targets; do
		msg "building target: $target"
		(
		local sixfour="$(echo $target | grep -q x86_64 && echo true || echo false)"
		if $sixfour; then
			local conf="--enable-lib64 --disable-lib32"
		else
			local conf="--enable-lib32 --disable-lib64"
		fi

		local bld="$srcdir"/bld-$target
		mkdir -p "$bld"
		cd "$bld"
		"$builddir"/configure \
			--prefix=/usr/$target \
			--host=$target \
			--target=$target \
			--enable-wildcard \
			$conf \
			CC=$target-gcc
		make
		)
	done
}

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

x32() {
	pkdesc="$pkdesc (i686 hosted CRT)"

	amove usr/i686-w64-mingw32
}

sha512sums="
568702100bbbb11f7f9c9596dd70df3fb2577492459cf55da38691af1504b91127cb5f3eff268850dcbaa461fdc72e4e2379f7a576fd4a0e537ee1fe42531c01  mingw-w64-v11.0.1.tar.bz2
"
