# Template build file for 'gcc'.
_majorver=8.2
_gmp_version=6.1.2
_mpfr_version=4.0.1
_mpc_version=1.1.0
_isl_version=0.19

pkgname=gcc
version=${_majorver}.0
revision=3
short_desc="The GNU C Compiler"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
homepage="http://gcc.gnu.org"
license="GFDL-1.2, GPL-3, LGPL-2.1"
# *-musl builders have issues fetching https://ftp.gnu.org/gnu/mpc/mpc-1.1.0.tar.gz
distfiles="
 https://fossies.org/linux/misc/gcc-${version}.tar.xz
 https://gmplib.org/download/gmp/gmp-${_gmp_version}.tar.xz
 http://www.mpfr.org/mpfr-${_mpfr_version}/mpfr-${_mpfr_version}.tar.xz
 https://fossies.org/linux/misc/mpc-${_mpc_version}.tar.gz
 http://isl.gforge.inria.fr/isl-${_isl_version}.tar.bz2"
checksum="
 196c3c04ba2613f893283977e6011b2345d1cd1af9abeac58e916b1aab3e0080
 87b565e89a9a684fe4ebeeddb8399dce2599f9c9049854ca8c0dfbdea0e21912
 67874a60826303ee2fb6affc6dc0ddd3e749e9bfcb4c8655e3953d0458a6e16e
 6985c538143c1208dcb1ac42cedad6ff52e267b47e5f970183a3e75125b43c2e
 d59726f34f7852a081fbd3defd1ab2136f174110fc2e0c8d10bb122173fa9ed8"

nopie=yes
lib32disabled=yes
bootstrap=yes
replaces="gcc-gcj<7.2.0 gcc-gcj-jdk-compat<7.2.0"
alternatives="cc:cc:/usr/bin/gcc"

if [ "$CHROOT_READY" ]; then
	hostmakedepends="perl flex"
fi
makedepends="zlib-devel"
depends="binutils libgcc-devel-${version}_${revision}
 libstdc++-devel-${version}_${revision} libssp-devel-${version}_${revision}"

subpackages="libgcc libgomp libgomp-devel libatomic libatomic-devel"
subpackages+=" libssp libssp-devel"

if [ -n "$CHROOT_READY" ]; then
	subpackages+=" gcc-fortran libgfortran-devel libgfortran"
	if [ -z "$CROSS_BUILD" ]; then
		subpackages+=" gcc-objc gcc-objc++ libobjc-devel libobjc"
		case "$XBPS_TARGET_MACHINE" in
		*-musl)	# Go won't link for musl libc
			;;
		*)	subpackages+=" gcc-go gcc-go-tools libgo-devel libgo"
			;;
		esac
	fi
fi

subpackages+=" libstdc++-devel libstdc++ libgcc-devel"

case "$XBPS_TARGET_MACHINE" in
	i686) _triplet="i686-pc-linux-gnu";;
	i686-musl) _triplet="i686-linux-musl";;
	x86_64) _triplet="x86_64-unknown-linux-gnu";;
	x86_64-musl) _triplet="x86_64-linux-musl";;
	armv5tel) _triplet="arm-linux-gnueabi";;
	armv5tel-musl) _triplet="arm-linux-musleabi";;
	armv6l) _triplet="arm-linux-gnueabihf";;
	armv7l) _triplet="armv7l-linux-gnueabihf";;
	armv6l-musl) _triplet="arm-linux-musleabihf";;
	armv7l-musl) _triplet="armv7l-linux-musleabihf";;
	aarch64) _triplet="aarch64-linux-gnu";;
	aarch64-musl) _triplet="aarch64-linux-musl";;
	mips-musl) _triplet="mips-linux-musl";;
	mipshf-musl) _triplet="mips-linux-muslhf";;
	mipsel-musl) _triplet="mipsel-linux-musl";;
	mipselhf-musl) _triplet="mipsel-linux-muslhf";;
esac
case "$XBPS_TARGET_MACHINE" in
	*-musl)	 depends+=" musl-devel";;
	aarch64) depends+=" glibc-devel";;
	*) 	 depends+=" glibc-devel"
		 subpackages+=" libsanitizer libsanitizer-devel"
		 ;;
esac
case "$XBPS_TARGET_MACHINE" in
	i686*|x86_64*)
		subpackages+=" libquadmath libquadmath-devel"
		subpackages+=" libvtv libvtv-devel"
		;;
esac
case "$XBPS_TARGET_MACHINE" in
	mips*) ;;
	x86_64*|i686) subpackages+=" libitm libitm-devel libmpx libmpx-devel";;
	*) subpackages+=" libitm libitm-devel";;
esac
if [ -n "$CROSS_BUILD" ]; then
	hostmakedepends+=" cross-${_triplet}"
	export GFORTRAN_FOR_TARGET=" ${_triplet}-gfortran"
fi

post_extract() {
	mkdir -p ${wrksrc}/{gmp,mpfr,mpc,isl}
	cp -ar ${XBPS_BUILDDIR}/gmp-${_gmp_version}/* ${wrksrc}/gmp/
	cp -ar ${XBPS_BUILDDIR}/mpfr-${_mpfr_version}/* ${wrksrc}/mpfr/
	cp -ar ${XBPS_BUILDDIR}/mpc-${_mpc_version}/* ${wrksrc}/mpc/
	cp -ar ${XBPS_BUILDDIR}/isl-${_isl_version}/* ${wrksrc}/isl/
}

pre_configure() {
	# _FORTIFY_SOURCE needs an optimization level.
	sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {gcc,libiberty}/configure
}
do_configure() {
	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=mip32r2 --with-float=hard";;
		mips-musl) _args+=" --with-arch=mip32r2 --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*) # use lib not lib64 by default
			_args+=" --with-arch=armv8-a"
			sed -i '/m64=/s/lib64/lib/' gcc/config/aarch64/t-aarch64-linux
			;;
		x86_64*) # use lib not lib64 by default
			sed -i '/m64=/s/lib64/lib/' gcc/config/i386/t-linux64
			sed -i 's/lib64/lib/' gcc/config/i386/linux64.h
			;;
	esac

	if [ -z "$CHROOT_READY" -o -n "$CROSS_BUILD" ]; then
		_langs="c,c++,lto"
	fi

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

		_args+=" --disable-multilib"
		_args+=" --host=$XBPS_CROSS_TRIPLET"
		_args+=" --with-build-sysroot=${XBPS_CROSS_BASE}"
		_args+=" --enable-sjlj-exceptions=no"
		_langs+=",fortran"
	else
		if [ -z "$CHROOT_READY" ]; then
			export LD_LIBRARY_PATH="${XBPS_MASTERDIR}/usr/lib"
			_args+=" --build=${_triplet}"
		else
			_langs="c,c++,objc,obj-c++,fortran,lto"
			case "$XBPS_TARGET_MACHINE" in
				*-musl)	# Linking libgo.so is broken for musl libc
					;;
				*)	_langs+=",go"
					;;
			esac
			_args+=" --build=${_triplet}"
			_args+=" --enable-fast-character"
		fi
	fi

	case "$XBPS_TARGET_MACHINE" in
		aarch64*|*-musl) _args+=" --disable-libsanitizer";;
	esac
	case "$XBPS_TARGET_MACHINE" in
		*-musl) _args+=" --disable-symvers"
			_args+=" libat_cv_have_ifunc=no"
			if [ "$XBPS_TARGET_MACHINE" = "i686-musl" ]; then
				# Remove libmpx because it won't compile
				rm -rf libmpx
			fi
			;;
	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/}"

	_args+=" --prefix=/usr"
	_args+=" --mandir=/usr/share/man"
	_args+=" --infodir=/usr/share/info"
	_args+=" --libexecdir=/usr/lib"
	_args+=" --libdir=/usr/lib"
	_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-vtable-verify"
	_args+=" --enable-linker-build-id"
	_args+=" --enable-serial-configure"
	_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-libunwind-exceptions"
	_args+=" --disable-target-libiberty"
	_args+=" --enable-languages=${_langs}"

	mkdir -p build
	cd build
	CONFIG_SHELL=/bin/bash ${wrksrc}/configure ${_args}

}
do_build() {
	if [ -z "$CHROOT_READY" ]; then
		export LD_LIBRARY_PATH="${XBPS_MASTERDIR}/usr/lib"
	fi
	cd build
	make ${makejobs}
}
pre_install() {
	if [ "$CROSS_BUILD" ]; then
		# XXX otherwise links to host libpthread
		for f in $(find ${wrksrc}/build/${XBPS_CROSS_TRIPLET} -type f -name libtool); do
			sed -e "s,add_dir=\"-L\$libdir\",add_dir=\"-L${XBPS_CROSS_BASE}\$libdir\",g" -i $f
		done
	fi
}
do_install() {
	cd build
	case "$XBPS_TARGET_MACHINE" in
	x86_64*|aarch64*)
		vmkdir usr/lib
		ln -sfr ${DESTDIR}/usr/lib ${DESTDIR}/usr/lib64
		;;
	esac
	make DESTDIR=${DESTDIR} install

	# Make version a symlink of major version to make all versions
	# from the same series work automagically.
	mv ${DESTDIR}/usr/lib/gcc/${_triplet}/${version} \
		${DESTDIR}/usr/lib/gcc/${_triplet}/${_majorver}
	ln -sfr ${DESTDIR}/usr/lib/gcc/${_triplet}/${_majorver} \
		${DESTDIR}/usr/lib/gcc/${_triplet}/${version}

	# Ditto for c++ headers.
	mv ${DESTDIR}/usr/include/c++/${version} \
		${DESTDIR}/usr/include/c++/${_majorver}
	ln -sfr ${DESTDIR}/usr/include/c++/${_majorver} \
		${DESTDIR}/usr/include/c++/${version}

	# rpcgen wants /lib/cpp, make a symlink
	ln -sfr ${DESTDIR}/usr/bin/cpp ${DESTDIR}/usr/lib/cpp

	if [ -e ${DESTDIR}/usr/lib64 ]; then
		rm -f ${DESTDIR}/usr/lib64
	fi

	# Remove libffi stuff.
	rm -f ${DESTDIR}/usr/lib/libffi*
	rm -f ${DESTDIR}/usr/share/man/man3/ffi*

	# Remove all python scripts in libdir.
	rm -f ${DESTDIR}/usr/lib/*.py

	# Remove more python stuff.
	if [ -d ${DESTDIR}/usr/share/gcc-${version}/python ]; then
		rm -rf ${DESTDIR}/usr/share/gcc-${version}/python
	fi

	# Install c89 and c99 wrappers and its manpages, from NetBSD.
	for f in c89 c99; do
		vbin ${FILESDIR}/${f}.sh ${f}
		vman ${FILESDIR}/${f}.1
	done

	case "$XBPS_TARGET_MACHINE" in
		i686*|x86_64*) ;;
		*) rm -f ${DESTDIR}/usr/share/info/libquadmath.info;;
	esac
}

gcc-go_package() {
	lib32disabled=yes
	depends="gcc>=${_majorver} libgo-devel>=${_majorver}"
	short_desc+=" - Go compiler frontend"
	pkg_install() {
		for f in gccgo ${_triplet}-gccgo; do
			vmove usr/bin/${f}
		done
		for f in buildid cgo go1 test2json vet; do
			vmove usr/lib/gcc/${_triplet}/${_majorver}/${f}
		done
		vmove usr/share/info/gccgo.info
		touch ${PKGDESTDIR}/usr/share/info/dir
		vmove usr/share/man/man1/gccgo.1
		vmove usr/lib/go
	}
}
gcc-go-tools_package() {
	lib32disabled=yes
	depends="gcc-go>=${_majorver}"
	short_desc+=" - Go tools"
	conflicts="go>=0"
	pkg_install() {
		vmove usr/bin/go
		vmove usr/bin/gofmt
		vmove usr/share/man/man1/go.1
		vmove usr/share/man/man1/gofmt.1
	}
}
gcc-fortran_package() {
	lib32disabled=yes
	depends="gcc>=${_majorver} libgfortran-devel>=${_majorver}"
	short_desc+=" - Fortran compiler frontend"
	pkg_install() {
		for f in gfortran ${_triplet}-gfortran; do
			vmove usr/bin/${f}
		done
		vmove usr/lib/gcc/${_triplet}/${_majorver}/f951
		vmove usr/lib/gcc/${_triplet}/${_majorver}/libcaf_single.a
		vmove usr/lib/gcc/${_triplet}/${_majorver}/finclude
		vmove usr/share/info/gfortran.info
		touch ${PKGDESTDIR}/usr/share/info/dir
		vmove usr/share/man/man1/gfortran.1
	}
}
gcc-objc++_package() {
	lib32disabled=yes
	depends="gcc-objc>=${_majorver}"
	short_desc+=" - Objective-C++ support"
	pkg_install() {
		vmove usr/lib/gcc/${_triplet}/${_majorver}/cc1objplus
	}
}
gcc-objc_package() {
	lib32disabled=yes
	depends="gcc>=${_majorver} libobjc>=${_majorver}"
	short_desc+=" - Objective-C support"
	pkg_install() {
		vmove usr/lib/gcc/${_triplet}/${_majorver}/include/objc
		vmove usr/lib/gcc/${_triplet}/${_majorver}/cc1obj
	}
}
libgfortran-devel_package() {
	depends="libgfortran>=${_majorver}"
	short_desc+=" - Fortran library - development files"
	pkg_install() {
		vmove usr/lib/libgfortran.a
		# This does not seem to exist anymore?
		if [ -f usr/lib/gcc/${_triplet}/${_majorver}/libgfortranbegin.a ]; then
			vmove usr/lib/gcc/${_triplet}/${_majorver}/libgfortranbegin.a
		fi
	}
}
libgfortran_package() {
	short_desc+=" - Fortran library"
	pkg_install() {
		vmove "usr/lib/libgfortran.so*"
		vmove usr/lib/libgfortran.spec
		vlicense ${wrksrc}/COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
	}
}
libgo-devel_package() {
	depends="libgo>=${_majorver}"
	short_desc+=" - Go library - development files"
	pkg_install() {
		vmove usr/lib/libgo.a
		vmove usr/lib/libgobegin.a
		vmove usr/lib/libgolibbegin.a
		vmove usr/lib/libgo.so
	}
}
libgo_package() {
	short_desc+=" - Go library"
	pkg_install() {
		vmove "usr/lib/libgo.so*"
		vlicense ${wrksrc}/COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
	}
}
libobjc-devel_package() {
	depends="libobjc>=${_majorver}"
	short_desc+=" - Objective-C library - development files"
	pkg_install() {
		vmove "usr/lib/libobjc.*a"
	}
}
libobjc_package() {
	short_desc+=" - Objective-C library"
	pkg_install() {
		vmove "usr/lib/libobjc.so*"
		vlicense ${wrksrc}/COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
	}
}
libquadmath-devel_package() {
	depends="libquadmath>=${_majorver}"
	short_desc+=" - quadmath library - development files"
	pkg_install() {
		vmove "usr/lib/gcc/${_triplet}/${_majorver}/include/quadmath*"
		vmove "usr/lib/libquadmath*.a"
		vmove usr/share/info/libquadmath.info
		touch ${PKGDESTDIR}/usr/share/info/dir
	}
}
libquadmath_package() {
	short_desc+=" - quadmath library"
	pkg_install() {
		vmove "usr/lib/libquadmath.so*"
		vlicense ${wrksrc}/COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
	}
}
libvtv-devel_package() {
	depends="libvtv>=${_majorver}"
	short_desc+=" - Vtable Verification Runtime Library - development files"
	pkg_install() {
		vmove usr/lib/libvtv.a
		vmove usr/lib/libvtv.so
	}
}
libvtv_package() {
	short_desc+=" - Vtable Verification Runtime Library"
	pkg_install() {
		vmove usr/lib/libvtv.so.*
	}
}
libatomic-devel_package() {
	depends="libatomic>=${_majorver}"
	short_desc+=" - Atomics library - development files"
	pkg_install() {
		vmove usr/lib/libatomic.a
		vmove usr/lib/libatomic.so
	}
}
libatomic_package() {
	short_desc+=" - Atomics library"
	pkg_install() {
		vmove usr/lib/libatomic.so.*
	}
}
libgcc_package() {
	short_desc+=" - GCC library"
	replaces="libgcj<7.2.0 libcilkrts<8.2.0"
	noverifyrdeps=yes
	pkg_install() {
		vmove "usr/lib/libgcc_s.so*"
		vlicense ${wrksrc}/COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
	}
}
libgcc-devel_package() {
	depends="libgcc-${version}_${revision}"
	short_desc+=" - GCC library - development files"
	replaces="libgcj-devel<7.2.0 libcilkrts-devel<8.2.0"
	pkg_install() {
		vmove "usr/lib/gcc/${_triplet}/${_majorver}/*.o"
		vmove "usr/lib/gcc/${_triplet}/${_majorver}/*.a"
	}
}
libgomp-devel_package() {
	depends="libgomp>=${_majorver}"
	short_desc+=" - OpenMP v4.0 library - development files"
	pkg_install() {
		vmove usr/lib/gcc/${_triplet}/${_majorver}/include/omp.h
		vmove usr/lib/libgomp.so
		vmove usr/lib/libgomp.a
		vmove usr/lib/libgomp.spec
		vmove usr/share/info/libgomp.info
		touch ${PKGDESTDIR}/usr/share/info/dir
	}
}
libgomp_package() {
	short_desc+=" - OpenMP v4.0 library"
	pkg_install() {
		vmove "usr/lib/libgomp*.so.*"
		vlicense ${wrksrc}/COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
	}
}
libitm-devel_package() {
	depends="libitm>=${_majorver}"
	short_desc+=" - Transactional Memory library - development files"
	replaces="libitm<4.9.0_1"
	pkg_install() {
		vmove usr/lib/libitm.a
		vmove usr/lib/libitm.so
		vmove usr/lib/libitm.spec
		vmove usr/share/info/libitm.info
		touch ${PKGDESTDIR}/usr/share/info/dir
	}
}
libitm_package() {
	short_desc+=" - Transactional Memory library"
	pkg_install() {
		vmove "usr/lib/libitm.so.*"
	}
}
libmpx-devel_package() {
	depends="libmpx>=${_majorver}"
	short_desc+=" - Intel MPX support - development files"
	pkg_install() {
		vmove usr/lib/libmpx.a
		vmove usr/lib/libmpx.so
		vmove usr/lib/libmpxwrappers.so
		vmove usr/lib/libmpxwrappers.a
	}
}
libmpx_package() {
	short_desc+=" - Intel MPX support"
	pkg_install() {
		vmove usr/lib/libmpx.so.*
		vmove usr/lib/libmpxwrappers.so.*
	}
}
libsanitizer-devel_package() {
	depends="libsanitizer>=${_majorver}"
	short_desc+=" - Sanitizer libraries - development files"
	pkg_install() {
		vmove "usr/lib/lib*san*.so"
		vmove "usr/lib/lib*san*.a"
		vmove usr/lib/libasan_preinit.o
		vmove usr/lib/libsanitizer.spec
	}
}
libsanitizer_package() {
	short_desc+=" - Sanitizer libraries"
	pkg_install() {
		vmove "usr/lib/lib*san*.so.*"
	}
}
libssp-devel_package() {
	depends="libssp>=${_majorver}"
	short_desc+=" - SSP (StackSmashingProtection) library - development files"
	pkg_install() {
		vmove "usr/lib/libssp*.a"
		vmove usr/lib/gcc/${_triplet}/${_majorver}/include/ssp
	}
}
libssp_package() {
	short_desc+=" - SSP (StackSmashingProtection) library"
	pkg_install() {
		vmove "usr/lib/libssp.so*"
		vlicense ${wrksrc}/COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
	}
}
libstdc++-devel_package() {
	depends="libstdc++>=${_majorver}"
	short_desc+=" - Standard C++ Library - development files"
	pkg_install() {
		vmove "usr/lib/libstdc++.*a"
		vmove "usr/lib/libsupc++.*"
		vmove usr/include/c++
	}
}
libstdc++_package() {
	short_desc+=" - Standard C++ Library"
	pkg_install() {
		vmove "usr/lib/libstdc++.so*"
		vlicense ${wrksrc}/COPYING.RUNTIME RUNTIME.LIBRARY.EXCEPTION
	}
}
