# SliTaz package receipt.

PACKAGE="gcc"
VERSION="10.2.0"
CATEGORY="development"
SHORT_DESC="The the GNU Compiler Collection."
MAINTAINER="pankso@slitaz.org"
TARBALL="$PACKAGE-$VERSION.tar.gz"
WEB_SITE="http://gcc.gnu.org/"
WGET_URL="ftp://gcc.gnu.org/pub/gcc/releases/gcc-$VERSION/$TARBALL"

# We need gawk since busybox awk is not sufficient. We have BUILD_DEPENDS
# for cookutils that are not used by tazwok/cook-toolchain since it install
# and use it's own copy.
DEPENDS="binutils libgomp libobjc gcc-lib-base mpc-library elfutils"
BUILD_DEPENDS="mpc-library mpfr-dev gmp-dev elfutils-dev zlib-dev gc-dev"

# Rules to compile & install the temporary toolchain.
precook_tmp_toolchain()
{
	report open-bloc
	cd $src

	# GCC requires the GMP, MPFR and MPC packages.
	tazwok get-src mpfr --target=$PWD/mpfr || { report close-bloc; return 1; }
	tazwok get-src gmp --target=$PWD/gmp || { report close-bloc; return 1; }
	tazwok get-src mpc-library --target=$PWD/mpc || { report close-bloc; return 1; }

	report step "Running compilation"

	# Use libiberty.a from binutils.
	sed -i 's/install_to_$(INSTALL_DEST) //' \
		libiberty/Makefile.in || return 1

	# Build it in a separate directory.
	mkdir ../gcc-build
	cd ../gcc-build

	{ $src/configure \
		--target=$HOST_SYSTEM \
		--disable-nls --disable-shared --disable-multilib \
		--disable-decimal-float --disable-threads \
		--disable-libmudflap --disable-libssp \
		--disable-libgomp --enable-languages=c \
		--with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs \
		--without-ppl --without-cloog &&
	make &&
	make install
	} || { report close-bloc; return 1; }

	# See LFS for more detais about this.
	ln -s libgcc.a `$HOST_SYSTEM-gcc -print-libgcc-file-name | \
    		sed 's/libgcc/&_eh/'`
	report close-bloc
}

cook_tmp_toolchain()
{
	report open-bloc
	cd $src
	patch -Np1 -i $stuff/gcc-$VERSION-startfiles_fix-1.patch || { report close-bloc; return 1; }

	# Details about theses lines are in LFS book.
	sed 's@\./fixinc\.sh@-c true@' -i gcc/Makefile.in
	sed 's/^T_CFLAGS =$/& -fomit-frame-pointer/' -i gcc/Makefile.in
	for file in \
		$(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
	do
		sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
			-e 's@/usr@/tools@g' -i $file
		echo '
#undef STANDARD_INCLUDE_DIR
#define STANDARD_INCLUDE_DIR 0
#define STANDARD_STARTFILE_PREFIX_1 ""
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
	done

	if [ "$ARCH" = x86_64 ]; then
		for file in $(find gcc/config -name t-linux64) ; do
			sed '/MULTILIB_OSDIRNAMES/d' -i $file
		done
	fi
	
	# GCC requires the GMP, MPFR and MPC packages.
	tazwok get-src mpfr --target=$PWD/mpfr || { report close-bloc; return 1; }
	tazwok get-src gmp --target=$PWD/gmp || { report close-bloc; return 1; }
	tazwok get-src mpc-library --target=$PWD/mpc || { report close-bloc; return 1; }

	report step "Running compilation"

	# Use libiberty.a from binutils.
	sed -i 's/install_to_$(INSTALL_DEST) //' \
		libiberty/Makefile.in || return 1

	# Build it in a separate directory.
	mkdir ../gcc-build
	cd ../gcc-build

	{ CC="$HOST_SYSTEM-gcc -B/tools/lib/" \
		AR=$HOST_SYSTEM-ar RANLIB=$HOST_SYSTEM-ranlib \
		$src/configure \
		--with-local-prefix=/tools --enable-clocale=gnu \
		--enable-shared --enable-threads=posix \
		--enable-__cxa_atexit --enable-languages=c,c++ \
		--disable-libstdcxx-pch --disable-multilib \
		--disable-bootstrap --disable-libgomp \
		--with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs \
		--without-ppl --without-cloog &&
	make &&
	make install
	} || { report close-bloc; return 1; }
	ln -s gcc /tools/bin/cc
	report close-bloc
}

# Rules to configure and make the package.
compile_rules()
{
	cd $src


	# SliTaz is a Busybox based OS, why we so small and fast. Using gawk by
	# default to build package will not ensure package work with Busybox awk
	# and so should NOT be use to cook.
	if [ -x /usr/bin/cook ]; then
		[ -d "/var/lib/tazpkg/installed/gawk" ] || tazpkg get-install gawk
	fi

	# Package slitaz-toolchain use 'cook --options' when rebuilding
	# the full SliTaz toolchain.
	[ "$2" == "--first-pass" ] && opt=$2
	[ "$3" == "--first-pass" ] && opt=$3
	
	# Use libiberty.a from binutils.
	sed -i 's/install_to_$(INSTALL_DEST) //' \
		libiberty/Makefile.in || return 1

	# Do not run fixincludes
	sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in

	mkdir -p ../gcc-build && cd ../gcc-build

	# This is the default GCC and we want a native build to cross compile after.
	# SliTaz target i486 host so we need a native i486 build for GCC.
	case "$opt" in
		--first-pass)
			# Used by slitaz-toolchain when rebuilding the full toolchain.
			echo "cook: configure GCC for: toolchain first pass"
			$src/configure \
				--libexecdir=/usr/lib \
				--disable-nls \
				--disable-multilib \
				--enable-shared \
				--enable-languages=c,c++ \
				--disable-libstdcxx-pch \
				--enable-__cxa_atexit \
				--enable-clocale=gnu \
				--enable-threads=posix \
				--disable-bootstrap \
				--build=$HOST_SYSTEM \
				--host=$HOST_SYSTEM &&
			make && make install ;;
		*)
			# Used by default to produce a full featured X86 GCC compiler.
			echo "cook: configure GCC for: final/full build"
			$src/configure \
				--libexecdir=/usr/lib \
				--disable-multilib \
				--enable-nls \
				--enable-languages=c,c++,objc,fortran \
				--enable-shared \
				--with-system-zlib \
				--enable-clocale=gnu \
				--enable-objc-gc \
				--enable-__cxa_atexit \
				--enable-lto \
				--enable-threads=posix \
				--disable-bootstrap \
				--with-pkgversion="SliTaz" \
				--with-tune=$ARCH \
				--build=$HOST_SYSTEM \
				--host=$HOST_SYSTEM &&
			make && make install ;;
	esac
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p  $fs/usr/share
	cp -a $_pkg/usr/bin $fs/usr
	
	# Copy all libs. Remove libgcc_s.so and libstdc++.so they goes in
	# the gcc-lib-base package.
        # Remove also libatomic.so go in the libatomic package.
	cp -a $_pkg/usr/lib $fs/usr
	rm -f $fs/usr/lib/libgcc_s.so*
	rm -f $fs/usr/lib/libstdc++.so*
	rm -f $fs/usr/lib/libgomp.so*
	rm -f $fs/usr/lib/libobjc.so*
        rm -f $fs/usr/lib/libatomic.so*

	# Include files.
	cp -a $_pkg/usr/include $fs/usr
	
	# Gfortran goes in gfortran package.
	rm -f $fs/usr/bin/*gfortran
	rm -f $fs/usr/lib/libgfortran*
	rm -f $fs/usr/lib/gcc/*/$VERSION/libgfortran*
	rm -rf $fs/usr/lib/gcc/*/$VERSION/f*
	
	# Remove build directory.
	rm -rf $WOK/$PACKAGE/source/$PACKAGE-build
	rm -rf $WOK/$PACKAGE/$PACKAGE-build
}

# Post install commands for Tazpkg.
post_install()
{
	local root
	root=$1
	echo "Processing post-install commands..."
	if [ ! -f "$root/lib/cpp" ]; then
		ln -s ../usr/bin/cpp $root/lib
	fi
	if [ ! -f "$root/usr/bin/cc" ]; then
		ln -s gcc $root/usr/bin/cc
	fi
}