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?
Pingback: jprieur's status on Sunday, 07-Jun-09 17:50:29 UTC - Identi.ca
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…
LikeLike
Thanks for the very clear and concise write-up!
LikeLike
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.
LikeLike
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.
LikeLike
Pingback: GStreamer development in embedded with sbox2 « Felipe Contreras
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
LikeLike
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.
LikeLike
@flaco that happens on recent distros, you need a newer sb2
Check it out from the new location:
http://maemo.gitorious.org/scratchbox2/
LikeLike
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!
LikeLike
@flaco /usr/lib/pkgconfig/ is for the local system, you need to specify the one for your cross-compiling: like /opt/gst.
LikeLike
Thanks.
I’ve been struggling with compile error until I found this article.
Regards,
LikeLike
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)
LikeLike
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:~#
LikeLike
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.
LikeLike
What’s the point to use this Scratchbox? I use OpenEmbedded and happy with it!
LikeLike
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.
LikeLike
Thanks a lot for your article, any one of you please help me in configuring git to access proxy server config.
LikeLike
@rajeevns Through which protocol? git? http? ssh?
LikeLike
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!
LikeLike
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 🙂
LikeLike
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.
LikeLike
Pingback: Confluence: Gruppo Sviluppo
hi
I get sbox2 from your link
generate configure with autogen.sh
then configure but when make it give me error like this:
————————————————————————————————————————-
/home/hiva/project/mini2440/sbox2/preload/exported.h:510:12: error: conflicting types for ‘scandir’
/usr/include/dirent.h:256:12: note: previous declaration of ‘scandir’ was here
/home/hiva/project/mini2440/sbox2/preload/exported.h:526:12: error: conflicting types for ‘scandir64’
/usr/include/dirent.h:279:12: note: previous declaration of ‘scandir64’ was here
luaif/sb_log.c: In function ‘write_to_logfile’:
luaif/sb_log.c:116:4: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
make[1]: *** [luaif/sb_log.o] Error 1
make[1]: Leaving directory `/home/hiva/project/mini2440/sbox2′
make: *** [regular] Error 2
——————————————————————————————————-
what i must do?
what is version of sbox2?
LikeLike
i tried compiling glib 2.2.3 in scratchbox. i can configure it, howevr i am gettin the following when i make it, Please suggest what should i do…
make all-recursive
make[1]: Entering directory `/home/ashwini/glib-2.2.3′
Making all in .
make[2]: Entering directory `/home/ashwini/glib-2.2.3′
make[2]: Leaving directory `/home/ashwini/glib-2.2.3′
Making all in m4macros
make[2]: Entering directory `/home/ashwini/glib-2.2.3/m4macros’
make[2]: Nothing to be done for `all’.
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/m4macros’
Making all in glib
make[2]: Entering directory `/home/ashwini/glib-2.2.3/glib’
Making all in libcharset
make[3]: Entering directory `/home/ashwini/glib-2.2.3/glib/libcharset’
make[3]: Nothing to be done for `all’.
make[3]: Leaving directory `/home/ashwini/glib-2.2.3/glib/libcharset’
make[3]: Entering directory `/home/ashwini/glib-2.2.3/glib’
make[3]: Nothing to be done for `all-am’.
make[3]: Leaving directory `/home/ashwini/glib-2.2.3/glib’
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/glib’
Making all in gobject
make[2]: Entering directory `/home/ashwini/glib-2.2.3/gobject’
make[2]: Nothing to be done for `all’.
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/gobject’
Making all in gmodule
make[2]: Entering directory `/home/ashwini/glib-2.2.3/gmodule’
/bin/sh ../libtool –mode=compile gcc -DHAVE_CONFIG_H -I. -I. -I.. -I.. -I../glib -I../gmodule -DG_LOG_DOMAIN=\”GModule\” -DG_DISABLE_CAST_CHECKS -DG_DISABLE_DEPRECATED -pthread -g -O2 -Wall -c gmodule.c
gcc -DHAVE_CONFIG_H -I. -I. -I.. -I.. -I../glib -I../gmodule -DG_LOG_DOMAIN=\”GModule\” -DG_DISABLE_CAST_CHECKS -DG_DISABLE_DEPRECATED -pthread -g -O2 -Wall -c gmodule.c -o gmodule.o
/bin/sh ../libtool –mode=link gcc -g -O2 -Wall -o libgmodule-2.0.la -rpath /usr/local/lib -Wl,–export-dynamic -version-info 200:3:200 -export-dynamic gmodule.lo -ldl ../glib/libglib-2.0.la
rm -fr .libs/libgmodule-2.0.la .libs/libgmodule-2.0.lai .libs/libgmodule-2.0.so .libs/libgmodule-2.0.so.0 .libs/libgmodule-2.0.so.0.200.3 .libs/libgmodule-2.0.so.0.200.3T
ar cru .libs/libgmodule-2.0.a gmodule.o
ranlib .libs/libgmodule-2.0.a
creating libgmodule-2.0.la
(cd .libs && rm -f libgmodule-2.0.la && ln -s ../libgmodule-2.0.la libgmodule-2.0.la)
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/gmodule’
Making all in gthread
make[2]: Entering directory `/home/ashwini/glib-2.2.3/gthread’
make[2]: Nothing to be done for `all’.
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/gthread’
Making all in tests
make[2]: Entering directory `/home/ashwini/glib-2.2.3/tests’
make[2]: Nothing to be done for `all’.
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/tests’
Making all in build
make[2]: Entering directory `/home/ashwini/glib-2.2.3/build’
Making all in win32
make[3]: Entering directory `/home/ashwini/glib-2.2.3/build/win32′
Making all in dirent
make[4]: Entering directory `/home/ashwini/glib-2.2.3/build/win32/dirent’
make[4]: Nothing to be done for `all’.
make[4]: Leaving directory `/home/ashwini/glib-2.2.3/build/win32/dirent’
make[4]: Entering directory `/home/ashwini/glib-2.2.3/build/win32′
make[4]: Nothing to be done for `all-am’.
make[4]: Leaving directory `/home/ashwini/glib-2.2.3/build/win32′
make[3]: Leaving directory `/home/ashwini/glib-2.2.3/build/win32′
make[3]: Entering directory `/home/ashwini/glib-2.2.3/build’
make[3]: Nothing to be done for `all-am’.
make[3]: Leaving directory `/home/ashwini/glib-2.2.3/build’
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/build’
Making all in po
make[2]: Entering directory `/home/ashwini/glib-2.2.3/po’
make[2]: Nothing to be done for `all’.
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/po’
Making all in docs
make[2]: Entering directory `/home/ashwini/glib-2.2.3/docs’
Making all in reference
make[3]: Entering directory `/home/ashwini/glib-2.2.3/docs/reference’
Making all in glib
make[4]: Entering directory `/home/ashwini/glib-2.2.3/docs/reference/glib’
make[4]: Nothing to be done for `all’.
make[4]: Leaving directory `/home/ashwini/glib-2.2.3/docs/reference/glib’
Making all in gobject
make[4]: Entering directory `/home/ashwini/glib-2.2.3/docs/reference/gobject’
make[4]: Nothing to be done for `all’.
make[4]: Leaving directory `/home/ashwini/glib-2.2.3/docs/reference/gobject’
make[4]: Entering directory `/home/ashwini/glib-2.2.3/docs/reference’
make[4]: Nothing to be done for `all-am’.
make[4]: Leaving directory `/home/ashwini/glib-2.2.3/docs/reference’
make[3]: Leaving directory `/home/ashwini/glib-2.2.3/docs/reference’
make[3]: Entering directory `/home/ashwini/glib-2.2.3/docs’
make[3]: Nothing to be done for `all-am’.
make[3]: Leaving directory `/home/ashwini/glib-2.2.3/docs’
make[2]: Leaving directory `/home/ashwini/glib-2.2.3/docs’
make[1]: Leaving directory `/home/ashwini/glib-2.2.3′
LikeLike
Reblogged this on Arun's Blog and commented:
The original scratchbox site scratchbox.org seems to be down time and again. There is a mirror available for the downloads – http://thecust.net/nelisquare/hathor-sdk/
LikeLike
Hi,
I am using i386 machine and trying to compile some packages for armv7.
I got [sbox-armv7] prompt. I tried to configure krb5 package by running the command ./configure –prefix=/usr….
I got the following error :
checking for gcc… gcc
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… configure: error: in `/scratchbox/krb5-1.10.5-signed/krb5-1.10.5/src’:
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `–host’.
See `config.log’ for more details
Why is it asking for ‘–host’?
Please clarify..
Thanks
-Sahil
LikeLike
hi,
I am trying to install sbox2 on my ububtu pc.But when i am trying to do make install the following eroor comes and I am not finding any solution to resolve the error.If any one known to this plz help me
root@tejbir:/opt/sb22/ban/sbox2# make install PREFIX=/opt/sb22/sbox_compiled/
make[1]: Entering directory `/opt/sb22/ban/sbox2′
[CC] luaif/sb_log.o
In file included from luaif/sb_log.c:49:0:
/opt/sb22/ban/sbox2/preload/exported.h:510:12: error: conflicting types for ‘scandir’
/usr/include/dirent.h:256:12: note: previous declaration of ‘scandir’ was here
/opt/sb22/ban/sbox2/preload/exported.h:526:12: error: conflicting types for ‘scandir64’
/usr/include/dirent.h:279:12: note: previous declaration of ‘scandir64’ was here
luaif/sb_log.c: In function ‘write_to_logfile’:
luaif/sb_log.c:116:4: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
make[1]: *** [luaif/sb_log.o] Error 1
make[1]: Leaving directory `/opt/sb22/ban/sbox2′
make: *** [regular] Error 2
LikeLike
I didnt get the following , Can you please tell me , how to relogin in sbox, and how to provide permissions …????
You’ll need to re-login to be in the sbox group and have proper permissions:
sudo su $USER
Can you please tell me , how to relogin in sbox, and how to provide permissions …????
LikeLike
Pingback: xz backdoor and autotools insanity | Felipe Contreras