bash
#!/bin/bash
##-------------------------------------------------------------
## Configuration variables
##-------------------------------------------------------------
root_dir="/home/prometheus/module-test/cross-compile"
target="powerpc-eabi"
clean_install=true
build_binutils=true
build_gcc_static=true
build_newlib=true
build_gcc=true
# gcc dependency
isl_tar_link="ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.15.tar.bz2"
mpfr_tar_link="ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2"
mpc_tar_link="ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz"
gmp_tar_link="ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2"
# use gcc492, confirmed ok
binutils_tar_link="http://ftp.gnu.org/gnu/binutils/binutils-2.27.tar.gz"
#binutils_tar_link="https://ftp.gnu.org/gnu/binutils/binutils-2.39.tar.gz"
#gcc_tar_link="http://mirrors.kernel.org/gnu/gcc/gcc-6.1.0/gcc-4.9.2.tar.gz"
gcc_tar_link="https://mirrors.kernel.org/gnu/gcc/gcc-6.1.0/gcc-6.1.0.tar.gz"
newlib_tar_link="ftp://sourceware.org/pub/newlib/newlib-2.4.0.tar.gz"
##-------------------------------------------------------------
## Function definitions
##-------------------------------------------------------------
function announce {
now=$(date +"%T")
echo
echo "[$now] $1"
echo
}
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
# download_and_extract (URL, tarball_name, target_dir)
download_and_extract () {
local tarball="${1##*/}"
# Eat up strings after 2 dots i.e. .tar.gz
local tarball_dir="${tarball%.*}"
local tarball_dir="${tarball_dir%.*}"
# Clean up older downloads
rm -rf $tarball_dir
rm -rf $2
# download
wget -nc $1
# extract
tar xf $tarball
mv $tarball_dir $2
}
##-------------------------------------------------------------
## Script main
##-------------------------------------------------------------
install_dir="${root_dir}/install"
PATH=${PATH}:$install_dir/bin
PATH=${PATH}:$install_dir
announce "Moving to root=$root_dir"
cd $root_dir
mkdir -p $install_dir
if [ "$clean_install" = "true" ]; then
pushd $install_dir
rm -rf ./*
popd
fi
#libs
#-------------------
announce "Working on libs..."
libs_dir="$root_dir/libs"
mkdir -p $libs_dir
pushd $libs_dir
# download source
download_and_extract $gmp_tar_link gmp
download_and_extract $mpfr_tar_link mpfr
download_and_extract $mpc_tar_link mpc
download_and_extract $isl_tar_link isl
popd # back to root dir
announce "Working on libs... Complete!"
if [ "$build_binutils" = "true" ]; then
#binutils
#-------------------
announce "Working on binutils..."
binutils_dir="$root_dir/binutils"
binutils_src_dir="$binutils_dir/binutils-src"
binutils_build_dir="$binutils_dir/build-$target"
mkdir -p $binutils_dir
pushd $binutils_dir
# download source
#git clone git://sourceware.org/git/binutils-gdb.git
download_and_extract $binutils_tar_link binutils-src
# link any required libraries
ln -s $libs_dir/isl $binutils_src_dir/isl
ln -s $libs_dir/gmp $binutils_src_dir/gmp
mkdir -p $binutils_build_dir
pushd $binutils_build_dir
# perform cleanup
pushd $binutils_src_dir
# make distclean > /dev/null
popd
# rm -rf ./*
# build and install
$binutils_src_dir/configure --prefix=$install_dir --target=$target > log-configure-$target.log
make -j2 > log-make-$target.log
make install > log-install-$target.log
popd
popd # back to root dir
tree $install_dir > $install_dir/post-binutils-install.tree
announce "Working on binutils... Complete!"
fi
if [ "$build_gcc_static" = "true" ]; then
#gcc-static
#-------------------
announce "Working on gcc-static..."
gcc_dir="$root_dir/gcc"
gcc_src_dir="$gcc_dir/gcc-src"
gcc_build_dir="$gcc_dir/build-$target"
mkdir -p $gcc_dir
pushd $gcc_dir
# download source
download_and_extract $gcc_tar_link gcc-src
# link any required libraries
ln -s $libs_dir/isl $gcc_src_dir/isl
ln -s $libs_dir/gmp $gcc_src_dir/gmp
ln -s $libs_dir/mpfr $gcc_src_dir/mpfr
ln -s $libs_dir/mpc $gcc_src_dir/mpc
mkdir -p $gcc_build_dir
pushd $gcc_build_dir
# perform cleanup
# rm -rf ./*
# build and install
announce "gcc-static configure"
CXXFLAGS="-fpermissive" $gcc_src_dir/configure --prefix=$install_dir --target=$target --enable-languages=c --disable-bootstrap --with-newlib > log-configure-$target.log
announce "gcc-static make"
make -j2 all-gcc > log-make-$target.log
announce "gcc-static install"
make install-gcc > log-install-$target.log
tree $install_dir > $install_dir/post-gcc-static-install.tree
announce "gcc-static-libgcc make"
make -j2 -k all-target-libgcc
announce "gcc-static-libgcc install"
make -i install-target-libgcc
popd
popd # back to root dir
tree $install_dir > $install_dir/post-gcc-static-libgcc-install.tree
announce "Working on gcc-static... Complete!"
fi
if [ "$build_newlib" = "true" ]; then
#newlib
#-------------------
announce "Working on newlib..."
newlib_dir="$root_dir/newlib"
newlib_src_dir="$newlib_dir/newlib-src"
newlib_build_dir="$newlib_dir/build-$target"
mkdir -p $newlib_dir
pushd $newlib_dir
# download source
download_and_extract $newlib_tar_link newlib-src
mkdir -p $newlib_build_dir
pushd $newlib_build_dir
# perform cleanup
#rm -rf ./*
# build and install
announce "newlib configure"
$newlib_src_dir/configure --prefix=$install_dir --target=$target > log-configure-$target.log
announce "newlib make"
make -j2 all > log-make-$target.log
announce "newlib install"
make install > log-install-$target.log
popd
popd # back to root dir
tree $install_dir > $install_dir/post-newlib-install.tree
announce "Working on newlib... Complete!"
fi
if [ "$build_gcc" = "true" ]; then
#gcc
#-------------------
announce "Working on gcc..."
gcc_dir="$root_dir/gcc"
gcc_src_dir="$gcc_dir/gcc-src"
gcc_build_dir="$gcc_dir/build-$target"
pushd $gcc_build_dir
# build and install
announce "gcc configure"
CXXFLAGS="-fpermissive" $gcc_src_dir/configure --prefix=$install_dir --target=$target --enable-languages=c,c++ --disable-bootstrap --with-newlib > log-configure-$target.log
announce "gcc make"
make -j2 all > log-make-$target.log
announce "gcc install"
make install > log-install-$target.log
popd # back to root dir
tree $install_dir > $install_dir/post-gcc-install.tree
announce "Working on gcc... Complete!"
fi
announce "Cross toolchain ready!"