# 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=g++-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 linux-headers gmp-dev mpfr-dev mpc1-dev zlib-dev isl-dev python3 gawk"
# riscv64: limited by gcc-cross-embedded
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
	"
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
	"

# TODO: reenable msp430-elf once https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119953
# is fixed
_targets_exotic="
	or1k-elf
	aarch64-none-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)
	# support all targets on typical workstation / notebook / CI servers
	_targets="$_targets_mainstream $_targets_exotic"
	;;
*)
	# support only mainstream targets on exotic hosts
	_targets="$_targets_mainstream"
	;;
esac

for target in $_targets; do
	subpackages="g++-$target:_install_subpkg $subpackages"
	# here only makedepends. The depends to binutils-$target, newlib-$target,
	# and gcc-$target will be set in the subpkg
	makedepends="$makedepends binutils-$target newlib-$target gcc-$target~$pkgver"
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
}

_build_gcc() {
	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

	msg "Running make for $target"
	make INHIBIT_LIBC_CFLAGS='-DUSE_TM_CLONE_REGISTRY=0'
}

build() {
	for target in $_targets; do
		# Build "regular" variant
		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'
		_build_gcc

		# Rebuild without exceptions to generate nano variant of libstc++
		workingdir="$srcdir/build-$target-nano"
		mkdir "$workingdir"
		cd "$workingdir"
		export CFLAGS_FOR_TARGET='-pipe -Os -ffunction-sections -fdata-sections  -fno-exceptions'
		export CXXFLAGS_FOR_TARGET='-pipe -Os -ffunction-sections -fdata-sections  -fno-exceptions -fno-use-cxa-atexit'
		_build_gcc
	done
}

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

_install_subpkg() {
	target="${subpkgname#g++-}"
	pkgdesc="C++ compiler from the GNU Compiler Collection for $target targets"
	depends="$depends binutils-$target newlib-$target gcc-$target~$pkgver"
	workingdir="$srcdir/build-$target"
	cd "$workingdir"
	# install regular folder into temporary folder
	make install DESTDIR="$subpkgdir-regular" -j1
	# now pick only C++ related stuff and move it into the subpkg folder
	mkdir -p \
		"$subpkgdir"/usr/bin \
		"$subpkgdir"/usr/lib/gcc/"$target"/"$pkgver"

	mv "$subpkgdir"-regular/usr/bin/"$target"-c++ \
		"$subpkgdir"-regular/usr/bin/"$target"-g++ \
		"$subpkgdir"/usr/bin/
	mv "$subpkgdir"-regular/usr/lib/gcc/"$target"/"$pkgver"/cc1plus \
		"$subpkgdir"/usr/lib/gcc/"$target"/"$pkgver"
	mv "$subpkgdir"-regular/usr/share "$subpkgdir"/usr/
	mv "$subpkgdir"-regular/usr/"$target" "$subpkgdir"/usr/

	# install nano-variant into temporary folder to obtain libstdc++ from there
	workingdir="$srcdir/build-$target-nano"
	cd "$workingdir"
	make install DESTDIR="$subpkgdir-nano" -j1

	# Fetch libstdc++ from nano variant
	multilibs=$("$subpkgdir"/usr/bin/"$target"-g++ -print-multi-lib 2>/dev/null)
	for multilib in $multilibs; do
		dir="${multilib%%;*}"
		from_dir="$subpkgdir-nano/usr/$target/lib/$dir"
		to_dir="$subpkgdir/usr/$target/lib/$dir"
		cp -f "$from_dir"/libstdc++.a "$to_dir"/libstdc++_nano.a
		cp -f "$from_dir"/libsupc++.a "$to_dir"/libsupc++_nano.a
	done

	# 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/"$target"/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 '{}' \;
}

sha512sums="
ddd35ca6c653dffa88f7c7ef9ee4cd806e156e0f3b30f4d63e75a8363361285cd566ee73127734cde6a934611de815bee3e32e24bfd2e0ab9f7ff35c929821c1  gcc-15.1.0.tar.xz
52e45ba12be74ce6f740eff3a79e3da87c80b06275fd00b2a1193b052fd0891e7c4c09fd6a9c204a82178c135318d69db3e250f12c37eca72edb1e3c0b0be2db  newlib-getentropy.patch
"
