summaryrefslogtreecommitdiff
path: root/DebianCrossCompilerHowto.mdwn
blob: bfebee0205f0b1dbb300ab160474a2d87e59bf15 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
[[!toc levels="3"]]

# Installing pre-built cross-compilation toolchain from Emdebian

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

Note: this HOWTO provides a procedure for building a cross-compilation toolchain by hand.  It might even still work.  However, you can now install pre-built cross-compiler packages from Emdebian, and you should do so if at all possible, rather than following the rest of this guide.  To use the Emdebian cross-compiler packages, add the following line to /etc/apt/sources.list:

    deb http://emdebian.org/debian/ stable main

Then, run the following commands:

    sudo apt-get install emdebian-archive-keyring
    sudo apt-get update
    sudo apt-get install gcc-4.4-powerpc-linux-gnu libc6-dev-powerpc-cross

(Change 4.4 to the current version of GCC available from Emdebian.)

This should work perfectly on Debian and on the most recent versions of Ubuntu.  Older versions of Ubuntu might need to manually install some libraries from newer versions; for instance, you might need to download and install [libmpfr4](http://packages.ubuntu.com/libmpfr4).

If these packages work for you, you can ignore the remainder of this guide.

# Debian Cross-Compiler HOWTO

This HOWTO will help you build Debian packages of a cross-compilation toolchain. 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.

As of this writing, the latest version of GCC in Debian is 4.0, so you will be building a cross-compiler from the gcc-4.0 package. Ignore the gcc package; it simply provides metapackages which depend on the standard version of GCC in Debian and provide unversioned symlinks (such as from gcc to gcc-4.0).

Specific version numbers for packages are listed as VERSION; substitute the version number of the latest package in unstable (or testing).

## Build a cross-compilation toolchain

### Create a working directory

This process will create many files and directories; you will want to work in a separate working directory to avoid cluttering your home directory.

    mkdir -p ~/src/cross-toolchain
    cd ~/src/cross-toolchain

### Get the source for binutils and GCC

In order to build a cross-compilation toolchain, you need to get the source for the Debian binutils and GCC packages. Note that you need binutils 2.16.1cvs20060413-1 or newer to build cross packages; if for some reason you want an older version, use or adapt one of the patches from [Debian bug 231707](http://bugs.debian.org/231707).

    apt-get source binutils gcc-4.0

### Install Build-Depends

GCC and binutils require several other packages in order to build; you can install all of them using "apt-get build-dep"; this will also install build-essential.

    sudo apt-get build-dep binutils gcc-4.0

Note that gcc-4.0 may have build-dependencies that apt-get can't figure out; this is not a problem, just make sure you install all the ones that apt-get understands. Unfortunately, apt-get build-dep may give an error if such a situation arises; in this case, take the list of packages apt-get provided before giving an error, and install them using "sudo aptitude install pkg1 pkg2 pkg3 ...".

### Install fakeroot

fakeroot allows you to build packages without being root.

    sudo aptitude install fakeroot

### Install dpkg-cross

dpkg-cross adds cross-compilation support to some of the basic Debian package tools. dpkg-cross can also convert packages designed for the target architecture into packages usable for cross-compilation to that architecture.

    sudo aptitude install dpkg-cross

### Build binutils

While still in the binutils directory:

    TARGET=powerpc-linux-gnu fakeroot debian/rules binary-cross > ../binutils.build 2>&1 || echo 'Build error'

### Install the new binutils package

The binutils package must be installed in order to build the GCC package.

    cd ..
    sudo dpkg -i binutils-powerpc-linux-gnu_VERSION_HOSTARCH.deb

### Convert library packages

You will need cross-compilation packages for various libraries; dpkg-cross can convert native packages for the target architecture into packages usable for cross-compilation.

    wget 'http://ftp.us.debian.org/debian/pool/main/g/glibc/libc6-dev_VERSION_powerpc.deb'
    wget 'http://ftp.us.debian.org/debian/pool/main/g/glibc/libc6_VERSION_powerpc.deb'
    wget 'http://ftp.us.debian.org/debian/pool/main/l/linux-kernel-headers/linux-kernel-headers_VERSION_powerpc.deb'
    dpkg-cross -a powerpc -b l*.deb
    sudo dpkg -i l*powerpc-cross*.deb

### Build GCC

Now that you have all the necessary prerequisites, you can build a cross-compiling GCC.

    cd gcc-VERSION
    export GCC_TARGET=powerpc
    debian/rules control
    dpkg-buildpackage -us -uc -rfakeroot -b > ../gcc.build 2>&1 || echo 'Build error'

### Install the new GCC packages

    cd ..
    sudo dpkg -i *-4.0-powerpc-linux-gnu*.deb *-powerpc-cross_4.0*.deb

## 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 aptitude install 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!


# Shell script for this process

[[cross-toolchain.sh]]