# Template build file for 'gcc'
pkgname=gcc
_majorver=4.7
version=${_majorver}.2
revision=1
makedepends="perl flex zlib-devel gmp-devel mpfr-devel mpc-devel ppl-devel>=1.0 cloog-devel"
short_desc="The GNU C Compiler"
maintainer="Juan RP <xtraeme@gmail.com>"
homepage="http://gcc.gnu.org"
license="GFDL-1.2, GPL-3, LGPL-2.1"
distfiles="http://ftp.gnu.org/pub/gnu/gcc/gcc-$version/gcc-$version.tar.bz2"
depends="binutils libgomp>=${_majorver} libitm>=${_majorver} libssp-devel>=${_majorver}"
checksum=8a9283d7010fb9fe5ece3ca507e0af5c19412626384f8a5e9434251ae100b084
long_desc="
 The GNU C Compiler, with support for building C code.
 
 For C++ install gcc-c++, for ObjectiveC install gcc-objc,
 for ObjectiveC++ install gcc-objc++, for Go install gcc-go,
 for Fortran install gcc-fortran."

if [ "${XBPS_MACHINE}" = "x86_64" ]; then
	# Required stuff for multilib packages.
	makedepends="${makedepends} glibc32-devel"
	subpackages="gcc-multilib gcc-c++-multilib"
	subpackages="${subpackages} gcc-objc-multilib libgcc32 libgomp32"
	subpackages="${subpackages} libmudflap32-devel libmudflap32"
	subpackages="${subpackages} libobjc32 libssp32 libstdc++32-devel"
	subpackages="${subpackages} libstdc++32 libitm32"

	_triplet="${XBPS_MACHINE}-unknown-linux-gnu"
else
	_triplet="${XBPS_MACHINE}-pc-linux-gnu"
fi

subpackages="${subpackages} libgcc libgo libssp libssp-devel libstdc++ libstdc++-devel libgomp"
subpackages="${subpackages} libquadmath libquadmath-devel libgo-devel libgomp-devel libitm"
subpackages="${subpackages} libmudflap libmudflap-devel libobjc libobjc-devel"
subpackages="${subpackages} libgfortran libgfortran-devel gcc-fortran"
subpackages="${subpackages} gcc-c++ gcc-objc gcc-objc++ gcc-go"

do_configure() {
	# 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

	./configure --libexecdir=/usr/lib \
		--enable-clocale=gnu --disable-bootstrap --libdir=/usr/lib \
		--enable-threads=posix --enable-__cxa_atexit --enable-multilib \
		--enable-languages=c,c++,objc,obj-c++,fortran,go,lto --enable-tls \
		--disable-rpath --with-system-zlib --enable-shared --enable-lto \
		--enable-linker-build-id --enable-gnu-unique-object \
		--enable-checking=release --disable-libstdcxx-pch \
		--with-ppl --enable-cloog-backend=isl --enable-libstdcxx-time \
		${CONFIGURE_SHARED_ARGS}
}

do_build() {
	make ${makejobs}
}

do_install() {
	if [ "$XBPS_MACHINE" = "x86_64" ]; then
		vmkdir usr/lib
		cd ${DESTDIR}/usr && ln -sf lib lib64
	fi
	cd ${wrksrc}
	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}
	cd ${DESTDIR}/usr/lib/gcc/${_triplet} && ln -sf ${_majorver} ${version}

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

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

	rm -f ${DESTDIR}/usr/lib64

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

	# Remove unused stuff for multilib.
	if [ "${XBPS_MACHINE}" = "x86_64" ]; then
		rm -f ${DESTDIR}/usr/lib32/libffi*
		rm -f ${DESTDIR}/usr/lib32/libgfortran*
		rm -f ${DESTDIR}/usr/lib32/libgo.{a,so*}
		rm -f ${DESTDIR}/usr/lib32/libgobegin.a
		rm -f ${DESTDIR}/usr/lib32/libquadmath*
		rm -rf ${DESTDIR}/usr/lib32/go
	fi

	# 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
		vinstall ${FILESDIR}/${f}.sh 755 usr/bin ${f}
		vinstall ${FILESDIR}/${f}.1 644 usr/share/man/man1 ${f}.1
	done
}
