Copyright © 2004, 2005 Nokia
Toolchain is a collection of tools used to develop software for a certain hardware target. Toolchains are based on particular versions of compiler, libraries, special headers and other tools. A cross-toolchain is a toolchain for compiling binaries for different CPU architecture than the host CPU.
Scratchbox is a cross-compilation toolkit for embedded Linux application development. It is designed for compiling software for different target CPU architectures. Scratchbox allows creating several target environments. Each target is a separate environment that has a selected toolchain, target CPU and an own file system.
Building toolchains is not always trivial. Scratchbox uses scripts for building predefined toolchains. After building a toolchain it should be tested to know that it works properly. Testing toolchains is harder than building toolchains.
Scratchbox toolchains provide the cross compilation tools for compiling binaries for the target environment. Each toolchain is built for a certain CPU target and they are based on certain gcc and C-library sources.
Scratchbox toolchains can be used both inside and outside Scratchbox. In Scratchbox each target uses a certain toolchain with a specific target CPU. Scratchbox uses wrappers to make the toolchains appear as if they were native toolchains. Outside Scratchbox the toolchains are used as any normal cross compilation toolchains.
All the installed toolchains can be found in the /scratchbox/compilers directory.
The toolchains use either glibc or uClibc C-libraries. The glibc toolchain is based on Debian gcc-3.3, binutils, libc6 and linux-kernel-headers packages. Also some Scratchbox patches are applied to the packages. The uClibc build uses Erik Andersen's uClibc toolchain build script which uses uClibc's own patches.
Scratchbox.org  offers prebuilt toolchains for x86 and ARM targets. Only these targets are currently supported because too much work to support all different configurations. With the Scratchbox toolchain sources you can build your own custom toolchains.
Scratchbox toolchain building scripts allow you to build easily predefined toolchains and give the possibility to build custom toolchains for different targets. Changing the toolchain binutils, compiler or C-library packages can be a more demanding task.
Scratchbox uses a gcc wrapper for wrapping most of the toolchain command binaries. In the /scratchbox/compilers/bin/ directory you can see the linked binaries. The wrapper knows how to handle each command and depending on the command it might change some of the command parameters. Then it runs the actual command from the correct path that depends from the selected target. The gcc wrapper reads all the target specific information from the target configuration files that can be found in the /scratchbox/users/username/targets/ directory.
Scratchbox has also an ld wrapper for linking the binaries properly. When compiling inside Scratchbox a fake-native ld is used. Outside Scratchbox a normally behaving ld is used. For example inside Scratchbox the dynamically linked binaries are linked against the libraries that are in standard library paths, not in the toolchain's library path.
The gcc wrapper uses ccache by default. The default cache directory is /scratchbox/ccache/. Ccache can be disabled by setting the environment variable SBOX_USE_CCACHE to “no”. The cache directory can be changed with the CCACHE_DIR environment variable.
The following binary packages are available:
For each toolchain that you want to use you need to create a Scratchbox target. For installation and target creation instructions see Installing Scratchbox .
Scratchbox toolchain build scripts are available in the sb-toolchains source package that is available at the Scratchbox download page  for each Scratchbox version.
Scratchbox toolchains are built with the GAR system . It is a mechanism for automating the compilation and installation of third-party source code. It appears in the form of a tree of directories containing Makefiles and other ancillary bookkeeping files (such as installation manifests and checksum lists).
The gcc/glibc directory contains the glibc toolchain scripts and gcc/uclibc the uClibc toolchain scripts. The gcc/glibc/versions file has all the information of the debian package versions. It tells which debian packages are used for compiling the toolchains. The gcc/glibc/files directory contains the Scratchbox patches. Only some of them are in use.
The sb-toolchains source package is used also to build arch tools and device tools which are target dependent. The build instructions in this document automatically build all of them; there is currently no documented way to disable that.
Toolchains have to be built with an already existing compiler. Scratchbox has the HOST target for this. HOST's host-gcc toolchain is for compiling binaries for the host. It's configured to make the compiled binaries use the Scratchbox's host libraries.
When building toolchains you need write privileges to the /scratchbox/compilers and /scratchbox/device_tools directories. The scratchbox source package contains the scripts/permhack script for changing the privileges, but you can also do it like this:
# chown -R username:groupname /scratchbox/compilers/ # chown -R username:groupname /scratchbox/device_tools/
Toolchain building needs the Debian devkit package to be installed; the build script uses the dpatch and dpkg commands.
Make sure you have the scratchbox-devkit-debian package installed on your system.
Download the sb-toolchains source package (see Section 3.1). Extract it into a directory that is visible inside Scratchbox (such as /scratchbox/users/username/home/username).
Change the permissions of the Scratchbox installation directories (see Section 3.2).
Login to Scratchbox.
Modify the HOST target to use the Debian devkit:
[sbox-HOST: ~] > sb-conf setup HOST --devkits debian --force
Install /etc/passwd and Debian-specific files on the HOST target:
[sbox-HOST: ~] > sb-conf install HOST --etc --devkits
Reselect the HOST target so that the environment gets updated:
[sbox-HOST: ~] > sb-conf select HOST
If you want to build the default toolchains, run the build script in the sb-toolchains directory:
[sbox-HOST: ~] > cd sb-toolchains [sbox-HOST: ~/sb-toolchains] > ./build --build
If you want to build custom toolchains, use the Makefile. You need to pass the following variables to make:
source directory; for example “gcc/glibc”
compiler name; for example “i686-gcc-3.3.4-glibc-2.3.2”
architecture; for example “i386”
sub-architecture; for example “i686”
CPU type (optional)
[sbox-HOST: ~/sb-toolchains] > make TC_SOURCE=gcc/glibc \ TC_NAME=armv3l-gcc-3.3.4-glibc-2.3.2 TC_ARCH=arm TC_SUBARCH=armv3l TC_CPU=arm7
[sbox-HOST: ~/sb-toolchains] > cat my-compiler.config TC_SOURCE = gcc/glibc TC_NAME = arm-gcc-3.3.4-glibc-2.3.2 TC_ARCH = arm TC_SUBARCH = arm [sbox-HOST: ~/sb-toolchains] > make TOOLCHAIN=my-compiler.config
Before changing the source package versions, test building the toolchains with the default configuration to see that the Scratchbox environment works properly.
The glibc and uClibc sources are fetched in different ways. Glibc toolchain build uses a version file for managing the package versions. Each package is downloaded separately from the download site that is defined by gar.conf.mk. (You can override the MASTER_SITES variable in the .gar-local.conf file.)
The uClibc toolchain source version is defined in the gcc/uclibs/Makefile. The same file also defines the sources download site.
The glibc toolchain packages can be updated with the following process:
Download the new source packages to the gcc/glibc/files directory. For example in Debian you can use this command:
$ cd /scratchbox/users/username/home/username/sb-toolchain/gcc/glibc/files $ apt-get source gcc-3.3
Update the new package names in versions file.
$ /scratchbox/login [sbox-HOST: ~] > cd sb-toolchains/gcc/glibc [sbox-HOST: ~/sb-toolchains/gcc/glibc] > vi versions
Generate MD5 checksums of the new packages.
[sbox-HOST: ~/sb-toolchains/gcc/glibc] > make makesums
Test that all used patches get written properly.
Compile and test the new toolchain.
The toolchains can be packaged outside or inside Scratchbox. You can create tarballs and Debian packages inside Scratchbox, but if you want to create RPMs you need the rpmbuild command which is not included in Scratchbox. If you are using a Debian GNU/Linux system, you can get rpmbuild from the “rpm” Debian package.
You can create all three package types of the default toolchains using the build script:
$ cd /scratchbox/users/username/home/username/sb-toolchains/ $ ./build --package
Custom toolchains are packaged using the packages, tarball, deb and/or rpm targets of Makefile. This time the following options are needed:
compiler name (see Section 3.3)
name of the binary package; for example “scratchbox-toolchain-i686-gcc3.3-glibc2.3”
name of a package this new package replaces (optional)
[sbox-HOST: ~/sb-toolchains] > make tarball deb \ TC_NAME=armv3l-gcc-3.3.4-glibc-2.3.2 \ TC_PACKAGE=scratchbox-toolchain-armv3l-gcc3.3-glibc2.3
$ cat my-compiler.config TC_SOURCE = gcc/glibc TC_NAME = arm-gcc-3.3.4-glibc-2.3.2 TC_ARCH = arm TC_SUBARCH = arm TC_PACKAGE = scratchbox-toolchain-arm-gcc3.3-glibc2.3 TC_REPLACE = scratchbox-toolchain-arm-glibc $ make packages TOOLCHAIN=my-compiler.config
All created compilers should be at least tested to compile, link and run a simple test program. The test program should be tested to link both statically and dynamically. These tests should be done with both a C and a C++ program.
The sb-toolchains/test_tools/test_scripts/init_tests.sh script can be used for these initial tests.
These tests are a collection of test for the C and C++ frontends of gcc. The tests can be found in gcc's directory gcc-VERSION/gcc/testsuite.
The result of running the testsuite are various *.sum and *.log files in the testsuite subdirectories. The *.log files contain a detailed log of the compiler invocations and the corresponding results, the *.sum files summarise the results. These summaries contain status codes for all tests:
PASS: the test passed as expected
XPASS: the test unexpectedly passed
FAIL: the test unexpectedly failed
XFAIL: the test failed as expected
UNSUPPORTED: the test is not supported on this platform
ERROR: the testsuite detected an error
WARNING: the testsuite detected a possible problem
The Scratchbox toolchains should be tested with these tests. These tests can be run with sb-toolchains/test_tools/test_scripts/reg_tests.sh or manually as described in GCC Testing .
Runnig the gcc testsuite requires DejaGnu and Expect host tools. These can be compiled running make install in the sb-toolchains/test_tools/dejagnu/ directory. This will also build Expect. Remember that these are host tools so they have to be compiled with the HOST target. You also need to change the permissions of the /scratchbox/tools directory.
# chown -R username:groupname /scratchbox/tools/
 Scratchbox download page .
 Installing Scratchbox , Valtteri Rahkonen.
 GAR Architecture .
 GCC Testing .