blob: e4193a098885259fd560c0fea51d5e7e9bd553dc [file] [log] [blame]
#!/usr/bin/env bash
OS=`uname`
HOSTARCH=`uname -m`
PARALLEL=
FETCH=0
GNU_FTP=ftp://ftp.gnu.org/gnu
ARCHIVES=archives
PATCHES=./patches/
# Get absolute path, will spawn a subshell then exit so our pwd is retained
SCRIPTROOT=$(cd "$(dirname $BASH_SOURCE[0])" && pwd)
function log()
{
"$@" 2>&1 | tee -a $SCRIPTROOT/build.log
}
function extract-tool()
{
#echo "extract-tool " $1 $2 $3 $4
TARFILE=${1}-${2}.tar$3
TARGETDIR=${1}-${2}
if [ -f ${TARGETDIR}/.extracted ]; then
log echo "$TARFILE already extracted into $TARGETDIR, skipping"
return 0
fi
if [ ! -f $ARCHIVES/$TARFILE ]; then
log echo "error, missing $TARFILE"
exit 1
fi
log echo extracting $TARFILE
rm -rf $TARGETDIR
tar xf $ARCHIVES/$TARFILE || exit 1
if [ ! -z "$4" ]; then
log echo patching $1
log patch -d $TARGETDIR -p1 < $4 || exit 1
fi
touch $TARGETDIR/.extracted || exit 1
}
if [ "$OS" = "Linux" ]; then
COUNT=`grep processor /proc/cpuinfo | wc -l`
PARALLEL=-j`expr $COUNT + $COUNT`
fi
if [ "$OS" = "Darwin" ]; then
export CPPFLAGS=-I/opt/local/include
export LDFLAGS=-L/opt/local/lib
export CXXFLAGS="-fbracket-depth=1024"
fi
if [ "$OS" = "FreeBSD" ]; then
export CPPFLAGS=-I/usr/local/include
export LDFLAGS=-L/usr/local/lib
fi
MAKE=make
if [ "$OS" = "FreeBSD" ]; then
MAKE=gmake
fi
if [ "$HOSTARCH" = "amd64" ]; then
HOSTARCH=x86_64
fi
ARGS=`getopt -o a:cfj: --long arch:,cache,fetch,parallel: -- "$@"`
if [ $# == "0" ]; then
echo "Options"
echo " -a <arch list> architectures to build"
echo " example: -a 'arm x86'"
echo " -c use compilation cache (ccache must be installed)"
echo " -f fetch source releases from upstream"
echo " -j<#> use <#> parallel workers to build [default: 8]"
exit 1
fi
eval set -- "$ARGS"
while true; do
case "$1" in
-a|--arch ) ARCHES=$2 ; shift 2 ;;
-c|--cache ) CCACHE=1 ; shift ;;
-j|--parallel ) PARALLEL="-j$2" ; shift 2 ;;
-f|--fetch ) FETCH=1 ; shift ;;
-- ) shift ; break ;;
* ) echo "unrecognized option '$1'" ; exit 1 ;;
esac
done
if [ -z "$ARCHES" ]; then
echo need to specify architectures to build
echo ie -a "arm sh"
exit 1
fi
export CC="cc"
export CXX="c++"
if [ "$CCACHE" = "1" ]; then
export CC="ccache $CC"
export CXX="ccache $CXX"
fi
# load GCCVER and BINVER
. toolvers
if [ "$FETCH" = "1" ]; then
if [ ! -f binutils-$BINVER.tar.bz2 ]; then
log wget -P $ARCHIVES -N $GNU_FTP/binutils/binutils-$BINVER.tar.bz2
fi
if [ ! -f gcc-$GCCVER.tar.bz2 ]; then
log wget -P $ARCHIVES -N $GNU_FTP/gcc/gcc-$GCCVER/gcc-$GCCVER.tar.bz2
fi
if [ ! -f gdb-$GDBVER.tar.xz ]; then
log wget -P $ARCHIVES -N $GNU_FTP/gdb/gdb-$GDBVER.tar.xz
fi
if [ ! -f mpfr-$MPFRVER.tar.bz2 ]; then
log wget -P $ARCHIVES -N $GNU_FTP/mpfr/mpfr-$MPFRVER.tar.bz2
fi
if [ ! -f mpc-$MPCVER.tar.gz ]; then
log wget -P $ARCHIVES -N $GNU_FTP/mpc/mpc-$MPCVER.tar.gz
fi
if [ ! -f gmp-$GMPVER.tar.bz2 ]; then
log wget -P $ARCHIVES -N $GNU_FTP/gmp/gmp-$GMPVER.tar.bz2
fi
fi
if [ ! -f .extracted-stamp ]; then
extract-tool binutils $BINVER .bz2 $PATCHES/binutils-patch.txt
extract-tool gcc $GCCVER .bz2 $PATCHES/gcc-patch.txt
extract-tool gdb $GDBVER .xz $PATCHES/gdb-patch.txt
extract-tool gmp $GMPVER .bz2
extract-tool mpc $MPCVER .gz
extract-tool mpfr $MPFRVER .bz2
touch .extracted-stamp
fi
# link the last three libs into gcc
pushd gcc-$GCCVER
ln -sf ../gmp-$GMPVER gmp
ln -sf ../mpc-$MPCVER mpc
ln -sf ../mpfr-$MPFRVER mpfr
popd
for ARCH in $ARCHES; do
if [ "$ARCH" == "arm" ]; then
TARGET=arm-eabi
else
TARGET=$ARCH-elf
fi
INSTALLPATH=`pwd`/$TARGET-$GCCVER-$OS-$HOSTARCH
BINBUILDPATH=build-binutils-$BINVER-$ARCH-$OS-$HOSTARCH
GCCBUILDPATH=build-gcc-$GCCVER-$ARCH-$OS-$HOSTARCH
GDBBUILDPATH=build-gdb-$GDBVER-$ARCH-$OS-$HOSTARCH
export PATH=$INSTALLPATH/bin:$PATH
# Building Binutils
if [ ! -f $BINBUILDPATH/built.txt ]; then
mkdir -p $BINBUILDPATH
pushd $BINBUILDPATH &&
log ../binutils-$BINVER/configure --target=$TARGET --prefix=$INSTALLPATH --disable-werror &&
#$MAKE configure-host &&
log $MAKE $PARALLEL &&
log $MAKE install &&
touch built.txt || exit 1
popd
fi
# Building GCC
if [ ! -f $GCCBUILDPATH/built.txt ]; then
ARCH_OPTIONS=
if [ $ARCH == "arm" ]; then
ARCH_OPTIONS="--with-cpu=arm926ej-s --with-fpu=vfp"
fi
mkdir -p $GCCBUILDPATH
pushd $GCCBUILDPATH &&
log ../gcc-$GCCVER/configure --target=$TARGET --prefix=$INSTALLPATH --enable-languages=c,c++ $ARCH_OPTIONS --disable-werror &&
log $MAKE all-gcc $PARALLEL &&
log $MAKE all-target-libgcc $PARALLEL &&
log $MAKE install-gcc &&
log $MAKE install-target-libgcc &&
touch built.txt || exit 1
popd
fi
if [ ! -f $GDBBUILDPATH/built.txt ]; then
mkdir -p $GDBBUILDPATH
pushd $GDBBUILDPATH &&
log ../gdb-$GDBVER/configure --target=$TARGET --prefix=$INSTALLPATH --disable-werror &&
log make $PARALLEL &&
log make install &&
touch built.txt || exit 1
popd
fi
done