Differences between revisions 7 and 8
Revision 7 as of 2006-12-04 01:11:02
Size: 4405
Editor: ?RodWhitby
Comment: Generalised the GCC version on the dpkg install line.
Revision 8 as of 2006-12-11 15:14:49
Size: 4453
Editor: ?JohnReiser
Comment:
Deletions are marked like this. Additions are marked like this.
Line 21: Line 21:
ARCH=arm export -n ARCH=arm # set and do not export to subshells

Building a Cross Compiler

This document will help you build Debian packages of a cross-compilation toolchain. It is based on [http://psas.pdx.edu/DebianCrossCompilerHowto a HOWTO] by Josh Triplett, with his kind permission.

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

We will be building a cross-compiler from the gcc-4.1 package. 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

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

Some useful variables

These will be used in the commands in this document. Replace "arm" with the architecture for which you are building a cross-compiler.

export -n ARCH=arm  # set and do not export to subshells

Get the source for binutils and GCC

apt-get source binutils gcc-4.1

Install Build-Depends

sudo apt-get build-dep binutils gcc-4.1

Note that gcc-4.1 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 apt-get install pkg1 pkg2 pkg3 ...".

Install fakeroot

fakeroot allows you to build packages without being root.

sudo apt-get 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 apt-get install dpkg-cross

Build and install binutils

dpkg-source -x binutils_VERSION.dsc
cd binutils-VERSION
TARGET=$ARCH-linux-gnu fakeroot debian/rules binary-cross > ../binutils.build 2>&1 || echo 'Build error'
cd ..
sudo dpkg -i binutils-$ARCH-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. Note that the mirror ftp.us.debian.org only has amd64 and i386 packages, so to build a cross-compilation toolchain for another architecture (e.g. arm), you will have to use a mirror other than ftp.us.debian.org.

wget http://ftp.XX.debian.org/debian/pool/main/g/glibc/libc6-dev_VERSION_$ARCH.deb
wget http://ftp.XX.debian.org/debian/pool/main/g/glibc/libc6_VERSION_$ARCH.deb
wget http://ftp.XX.debian.org/debian/pool/main/l/linux-kernel-headers/linux-kernel-headers_VERSION_$ARCH.deb
dpkg-cross -a $ARCH -b l*.deb
sudo dpkg -i l*$ARCH-cross*.deb

Build and install GCC

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

dpkg-source -x gcc-VERSION.dsc
cd gcc-VERSION
export GCC_TARGET=$ARCH
debian/rules control
dpkg-buildpackage -us -uc -rfakeroot -b > ../gcc.build 2>&1 || echo 'Build error'
cd ..
sudo dpkg -i *-VERSION-$ARCH-linux-gnu*.deb *-$ARCH-cross_VERSION*.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.

$ARCH-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

for the PowerPC architecture.

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 apt-get install qemu

Run the test program with qemu

To run the test program with qemu, just prefix it with "qemu-$ARCH" (assuming of course that your architecture is supported by qemu).

qemu-$ARCH ./hello

You should see:

Hello cross-compiling world!