summaryrefslogtreecommitdiff
path: root/GentooCrossCompilerHowto.mdwn
blob: 0b8b29c9f4adf86438adc32d18b1eb2e827052fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
[[toc levels="3"]]

# 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|SudoSetup]] 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!