# SPDX-License-Identifier: CC0-1.0
#
# SPDX-FileContributor: Adrian "asie" Siekierka, 2023

pkgname=(toolchain-gcc-ia16-elf-gcc toolchain-gcc-ia16-elf-gcc-libs)
_pkgver=6.3.0
pkgver=6.3.0.r147141.4cac029b8c6
pkgrel=1
_gmpver=6.1.2
_mpfrver=3.1.5
_mpcver=1.0.3
_islver=0.16.1
epoch=
pkgdesc="The GNU Compiler Collection (IA-16 fork, C compiler only)"
arch=("x86_64" "aarch64")
depends=(runtime-gcc-libs runtime-musl toolchain-gcc-ia16-elf-binutils)
makedepends=(runtime-musl-dev)
groups=(toolchain-gcc-ia16-elf)
url="https://github.com/WonderfulToolchain/gcc"
license=("GPL-3.0-or-later")
source=("gcc::git+https://github.com/WonderfulToolchain/gcc#branch=wonderful/gcc-ia16-6.3.0"
        "http://gmplib.org/download/gmp/gmp-$_gmpver.tar.xz"
        "http://www.mpfr.org/mpfr-$_mpfrver/mpfr-$_mpfrver.tar.xz"
        "http://ftp.gnu.org/gnu/mpc/mpc-$_mpcver.tar.gz"
	"https://libisl.sourceforge.io/isl-$_islver.tar.xz")
sha256sums=(
	'SKIP'
	'87b565e89a9a684fe4ebeeddb8399dce2599f9c9049854ca8c0dfbdea0e21912'
	'015fde82b3979fbe5f83501986d328331ba8ddf008c1ff3da3c238f49ca062bc'
	'617decc6ea09889fb08ede330917a00b16809b8db88c29c31bfbb49cbf88ecc3'
	'45292f30b3cb8b9c03009804024df72a79e9b5ab89e41c94752d6ea58a1e4b02'
)

. "/wf/config/runtime-env-vars.sh"

prepare() {
	mkdir -p "gcc-build"
	cd gcc

	# HACK: hijack RTEMS's libstdc++ crossconfig for our own purposes (which has the dynamic feature checks we want)
	sed -i "s/\*-rtems\*/*-unknown*/" libstdc++-v3/configure

	ln -s ../"gmp-$_gmpver" gmp
	ln -s ../"mpfr-$_mpfrver" mpfr
	ln -s ../"mpc-$_mpcver" mpc
	ln -s ../"isl-$_islver" isl
}

GIT_REPO_DIR=gcc
. "../templates/git-pkgver.PKGBUILD"

build() {
	export PATH=$WF_PATH/toolchain/gcc-ia16-elf/bin:$PATH

	cd gcc-build

	# workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108300
	if [ "$WF_HOST_OS" == "windows" ]; then
		CPPFLAGS='-DWIN32_LEAN_AND_MEAN'
	fi

	../gcc/configure \
		--prefix="$WF_PATH/toolchain/gcc-ia16-elf" \
		--target=ia16-elf \
		--with-bugurl="$WF_BUGURL" \
		--with-stage1-ldflags="$WF_RUNTIME_LDFLAGS" \
		--without-headers \
		--enable-languages=c,c++ \
		--enable-lto \
		--disable-bootstrap \
		--disable-gcov \
		--disable-nls \
		--disable-shared \
		--disable-werror \
		--disable-libquadmath \
		--disable-libssp \
		--disable-libstdcxx \
		--disable-libstdcxx-pch \
		--disable-libunwind-exceptions \
		--disable-multilib \
		--with-gnu-as \
		--with-gnu-ld \
		--with-isl

	make
}

package_toolchain-gcc-ia16-elf-gcc() {
	cd gcc-build
	make DESTDIR="$pkgdir" install-gcc install-libcc1
	cd "$pkgdir"
	wf_relocate_path_to_destdir
	rm toolchain/gcc-ia16-elf/share/info/dir
}

package_toolchain-gcc-ia16-elf-gcc-libs() {
	pkgdesc="GCC-provided libraries"
	depends=("toolchain-gcc-ia16-elf-binutils" "toolchain-gcc-ia16-elf-gcc")
	options=(!strip)

	cd gcc-build

	# DS == SS, non-segelf memory model versions of libgcc
	for MEMORY_MODEL in tiny small; do
		OPT_LEVEL=-Os

	        make clean-target-libgcc
        	make all-target-libgcc \
			CFLAGS_FOR_TARGET="$OPT_LEVEL -march=v30mz -mtune=v30mz -mregparmcall -mcmodel=$MEMORY_MODEL" \
			MULTIFLAGS="-march=v30mz -mtune=v30mz -mregparmcall -mcmodel=$MEMORY_MODEL"
        	mkdir -p "$pkgdir$WF_DESTDIR"/toolchain/gcc-ia16-elf/lib/v30mz/$MEMORY_MODEL/
        	install -m 644 ia16-elf/libgcc/libgcc.a "$pkgdir$WF_DESTDIR"/toolchain/gcc-ia16-elf/lib/v30mz/$MEMORY_MODEL/libgcc.a
	done

	# DS != SS, non-segelf memory model versions of libgcc
	for MEMORY_MODEL in tiny small; do
		OPT_LEVEL=-Os

	        make clean-target-libgcc
        	make all-target-libgcc \
			CFLAGS_FOR_TARGET="$OPT_LEVEL -march=v30mz -mtune=v30mz -mregparmcall -mcmodel=$MEMORY_MODEL -mno-callee-assume-ss-data-segment" \
			MULTIFLAGS="-march=v30mz -mtune=v30mz -mregparmcall -mcmodel=$MEMORY_MODEL -mno-callee-assume-ss-data-segment"
        	mkdir -p "$pkgdir$WF_DESTDIR"/toolchain/gcc-ia16-elf/lib/v30mz-separate-stack/$MEMORY_MODEL/
        	install -m 644 ia16-elf/libgcc/libgcc.a "$pkgdir$WF_DESTDIR"/toolchain/gcc-ia16-elf/lib/v30mz-separate-stack/$MEMORY_MODEL/libgcc.a
	done

	# DS == SS, segelf memory model versions of libgcc
	for MEMORY_MODEL in small medium; do
		OPT_LEVEL=-Os
		if [ "$MEMORY_MODEL" = "medium" ]; then
			OPT_LEVEL=-O2
		fi

	        make clean-target-libgcc
        	make all-target-libgcc \
			CFLAGS_FOR_TARGET="$OPT_LEVEL -march=v30mz -mtune=v30mz -mregparmcall -mcmodel=$MEMORY_MODEL -msegelf" \
			MULTIFLAGS="-march=v30mz -mtune=v30mz -mregparmcall -mcmodel=$MEMORY_MODEL -msegelf"
        	mkdir -p "$pkgdir$WF_DESTDIR"/toolchain/gcc-ia16-elf/lib/v30mz-segelf/$MEMORY_MODEL/
        	install -m 644 ia16-elf/libgcc/libgcc.a "$pkgdir$WF_DESTDIR"/toolchain/gcc-ia16-elf/lib/v30mz-segelf/$MEMORY_MODEL/libgcc.a
	done

	# DS != SS, segelf memory model versions of libgcc
	for MEMORY_MODEL in small medium; do
		OPT_LEVEL=-Os
		if [ "$MEMORY_MODEL" = "medium" ]; then
			OPT_LEVEL=-O2
		fi

	        make clean-target-libgcc
        	make all-target-libgcc \
			CFLAGS_FOR_TARGET="$OPT_LEVEL -march=v30mz -mtune=v30mz -mregparmcall -mcmodel=$MEMORY_MODEL -mno-callee-assume-ss-data-segment -msegelf" \
			MULTIFLAGS="-march=v30mz -mtune=v30mz -mregparmcall -mcmodel=$MEMORY_MODEL -mno-callee-assume-ss-data-segment -msegelf"
        	mkdir -p "$pkgdir$WF_DESTDIR"/toolchain/gcc-ia16-elf/lib/v30mz-segelf-separate-stack/$MEMORY_MODEL/
        	install -m 644 ia16-elf/libgcc/libgcc.a "$pkgdir$WF_DESTDIR"/toolchain/gcc-ia16-elf/lib/v30mz-segelf-separate-stack/$MEMORY_MODEL/libgcc.a
	done
}
