I’ve tried many different cross-compilation methods and so far scratchbox is the simplest and most effective. Here I’ll try to introduce the benefits of using it, and how to get started as simply as possible.
If you are not familiar with scratchbox; it’s a cross-compilation toolkit which allows you to use native tools (gcc, binutils, perl) when possible and emulate the target through qemu when needed.
It’s needed because of autotools; have you seen these checks?
checking whether the C compiler works... yes
The configure script actually compiles a small program and then runs it to validate it’s working, and sometimes extract information, such a the size of certain structures which might be different depending on the platform. If you tell ‘configure’ that you are cross-compiling it will go through a different path (which is much less tested) and ultimately will end up with wrong information that you need to correct.
OpenEmbedded and other distributions go through each and every package, make sure cross-compilation works, sometimes patching configure.ac, and often providing some information that normally would be obtained by running some test program.
This is an example of a typical GLib cross-compilation
./configure ---host=arm-linux --build=i386-linux
You’ll see something like:
checking whether the C compiler works... yes
checking whether we are cross compiling... yes
checking for growing stack pointer... configure: error: in `glib-2.20.3':
configure: error: cannot run test program while cross compiling
Of course, ‘configure’ has no way of knowing whether the stack grows on this particular platform. So you need to tell him yourself by creating an arm-linux.cache file like this:
And then running:
./configure --host=arm-linux --build=i386-linux --cache-file=arm-linux.cache
Of course that is not enough, you need to specify more:
And then the compilation fails because I don’t have glib-genmarshal in my system, and it’s needed by the build. Normally it’s compiled and run in the build, but now we can’t do that.
All these problems disappear with scratchbox.
Installing scratchbox 1
Many people think it’s difficult to install, but it’s not. Just follow these easy steps:
Download the basic packages:
wget -c http://scratchbox.org/download/files/sbox-releases/apophis/tarball/scratchbox-core-1.0.14-i386.tar.gz
wget -c http://scratchbox.org/download/files/sbox-releases/apophis/tarball/scratchbox-libs-1.0.14-i386.tar.gz
wget -c http://scratchbox.org/download/files/sbox-releases/apophis/tarball/scratchbox-devkit-qemu-0.10.0-0sb5-i386.tar.gz
wget -c http://scratchbox.org/download/files/sbox-releases/apophis/tarball/scratchbox-toolchain-cs2007q3-glibc2.5-arm7-1.0.12-9-i386.tar.gz
sudo tar -xf /tmp/sb/scratchbox-core-1.0.14-i386.tar.gz -C /opt
sudo tar -xf /tmp/sb/scratchbox-libs-1.0.14-i386.tar.gz -C /opt
sudo tar -xf /tmp/sb/scratchbox-devkit-qemu-0.10.0-0sb5-i386.tar.gz -C /opt
sudo tar -xf /tmp/sb/scratchbox-toolchain-cs2007q3-glibc2.5-arm7-1.0.12-9-i386.tar.gz -C /opt
Setup scratchbox, and add your user:
sudo /opt/scratchbox/sbin/sbox_adduser $USER yes
You'll need to re-login to be in the sbox group and have proper permissions:
sudo su $USER
Finally, setup an armv7 target (you can have multiple targets inside scratchbox):
/opt/scratchbox/tools/bin/sb-conf setup armv7 --force --compiler="cs2007q3-glibc2.5-arm7" --devkits="qemu" --cputransp="qemu-arm-sb"
/opt/scratchbox/tools/bin/sb-conf select armv7
/opt/scratchbox/tools/bin/sb-conf install armv7 --clibrary --devkits --fakeroot --etc
That's it, you have scratchbox setup :) I explicitly mentioned all the commands, but instead you can run this script that I wrote.
Before running scratchbox you'll need to do some steps as root:
echo 0 > /proc/sys/vm/vdso_enabled
echo 4096 > /proc/sys/vm/mmap_min_addr
And then as user:
This will get you to this screen:
Welcome to Scratchbox, the cross-compilation toolkit!
Use 'sb-menu' to change your compilation target.
See /scratchbox/doc/ for documentation.
[sbox-armv7: ~] >
Now if you want to cross-compile GLib, you do it as in your PC:
./configure && make install
Much easier, now scratchbox does all the magic ;)
Scratchbox 1 serves it's purpose, but there are many corner-cases where things get overly complicated so people came up with a much more elegant approach: Scratchbox 2.
In sb1 you need to login to a target (e.g. armv7, armv6, fremantle, diablo, etc.) in order to do anything, you can use only one target at a time, and each target is independent, in order to share tools between targets you need a devkit. Also, toolchains must be packaged in a special way.
In sb2, you don't login, you can setup any toolchain easily, you can use multiple targets at the same time, and you can configure it to do pretty much anything you want.
sb2 doesn't include QEMU, you must have it already, this is how I compile it:
git clone git://git.savannah.nongnu.org/qemu.git
git checkout -b stable v0.10.5
./configure --prefix=/opt/qemu --target-list=arm-linux-user
Compile and install like this:
git clone git://anongit.freedesktop.org/git/sbox2
Add sb2 to the PATH:
Now it's time to configure a target, I have a CodeSourcery toolchain installed on /opt/arm-2008q3, so:
sb2-init -c /opt/qemu/bin/qemu-arm armv7 /opt/arm-2008q3/bin/arm-none-linux-gnueabi-gcc
You don't need to log-in, just prefix your commands with sb2 to do the magic:
sb2 ./configure --prefix=/opt/arm/
If you want to use a different target just use the -t option:
sb2 -t armv8 ./configure --prefix=/opt/arm/
How cool is that?