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

# source: CLFS 3.0.0 with bits and patches from original LFS 7.5 instructions
TARBALL=gcc-4.8.3.tar.bz2
MD5SUM=7c60f24fab389f77af203d2516ee110f
SRC_URL=http://distro.ibiblio.org/fatdog/source/710/clfs-packages-3.0.0.tar
BUNDLE=clfs-packages-3.0.0.tar

SLACKREQ='binutils libgmp libmpfr libmpc glibc zlib'
SLACKDESC="gcc: gcc $PKGVER (GNU C Compiler)  
gcc:  
gcc: This package contains the C and C++ compiler from GCC (GNU Compiler 
gcc: Collection). GCC, along with glibc and binutils is called the "toolchain" 
gcc: and is required to rebuild the system. This package also provides 
gcc: libstdc++ runtime and libgcc_s.so runtime, which is required by most 
gcc: programs  
gcc: 
gcc: 
gcc: 
gcc:
"


### 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 $SRC_DIR/gcc-4.8.3-branch_update-1.patch
}

### 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
	# 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-4.8.3/configure --prefix=/usr --libdir=/usr/lib64 \
		--libexecdir=/usr/lib64 --enable-threads=posix --enable-__cxa_atexit \
		--enable-clocale=gnu --enable-languages=c,c++ --disable-libstdcxx-pch \
		--with-system-zlib --enable-checking=release --enable-libstdcxx-time \
		--build=x86_64-fatdog-linux-gnu --enable-shared &&
	   #--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-4.8.3/include/libiberty.h /usr/include &&
	ln -sv ../usr/bin/cpp /lib &&
	ln -sv ../usr/bin/cpp /lib64 &&
	ln -sv 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	
	#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
} 

