Differences between revisions 9 and 51 (spanning 42 versions)
Revision 9 as of 2006-12-11 15:24:51
Size: 4460
Editor: ?JohnReiser
Comment:
Revision 51 as of 2016-01-27 16:08:17
Size: 4251
Editor: ?Lemmata
Comment: fixed errors in example code where target and host arch were accidentally swapped
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
(Crosstoolchain documentation is now collected and indexed on CrossToolchains)
Line 3: Line 5:
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. Very few people need to build a cross-toolchain. It is a complicated business and unless you are doing something really quite obscure you should just install a suitable apt-able toolchain. Installation info is on CrossToolchains.
Line 5: Line 7:
Commands you need to run as root assume you have already set up sudo on your system. We'll say that again: '''MOST PEOPLE SHOULDN'T BE BUILDING THEIR OWN CROSS-TOOLCHAIN - THEY SHOULD JUST INSTALL A PRE-BUILT ONE LIKE ANY OTHER PACKAGE'''.
Line 7: Line 9:
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). But there are reasons why you might need to do this.
Line 9: Line 11:
== Build a cross-compilation toolchain == = I really do want to build a cross compiler =
Line 11: Line 13:
=== Create a working directory === Pick the most relevant heading from the list below.

== Nomenclature ==

TARGET is the architecture the compiler builds code for.
HOST (same as BUILD in this case) is the architecture the compiler runs on (and is currently being built on)

== I want a combination of HOST x TARGET not in Debian (on jessie or later) ==

=== TARGET is a debian release architecture ===

If TARGET is a debian release architecture then this is fairly straightforward:

 * Install the cross-gcc-dev binary package
Line 13: Line 28:
mkdir -p ~/src/cross-toolchain
cd ~/src/cross-toolchain
apt-get install cross-gcc-dev
}}}
This is available from jessie (Debian 8) onwards

 * Generate a cross-gcc-<gccver>-<arch> source package
{{{
TARGET_LIST=<targetarch> HOST_LIST=<hostarch> cross-gcc-gensource <gccver>
}}}
Fill in your own <targetarch>, <hostarch> and <gccver> above. (e.g. {{{TARGET_LIST="armel armhf" HOST_LIST="amd64" cross-gcc-gensource 4.9}}})
In jessie only '4.9' is supported as a gcc version.

This will generate (in a directory called cross-gcc-packages-<buildarch>) a cross-gcc-<gccver>-<arch> source package for each arch in the TARGET_LIST.

'''Note''': in jessie cross-gcc-gensource will fail if dash is your default shell. (DebianBug:#780583). Either set the /bin/sh link to bash {{{sudo ln -sf bash /bin/sh}}} or add
{{{SHELL := bash}}} near the start of /usr/share/cross-gcc/template/rules.generic
(fixed in cross-gcc 17)

 * Build it
{{{
cd cross-gcc-<gccver>-<arch>
sbuild -d jessie
}}}
You will need sbuild 0.64.3 or later installed, and a suitable chroot or build env for the target suite. You will also need a <triplet>-binutils package.

=== Building corresponding binutils and gcc-metapackages ===

If the cross-binutils you need is not available on your arch, build one (or more) like this:
{{{
apt-get source cross-binutils
cd cross-binutils-0.23
sudo apt-get build-dep cross-binutils
TARGET_LIST="arm64" HOST_LIST="armhf" debian/rules control
TARGET_LIST="arm64" HOST_LIST="armhf" dpkg-buildpackage
Line 17: Line 63:
=== 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.
To make your toolchain convenient to use you need a set of metapackages which provide links from tools to versioned tools (so gcc-arm-linux-gnueabihf links to gcc-4.9-arm-linux-gnueabihf etc). The cross-gcc-defaults package provides those:
Line 21: Line 65:
export -n ARCH=arm # set and do not export to invoked commands apt-get source cross-gcc-defaults
cd cross-gcc-defaults-0.7
TARGET_LIST='armhf' debian/generate-pkgfiles.pl
dpkg-buildpackage
Line 23: Line 70:
cross-gcc-defaults is available in the jessie external toolchain repository and in unstable.
Line 24: Line 72:
=== Get the source for binutils and GCC === A more manual build of cross-gcc, probably best done in a chroot, could be
Line 26: Line 74:
apt-get source binutils gcc-4.1 cd packages/cross-gcc-4.9-<hostarch>
dpkg --add-architecture <hostarch>
apt-get update
[install build-deps]
dpkg-buildpackage
Line 28: Line 80:
(This essentially automates the procedure described on MultiarchCrossToolchainBuild).
Line 29: Line 82:
=== Install Build-Depends ===
{{{
sudo apt-get build-dep binutils gcc-4.1
}}}
=== TARGET is not a debian release architecture ===
Line 34: Line 84:
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 ...". If TARGET is not a debian release architecture (debian-ports architecture or a new arch), then it may be much harder because you need to do a full kernel/libc/gcc bootstrap. The cross-toolchain-base package (in unstable) can do this for some architectures. The rebootstrap tool can do it for any architecture.
Line 36: Line 86:
=== Install fakeroot ===
fakeroot allows you to build packages without being root.
{{{
sudo apt-get install fakeroot
}}}
Line 42: Line 87:
=== 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
}}}
=== I want the very latest arm support ===
Line 48: Line 89:
=== 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
}}}
Linaro's cross-toolchains (tarball install, sadly) are very up to date with patches and support, and may well be useful. But they are supplied as a binary tarball, not as debian packages.
Line 57: Line 91:
=== 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
}}}
=== I want toolchain packages for wheezy ===
Line 67: Line 93:
=== 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
}}}
The packages on emdebian.org aimed at wheezy have been uninstallable for a long time.
Install buildcross from experimental and use that to build your own.
Line 79: Line 96:
== 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!
}}}
<some instructions here would be helpful>

(Crosstoolchain documentation is now collected and indexed on CrossToolchains)

Building a Cross Compiler

Very few people need to build a cross-toolchain. It is a complicated business and unless you are doing something really quite obscure you should just install a suitable apt-able toolchain. Installation info is on CrossToolchains.

We'll say that again: MOST PEOPLE SHOULDN'T BE BUILDING THEIR OWN CROSS-TOOLCHAIN - THEY SHOULD JUST INSTALL A PRE-BUILT ONE LIKE ANY OTHER PACKAGE.

But there are reasons why you might need to do this.

I really do want to build a cross compiler

Pick the most relevant heading from the list below.

Nomenclature

TARGET is the architecture the compiler builds code for. HOST (same as BUILD in this case) is the architecture the compiler runs on (and is currently being built on)

I want a combination of HOST x TARGET not in Debian (on jessie or later)

TARGET is a debian release architecture

If TARGET is a debian release architecture then this is fairly straightforward:

  • Install the cross-gcc-dev binary package

apt-get install cross-gcc-dev

This is available from jessie (Debian 8) onwards

  • Generate a cross-gcc-<gccver>-<arch> source package

TARGET_LIST=<targetarch> HOST_LIST=<hostarch> cross-gcc-gensource <gccver>

Fill in your own <targetarch>, <hostarch> and <gccver> above. (e.g. TARGET_LIST="armel armhf" HOST_LIST="amd64" cross-gcc-gensource 4.9) In jessie only '4.9' is supported as a gcc version.

This will generate (in a directory called cross-gcc-packages-<buildarch>) a cross-gcc-<gccver>-<arch> source package for each arch in the TARGET_LIST.

Note: in jessie cross-gcc-gensource will fail if dash is your default shell. (). Either set the /bin/sh link to bash sudo ln -sf bash /bin/sh or add SHELL := bash near the start of /usr/share/cross-gcc/template/rules.generic (fixed in cross-gcc 17)

  • Build it

cd cross-gcc-<gccver>-<arch>
sbuild -d jessie 

You will need sbuild 0.64.3 or later installed, and a suitable chroot or build env for the target suite. You will also need a <triplet>-binutils package.

Building corresponding binutils and gcc-metapackages

If the cross-binutils you need is not available on your arch, build one (or more) like this:

apt-get source cross-binutils
cd cross-binutils-0.23
sudo apt-get build-dep cross-binutils
TARGET_LIST="arm64" HOST_LIST="armhf" debian/rules control
TARGET_LIST="arm64" HOST_LIST="armhf" dpkg-buildpackage

To make your toolchain convenient to use you need a set of metapackages which provide links from tools to versioned tools (so gcc-arm-linux-gnueabihf links to gcc-4.9-arm-linux-gnueabihf etc). The cross-gcc-defaults package provides those:

apt-get source cross-gcc-defaults
cd cross-gcc-defaults-0.7
TARGET_LIST='armhf' debian/generate-pkgfiles.pl
dpkg-buildpackage

cross-gcc-defaults is available in the jessie external toolchain repository and in unstable.

A more manual build of cross-gcc, probably best done in a chroot, could be

cd packages/cross-gcc-4.9-<hostarch>
dpkg --add-architecture <hostarch>
apt-get update
[install build-deps]
dpkg-buildpackage

(This essentially automates the procedure described on MultiarchCrossToolchainBuild).

TARGET is not a debian release architecture

If TARGET is not a debian release architecture (debian-ports architecture or a new arch), then it may be much harder because you need to do a full kernel/libc/gcc bootstrap. The cross-toolchain-base package (in unstable) can do this for some architectures. The rebootstrap tool can do it for any architecture.

I want the very latest arm support

Linaro's cross-toolchains (tarball install, sadly) are very up to date with patches and support, and may well be useful. But they are supplied as a binary tarball, not as debian packages.

I want toolchain packages for wheezy

The packages on emdebian.org aimed at wheezy have been uninstallable for a long time. Install buildcross from experimental and use that to build your own.

<some instructions here would be helpful>