#!/bin/bash

#echo "Press Key - Installing ${TARGET} for build the Cross Compil Chain for Brew"
#sudo apt-get update
#sudo apt-get install libncurses5-dev gcc texinfo libmpc-dev libmpfr-dev

#Target
#TARGET=arm-none-eabi
TARGET=arm-elf

#Versions
BINUTILS_VER=2.20.1
GCC_VER=4.5.0
NEWLIB_VER=1.18.0
GDB_VER=7.1
PREFIX=/opt/toolchains/${TARGET}_${BINUTILS_VER}_${GCC_VER}_${NEWLIB_VER}_${GDB_VER}

#Options
BINUTILS_OPTIONS='--target='${TARGET}' --prefix='${PREFIX}' --enable-interwork --enable-multilib --disable-nls --disable-shared --disable-threads --with-gcc --with-gnu-as --with-gnu-ld'
#BOOT_GCC_OPTIONS='--target='${TARGET}' --prefix='${PREFIX}' --enable-interwork --enable-multilib --disable-nls --disable-shared --disable-threads --disable-libstdc++ --disable-libssp --disable-libstdcxx-pch --disable-libmudflap --disable-libgomp --with-gmp='${PREFIX}' --with-mpfr='${PREFIX}' --with-mpc='${PREFIX}' --enable-languages=c --with-newlib --with-headers=../newlib-'${NEWLIB_VER}'/newlib/libc/include --with-gcc --with-gnu-as --with-dwarf2  -v'
#--with-specs='%{O2:%{!fno-remove-local-statics: -fremove-local-statics}} %{O*:%{O|O0|O1|O2|Os:;:%{!fno-remove-local-statics: -fremove-local-statics}}}'
#GCC_OPTIONS='--target='${TARGET}' --prefix='${PREFIX}' --enable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --enable-extra-sgxxlite-multilibs --with-gnu-as --with-gnu-ld  --enable-languages=c,c++ --disable-shared --disable-lto --with-newlib --disable-nls --with-headers=yes --disable-libgomp --enable-poison-system-directories --with-float=soft --disable-werror'
GCC_OPTIONS='--target='${TARGET}' --prefix='${PREFIX}' --disable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --enable-languages=c,c++ --disable-shared --with-newlib --disable-nls --with-headers=yes --disable-libgomp --with-float=soft --disable-werror'
#--with-gmp='${PREFIX}' --with-mpfr='${PREFIX} '--with-headers=../newlib-'${NEWLIB_VER}'/newlib/libc/include --with-mpc='${PREFIX}
#GCC_OPTIONS='--target='${TARGET}' --prefix='${PREFIX}' --enable-interwork --enable-multilib --disable-nls --disable-shared --disable-threads --with-gcc --with-gnu-as --with-gnu-ld --with-dwarf2 --enable-languages=c,c++ --with-newlib --with-headers=../newlib-'${NEWLIB_VER}'/newlib/libc/include --disable-libssp --disable-libstdcxx-pch --disable-libmudflap --disable-libgomp --with-gmp='${PREFIX}' --with-mpfr='${PREFIX}' --with-mpc='${PREFIX}' -v'
GDB_OPTIONS='--target='${TARGET}' --prefix='${PREFIX}' --disable-nls'
NEWLIB_OPTIONS='--target='${TARGET}' --prefix='${PREFIX}' --enable-interwork --enable-multilib --disable-newlib-supplied-syscalls'

#Display settings
echo "Prefix : "${PREFIX}
echo "binutils options : "${BINUTILS_OPTIONS}
echo "gcc options : "${GCC_OPTIONS}
echo "gdb options : "${GDB_OPTIONS}
echo "newlib options : "${NEWLIB_OPTIONS}

echo "Press Key - Downloading CCC from web"
test ! -f binutils-${BINUTILS_VER}.tar.bz2 && wget http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VER}.tar.bz2
test ! -f gcc-core-${GCC_VER}.tar.bz2 && wget http://ftp.gnu.org/gnu/gcc/gcc-${GCC_VER}/gcc-core-${GCC_VER}.tar.bz2
test ! -f gcc-g++-${GCC_VER}.tar.bz2 && wget http://ftp.gnu.org/gnu/gcc/gcc-${GCC_VER}/gcc-g++-${GCC_VER}.tar.bz2
test ! -f newlib-${NEWLIB_VER}.tar.gz && wget ftp://sources.redhat.com/pub/newlib/newlib-${NEWLIB_VER}.tar.gz
test ! -f gdb-${GDB_VER}.tar.bz2 && wget http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VER}.tar.bz2 

echo "Press Key - Cleaning ..."
rm -rf binutils-${BINUTILS_VER} newlib-${NEWLIB_VER} gdb-${GDB_VER} ${TARGET} gcc-${GCC_VER}
rm -rf ${PREFIX}
mkdir ${PREFIX}
mkdir ${PREFIX}/bin
export PATH=${PATH}:${PREFIX}/bin
echo "Path : "${PATH}
 
tar jxvf binutils-${BINUTILS_VER}.tar.bz2
tar jxvf gcc-core-${GCC_VER}.tar.bz2
tar jxvf gcc-g++-${GCC_VER}.tar.bz2
tar zxvf newlib-${NEWLIB_VER}.tar.gz
tar jxvf gdb-${GDB_VER}.tar.bz2 

echo "Press Key - Building of binutils !"
cd binutils-${BINUTILS_VER}
mkdir build
cd build
../configure ${BINUTILS_OPTIONS}
make
make install
cd ../..

echo "Press Key - Building of boot gcc !"
read -n 1 c #getchar
cd gcc-${GCC_VER}
mkdir build
cd build
#mkdir -p host-i686-pc-linux-gnu/fixincludes
../configure ${GCC_OPTIONS} #2>err.log
#mkdir -p libiberty libcpp fixincludes
echo "boot gcc make " > ../err.log
make all-gcc 2>> ../err.log
echo "install boot gcc " >> ../err.log
make install-gcc 2>> ../err.log
cd ../..

echo "Press Key - Building of newlib !"
read -n 1 c #getchar
cd newlib-${NEWLIB_VER}
mkdir build
cd build
../configure ${NEWLIB_OPTIONS}
make
make install
cd ../..

echo "Press Key - Building of full gcc"
read -n 1 c #getchar
cd gcc-${GCC_VER}/build
../configure ${GCC_OPTIONS} #2>err.log
echo "full gcc make " >> ../err.log
make all 2>> ../err.log
echo "full gcc make install" >> ../err.log
make install 2>> ../err.log
cd ../..

echo "Press Key - Building of gdb !"
read -n 1 c #getchar
cd gdb-${GDB_VER}
mkdir build
cd  build
../configure ${GDB_OPTIONS}
make
make install
cd ../..

