[deps]
m4
gmp
mpfr
mpc
zlib
elfutils
g++
binutils

[main]
filesize=28252641
sha512=a9812039b5588cd46d61b933e6d58b031397ff8ae0d7567d00845f9e71001fbe4dffdddc5076a78d5129216f9bd80c7e76b21605e4a6cb9a9149e5ef42664004
tardir=gcc-4.5.3

[mirrors]
http://ftpmirror.gnu.org/gcc/gcc-4.5.3/gcc-core-4.5.3.tar.bz2

[build]
#busybox awk doesnt like the two calls to attribute_mode function which is commented out
sed -i '/   *attribute_mode/s@attr@#&@' gcc/config/i386/i386-builtin-types.awk

# we're doing a bit of hack to place the g++ contents in this folder
# the build system isnt really laid out for extracting multiple tarballs into
# the same dir...

dir=$PWD
cd ..
tar xf $C/gcc-g++-4.5.3.tar.bz2
cd $dir
#more g++ hackery
sed -i 's,# include <syscall.h>,#include <sys/syscall.h>,' ./libstdc++-v3/libsupc++/guard.cc
#sed -i 's,# define _GLIBCXX_USE_FUTEX,,' ./libstdc++-v3/libsupc++/guard.cc

#todo check if this isnt to intrusive
sed -i 's,gcc_no_link=yes,gcc_no_link=no,' ./libstdc++-v3/configure

#and even more g++ hackery
mv ./libstdc\+\+-v3/config/os/gnu-linux ./libstdc\+\+-v3/config/os/gnu-linux.org
cp -r ./libstdc\+\+-v3/config/os/generic ./libstdc\+\+-v3/config/os/gnu-linux
#g++ hackery done

sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in

sed -i 's@/lib/ld-linux.so.2@/lib/ld-musl-i386.so.1@' gcc/config/i386/linux.h
sed -i 's@/lib/ld-linux.so.2@/lib/ld-musl-i386.so.1@' gcc/config/i386/linux64.h
sed -i 's@/lib/ld-linux.so.3@/lib/ld-musl-arm.so.1@' gcc/config/arm/linux-eabi.h
sed -i 's@/lib/ld-linux.so.2@/lib/ld-musl-arm.so.1@' gcc/config/arm/linux-elf.h
sed -i 's@/lib64/ld-linux-x86-64.so.2@/lib/ld-musl-x86_64.so.1@' gcc/config/i386/linux64.h

patch -p1 < $K/gcc-453-patch0.diff || exit 1
if [ x$A = xarm ] ; then
	patch -p1 < $K/gcc-453-patch1.diff || exit 1
	patch -p1 < $K/gcc-453-patch2.diff || exit 1
fi

#create directory structure for static toolchain
TC_PATH=$R/opt/toolchain
mkdir -p $TC_PATH
SELF=`pwd`
if [[ ! -e $TC_PATH/usr ]] ; then
        cd $TC_PATH
        ln -sf . usr
        cd $SELF
fi

export CC="$CC -static -D_GNU_SOURCE -I$R/include -L$R/lib"

CFLAGS="-O0 -s" LDFLAGS="-Wl,--no-keep-memory" ./configure -C --with-newlib --with-headers=no --prefix=/ \
  --disable-multilib --disable-nls --disable-shared --disable-mudflap \
  --disable-libmudflap --disable-libssp --disable-libgomp \
  --disable-debug --disable-bootstrap \
  --enable-lto --with-system-zlib \
  --with-build-sysroot=/ --with-sysroot=$TC_PATH \
  --with-target-libiberty=no --with-target-zlib=no \
  --enable-languages=c,c++ --enable-clocale=generic \
  LD_FOR_TARGET=$TC_PATH/bin/ld \
  || exit 1

#  --disable-hosted-libstdcxx --with-target-libstdcxx=no \
#we use LD_FOR_TARGET, since the bootstrapped xgcc depends on a linker with sysroot                             


make -j$MAKE_THREADS BOOT_LDFLAGS="-static" LDFLAGS="-static" || exit 1
#according to some gnu ppl, install-gcc will only install the relevant
#stuff and leaves libiberty and co out.
make -j1 DESTDIR=$TC_PATH install-gcc install-lto-plugin install-target-libgcc install-target-libstdc++-v3 || exit 1

#create a host_tuple dummy entry, so binutils stuff will get into /bin, etc
lib_gcc=`$TC_PATH/bin/gcc -print-libgcc-file-name`
lib_gcc_minus_filename=`echo $lib_gcc | sed 's,4\.5\.3.*,,'`
host_tuple=`basename $lib_gcc_minus_filename`
echo host tuple is $host_tuple

if [[ ! -e ${TC_PATH}/${host_tuple} ]] ; then
        cd $TC_PATH
        ln -sf . ${host_tuple}
        cd $SELF
fi

#remove gcc's stddef.h which is not compatible
rm $TC_PATH/lib/gcc/$host_tuple/4.5.3/include/stddef.h

[ "$A" = x86_64 ] && mv $TC_PATH/lib64/* $TC_PATH/lib/ || true

#prevent custom buildscripts from messing with the toolchain
exit 0

