# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
# Based upon Alpine's avr-gcc and Arch Linux's arm-none-eabi-gcc, to which
# the following people contributed:
# Contributor: Pedro Filipe <xpecex@outlook.com>
# Contributor: Stefan Wagner <stw@bit-strickerei.de>
# Contributor: Anatol Pomozov <anatol.pomozov@gmail.com>
# Contributor: Martin Schmölzer <mschmoelzer@gmail.com>
pkgname=gcc-cross-embedded
pkgver=15.1.0
pkgrel=0
pkgdesc="C compilers from the GNU Compiler Collection for embedded targets"
url="https://gcc.gnu.org/"
depends="gcc"
makedepends="
	bash
	gmp-dev
	isl-dev
	linux-headers
	mpc1-dev
	mpfr-dev
	python3
	zlib-dev
	"
# riscv64: CI is still too slow
arch="all !riscv64"
license="GPL-3.0-or-later"
source="
	https://mirrors.kernel.org/gnu/gcc/gcc-$pkgver/gcc-$pkgver.tar.xz

	newlib-getentropy.patch
	xtensa-fix-specs.patch
	"
options="!check"
builddir="$srcdir/gcc-$pkgver"

# classify targets as exotic or mainstream. Only build support for
# exotic targets on typical developer / CI hardware (x86_86, aarach64).
_targets_mainstream="
	arm-none-eabi
	riscv-none-elf
	"

_targets_exotic="
	msp430-elf
	or1k-elf
	aarch64-none-elf
	xtensa-none-elf
	xtensa-esp32-elf
	xtensa-esp32s2-elf
	xtensa-esp32s3-elf
	xtensa-esp8266-elf
	"

# This specifies the supported RISC-V multilib ISA extensions.
# For more information on the syntax, see the comment in gcc/config/riscv/multilib-generator.
#
# The selected ISA extensions are modeled after the Debian package.
# See: https://salsa.debian.org/debian/gcc-riscv64-unknown-elf/-/blob/4a1de76060cb8721c06bcfb4c846bc82ee6b3fc3/debian/patches/0002-Add-more-multi-lib-for-rv32-and-rv64.patch
_riscv_multilib="rv32e-ilp32e--c;rv32ea-ilp32e--m;\
rv32em-ilp32e--c;rv32eac-ilp32e--;rv32emac-ilp32e--;\
rv32i-ilp32--c;rv32ia-ilp32--m;rv32im-ilp32--c;\
rv32if-ilp32f-rv32ifd-c;rv32iaf-ilp32f-rv32imaf,rv32iafc-d;\
rv32imf-ilp32f-rv32imfd-c;rv32iac-ilp32--;rv32imac-ilp32--;\
rv32imafc-ilp32f-rv32imafdc-;rv32ifd-ilp32d--c;rv32imfd-ilp32d--c;\
rv32iafd-ilp32d-rv32imafd,rv32iafdc-;rv32imafdc-ilp32d--;rv64i-lp64--c;\
rv64ia-lp64--m;rv64im-lp64--c;rv64if-lp64f-rv64ifd-c;\
rv64iaf-lp64f-rv64imaf,rv64iafc-d;rv64imf-lp64f-rv64imfd-c;\
rv64iac-lp64--;rv64imac-lp64--;rv64imafc-lp64f-rv64imafdc-;\
rv64ifd-lp64d--m,c;rv64iafd-lp64d-rv64imafd,rv64iafdc-;rv64imafdc-lp64d--"

case "$CARCH" in
x86_64|aarch64|loongarch64)
	# support all targets on typical workstation / notebook / CI servers
	_targets="$_targets_mainstream $_targets_exotic"
	makedepends="$makedepends xtensa-dynconfig"
	;;
*)
	# support only mainstream targets on exotic hosts
	_targets="$_targets_mainstream"
	;;
esac

for target in $_targets; do
	case "$target" in
	xtensa-esp*-elf)
		subpackages="gcc-$target:_install_esp $subpackages"
		;;
	*)
		subpackages="gcc-$target:_install_subpkg $subpackages"
		;;
	esac
	makedepends="$makedepends binutils-$target"
done

prepare() {
	default_prepare

	# hack! - some configure tests for header files using "$CPP $CPPFLAGS"
	for dir in libiberty gcc; do
		sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" $dir/configure
	done
}

_configure_gcc() {
	local target_specific_flags
	case "$target" in
		# Support M and R architecture profiles for ARM
		arm-none-eabi) target_specific_flags="--with-multilib-list=rmprofile" ;;
		riscv-none-elf) target_specific_flags="--with-multilib-generator=$_riscv_multilib" ;;
		*) target_specific_flags="" ;;
	esac

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

	msg "Running ./configure for $target"
	"$builddir"/configure \
		--target=$target \
		--prefix=/usr \
		--with-sysroot=/usr/$target \
		--with-native-system-header-dir=/include \
		--with-headers=/usr/$target/include \
		--with-python-dir=share/gcc-$target \
		--libexecdir=/usr/lib \
		--infodir=/deleteme/info \
		--htmldir=/deleteme/html \
		--pdfdir=/deleteme/pdf \
		--mandir=/deleteme/man \
		--enable-languages=c \
		--enable-lto \
		--enable-plugins \
		--enable-gnu-indirect-function \
		--disable-decimal-float \
		--disable-libffi \
		--disable-libgomp \
		--disable-libmudflap \
		--disable-libquadmath \
		--disable-libssp \
		--disable-libstdcxx-pch \
		--disable-nls \
		--disable-shared \
		--disable-threads \
		--disable-werror \
		--with-gmp \
		--with-gnu-as \
		--with-gnu-ld \
		--with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' \
		--with-isl \
		--with-libelf \
		--with-mpc \
		--with-mpfr \
		--with-newlib \
		--with-system-zlib \
		--with-pkgversion='Alpine Linux' \
		$target_specific_flags
}

build() {
	local target
	for target in $_targets; do
		local workingdir
		workingdir="$srcdir/build-$target"
		mkdir "$workingdir"
		cd "$workingdir"
		export CFLAGS_FOR_TARGET='-pipe -Os -ffunction-sections -fdata-sections'
		export CXXFLAGS_FOR_TARGET='-pipe -Os -ffunction-sections -fdata-sections -fno-use-cxa-atexit'
		case "$target" in
		xtensa-esp*-elf)
			local mcu
			mcu="${target#xtensa-}"
			mcu="${mcu%-elf}"
			export XTENSA_GNU_CONFIG="/usr/lib/xtensa-dynconfig/xtensa_$mcu.so"
			target=xtensa-none-elf
			_configure_gcc
			msg "Running make for xtensa-$mcu-elf"
			make INHIBIT_LIBC_CFLAGS='-DUSE_TM_CLONE_REGISTRY=0' all-target-libgcc
			;;
		*)
			unset XTENSA_GNU_CONFIG
			_configure_gcc
			msg "Running make for $target"
			make INHIBIT_LIBC_CFLAGS='-DUSE_TM_CLONE_REGISTRY=0'
		esac
	done
}

package() {
	local target
	for target in $_targets; do
		depends="$depends gcc-$target"
	done
	mkdir -p "$pkgdir"
}

_install_subpkg() {
	target="${subpkgname#gcc-}"
	pkgdesc="C compiler of the GNU Compiler Collection for $target targets"
	depends="$depends binutils-$target"
	local workingdir
	workingdir="$srcdir/build-$target"
	cd "$workingdir"
	make install DESTDIR="$subpkgdir" -j1

	# Delete documentation. (The user can use the host GCC doc instead)
	rm -rf "$subpkgdir"/deleteme
	# Delete libcc1.so*, which is already part of the host's gcc
	rm -f "$subpkgdir"/usr/lib/libcc1.so*

	# Strip libs using target binutils
	find "$subpkgdir"/usr/lib -type f -name "*.a" -exec /usr/bin/$target-strip --strip-debug '{}' \;

	# Strip executables using host binutils
	find "$subpkgdir"/usr/bin -type f -executable -exec strip '{}' \;

	case "$target" in
	xtensa-none-elf)
		# We don't want libgcc and crt* for generic xtensa (which does not exist
		# in hardware), but only for concrete xtensa CPUs
		find "$subpkgdir/usr/lib/gcc/xtensa-none-elf/$pkgver" \
			\( -name '*.o' -o -name '*.a' \) \
			-exec rm {} \;
		;;
	esac
}

_install_esp() {
	target="${subpkgname#gcc-}"
	local mcu
	mcu="${target#xtensa-}"
	mcu="${mcu%-elf}"
	pkgdesc="C compiler of the GNU Compiler Collection for $target targets"
	depends="binutils-$target gcc-xtensa-none-elf=$pkgver-r$pkgrel xtensa-dynconfig-$mcu"
	local workingdir
	workingdir="$srcdir/build-$target"
	cd "$workingdir"
	make install-target-libgcc DESTDIR="$subpkgdir"/tmp -j1
	mkdir -p "$subpkgdir/usr/$target/lib/xtensa-none-elf/$pkgver"
	find "$subpkgdir/tmp/usr/lib/gcc/xtensa-none-elf/$pkgver" \
		\( -name '*.o' -o -name '*.a' \) \
		-exec mv {} "$subpkgdir/usr/$target/lib/xtensa-none-elf/$pkgver" \;
	rm -rf "$subpkgdir"/tmp

	local cmds
	cmds="
		gcc-ar
		gcc-nm
		gcc-ranlib
		gcov
		gcov-dump
		gcov-tool
		lto-dump
		"
	mkdir -p "$subpkgdir/usr/bin"
	for cmd in $cmds; do
		cat > "$subpkgdir/usr/bin/$target-$cmd" << EOF
#!/bin/sh
exec env XTENSA_GNU_CONFIG=/usr/lib/xtensa-dynconfig/xtensa_$mcu.so xtensa-none-elf-$cmd "\$@"
EOF
		chmod +x "$subpkgdir/usr/bin/$target-$cmd"
	done

	cmds="
		cpp
		gcc
		"
	mkdir -p "$subpkgdir/usr/bin"
	for cmd in $cmds; do
		cat > "$subpkgdir/usr/bin/$target-$cmd" << EOF
#!/bin/sh
exec env XTENSA_GNU_CONFIG=/usr/lib/xtensa-dynconfig/xtensa_$mcu.so xtensa-none-elf-$cmd --sysroot=/usr/$target "\$@"
EOF
		chmod +x "$subpkgdir/usr/bin/$target-$cmd"
	done
}

sha512sums="
ddd35ca6c653dffa88f7c7ef9ee4cd806e156e0f3b30f4d63e75a8363361285cd566ee73127734cde6a934611de815bee3e32e24bfd2e0ab9f7ff35c929821c1  gcc-15.1.0.tar.xz
52e45ba12be74ce6f740eff3a79e3da87c80b06275fd00b2a1193b052fd0891e7c4c09fd6a9c204a82178c135318d69db3e250f12c37eca72edb1e3c0b0be2db  newlib-getentropy.patch
ce9873d26ea4ab6fd9a50b8d5ac55c88abbad18fb16a79a9f6ac3d1ae3a1c7a9b876bfa9fcba11bec16672ef7a10ffaf3923ce361fc530f0f811858dcc9da9eb  xtensa-fix-specs.patch
"
