### this file is sourced not run
PKGVER=7.3.0
PKGBUILD=1

# source: CLFS 3.0.0 with bits and patches from original LFS 7.5 instructions
TARBALL=gcc-$PKGVER.tar.xz
MD5SUM=be2da21680f27624f3a87055c4ba5af2
BUNDLE=fatdog-base-8.2.tar
SRC_URL=http://distro.ibiblio.org/fatdog/source/800/$BUNDLE

SLACKREQ='binutils libgmp libmpfr libmpc glibc zlib'
SLACKDESC="gcc-full: gcc-full $PKGVER (GNU Compiler Collection)
gcc-full: 
gcc-full: This package contains compilers for the all the languages supported 
gcc-full: by GNU Compiler Collection (GCC): C, C++, Fortran, Objective-C, 
gcc-full: Objective-C++, and Go. It supersedes the standard gcc package which 
gcc-full: only supports C and C++.
gcc-full: 
gcc-full: Note: Ada compiler is **not** included in this package.
gcc-full: 
gcc-full: 
gcc-full: 
"


### override download for LFS sources
# $1-src-dir $2-tarball 
pkg_download() {
	echo downloading $BUNDLE for $TARBALL
	wget -P $1 $SRC_URL
	tar -xf $1/$BUNDLE --strip-components=1 -C $1 
	rm $1/$BUNDLE
}

### prepare
pkg_prepare() {
	pkg_prepare_default &&
	cd tmp/gcc* &&
	patch -Np1 -i ../pkg/libasan-i386-fix.patch &&
	patch -Np1 -i ../pkg/gcc.git-fceecb5f37d65944c822bd172cde89e670669d49.patch &&
	true
}

### default pkg_package
### build
pkg_build() {
	cd /tmp/gcc*
	unset CFLAGS CXXFLAGS # clear flags defined in env.sh
	
	# CLFS 3.0.0 - kill fixincludes 
	sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in &&
	
	# LFS 7.5 patches - this was for gcc 4.8.3
	# use -fomit-frame-pointer for x86
	#case `uname -m` in
	#  i?86) sed -i 's/^T_CFLAGS =$/& -fomit-frame-pointer/' gcc/Makefile.in ;;
	#esac
	
	# LFS 7.5 patches
	# fix an error in one of the check Makefiles and disable one test in the g++ libmudflap test suite
	#sed -i -e /autogen/d -e /check.sh/d fixincludes/Makefile.in 
	#mv -v libmudflap/testsuite/libmudflap.c++/pass41-frag.cxx{,.disable}

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

	#SED=sed CC="gcc -isystem /usr/include ${BUILD64}" \
	#CXX="g++ -isystem /usr/include ${BUILD64}" \
	LDFLAGS="-Wl,-rpath-link,/usr/lib64:/lib64:/usr/lib:/lib" \
	  ../gcc-$PKGVER/configure --prefix=/usr --libdir=/usr/lib64 --libexecdir=/usr/lib64 \
		--enable-languages=c,c++,fortran,objc,obj-c++ --with-system-zlib \
		--enable-install-libiberty \
		--disable-libstdcxx-pch \
		--build=x86_64-fatdog-linux-gnu &&
	   #--disable-bootstrap \
	   #--with-arch=armv7-a --with-cpu=cortex-a8 --with-tune=cortex-a8 \
       #--with-fpu=vfpv3-d16 --with-float=hard \
	#echo done configure && sh &&

	make $MAKEFLAGS &&
	#echo done build && sh &&
	
	# install, create extra symlinks, and move wrong .py file
	make install &&
	#cp -v ../gcc-$PKGVER/include/libiberty.h /usr/include && # already using --enable-install-libiberty
	ln -sfv ../usr/bin/cpp /lib &&
	ln -sfv ../usr/bin/cpp /lib64 &&
	ln -sfv gcc /usr/bin/cc &&
	
	mkdir -pv /usr/share/gdb/auto-load/usr/lib{,64} &&	
	mv -v /usr/lib/libstdc++*gdb.py /usr/share/gdb/auto-load/usr/lib &&
	mv -v /usr/lib64/libstdc++*gdb.py /usr/share/gdb/auto-load/usr/lib64 &&

	# LFS 8.1 add these - for compatibility with LTO
	install -v -dm755 /usr/lib64/bfd-plugins &&
	ln -sfv ../../libexec/gcc/$(gcc -dumpmachine)/7.2.0/liblto_plugin.so \
			/usr/lib64/bfd-plugins/	 &&
	#echo done install && sh &&

	# tests
	echo 'int main(){return 0;}' > dummy.c                      &&
	cc dummy.c -v -Wl,--verbose &> dummy.log                    &&
	readelf -l a.out | grep ': /lib'                            &&
	grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log          &&
	grep -B4 '^ /usr/include' dummy.log                         &&
	grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'          &&
	grep "/lib.*/libc.so.6 " dummy.log                          &&
	grep found dummy.log                                        &&
	./a.out && # echo done tests && sh &&
	pkg_build_slackdesc &&

	# LFS 8.1 export debug symbols
	for libdir in lib lib64; do
		cd /usr/$libdir &&
		for LIB in libquadmath.so.0.0.0 libstdc++.so.6.0.24 \
				libmpx.so.2.0.1 libmpxwrappers.so.2.0.1 libitm.so.1.0.0 \
				libcilkrts.so.5.0.0 libatomic.so.1.2.0;
        do
			objcopy --only-keep-debug $LIB $LIB.dbg &&
			strip --strip-unneeded $LIB &&
			objcopy --add-gnu-debuglink=$LIB.dbg $LIB || return 1
        done
	done	
} 

