Skip to content

Instantly share code, notes, and snippets.

@MTRNord
Forked from preshing/build_cross_gcc
Last active June 12, 2016 09:19
Show Gist options
  • Save MTRNord/d31217cdfe55b7133057ef47b4f9ffc6 to your computer and use it in GitHub Desktop.
Save MTRNord/d31217cdfe55b7133057ef47b4f9ffc6 to your computer and use it in GitHub Desktop.
A shell script to download packages for, configure, build and install a GCC cross-compiler.
YOU NEED TO HAVE INSTALLED THE RASPBERRY PI TOOLCHAIN!
https://www.raspberrypi.org/documentation/linux/kernel/building.md
RUN AS ROOT
#! /bin/bash
set -e
trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
trap 'echo FAILED COMMAND: $previous_command' EXIT
#-------------------------------------------------------------------------------------------
# This script will download packages for, configure, build and install a GCC cross-compiler.
# Customize the variables (INSTALL_PATH, TARGET, etc.) to your liking before running.
# If you get an error and need to resume the script from some point in the middle,
# just delete/comment the preceding lines before running it again.
#
# See: http://preshing.com/20141119/how-to-build-a-gcc-cross-compiler
#-------------------------------------------------------------------------------------------
INSTALL_PATH=/opt/cross
TARGET=arm-linux-gnueabihf
LINUX_ARCH=arm
CONFIGURATION_OPTIONS="--disable-multilib --disable-werror"
PARALLEL_MAKE=-j2
BINUTILS_VERSION=binutils-2.26
GCC_VERSION=gcc-5.4.0
GLIBC_VERSION=glibc-2.23
MPFR_VERSION=mpfr-2.4.2
GMP_VERSION=gmp-4.3.2
MPC_VERSION=mpc-0.8.1
ISL_VERSION=isl-0.14
CLOOG_VERSION=cloog-0.18.1
export PATH=$INSTALL_PATH/bin:$PATH
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
sudo apt-get install M4 texinfo autotools-dev automake libgmp-dev libmpfr-dev libmpc-dev libc6-dev
# Download packages
echo "Download packages!"
export http_proxy=$HTTP_PROXY https_proxy=$HTTP_PROXY ftp_proxy=$HTTP_PROXY
wget -nc https://ftp.gnu.org/gnu/binutils/$BINUTILS_VERSION.tar.gz
wget -nc https://ftp.gnu.org/gnu/gcc/$GCC_VERSION/$GCC_VERSION.tar.gz
git clone --depth=1 https://github.com/raspberrypi/linux
wget -nc https://ftp.gnu.org/gnu/glibc/$GLIBC_VERSION.tar.xz
wget -nc ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR_VERSION.tar.bz2
wget -nc ftp://gcc.gnu.org/pub/gcc/infrastructure/$GMP_VERSION.tar.bz2
wget -nc ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPC_VERSION.tar.gz
wget -nc ftp://gcc.gnu.org/pub/gcc/infrastructure/$ISL_VERSION.tar.bz2
wget -nc ftp://gcc.gnu.org/pub/gcc/infrastructure/$CLOOG_VERSION.tar.gz
sleep 15
# Extract everything
echo "Extract everything"
for f in *.tar*; do echo $f && tar xfk $f; done
sleep 15
# Make symbolic links
echo "Make symbolic links"
cd $GCC_VERSION
mkdir mpc
ln -sf `ls -1d ../mpfr-*/` mpfr
ln -sf `ls -1d ../gmp-*/` gmp
ln -sf `ls -1d ../mpc-*/` mpc
ln -sf `ls -1d ../isl-*/` isl
ln -sf `ls -1d ../cloog-*/` cloog
cd ..
sleep 15
# Step 1. Binutils
echo "Step 1. Binutils"
mkdir -p build-binutils
cd build-binutils
../$BINUTILS_VERSION/configure --prefix=$INSTALL_PATH --target=$TARGET $CONFIGURATION_OPTIONS
make $PARALLEL_MAKE
make install
cd ..
sleep 15
# Step 2. Linux Kernel Headers
echo "Step 2. Linux Kernel Headers"
cd linux
export KERNEL=kernel7
make ARCH=$LINUX_ARCH CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
make ARCH=$LINUX_ARCH CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_HDR_PATH=$INSTALL_PATH/$TARGET headers_install
cd ..
sleep 15
# Step 3. C/C++ Compilers
echo "Step 3. C/C++ Compilers"
mkdir -p build-gcc
cd build-gcc
../$GCC_VERSION/configure --prefix=$INSTALL_PATH --target=$TARGET --enable-languages=c,c++ $CONFIGURATION_OPTIONS
make $PARALLEL_MAKE all-gcc CFLAGS='-fbracket-depth=1024'
make install
cd ..
sleep 15
# Step 4. Standard C Library Headers and Startup Files
echo "Step 4. Standard C Library Headers and Startup Files"
mkdir -p build-glibc
cd build-glibc
../$GLIBC_VERSION/configure --prefix=$INSTALL_PATH/$TARGET --build=$MACHTYPE --host=$TARGET --target=$TARGET --with-headers=$INSTALL_PATH/$TARGET/include $CONFIGURATION_OPTIONS libc_cv_forced_unwind=yes
make install-bootstrap-headers=yes install-headers CFLAGS='-fbracket-depth=1024'
make $PARALLEL_MAKE csu/subdir_lib CFLAGS='-fbracket-depth=1024'
install csu/crt1.o csu/crti.o csu/crtn.o $INSTALL_PATH/$TARGET/lib
$TARGET-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o $INSTALL_PATH/$TARGET/lib/libc.so
touch $INSTALL_PATH/$TARGET/include/gnu/stubs.h
cd ..
sleep 15
# Step 5. Compiler Support Library
echo "Step 5. Compiler Support Library"
cd build-gcc
make $PARALLEL_MAKE all-target-libgcc CFLAGS='-fbracket-depth=1024'
make install-target-libgcc
cd ..
sleep 15
# Step 6. Standard C Library & the rest of Glibc
echo "Step 6. Standard C Library & the rest of Glibc"
cd build-glibc
make $PARALLEL_MAKE CFLAGS='-fbracket-depth=1024'
make install
cd ..
sleep 15
# Step 7. Standard C++ Library & the rest of GCC
echo "Step 7. Standard C++ Library & the rest of GCC"
cd build-gcc
make $PARALLEL_MAKE all CFLAGS='-fbracket-depth=1024'
make install
cd ..
trap - EXIT
echo 'Success!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment