#!/bin/sh
# Builds a arm-elf cross compiler for use on linux
# host systems. This target has no file system or kernel.
#
# Original version from http://chdk.wikia.com/wiki/Gcc452
# Edited 2013-09-13 by PAPPP to bump versions for upstream 
#  bugfixes, and disable building of gcc's texinfo 
#  documentation, to address a known issue building gcc4.5 
#  docs with texinfo>5, shown here:
#  https://dev.openwrt.org/ticket/13039

TARGET="arm-elf"
BUILDDIR=`pwd`
BUILDDIR="${BUILDDIR}/build-dir"
mkdir ${BUILDDIR}
SRC="${BUILDDIR}/packages" # package directory
PREFIX="${BUILDDIR}/arm/toolchain" # final toolchain directory
PARALLEL="-j 2"
BINUTILS=binutils-2.18
GCC=gcc-4.5.4
GMP=gmp-5.0.5
MPFR=mpfr-3.1.2
MPC=mpc-0.8.2
NEWLIB=newlib-1.18.0
export PATH="$PATH:$PREFIX/bin"
# Before we do anything we need all the source files.
# We keep them in one place and we never need to
# download them twice if this build fails.
mkdir $SRC
cd $SRC
echo "Downloading source packages. This may take a while."
echo
wget -c http://ftp.gnu.org/gnu/binutils/$BINUTILS.tar.bz2
wget -c ftp://ftp.gnu.org/gnu/gcc/$GCC/$GCC.tar.bz2
wget -c ftp://ftp.gmplib.org/pub/$GMP/$GMP.tar.bz2
wget -c http://www.mpfr.org/mpfr-current/$MPFR.tar.bz2
wget -c http://www.multiprecision.org/mpc/download/$MPC.tar.gz
wget -c ftp://sources.redhat.com/pub/newlib/$NEWLIB.tar.gz
cd ..
echo "Starting Build..."
echo
mkdir build
echo "Building binutils"
echo
tar xfj $SRC/$BINUTILS.tar.bz2
cd $BINUTILS
./configure --target=$TARGET --prefix=$PREFIX --disable-werror
# --enable-interwork --disable-multilib --with-gnu-as
# --with-gnu-ld --disable-nls --disable-werror
make $PARALLEL
make install
cd ..
rm -rf build/* $BINUTILS
echo "Building GCC pass 1"
tar xfj $SRC/$GCC.tar.bz2
# the compiler math prereqs
tar -jxf $SRC/$GMP.tar.bz2
tar -jxf $SRC/$MPFR.tar.bz2
tar -zxf $SRC/$MPC.tar.gz
cd $GCC
mv ../$GMP gmp
mv ../$MPFR mpfr
mv ../$MPC mpc
cd ../build
#
ed ../$GCC/gcc/config/arm/t-arm-elf << EOF
/MULTILIB_OPTIONS/s/^M/# M/
/MULTILIB_DIRNAMES/s/^M/# M/
/mno-thumb-interwork/s/^# //
/normal interwork/s/^# //
wq
EOF
#
../$GCC/configure --target=$TARGET --prefix=$PREFIX --enable-interwork \
--enable-multilib --enable-languages="c" --with-newlib \
--with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs \
--without-headers --disable-shared --disable-nls --with-gnu-as --with-gnu-ld
make $PARALLEL all-gcc
make install-gcc
cd ..
rm -rf build/* # (We retain the GCC directory for pass 2)
echo "Building Newlib"
echo
tar xfz $SRC/$NEWLIB.tar.gz
cd build
../$NEWLIB/configure --target=$TARGET --prefix=$PREFIX --enable-interwork \
--enable-multilib --with-gnu-as --with-gnu-ld --disable-nls
make $PARALLEL
make install
cd ..
rm -rf build/* $NEWLIB
echo " Building GCC pass 2"
echo
cd build
# Added MAKEINFO=missing to avoid dealing with the texinfo >=5 strictness bugs
#	Throws a @itemx must follow @item errors if the docs are built.
../$GCC/configure --target=$TARGET --prefix=$PREFIX --enable-interwork \
--enable-multilib --enable-languages="c" --with-newlib \
--with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs \
--without-headers --disable-libssp --disable-nls --disable-zlib --disable-libc --disable-libm --disable-intl \
--disable-hardfloat --disable-threads --with-gnu-as --with-gnu-ld MAKEINFO=missing 
make $PARALLEL
make install
cd ..
rm -rf build/* $GCC
echo "Compiler Finished for "$TARGET
