# Maintainer: Maxr1998 <aur@maxr1998.de>
# Contributor: Emil Renner Berthing <aur@esmil.dk>

_target=riscv64-unknown-elf
pkgname=${_target}-gcc
pkgver=16.2.0
_islver=0.26
pkgrel=1
pkgdesc='The GNU Compiler Collection - cross compiler for 32bit and 64bit RISC-V bare-metal'
arch=('i686' 'x86_64')
url='http://gcc.gnu.org/'
license=('GPL-3.0-or-later WITH GCC-exception-3.1 AND GFDL-1.3-or-later')
depends=("${_target}-binutils" 'zlib' 'libmpc')
options=(!emptydirs !strip)
source=("https://gcc.gnu.org/pub/gcc/releases/gcc-${pkgver}/gcc-${pkgver}.tar.xz"
	"https://libisl.sourceforge.io/isl-${_islver}.tar.xz")
sha256sums=('e6738e29597f733270731aa90600f37ffdc045079dfc27ec7e8192cc81085c3e'
            'a0b5cb06d24f9fa9e77b55fabbe9a3c94a336190345c2555f9915bb38e976504')

if [[ -n "${_snapshot}" ]]; then
	_basedir=gcc-${_snapshot}
else
	_basedir=gcc-${pkgver}
fi

prepare() {
	cd "${_basedir}/"

	# link isl for in-tree builds
	ln -sf "../isl-${_islver}" isl

	echo "${pkgver}" >gcc/BASE-VER

	# hack! - some configure tests for header files using "$CPP $CPPFLAGS"
	sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure

	mkdir -p "${srcdir}/build/"
}

build() {
	cd build

	# Credits @allanmcrae
	# https://github.com/allanmcrae/toolchain/blob/f18604d70c5933c31b51a320978711e4e6791cf1/gcc/PKGBUILD
	# TODO: properly deal with the build issues resulting from this
	CFLAGS=${CFLAGS/-Werror=format-security/}
	CXXFLAGS=${CXXFLAGS/-Werror=format-security/}

	"${srcdir}/${_basedir}/configure" \
		CFLAGS_FOR_TARGET='-Os -mcmodel=medany -ffunction-sections -fdata-sections' \
		CXXFLAGS_FOR_TARGET='-Os -mcmodel=medany -ffunction-sections -fdata-sections' \
		--target=${_target} \
		--prefix=/usr \
		--with-sysroot=/usr/${_target} \
		--with-native-system-header-dir=/include \
		--libexecdir=/usr/lib \
		--enable-languages=c,c++ \
		--enable-plugins \
		--enable-multilib \
		--disable-decimal-float \
		--disable-libffi \
		--enable-checking=yes \
		--disable-libgomp \
		--disable-libmudflap \
		--disable-libquadmath \
		--disable-libssp \
		--disable-libstdcxx-pch \
		--disable-libstdcxx \
		--disable-fixinc \
		--disable-nls \
		--disable-shared \
		--disable-threads \
		--disable-werror \
		--enable-tls \
		--with-gnu-as \
		--with-gnu-ld \
		--with-system-zlib \
		--with-newlib \
		--with-headers=/usr/${_target}/include \
		--with-python-dir=share/gcc-${_target} \
		--with-gmp \
		--with-mpfr \
		--with-mpc \
		--with-isl \
		--with-libelf \
		--with-arch=rv64gc_zifencei \
		--with-abi=lp64d

	make INHIBIT_LIBC_CFLAGS='-DUSE_TM_CLONE_REGISTRY=0'
}

package() {
	cd build
	make DESTDIR="${pkgdir}" install

	# strip host binaries
	find "${pkgdir}/usr/bin/" "${pkgdir}/usr/lib/gcc/${_target}/$pkgver" -type f -and \( -executable \) -exec strip '{}' \;

	# Remove files that conflict with host gcc package
	rm -r "${pkgdir}/usr/share/man/man7"
	rm -r "${pkgdir}/usr/share/info"
	rm ${pkgdir}/usr/lib/libcc1.*

	install -Dm644 "${srcdir}/${_basedir}/COPYING" -t "${pkgdir}/usr/share/licenses/${pkgname}/"
}
