#!/bin/bash
#
# This script aims to build a complete toolchain.
# Please verify the given pathes and versions!
# You have to set up the BUILD-, HOST- and TARGET-variables correctly
#
# If you want to build for running on mingw, you have to build a toolchain
# for that target first, use the mingw.sg script!
#
# Building a toolchain for a atmel-target, use the patched atmel-sources
# instead of the original gnu ones!
#
# 03/13/2006 by Jonatan Antoni <jantoni@rea.de>
#
############
# Paths:
#
# Where to find the sources (unpacked!)?
PACKAGE_DIR=`pwd`/src
# Where to build (temporaily build files)?
BUILD_ROOT=`pwd`/build
# Where to place the local toolchain (for TARGET on BUILD)?
PREFIX=`pwd`/cross-arm
export PATH=${PATH}:${PREFIX}/bin/
# Where to place the distributing toolchain (for TARGET on HOST)?
DISTPREFIX=`pwd`/dist-mingw
# Where to find precompiled glibc for TARGET
PREC_GLIBC=/opt/elinos-4.0/cdk/arm/v4le/glibc-2.3.4
#
############
# Versions:
#
GCC_VERSION="3.4.4"
BINUTILS_VERSION="2.16.1"
NEWLIB_VERSION="1.14.0"
GDB_VERSION="6.3.50"
#INSIGHT_VERSION="6.3-mingw"
#
############
# Architectures:
#
# On which system we do the build?
BUILD=i686-pc-linux-gnu
#
# On which system should the toolchain be used?
# HOST=${BUILD} # if we do no canadian crosscompile!
HOST=i586-pc-mingw32
#
# And for which target-system?
TARGET=armel-linux-gnueabi
#
############
############
#
# A. Build toolchain for target 'TARGET' on 'BUILD'
#
############
# Don't touch the following, if you don't know really what you do!
SYSROOT=${PREFIX}/${TARGET}
HEADERDIR=${SYSROOT}/include
LIBDIR=${SYSROOT}/lib
#
############
# 1. Build BINUTILS
#
mkdir -p ${SYSROOT}/lib
rm -rf ${BUILD_ROOT}/${BUILD}/${TARGET}/binutils
mkdir -p ${BUILD_ROOT}/${BUILD}/${TARGET}/binutils

cd ${BUILD_ROOT}/${BUILD}/${TARGET}/binutils

${PACKAGE_DIR}/binutils-${BINUTILS_VERSION}/configure \
	--prefix=${PREFIX} \
	--build=${BUILD} \
	--host=${BUILD} \
	--target=${TARGET} \
	--with-headers=${HEADERDIR} \
	--with-libs=${LIBDIR} \
	--with-sysroot=${SYSROOT} \
	2>&1 |tee configure.log || exit 1

make CFLAGS_FOR_BUILD="-O -pipe" all 2>&1 |tee build.log || exit 1
make install 2>&1 |tee install.log || exit 1
#
############
# 2. Copying precompiled glibc
#
cp -af ${PREC_GLIBC}/${TARGET}/{include,lib} ${SYSROOT}
cp -af ${PREC_GLIBC}/{include,lib} ${PREFIX}
#
############
# 3. Build GCC
#
rm -rf ${BUILD_ROOT}/${BUILD}/${TARGET}/gcc
mkdir -p ${BUILD_ROOT}/${BUILD}/${TARGET}/gcc

cd ${BUILD_ROOT}/${BUILD}/${TARGET}/gcc

${PACKAGE_DIR}/gcc-${GCC_VERSION}/configure \
	--prefix=${PREFIX} \
	--build=${BUILD} \
	--host=${BUILD} \
	--target=${TARGET} \
	--enable-c99 \
	--enable-shared \
	--enable-threads=posix \
        --enable-languages=c,c++ \
        --disable-__cxa_atexit \
        --with-gnu-ld \
	--enable-target-optspace \
	--disable-nls \
        --with-arch=armv4 \
	--without-fp \
	--with-float=soft \
        --with-headers=${HEADERDIR} \
	--with-libs=${LIBDIR} \
	--with-local-prefix=${SYSROOT} \
        2>&1 |tee configure.log || exit 1

make LIMITS_H_TEST=true CFLAGS="-O1" all-gcc 2>&1 |tee build.log || exit 1
make install-gcc 2>&1 |tee install.log || exit 1
#
############
# 4. Strip targets
#
strip ${PREFIX}/bin/*
#
############
############
#
# B. Build toolchain for target 'TARGET' on 'HOST'
#
############
# Don't touch the following, if you don't know really what you do!
SYSROOT=${DISTPREFIX}/${TARGET}
HEADERDIR=${SYSROOT}/include
LIBDIR=${SYSROOT}/lib
#
############
# 1. Build BINUTILS
#
mkdir -p ${SYSROOT}/lib
rm -rf ${BUILD_ROOT}/${HOST}/${TARGET}/binutils
mkdir -p ${BUILD_ROOT}/${HOST}/${TARGET}/binutils

cd ${BUILD_ROOT}/${HOST}/${TARGET}/binutils

${PACKAGE_DIR}/binutils-${BINUTILS_VERSION}/configure \
	--prefix=${DISTPREFIX} \
	--build=${BUILD} \
	--host=${HOST} \
	--target=${TARGET} \
	--with-sysroot=${SYSROOT} \
	--enable-install-libbfd
	2>&1 |tee configure.log || exit 1

make CFLAGS_FOR_BUILD="-O -pipe" all 2>&1 |tee build.log || exit 1
make install 2>&1 |tee install.log || exit 1
#
############
# 2. Copy precompiled glibc
#
cp -af ${PREC_GLIBC}/${TARGET}/{include,lib} ${SYSROOT}
cp -af ${PREC_GLIBC}/{include,lib} ${DISTPREFIX}
#
############
# 3. Build GCC
#
rm -rf ${BUILD_ROOT}/${HOST}/${TARGET}/gcc
mkdir -p ${BUILD_ROOT}/${HOST}/${TARGET}/gcc

cd ${BUILD_ROOT}/${HOST}/${TARGET}/gcc

${PACKAGE_DIR}/gcc-${GCC_VERSION}/configure \
	--prefix=${DISTPREFIX} \
	--build=${BUILD} \
	--host=${HOST} \
	--target=${TARGET} \
        --enable-c99 \
	--enable-shared \
	--enable-threads=posix \
	--enable-languages=c,c++ \
        --disable-__cxa_atexit \
        --with-gnu-ld \
	--enable-target-optspace \
	--disable-nls \
        --with-arch=armv4 \
	--without-fp \
	--with-float=soft \
        --with-headers=${HEADERDIR} \
	--with-libs=${LIBDIR} \
        2>&1 |tee configure.log || exit 1

make LIMITS_H_TEST=true CFLAGS="-O1" all-gcc 2>&1 |tee build.log || exit 1
make install-gcc 2>&1 |tee install.log || exit 1
#
############
# 4. Copy precompiled glibc again
#
cp -af ${PREC_GLIBC}/{include,lib} ${DISTPREFIX}
#
############
# 5. Build GDB
#
rm -rf ${BUILD_ROOT}/${HOST}/${TARGET}/gdb
mkdir -p ${BUILD_ROOT}/${HOST}/${TARGET}/gdb

cd ${BUILD_ROOT}/${HOST}/${TARGET}/gdb

export gdbsrv_cv_have_ptrace_getfpxregs=no
export gdb_cv_have_ptrace_getfpxregs=no

${PACKAGE_DIR}/gdb-${GDB_VERSION}/configure \
  --prefix=${DISTPREFIX} \
  --build=${BUILD} \
  --host=${HOST} \
  --target=${TARGET} \
  --disable-sim

make all 2>&1 |tee build.log || exit 1
make -C gdb install 2>&1 |tee install.log || exit 1
#
############
# 6. Strip targets
#
${HOST}-strip ${DISTPREFIX}/bin/*
#
############
# 7. Build newlib
#
rm -rf ${BUILD_ROOT}/${TARGET}/newlib
mkdir -p ${BUILD_ROOT}/${TARGET}/newlib

cd ${BUILD_ROOT}/${TARGET}/newlib

${PACKAGE_DIR}/newlib-${NEWLIB_VERSION}/configure \
	--prefix=${DISTPREFIX} \
	--build=${BUILD} \
	--host=${HOST} \
	--target=${TARGET} \
	--disable-nls \
	--disable-interwork \
	2>&1 |tee configure.log || exit 1

make 2>&1 |tee build.log || exit 1
cp -af ${TARGET}/newlib/lib{c,g,m}.a ${SYSROOT}/lib
cp -af ${TARGET}/libgloss/libnosys/libnosys.a ${SYSROOT}/lib
#
############

