Skip to content

Building a Cross Compiler

Peter Cannici edited this page Oct 19, 2016 · 3 revisions

Note: This document corresponds to docs/cross-compile.md in the repo, and may or may not be up-to-date with it.

Prerequisites

  • Standard UNIX utilities like wget/curl and tar
  • An existing working GCC installation
  • GNU Make
  • Bison, Flex, and Texinfo; packages vary depending on OS and distro.

Note: It might be a good idea to check that your GCC version is up-to-date. Having matching versions of GCC and your cross-compiler will minimize any potential problems.

Setup Directories

Decide where you're going to put your cross-compiler, e.g. ~/toolchain/i586-elf. This will be your $PREFIX for installing GCC and binutils. Set your $TARGET variable to i586-elf and make some directories to hold build output:

export TARGET=i586-elf
mkdir ~/toolchain && mkdir ~/toolchain/$TARGET && cd ~/toolchain/$TARGET
mkdir build && mkdir build/binutils && mkdir build/gcc && mkdir build/gmp && mkdir build/mpc && mkdir build/mpfr

Now point $PREFIX at your toolchain directory:

export PREFIX=~/toolchain/$TARGET

Download and Unpack Sources

A working GNU cross-compiler toolchain needs 5 packages:

You'll want the latest versions of each, unless your system GCC is severely out of date. Download and unpack the tarballs:

mkdir ~/toolchain/src && cd ~/toolchain/src
curl -L ftp://ftp.gnu.org/gnu/binutils/binutils-VERSION.tar.bz2 | tar -xf -
curl -L ftp://ftp.gnu.org/gnu/gcc/gcc-VERSION/gcc-VERION.tar.bz2 | tar -xf -
curl -L ftp://ftp.gnu.org/gnu/gmp/gmp-VERSION.tar.bz2 | tar -xf -
curl -L ftp://ftp.gnu.org/gnu/mpc/mpc-VERSION.tar.gz | tar -xf -
curl -L ftp://ftp.gnu.org/gnu/mpfr/mpfr-VERSION.tar.bz2 | tar -xf -

In each command, replace VERSION with the latest version of the package.

Note: You don't need to put these in your $TARGET directory. You can build multiple cross-compilers from the same source.

You might want to export $SRC to save some typing below:

export SRC=~/toolchain/src

Build!

At this point you just have to configure-make-install and wait for a while. Be sure to build and install GMP, MPC, and MPFR before building GCC.

Note: You don't have to run make install as root since your $PREFIX is writable by you.

Non-Linux users: use gmake instead of make for the commands below.

Tip: Use -jN in the make commands below to use N CPU cores. Usually N should be the number of physical cores in your machine ± 1.

Binutils

cd ~/toolchain/$TARGET/build/binutils
$SRC/binutils-VERSION/configure --target=$TARGET --prefix=$PREFIX --disable-nls --with-sysroot
make all
make install

GMP

cd ~/toolchain/$TARGET/build/gmp
$SRC/gmp-VERSION/configure --prefix=$PREFIX
make
make install

MPC

cd ~/toolchain/$TARGET/build/mpc
$SRC/mpc-VERSION/configure --prefix=$PREFIX
make
make install

MPFR

cd ~/toolchain/$TARGET/build/mpfr
$SRC/mpfr-VERSION/configure --prefix=$PREFIX
make
make install

GCC

Now that we've got everything else ready, time to build GCC. This could take quite a while depending on your hardware. Don't forget to use -j to speed things up (I forgot to do this…).

GCC has a lot of options. Run $SRC/gcc-VERSION/configure --help to get an idea for what you can do.

cd ~/toolchain/$TARGET/build/gcc
$SRC/gcc-VERSION/configure --target=$TARGET --target=$TARGET --prefix=$PREFIX \
  --disable-nls --enable-languages=c,c++ --without-headers \
  --with-gmp=$PREFIX --with-mpc=$PREFIX --with-mpfr=$PREFIX

make all-gcc
make all-target-libgcc
make install-gcc
make install-target-libgcc

Next: Building Zambesii

If all went well, ~$PREFIX/bin/$TARGET-gcc --version should print out some info about your cross-compiler.

Now you can move on to building the Zambesii kernel.