Installing scratchbox 1 and 2 for ARM cross-compilation

Hi,

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.

Intro

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

And then:

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:
glib_cv_stack_grows=no

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:
glib_cv_stack_grows=no
glib_cv_uscore=no
ac_cv_func_posix_getgrgid_r=yes
ac_cv_func_posix_getpwuid_r=yes

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:

install

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

Extract them:
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/run_me_first.sh
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

target

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.

start

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
/opt/scratchbox/sbin/sbox_ctl start

And then as user:
/opt/scratchbox/login

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 2

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.

QEMU

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
cd qemu
git checkout -b stable v0.10.5
./configure --prefix=/opt/qemu --target-list=arm-linux-user
make install

sbox2

Compile and install like this:
git clone git://anongit.freedesktop.org/git/sbox2
cd sbox2
./configure --prefix=/opt/sb2
make install

Add sb2 to the PATH:
export PATH=/opt/sb2/bin:$PATH

target

Now it's time to configure a target, I have a CodeSourcery toolchain installed on /opt/arm-2008q3, so:
cd /opt/arm-2008q3/arm-none-linux-gnueabi/libc/
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?

22 Responses to Installing scratchbox 1 and 2 for ARM cross-compilation

  1. Pingback: jprieur's status on Sunday, 07-Jun-09 17:50:29 UTC - Identi.ca

  2. mibus says:

    What are you cross-compiling all the time then? :)

    I’ve done a little for Maemo (I have an N810) but am somewhat project-less ATM…

  3. doc says:

    Thanks for the very clear and concise write-up!

  4. FelipeC says:

    mibus: well, I work for Nokia in Maemo, so most of my day is spent cross-compiling :)

    I’ll make another blog post with the projects I’m working on.

  5. mv -f says:

    Many thanks for your useful explanations, on scratchbox and other topics.

    A small addition: to compile the version of sbox2 I just got from git, you’ll have to perform ./autogen.sh before running ./configure.

  6. Pingback: GStreamer development in embedded with sbox2 « Felipe Contreras

  7. Mohamed says:

    Hi all,
    I wanna to use the scratch box for compiling some libraries such as GTK+ for ARM9 and this cross compilation must be against gcc 4.3.2
    could any body help me to use the scratchbox to accomplish this task

    Thanks
    m.Hamed

  8. flaco says:

    Hi Felipe, I follow the steps for scratchbox2, but I get this ( http://dpaste.com/185446/ ) when I try to install scartchbox2 any ideas??

    Thanks.

  9. FelipeC says:

    @flaco that happens on recent distros, you need a newer sb2

    Check it out from the new location:
    http://maemo.gitorious.org/scratchbox2/

  10. flaco says:

    Thanks felipe, I have installed sb2.. but I got another issue..
    I’m trying to cross-compile your gst-player (on google code). so I start a sb2 session, export PKG_CONFIG_PATH (on karmic is in /usr/lib/pkgconfig/) but that part is not working, I list the packages and I only get a few..

    any ideas?

    Gracias!

  11. FelipeC says:

    @flaco /usr/lib/pkgconfig/ is for the local system, you need to specify the one for your cross-compiling: like /opt/gst.

  12. ahmad says:

    Thanks.
    I’ve been struggling with compile error until I found this article.

    Regards,

  13. None says:

    Sadly, git is down :(
    $ git config –global http.proxy http://myproxy:8080
    $ git clone git://anongit.freedesktop.org/git/sbox2
    Initialized empty Git repository in /home/sl/sbox2/.git/
    anongit.freedesktop.org[0: 131.252.210.176]: errno=Connection timed out
    fatal: unable to connect a socket (Connection timed out)

    (note that the proxy works because I can use wget and apt-get with it)

  14. None says:

    Erm, this’ll never work:

    root@AMS-aa:~# ls -l /proc/sys/vm/vdso_enabled
    ls: cannot access /proc/sys/vm/vdso_enabled: No such file or directory
    root@AMS-aa:~# find /proc|grep vdso
    root@AMS-aaL:~#

  15. FelipeC says:

    FTR, scratchbox 2 has moved to:
    git clone git://gitorious.org/scratchbox2/scratchbox2.git

    And for vdso you need a kernel with CONFIG_COMPAT_VDSO=y.

  16. Maxim says:

    What’s the point to use this Scratchbox? I use OpenEmbedded and happy with it!

  17. FelipeC says:

    OpenEmbedded needs recipes for every package, you cannot just plug any package without a recipe and expect it to work.

    Moreover, it’s not good for development because you cannot recompile a certain package you are working on. At least that was the case the last time I tried.

  18. rajeevns says:

    Thanks a lot for your article, any one of you please help me in configuring git to access proxy server config.

  19. FelipeC says:

    @rajeevns Through which protocol? git? http? ssh?

  20. tkcast says:

    Hi Felipe,

    i’m trying to install scratchbox to compile for my nokia n900 maemo. I wasn’t able to install it neither on my ubuntu maverick desktop neither in my ubuntu natty laptop.

    when i try to run your script or manually, i receive this error:

    /tools/bin/sb-conf

    You dont have active target in scratchbox chroot.
    Please create one by running “sb-menu” before continuing

    and even if i try to run sb-menu, i receive the same error!

    what should i do?
    thanks!

  21. Justin says:

    Hey I’m trying to configure sbox2 and I am getting the following error:

    ./configure: No such file or directory

    I got the scratchbox 2 from

    git://gitorious.org/scratchbox2/scratchbox2.git

    Any help would be much appreciated… After this I need to compile gst-openmax for arm-angstrom-linux-gnueabi :)

  22. Justin says:

    It appears I just needed to autogen… May still need help in future with installation of gst-openmax. I have been trying to install it (without scratchbox 2) for a month now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers