#!/bin/bash
cd /usr/src/gcc-*/ ||exit

#gcc 3.3.3 needs 189MB Source + 530MB/143MB Build + 175MB/20MB Install = 894MB/350MB (with/without fortran+java)

#the test suite for GCC in this section is considered critical.

#LFSbook says: this package is known to behave badly when you have changed its
#default optimization flags (including the -march and -mcpu options),
#but optimization works fine


#suppress the installation of libiberty.a, the one from binutils will be used:
sed -i 's@install_to_$(INSTALL_DEST) @@' libiberty/Makefile.in

#add -fomit-frame-pointer for non-bootstraping too (for consistency), my CFLAGS are better, but that doesnt hurd
sed -i 's@^XCFLAGS =$@& -fomit-frame-pointer@' gcc/Makefile.in

#suppress running fixincludes (may copy host stuff to target, target is known good):
sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in

#simulate existence of mktemp (installed later):
sed -i 's/@have_mktemp_command@/yes/' gcc/gccbug.in


#my own compilation fix:
patch -p 1 <../gcc-4.2-20060520-compilation-1.patch


#use posix head & tail syntax:
#patch -p 1 <../gcc-3.3-posix-1.patch
#patch -p 1 <../gcc-3.4.2-posix-1.patch
#patch -p 1 <../gcc-3.4.4-posix-1.patch
#patch -p 1 <../gcc-3.5-posix-3.patch
patch -p 1 <../gcc-4.2-20060520-posix-1.patch



cd .. &&
rm -rf _gcc &&
mkdir _gcc &&
cd _gcc &&
#CPPFLAGS=$CFLAGS CXXFLAGS=$CFLAGS ../gcc-*/configure --prefix=/usr --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c,c++ &&
#if you do need fortran and java: use --enable-languages=c,c++,f77,java instead:
CPPFLAGS=$CFLAGS CXXFLAGS=$CFLAGS ../gcc-*/configure --prefix=/usr \
  --libexecdir=/usr/lib --enable-shared --enable-threads=posix \
  --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c,c++ &&
export MYCFLAGS=$CFLAGS &&
make CFLAGS='$(MYCFLAGS)' LIBCLFAGS='$(MYCFLAGS)' LIBCXXFLAGS='$(MYCFLAGS) -no-implicit-templates' BOOT_CFLAGS='$(MYCFLAGS)' bootstrap &&
#make -k check
rm -f /usr/bin/{{,i686-pc-linux-gnu-}{g++,gcc*},c++} &&
make install &&
ln -sfv ../usr/bin/cpp /lib &&
ln -sfv gcc /usr/bin/cc &&
ln -s i686-pc-linux-gnu-c++ c++ &&
ln -s i686-pc-linux-gnu-c++ g++ &&
ln -s i686-pc-linux-gnu-c++ i686-pc-linux-gnu-g++ &&
ln -s i686-pc-linux-gnu-gcc-* i686-pc-linux-gnu-gcc &&
ln -s i686-pc-linux-gnu-gcc gcc


#tiny testsuite:
echo 'main(){}' &gt; dummy.c
cc dummy.c -Wl,--verbose &amp;&gt; dummy.log
readelf -l a.out | grep ': /lib'

grep -o '/usr/lib.*/crt[1in].* .*' dummy.log
grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'
grep "/lib/libc.so.6 " dummy.log
grep found dummy.log

rm -v dummy.c a.out dummy.log
