# Template file for 'libgccjit'
# Revbump libtool when updating gcc, since it hardcodes some internal compiler paths
# which use the version number.

pkgname=libgccjit
version=13.2.0
revision=1
_gmp_version=6.3.0
_mpfr_version=4.2.0
_mpc_version=1.3.1
_isl_version=0.24
create_wrksrc=yes
short_desc="Just-In-Time Compilation library for GCC"
maintainer="Lane Biocini <lane@mailbox.org>"
homepage="https://gcc.gnu.org"
license="GFDL-1.2-or-later, GPL-3.0-or-later, LGPL-2.1-or-later"
# *-musl builders have issues fetching https://ftp.gnu.org/gnu/mpc/mpc-1.1.0.tar.gz
distfiles="
 ${GNU_SITE}/gcc/gcc-${version}/gcc-${version}.tar.xz
 https://gmplib.org/download/gmp/gmp-${_gmp_version}.tar.xz
 ${GNU_SITE}/mpfr/mpfr-${_mpfr_version}.tar.xz
 ${GNU_SITE}/mpc/mpc-${_mpc_version}.tar.gz
 ${SOURCEFORGE_SITE}/libisl/isl-${_isl_version}.tar.bz2"
checksum="e275e76442a6067341a27f04c5c6b83d8613144004c0413528863dc6b5c743da
 a3c2b80201b89e68616f4ad30bc66aee4927c3ce50e33929ca819d5c43538898
 06a378df13501248c1b2db5aa977a2c8126ae849a9d9b7be2546fb4a9c26d993
 ab642492f5cf882b74aa0cb730cd410a81edcdbec895183ce930e706c1c759b8
 fcf78dd9656c10eb8cf9fbd5f59a0b6b01386205fe1934b3b287a0a1898145c0"
nopie=yes
lib32disabled=yes
makedepends="zlib-devel"
hostmakedepends="tar texinfo perl flex"
depends="gcc"
checkdepends="dejagnu"

post_extract() {
	mv gcc-*/* .
	mv gmp-${_gmp_version} gmp
	mv mpfr-${_mpfr_version} mpfr
	mv mpc-${_mpc_version} mpc
	mv isl-${_isl_version} isl
}

pre_configure() {
	if [ "$XBPS_TARGET_LIBC" = musl ]; then
		for p in ${FILESDIR}/*-musl.patch; do
			patch -p1 -i $p
		done
	fi
}

do_configure() {
	# Define triplet for gcc build target
	case "$XBPS_TARGET_MACHINE" in
		i686) _target_triplet="i686-pc-linux-gnu";;
		i686-musl) _target_triplet="i686-linux-musl";;
		x86_64) _target_triplet="x86_64-unknown-linux-gnu";;
		x86_64-musl) _target_triplet="x86_64-linux-musl";;
		armv5tel) _target_triplet="arm-linux-gnueabi";;
		armv5tel-musl) _target_triplet="arm-linux-musleabi";;
		armv6l) _target_triplet="arm-linux-gnueabihf";;
		armv7l) _target_triplet="armv7l-linux-gnueabihf";;
		armv6l-musl) _target_triplet="arm-linux-musleabihf";;
		armv7l-musl) _target_triplet="armv7l-linux-musleabihf";;
		aarch64) _target_triplet="aarch64-linux-gnu";;
		aarch64-musl) _target_triplet="aarch64-linux-musl";;
		ppc) _target_triplet="powerpc-linux-gnu";;
		ppc-musl) _target_triplet="powerpc-linux-musl";;
		ppcle) _target_triplet="powerpcle-linux-gnu";;
		ppcle-musl) _target_triplet="powerpcle-linux-musl";;
		ppc64le) _target_triplet="powerpc64le-linux-gnu";;
		ppc64le-musl) _target_triplet="powerpc64le-linux-musl";;
		ppc64) _target_triplet="powerpc64-linux-gnu";;
		ppc64-musl) _target_triplet="powerpc64-linux-musl";;
		mips-musl) _target_triplet="mips-linux-musl";;
		mipshf-musl) _target_triplet="mips-linux-muslhf";;
		mipsel-musl) _target_triplet="mipsel-linux-musl";;
		mipselhf-musl) _target_triplet="mipsel-linux-muslhf";;
	esac

	local _langs _args _hash
	_hash=gnu
	case "$XBPS_TARGET_MACHINE" in
		mipselhf-musl) _args+=" --with-arch=mips32r2 --with-float=hard"; _hash=sysv;;
		mipsel-musl) _args+=" --with-arch=mips32r2 --with-float=soft"; _hash=sysv;;
		mipshf-musl) _args+=" --with-arch=mips32r2 --with-float=hard";;
		mips-musl) _args+=" --with-arch=mips32r2 --with-float=soft";;
		armv5*) _args+=" --with-arch=armv5te --with-float=soft";;
		armv6l*) _args+=" --with-arch=armv6 --with-fpu=vfp --with-float=hard";;
		armv7l*) _args+=" --with-arch=armv7-a --with-fpu=vfpv3 --with-float=hard";;
		aarch64*) _args+=" --with-arch=armv8-a";;
		ppc64le*) _args+=" --with-abi=elfv2 --enable-secureplt --enable-targets=powerpcle-linux";;
		ppc64*) _args+=" --with-abi=elfv2 --enable-secureplt --enable-targets=powerpc-linux";;
		ppc*) _args+=" --enable-secureplt";;
	esac

	# fix: unknown long double size, cannot define BFP_FMT
	case "$XBPS_TARGET_MACHINE" in
		ppc*-musl) _args+=" --disable-decimal-float";;
	esac

	if [ "$CROSS_BUILD" ]; then
		export CC_FOR_TARGET="$CC"
		export GCC_FOR_TARGET="$CC"
		export CXX_FOR_TARGET="$CXX"

		_args+=" --host=$XBPS_CROSS_TRIPLET"
		_args+=" --with-build-sysroot=${XBPS_CROSS_BASE}"
	elif [ -z "$CHROOT_READY" ]; then
		export LD_LIBRARY_PATH="${XBPS_MASTERDIR}/usr/lib${XBPS_TARGET_WORDSIZE}"
		_args+=" --build=${_target_triplet}"
	else
		_args+=" --build=${_target_triplet}"
	fi

	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		_args+=" --disable-gnu-unique-object"
		_args+=" --disable-symvers"
		_args+=" libat_cv_have_ifunc=no"
	else
		_args+=" --enable-gnu-unique-object"
	fi

	case "$XBPS_TARGET_MACHINE" in
		ppc*) _args+=" --disable-vtable-verify";;
		*) _args+=" --enable-vtable-verify";;
	esac

	export CFLAGS="${CFLAGS/-D_FORTIFY_SOURCE=2/}"
	export CXXFLAGS="${CXXFLAGS/-D_FORTIFY_SOURCE=2/}"

	# Disable explicit -fno-PIE, gcc will figure this out itself.
	export CFLAGS="${CFLAGS//-fno-PIE/}"
	export CXXFLAGS="${CXXFLAGS//-fno-PIE/}"
	export LDFLAGS="${LDFLAGS//-no-pie/}"

	# Needs explicit -fPIC
	export CFLAGS+=" -fPIC"
	export CXXFLAGS+=" -fPIC"

	_args+=" --prefix=/usr"
	_args+=" --mandir=/usr/share/man"
	_args+=" --infodir=/usr/share/info"
	_args+=" --libexecdir=/usr/lib${XBPS_TARGET_WORDSIZE}"
	_args+=" --libdir=/usr/lib${XBPS_TARGET_WORDSIZE}"
	_args+=" --enable-threads=posix"
	_args+=" --enable-__cxa_atexit"
	_args+=" --disable-multilib"
	_args+=" --with-system-zlib"
	_args+=" --enable-shared"
	_args+=" --enable-lto"
	_args+=" --enable-plugins"
	_args+=" --enable-linker-build-id"
	_args+=" --disable-werror"
	_args+=" --disable-nls"
	_args+=" --enable-default-pie"
	_args+=" --enable-default-ssp"
	_args+=" --enable-checking=release"
	_args+=" --disable-libstdcxx-pch"
	_args+=" --with-isl"
	_args+=" --with-linker-hash-style=$_hash"
	_args+=" --disable-sjlj-exceptions"
	_args+=" --disable-target-libiberty"

	# libgccjit compilation specific
	_args+=" --enable-host-shared"
	_args+=" --disable-bootstrap"
	_args+=" --disable-libquadmath"
	_args+=" --disable-libquadmath-support"
	_args+=" --disable-libgomp"
	_args+=" --disable-liboffloadmic"
	_args+=" --disable-libada"
	_args+=" --disable-libvtv"
	_args+=" --disable-libsanitizer"
	_args+=" --with-bugurl='https://github.com/void-linux/void-packages/issues'"

	mkdir -p build
	cd build

	# If we are checking packages, we need to have c++ compiled
	[ -z "$XBPS_CHECK_PKGS" ] && _langs="jit" || _langs="jit,c++"
	_args+=" --enable-languages=${_langs}"
	CONFIG_SHELL=/bin/bash \
		${wrksrc}/configure ${_args}
}

do_build() {
	make -C build ${makejobs}
}

do_check() {
	make -C build/gcc check-jit RUNTESTFLAGS="-v -v -v"
}

do_install() {
	make -C build/gcc DESTDIR=${DESTDIR} jit.install-common jit.install-info
}

post_install() {
	vlicense COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
}

libgccjit-devel_package() {
	short_desc+=" - development files"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/*.so
		vmove usr/share/info
	}
}
