# Template file for 'cross-x86_64-w64-mingw32'
pkgname=cross-x86_64-w64-mingw32
version=12.0.0
revision=1
_gcc_version=14.2.0
_binutils_version=2.43
_gmp_version=6.3.0
_mpfr_version=4.2.1
_mpc_version=1.3.1
_isl_version=0.26
_mingw_version="${version}"
create_wrksrc=yes
hostmakedepends="tar flex perl texinfo"
makedepends="zlib-devel"
# it's ok to build with current's -devel packages
# although it might break the package during compiler updates
#makedepends="zlib-devel gmp-devel mpfr-devel libmpc-devel isl15-devel"
depends="cross-x86_64-w64-mingw32-crt-${version}_${revision}"
short_desc="Cross toolchain for Win64 (GCC ${_gcc_version})"
maintainer="Aleksey Tulinov <aleksey.tulinov@gmail.com>"
license="GPL-2.0-or-later, GPL-3.0-or-later, ZPL-2.1"
homepage="https://sourceforge.net/projects/mingw-w64/"
distfiles="
 ${GNU_SITE}/binutils/binutils-${_binutils_version}.tar.xz
 ${GNU_SITE}/gcc/gcc-${_gcc_version}/gcc-${_gcc_version}.tar.xz
 ${GNU_SITE}/mpc/mpc-${_mpc_version}.tar.gz
 ${GNU_SITE}/gmp/gmp-${_gmp_version}.tar.xz
 ${GNU_SITE}/mpfr/mpfr-${_mpfr_version}.tar.xz
 ${SOURCEFORGE_SITE}/libisl/isl-${_isl_version}.tar.bz2
 ${SOURCEFORGE_SITE}/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-v${_mingw_version}.tar.bz2"
checksum="b53606f443ac8f01d1d5fc9c39497f2af322d99e14cea5c0b4b124d630379365
 a7b39bc69cbf9e25826c5a60ab26477001f7c08d85cec04bc0e29cabed6f3cc9
 ab642492f5cf882b74aa0cb730cd410a81edcdbec895183ce930e706c1c759b8
 a3c2b80201b89e68616f4ad30bc66aee4927c3ce50e33929ca819d5c43538898
 277807353a6726978996945af13e52829e3abd7a9a5b7fb2793894e18f1fcbb2
 5eac8664e9d67be6bd0bee5085d6840b8baf738c06814df47eaf4166d9776436
 cc41898aac4b6e8dd5cffd7331b9d9515b912df4420a3a612b5ea2955bbeed2f"

nocross=yes
nopie=yes
nodebug=yes
noverifyrdeps=yes
noshlibprovides=yes
lib32disabled=yes  # disable build of 32bits packages
nostrip_files="libgcc.a libgcc_eh.a libgcc_s.a libgcov.a
 libatomic.a libatomic.dll.a libquadmath.a libquadmath.dll.a
 libssp.a libssp.dll.a libssp_nonshared.a
 libstdc++.a libstdc++.dll.a libstdc++fs.a libsupc++.a"

# https://sourceforge.net/p/mingw-w64/wiki2/Cross%20Win32%20and%20Win64%20compiler/
# https://gcc.gnu.org/install/configure.html
# http://mingw-w64.org/doku.php/configure

build_options="ucrt"
desc_option_ucrt="Build mingw32ucrt compiler"
build_options_default="ucrt"

subpackages="cross-x86_64-w64-mingw32-crt cross-i686-w64-mingw32 cross-i686-w64-mingw32-crt"
if [ "$build_option_ucrt" ]; then
	subpackages+=" cross-x86_64-w64-mingw32ucrt cross-x86_64-w64-mingw32ucrt-crt"
fi

_binutils_build() {
	_target=$1
	_sysroot="/usr/${_target}"
	_builddir=${wrksrc}/"build-binutils-${_target}"

	msg_normal "Building binutils: ${_target}\n"

	mkdir -p ${_builddir} && cd ${_builddir}

	../binutils-${_binutils_version}/configure \
		--prefix="/usr" \
		--with-sysroot=${_sysroot} \
		--target=${_target} \
		--disable-multilib \
		--disable-shared \
		--disable-nls \
		--disable-werror \
		--enable-lto \
		--with-system-zlib

	make ${makejobs} && make install
}

_mingw_headers() {
	_target=$1
	_sysroot="/usr/${_target}"
	_builddir=${wrksrc}/"build-mingw-headers-${_target}"
	_configure_args=" "

	msg_normal "Building MinGW headers: ${_target}\n"

	mkdir -p ${_builddir} && cd ${_builddir}

	if [ "${_target##*-}" == "mingw32ucrt" ]; then
		_configure_args+=" --with-default-msvcrt=ucrt"
	else
		_configure_args+=" --with-default-msvcrt=msvcrt"
	fi

	../mingw-w64-v${_mingw_version}/mingw-w64-headers/configure \
		--prefix=${_sysroot} \
		--host=${_target} \
		--disable-werror \
		--enable-secure-api \
		--enable-sdk=all \
		${_configure_args}

	make ${makejobs} && make install

	# manually create required symlinks
	(cd ${_sysroot} && ln -sfT "." "mingw")
	(cd ${_sysroot} && ln -sf "lib" "lib64")
}

_gcc_bootstrap() {
	_target=$1
	_sysroot="/usr/${_target}"
	_builddir=${wrksrc}/"build-gcc-${_target}"

	msg_normal "Building GCC bootsrap: ${_target}\n"

	# not needed when using current's -devel packages
	ln -sf ${wrksrc}/"gmp-${_gmp_version}" ${wrksrc}/"gcc-${_gcc_version}/gmp"
	ln -sf ${wrksrc}/"mpc-${_mpc_version}" ${wrksrc}/"gcc-${_gcc_version}/mpc"
	ln -sf ${wrksrc}/"mpfr-${_mpfr_version}" ${wrksrc}/"gcc-${_gcc_version}/mpfr"
	ln -sf ${wrksrc}/"isl-${_isl_version}" ${wrksrc}/"gcc-${_gcc_version}/isl"

	mkdir -p ${_builddir} && cd ${_builddir}

	../gcc-${_gcc_version}/configure \
		--prefix="/usr" \
		--with-sysroot=${_sysroot} \
		--target=${_target} \
		--disable-werror \
		--disable-nls \
		--disable-multilib \
		--disable-gcov \
		--disable-libcc1 \
		--enable-lto \
		--enable-shared \
		--enable-static \
		--enable-threads=posix \
		--with-system-zlib \
		--enable-languages=c,c++,lto

	make ${makejobs} all-gcc && make install-gcc
}

_mingw_crt_build() {
	_target=$1
	_sysroot="/usr/${_target}"
	_builddir=${wrksrc}/"build-mingw-crt-${_target}"

	msg_normal "Building MinGW CRT: ${_target}\n"

	if [ ${_target} == "i686-w64-mingw32" ]; then
		_crt_configure_args="--disable-lib64 --enable-lib32 --with-default-msvcrt=msvcrt"
	elif [ ${_target} == "x86_64-w64-mingw32" ]; then
		_crt_configure_args="--disable-lib32 --enable-lib64 --with-default-msvcrt=msvcrt"
	elif [ ${_target} == "x86_64-w64-mingw32ucrt" ]; then
		_crt_configure_args="--disable-lib32 --enable-lib64 --with-default-msvcrt=ucrt"
	fi

	mkdir -p ${_builddir} && cd ${_builddir}

	CC="${_target}-gcc" \
	CPP="${_target}-gcc -E" \
	AR="${_target}-ar" \
	RANLIB="${_target}-ranlib" \
	AS="${_target}-as" \
	STRIP="${_target}-strip" \
	OBJDUMP="${_target}-objdump" \
	WINDRES="${_target}-windres" \
	../mingw-w64-v${_mingw_version}/mingw-w64-crt/configure \
		--prefix=${_sysroot} \
		--host=${_target} \
		${_crt_configure_args}

	make ${makejobs} && make install
}

_mingw_winpthreads_build() {
	_target=$1
	_sysroot="/usr/${_target}"
	_builddir=${wrksrc}/"build-mingw-winpthreads-${_target}"

	msg_normal "Building MinGW winpthreads: ${_target}\n"

	mkdir -p ${_builddir} && cd ${_builddir}

	CC="${_target}-gcc" \
	CPP="${_target}-gcc -E" \
	AR="${_target}-ar" \
	RANLIB="${_target}-ranlib" \
	AS="${_target}-as" \
	STRIP="${_target}-strip" \
	OBJDUMP="${_target}-objdump" \
	WINDRES="${_target}-windres" \
	../mingw-w64-v${_mingw_version}/mingw-w64-libraries/winpthreads/configure \
		--prefix=${_sysroot} \
		--host=${_target} \
		--enable-static \
		--enable-shared

	make ${makejobs} && make install
}

_gcc_build() {
	_target=$1
	_sysroot="/usr/${_target}"
	_builddir=${wrksrc}/"build-gcc-${_target}"

	msg_normal "Building GCC: ${_target}\n"

	cd ${_builddir}

	# should be already configured previously
	# no need for install since this is the last step
	make ${makejobs}
}

_build_cross() {
	# ensure we use sane environment
	unset CC CXX CPP LD AS AR RANLIB OBJDUMP READELF NM
	unset STRIP OBJDUMP WINDRES
	unset CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

	_target=$1

	(_binutils_build ${_target})
	(_mingw_headers ${_target})
	(_gcc_bootstrap ${_target})
	(_mingw_crt_build ${_target})
	(_mingw_winpthreads_build ${_target})
	(_gcc_build ${_target})
}

do_build() {
	if [ "$build_option_ucrt" ]; then
		(_build_cross "x86_64-w64-mingw32ucrt")
	fi
	(_build_cross "x86_64-w64-mingw32")
	(_build_cross "i686-w64-mingw32")
}

_install_toolchain() {
	_target=$1

	(cd ${wrksrc}/"build-binutils-${_target}" && DESTDIR="$DESTDIR" make install)
	(cd ${wrksrc}/"build-gcc-${_target}" && DESTDIR="$DESTDIR" make install)

	# remove unnecessary stuff
	rm -rf ${DESTDIR}/usr/share/
	rm -rf ${DESTDIR}/usr/lib/libcc1*
	rm -f ${DESTDIR}/usr/lib/bfd-plugins/libdep.*
}

_install_crt() {
	_target=$1

	(cd ${wrksrc}/"build-mingw-headers-${_target}" && DESTDIR="$DESTDIR" make install)
	(cd ${wrksrc}/"build-mingw-crt-${_target}" && DESTDIR="$DESTDIR" make install)
	(cd ${wrksrc}/"build-mingw-winpthreads-${_target}" && DESTDIR="$DESTDIR" make install)
}

do_install() {
	(_install_toolchain "x86_64-w64-mingw32")
	# i686 version will be installed in subpackage (below)
}

_clean_cross() {
	_target=$1

	rm -rf /usr/${_target}
	rm -f  /usr/bin/${_target}*
	rm -rf /usr/lib/gcc/${_target}
	rm -rf /usr/libexec/gcc/${_target}
}

do_clean() {
	if [ "$build_option_ucrt" ]; then
		(_clean_cross "x86_64-w64-mingw32ucrt")
	fi
	(_clean_cross "x86_64-w64-mingw32")
	(_clean_cross "i686-w64-mingw32")
}

cross-x86_64-w64-mingw32-crt_package() {
	short_desc="CRT for Win64 cross toolchain"
	nopie=yes
	nodebug=yes
	# this subpackage exists to skip strip step on CRT files
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	lib32disabled=yes
	pkg_install() {
		DESTDIR="$PKGDESTDIR" _install_crt "x86_64-w64-mingw32"
	}
}

cross-i686-w64-mingw32_package() {
	short_desc="Cross toolchain for Win32 (GCC ${_gcc_version})"
	depends="cross-i686-w64-mingw32-crt-${version}_${revision}"
	nopie=yes
	nodebug=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	lib32disabled=yes
	nostrip_files="libgcc.a libgcc_eh.a libgcc_s.a libgcov.a
	 libatomic.a libatomic.dll.a libquadmath.a libquadmath.dll.a
	 libssp.a libssp.dll.a libssp_nonshared.a
	 libstdc++.a libstdc++.dll.a libstdc++fs.a libsupc++.a"
	pkg_install() {
		DESTDIR="$PKGDESTDIR" _install_toolchain "i686-w64-mingw32"
	}
}

cross-i686-w64-mingw32-crt_package() {
	short_desc="CRT for Win32 cross toolchain"
	nopie=yes
	nodebug=yes
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	lib32disabled=yes
	pkg_install() {
		DESTDIR="$PKGDESTDIR" _install_crt "i686-w64-mingw32"
	}
}

cross-x86_64-w64-mingw32ucrt_package() {
	short_desc="Cross toolchain for Win32 (GCC ${_gcc_version})"
	depends="cross-x86_64-w64-mingw32ucrt-crt-${version}_${revision}"
	nopie=yes
	nodebug=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	lib32disabled=yes
	nostrip_files="libgcc.a libgcc_eh.a libgcc_s.a libgcov.a
	 libatomic.a libatomic.dll.a libquadmath.a libquadmath.dll.a
	 libssp.a libssp.dll.a libssp_nonshared.a
	 libstdc++.a libstdc++.dll.a libstdc++fs.a libsupc++.a"
	pkg_install() {
		DESTDIR="$PKGDESTDIR" _install_toolchain "x86_64-w64-mingw32ucrt"
	}
}

cross-x86_64-w64-mingw32ucrt-crt_package() {
	short_desc="CRT for Win32 cross toolchain"
	nopie=yes
	nodebug=yes
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	lib32disabled=yes
	pkg_install() {
		DESTDIR="$PKGDESTDIR" _install_crt "x86_64-w64-mingw32ucrt"
	}
}
