#!/bin/sh

################################################################################

dirsrc_binutils=binutils-2.19
dirbld_binutils=binutils-2.19_build
dirsrc_gcc=gcc-4.3.3
dirbld_gcc=gcc-4.3.3_build
dirsrc_newlib=newlib-1.17.0
dirbld_newlib=newlib-1.17.0_build

dirsrc_gmp=gmp-4.2.4
dirsrc_mpfr=mpfr-2.4.1

rplace_newlib_iput='CFLAGS_FOR_TARGET = -g -Os'
rplace_newlib_oput='CFLAGS_FOR_TARGET = -g -Os -DREENTRANT_SYSCALLS_PROVIDED -DINTEGER_ONLY -D__BUFSIZ__=256 -ffunction-sections -fdata-sections'

target=arm-elf
prefix=/home/widar/gcc_arm_elf
makearg=""

################################################################################

ln --symbolic $dirsrc_newlib/newlib   $dirsrc_gcc/newlib
ln --symbolic $dirsrc_newlib/libgloss $dirsrc_gcc/libgloss
ln --symbolic $dirsrc_gmp             $dirsrc_gcc/gmp
ln --symbolic $dirsrc_mpfr            $dirsrc_gcc/mpfr

################################################################################

export CFLAGS='-O2 -pipe'
export CXXFLAGS='-O2 -pipe'
export LDFLAGS='-s'
export DEBUG_FLAGS=''

export PATH="$prefix/bin:/home/install/bin":$PATH

##### BINUTILS #################################################################

cls
cd $dirbld_binutils
pwd

../$dirsrc_binutils/configure --target=$target --prefix=$prefix       \
--with-gcc --with-gnu-ld --with-gnu-as --with-stabs --with-float=soft \
--enable-interwork --enable-multilib                                  \
--disable-nls --disable-shared --disable-debug --disable-threads      \
2>&1 | tee __01_cnfg.log

make all     | tee __02_make_all.log     2>&1
make install | tee __03_make_install.log 2>&1

cd ..

##### GCC PART 01 ##############################################################

cls
cd $dirbld_gcc
pwd

../$dirsrc_gcc/configure --target=$target --prefix=$prefix            \
--with-gcc --with-gnu-ld --with-gnu-as --with-stabs --with-float=soft \
--enable-interwork --enable-multilib                                  \
--disable-nls --disable-shared --disable-debug --disable-threads      \
--with-newlib --with-dwarf2                                           \
--without-headers                                                     \
--enable-languages="c,c++" --enable-target-optspace                   \
--disable-win32-registry                                              \
-v                                                                    \
2>&1 | tee __01_cnfg.log

make all-gcc     | tee __02_make_all_gcc.log     2>&1
make install-gcc | tee __03_make_install_gcc.log 2>&1

cd ..

##### NEWLIB ###################################################################

cls
cd $dirbld_newlib
pwd

../$dirsrc_newlib/configure --target=$target --prefix=$prefix         \
--with-gcc --with-gnu-ld --with-gnu-as --with-stabs --with-float=soft \
--enable-interwork --enable-multilib                                  \
--disable-nls --disable-shared --disable-debug --disable-threads      \
--disable-newlib-supplied-syscalls                                    \
2>&1 | tee __01_cnfg.log

mv Makefile Makefile.org
sed -e "s/$rplace_newlib_iput/$rplace_newlib_oput/" Makefile.org > Makefile

make all     | tee __02_make_all.log     2>&1
make install | tee __03_make_install.log 2>&1

cd ..

##### GCC PART 02 ##############################################################

cls
cd $dirbld_gcc
pwd

make $makearg         | tee __05_make_all.log     2>&1
make $makearg install | tee __06_make_install.log 2>&1

cd ..

################################################################################

rm -r ./$dirsrc_gcc/newlib
rm -r ./$dirsrc_gcc/libgloss
rm -r ./$dirsrc_gcc/gmp
rm -r ./$dirsrc_gcc/mpfr
