# Template build file for 'chroot-gcc'
#
# WARNING: version and revision must match gcc, otherwise exact dependencies
# won't work (libgfortran depends on libgcc-${version}_${revision}).
#
_majorver=4.7
_gmp_ver=5.1.3
_mpfr_ver=3.1.2
_mpc_ver=1.0.2

pkgname=chroot-gcc
version=${_majorver}.3
revision=10
wrksrc="gcc-${version}"
homepage="http://gcc.gnu.org"
short_desc="The GNU C Compiler suite -- for xbps-src use"
maintainer="Juan RP <xtraeme@gmail.com>"
license="GPL-3"
distfiles="
 http://ftp.gnu.org/pub/gnu/gcc/gcc-${version}/gcc-${version}.tar.bz2
 http://ftp.gnu.org/pub/gnu/gmp/gmp-${_gmp_ver}.tar.bz2
 http://www.mpfr.org/mpfr-current/mpfr-${_mpfr_ver}.tar.bz2
 http://www.multiprecision.org/mpc/download/mpc-${_mpc_ver}.tar.gz"
checksum="
 2f7c37eb4fc14422ff2358a9ef59c974a75ab41204ef0e49fc34ab1d8981a9c3
 752079520b4690531171d0f4532e40f08600215feefede70b24fabdc6f1ab160
 79c73f60af010a30a5c27a955a1d2d01ba095b72537dab0ecaad57f5a7bb1b6b
 b561f54d8a479cee3bc891ee52735f18ff86712ba30f036f8b8537bae380c488"

create_wrksrc=yes
bootstrap=yes
provides="
	libgcc-${version}_${revision}
	libstdc++-${version}_${revision}
	libstdc++-devel-${version}_${revision}
	gcc-${version}_${revision}
	gcc-c++-${version}_${revision}"
conflicts="
	libgcc>=0
	libstdc++>=0
	libstdc++-devel>=0
	gcc>=0
	gcc-c++>=0"
noverifyrdeps=yes

if [ "$CHROOT_READY" ]; then
	hostmakedepends+=" perl flex"
fi

pre_configure() {
	cd gcc-${version}
	case "$XBPS_TARGET_MACHINE" in
		*-musl) patch -Np1 -i ${FILESDIR}/gcc-${version}-musl.diff;;
	esac
	for f in ${FILESDIR}/*.patch; do
		patch -Np1 -i $f
	done
	# As specified in the LFS book, disable installing libiberty.
	sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
	# Do not run fixincludes
	sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in
}

_gmp_build() {
	cd ${wrksrc}
	[ ! -d gmp-build ] && mkdir gmp-build
	cd gmp-build
	[ -f .gmp_build_done ] && return
	cp -f ${XBPS_CROSSPFDIR}/config.sub ../gmp-${_gmp_ver}/configfsf.sub

	case "$XBPS_TARGET_MACHINE" in
		i686*) _abi="env ABI=32";;
	esac
	${_abi} ../gmp-${_gmp_ver}/configure --prefix=${wrksrc}/buildtmp \
		--disable-shared --enable-static "$@"

	make ${makejobs}
	make install

	rm -f ${wrksrc}/buildtmp/lib/*.la

	touch .gmp_build_done
}

_mpfr_build() {
	cd ${wrksrc}
	[ ! -d mpfr-build ] && mkdir mpfr-build
	cd mpfr-build
	[ -f .mpfr_build_done ] && return
	cp -f ${XBPS_CROSSPFDIR}/config.sub ../mpfr-${_mpfr_ver}/

	../mpfr-${_mpfr_ver}/configure --prefix=${wrksrc}/buildtmp \
		--with-gmp=${wrksrc}/buildtmp \
		--disable-shared --enable-static "$@"

	make ${makejobs}
	make install

	rm -f ${wrksrc}/buildtmp/lib/*.la

	touch .mpfr_build_done
}

_mpc_build() {
	cd ${wrksrc}
	[ ! -d mpc-build ] && mkdir mpc-build
	cd mpc-build
	[ -f .mpc_build_done ] && return
	cp -f ${XBPS_CROSSPFDIR}/config.sub ../mpc-${_mpc_ver}/

	../mpc-${_mpc_ver}/configure --prefix=${wrksrc}/buildtmp \
		--with-gmp=${wrksrc}/buildtmp \
		--with-mpfr=${wrksrc}/buildtmp \
		--disable-shared --enable-static "$@"

	make ${makejobs}
	make install

	rm -f ${wrksrc}/buildtmp/lib/*.la

	touch .mpfr_build_done
}

do_configure() {
	local _floatabi _args

	case "$XBPS_TARGET_MACHINE" in
		armv6*) _args+=" --with-arch=armv6 --with-fpu=vfp --with-float=hard";;
		armv7*) _args+=" --with-arch=armv7-a --with-fpu=vfpv3 --with-float=hard";;
	esac

	if [ "$CROSS_BUILD" ]; then
		_args+=" --host=$XBPS_CROSS_TRIPLET"
		export CC_FOR_TARGET="$CC"
		export GCC_FOR_TARGET="$CC"
		export CXX_FOR_TARGET="$CXX"
	fi

	_gmp_build ${_args}
	_mpfr_build ${_args}
	_mpc_build ${_args}

	cd ${wrksrc}
	[ ! -d gcc-build ] && mkdir gcc-build
	cd gcc-build

	CONFIG_SHELL=/bin/bash \
		../gcc-${version}/configure --disable-bootstrap \
		--prefix=/usr --bindir=/usr/bin \
		--mandir=/usr/share/man --infodir=/usr/share/info \
		--libdir=/usr/lib --libexecdir=/usr/lib \
		--enable-clocale=gnu --enable-shared \
		--enable-threads=posix --enable-__cxa_atexit \
		--disable-gold --enable-lto --enable-linker-build-id \
		--with-gmp=${wrksrc}/buildtmp \
		--with-mpfr=${wrksrc}/buildtmp \
		--with-mpc=${wrksrc}/buildtmp \
		--enable-gnu-unique-object --enable-checking=release \
		--enable-languages=c,c++,lto --disable-nls --disable-multilib \
		--without-cloog --without-ppl --disable-libstdcxx-pch \
		--disable-libquadmath --disable-libquadmath-support \
		--disable-libgomp --disable-libssp --disable-libitm \
		--disable-libmudflap ${_args}
}

do_build() {
	cd gcc-build
	make ${makejobs}
}

do_install() {
	local _triplet

	case "$XBPS_TARGET_MACHINE" in
		x86_64) _triplet="x86_64-unknown-linux-gnu";;
		x86_64-musl) _triplet="x86_64-unknown-linux-musl";;
		i686) _triplet="i686-pc-linux-gnu";;
		i686-musl) _triplet="i686-linux-musl";;
		armv6l) _triplet="arm-linux-gnueabihf";;
		armv7l) _triplet="arm-linux-gnueabihf7";;
		armv6l-musl) _triplet="arm-linux-musleabi";;
		armv7l-musl) _triplet="arm-linux-musleabi7";;
		*) _triplet="$XBPS_CROSS_TRIPLET";;
	esac

	cd gcc-build
	make DESTDIR=${DESTDIR} install

	if [ -d ${DESTDIR}/usr/lib64 ]; then
		mv ${DESTDIR}/usr/lib64/* ${DESTDIR}/usr/lib/
		rmdir ${DESTDIR}/usr/lib64
	fi

	# 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}

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

	# Remove unused stuff.
	rm -f ${DESTDIR}/usr/lib/libffi*
	rm -f ${DESTDIR}/usr/lib/libmudflap*
	rm -rf ${DESTDIR}/usr/share

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