#!/bin/bash
# Written by Uwe Hermann <uwe@hermann-uwe.de>, released as public domain.
# Modified by Piotr Esden-Tempski <piotr@esden.net>, released as public domain.

#
# Requirements (example is for Debian, replace package names as needed):
#
# apt-get install flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo build-essential
#
# Or on Ubuntu Maverick give `apt-get build-dep gcc-4.5` a try.
#

# Stop if any command fails
set -e

##############################################################################
# Settings section
# You probably want to customize those
##############################################################################
TARGET=arm-none-eabi		# Or: TARGET=arm-elf
PREFIX=${HOME}/ARM_tools/arm-none-eabi	# Install location of your final toolchain
PARALLEL=""		# Or: PARALLEL="-j 5" for 4 CPUs

# Set to 'sudo' if you need superuser privileges while installing
SUDO=

# Set to 1 to be quieter while running
QUIET=0

##############################################################################
# Version and download url settings section
##############################################################################
	# For FSF GCC:
GCCVERSION=4.6.0
GCC=gcc-${GCCVERSION}
GCCURL=http://ftp.gnu.org/gnu/gcc/${GCC}/${GCC}.tar.gz

BINUTILS=binutils-2.20.1
NEWLIB=newlib-1.19.0
GDB=gdb-7.2

##############################################################################
# Flags section
##############################################################################

GDBFLAGS=
BINUTILFLAGS=
GCCFLAGS=

MAKEFLAGS=${PARALLEL}
TARFLAGS=v

if [ ${QUIET} != 0 ]; then
    TARFLAGS=
    MAKEFLAGS="${MAKEFLAGS} -s"
fi

export PATH="${PREFIX}/bin:${PATH}"

SUMMON_DIR=$(pwd)
SOURCES=${SUMMON_DIR}/sources
STAMPS=${SUMMON_DIR}/stamps

##############################################################################
# Building section
# You probably don't have to touch anything after this
##############################################################################

# Fetch a versioned file from a URL
function fetch {
    if [ ! -e ${STAMPS}/$1.fetch ]; then
        log "Downloading $1 sources..."
        wget -c --no-passive-ftp $2
        touch ${STAMPS}/$1.fetch
    fi
}

# Log a message out to the console
function log {
    echo "******************************************************************"
    echo "* $*"
    echo "******************************************************************"
}

# Unpack an archive
function unpack {
    log Unpacking $*
    # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
    tar xaf${TARFLAGS} ${SOURCES}/$1.tar.*
}

# Install a build
function install {
    log $1
    ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
}


mkdir -p ${STAMPS} ${SOURCES}
#mkdir -p ${STAMPS}

cd ${SOURCES}

fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
fetch ${GCC} ${GCCURL}
fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2


cd ${SUMMON_DIR}

if [ ! -e build ]; then
    mkdir build
fi

if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
    unpack ${BINUTILS}
    cd build
    log "Configuring ${BINUTILS}"
    ../${BINUTILS}/configure --target=${TARGET} \
                           --prefix=${PREFIX} \
                           --enable-interwork \
                           --enable-multilib \
                           --with-gnu-as \
                           --with-gnu-ld \
                           --disable-nls \
                           --disable-werror \
			   ${BINUTILFLAGS}
    log "Building ${BINUTILS}"
    make ${MAKEFLAGS}
    install ${BINUTILS} install
    cd ..
    log "Cleaning up ${BINUTILS}"
    touch ${STAMPS}/${BINUTILS}.build
    rm -rf build/* 
fi

if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
    unpack ${GCC} boot
    cd build
    log "Configuring ${GCC}-boot"
    ../${GCC}/configure --target=${TARGET} \
                      --prefix=${PREFIX} \
                      --enable-interwork \
                      --enable-languages="c" \
                      --with-newlib \
                      --without-headers \
                      --disable-shared \
                      --with-gnu-as \
                      --with-gnu-ld \
                      --disable-nls \
                      --disable-werror \
		      ${GCCFLAGS}
    log "Building ${GCC}-boot"
    make ${MAKEFLAGS} all-gcc
    install ${GCC}-boot install-gcc
    cd ..
    log "Cleaning up ${GCC}-boot"
    touch ${STAMPS}/${GCC}-boot.build
    rm -rf build/* 
fi

if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
    unpack ${NEWLIB}
    cd build
    log "Configuring ${NEWLIB}"
    ../${NEWLIB}/configure --target=${TARGET} \
                         --prefix=${PREFIX} \
                         --enable-interwork \
                         --enable-multilib \
                         --with-gnu-as \
                         --with-gnu-ld \
                         --disable-nls \
                         --disable-werror \
                         --disable-newlib-supplied-syscalls
    log "Building ${NEWLIB}"
    make ${MAKEFLAGS}
    install ${NEWLIB} install
    cd ..
    log "Cleaning up ${NEWLIB}"
    touch ${STAMPS}/${NEWLIB}.build
    rm -rf build/* 
fi

# Yes, you need to build gcc again!
if [ ! -e ${STAMPS}/${GCC}.build ]; then
    unpack ${GCC}
    cd build
    log "Configuring ${GCC}"
    ../${GCC}/configure --target=${TARGET} \
                      --prefix=${PREFIX} \
                      --enable-interwork \
                      --enable-languages="c,c++" \
                      --with-newlib \
                      --disable-shared \
                      --with-gnu-as \
                      --with-gnu-ld \
		      --disable-nls \
                      --disable-werror \
	 	     ${GCCFLAGS}
    log "Building ${GCC}"
    make ${MAKEFLAGS}
    install ${GCC} install
    cd ..
    log "Cleaning up ${GCC}"
    touch ${STAMPS}/${GCC}.build
    rm -rf build/* 
fi

if [ ! -e ${STAMPS}/${GDB}.build ]; then
    unpack ${GDB}
    cd build
    log "Configuring ${GDB}"
    ../${GDB}/configure --target=${TARGET} \
                      --prefix=${PREFIX} \
                      --enable-interwork \
                      --enable-multilib \
                      --disable-werror \
		      ${GDBFLAGS}
    log "Building ${GDB}"
    make ${MAKEFLAGS}
    install ${GDB} install
    cd ..
    log "Cleaning up ${GDB}"
    touch ${STAMPS}/${GDB}.build
    rm -rf build/* ${GDB}
fi





