wiki/ GentooCrossCompilerHowto
  1. Gentoo Cross-Compiler HOWTO
    1. Build a cross-compilation toolchain
    2. Test the cross-compile environment
      1. Test compilation
      2. Install qemu
      3. Run the test program with qemu

Gentoo Cross-Compiler HOWTO

This HOWTO will help you setup a cross-compilation toolchain on Gentoo Linux. The example commands assume the PowerPC architecture, since the PSAS flight computer uses PowerPC; however, the same procedure should work to build a cross-compilation toolchain for any supported architecture.

Commands you need to run as root assume you have already set up sudo on your system.

Build a cross-compilation toolchain

Gentoo provides a utility named crossdev for installing and managing cross-compilation toolchains. To install crossdev run the following command:

sudo emerge crossdev

Now that crossdev is installed we can create the toolchain with the following command:

sudo crossdev -t powerpc-linux-gnu

Be patient this may take a while, as crossdev will compile and install cross-compiler enabled versions of binutils, C compiler, kernel-headers, C library and C++ compiler.

Note: crossdev creates dummy-portage packages which point to your system toolchain packages along with cross-compilation options in /etc/portage/. So when you update your system and a newer version of a toolchain package is emerged, the corresponding cross-compilation toolchain package will also be updated. Basically, you don't have to rerun crossdev everytime a newer version of gcc or glibc becomes available.

Crossdev provides the additional command line options:

# crossdev -h
Options:
    --b, --binutils ver   Specify version of binutils to use
    --g, --gcc ver        Specify version of gcc to use
    --k, --kernel ver     Specify version of kernel headers to use
    --l, --libc ver       Specify version of libc to use
    -C, --clean target    Uninstall specified target
    -b, -d, -p, -v, -q    Options to pass to emerge (see emerge(1))
Stage Options:
    -s0, --stage0         Build just binutils
    -s1, --stage1         Also build a C compiler (no libc/C++)
    -s2, --stage2         Also build kernel headers
    -s3, --stage3         Also build the C library (no C++)
    -s4, --stage4         Also build a C++ compiler [default]
Extra Fun (must be run after above stages):
    --ex-only             Skip the stage steps above
    --ex-gcc              Build extra gcc targets (gcj/ada/etc...)
    --ex-gdb              Build a cross gdb
Target (-t):   takes the form: ARCH-VENDOR-OS-LIBC
    Run 'crossdev -t help' for examples

Crossdev also provides the following target specific options:

# crossdev -t help
Supported Architectures:
   - alpha                                     - arm / armeb
   - hppa (parisc)                             - ia64
   - i386 / i486 / i586 / i686 (x86)           - m68k
   - mips / mipsel / mips64 / mips64el
   - powerpc (ppc) / powerpc64 (ppc64)
   - sparc / sparc64                           - s390 / s390x
   - sh / sh[1-5] / sh64                       - x86_64 (amd64)
Supported C Libraries:
   - gnu (glibc)
   - klibc       [prob wont work]
   - uclibc      [not all arches are ported]
Special Targets:
   - avr      http://www.nongnu.org/avr-libc/
   - bfin     http://blackfin.uclinux.org/
   - mingw32  http://www.mingw.org/
   - msp430   http://mspgcc.sourceforge.net/
   - nios2    http://www.altera.com/products/ip/processors/nios2/ni2-index.html
   - ee / iop / dvp (ps2) [Playstation 2 targets]
Softfloat toolchains:
   Set the 'vendor' field to 'softfloat'
   e.g. armeb-softfloat-linux-uclibc
        powerpc-softfloat-linux-gnu

Test the cross-compile environment

Test compilation

Create a file "hello.c", containing the following code:

#include <stdio.h>
int main()
{
    printf("Hello cross-compiling world!\n");
    return 0;
}

Compile it statically with the new cross-compiler.

powerpc-linux-gnu-gcc -static hello.c -o hello

Check the binary's type with "file".

file hello

You should see something like:

hello: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1 (SYSV), for GNU/Linux 2.2.0, statically linked, not stripped

Install qemu

qemu is an emulator for various architectures. It supports both whole-system emulation as well as single-program emulation with system-call conversion.

sudo emerge qemu

Run the test program with qemu

To run the test program with qemu, just prefix it with "qemu-ppc" (or the appropriate version of qemu for your target architecture).

qemu-ppc ./hello

You should see:

Hello cross-compiling world!