This document is not yet another howto on building a cross
compiler. There are already some documents on this topic, see the
References section. Its goal is to explain what are the required steps
and how it is supposed to work in an ideal world. We will not talk
about patches here (or just for reference). Of course, INSTRUCTIONS
HERE DO NOT WORK.
- Installing Linux® kernel headers
Linux® kernel headers are needed to compile glibc. However, we need the
same Linux® kernel headers as they would be on the host system. For
instance, the asm
symlinks should point to the proper
directory. Another way to cope with this situation and which is needed
on multi-arch system is to have multi-arch Linux® kernel headers.
Since we already use multi-arch Linux® kernel headers, this step is
simply a directory copy.
mkdir -p $SYSROOT/usr/include &&
cp -a /usr/include/asm* /usr/include/linux $SYSROOT/usr/include
- Installing binutils
binutils provides two important tools: the GNU assembler and the GNU
linker which are both used as backends by the GNU C compiler. We need
here a cross binutils, i.e. a GNU assembler and a GNU linker that will
run on the build system and produces binaries suitable for the host
system.
The --with-sysroot
flag is quite important. It will be
hardcoded in the resulting binary, i.e. $HOST-ld. It is used at
runtime to locate libraries when the library search path includes a
path starting with the = sign. This is the case for the default
library search path which is :
=/usr/local/lib:=/lib:=/usr/lib
. This might seem quite
complex at first but it allow libraries to be found by the linker as
if we were compiling on the host system itself.
tar jxf binutils-2.15.tar.bz2 &&
mkdir binutils-build &&
cd binutils-build &&
../binutils-2.15/configure --prefix=$PREFIX \
--target=$HOST \
--with-sysroot=$SYSROOT &&
make all &&
make install
- Installing glibc headers
We need the glibc headers for building gcc. We have used the
linuxthreads add-ons instead of nptl since nptl was not working in
this step. We specify both the build and host system on the configure
command line to force the cross compilation mode. BUILD can be
determined by running the config.guess
script in the
binutils sources.
tar jxf glibc-2.3.4.tar.bz2 &&
(cd glibc-2.3.4 &&
tar jxf glibc-linuxthreads-2.3.4.tar.bz2) &&
mkdir glibc-build &&
cd glibc-build &&
../glibc-2.3.4/configure --prefix=/usr \
--build=$BUILD --host=$HOST \
--with-headers=$SYSROOT/usr/include \
--without-cvs --disable-profile --disable-debug --without-gd \
--enable-add-ons=linuxthreads --with-tls --without-__thread \
--enable-kernel=2.4 &&
make install-headers install_root=$SYSROOT
- Installing bootstrap gcc
At this step, we can compile a first cross compiler. It will not be
able to produce shared libraries since shared libraries need glibc
shared libraries.
Like with binutils, the --with-sysroot
flag is quite important
too. It will be used at runtime to handle the default headers search
path as if we were compiling on the host system. The default headers
search path is : /usr/local/include:/usr/include
. Some
internal gcc directories might be added as well
($PREFIX/lib/gcc/$HOST/3.4.3/include
here)
tar jxf gcc-core-3.4.3.tar.bz2 &&
mkdir gcc-build &&
cd gcc-build &&
../gcc-3.4.3/configure --prefix=$PREFIX \
--target=$HOST \
--with-sysroot=$SYSROOT --with-headers=$SYSROOT/usr/include \
--disable-threads --disable-shared --enable-language=c &&
make &&
make install
- Installing glibc
We now have a basic cross compiler/assembler/linker. We will use it to
build a host glibc. We need to add the correct directory to PATH so
that the configure script could find our cross compiler.
export PATH=$PREFIX/bin:$PATH &&
tar jxf glibc-2.3.4.tar.bz2 &&
(cd glibc-2.3.4 &&
tar jxf glibc-linuxthreads-2.3.4.tar.bz2) &&
../glibc-2.3.4/configure \
--prefix=/usr \
--build=$BUILD --host=$HOST \
--with-headers=$SYSROOT/usr/include \
--without-cvs --disable-profile --disable-debug --without-gd \
--enable-add-ons=linuxthreads --with-tls --without-__thread \
--enable-kernel=2.4 &&
make &&
make install install_root=$SYSROOT &&
- Installing gcc
Since we now have a host glibc, we can rebuild our cross compiler to
include C++ support (this requires building a shared library and can
not be done in the former step).
tar jxf gcc-core-3.4.3.tar.bz2 &&
tar jxf gcc-g++-3.4.3.tar.bz2 &&
mkdir gcc-build &&
cd gcc-build &&
../gcc-3.4.3/configure --prefix=$PREFIX \
--target=$HOST \
--with-sysroot=$SYSROOT --with-headers=$SYSROOT/usr/include \
--enable-threads=posix --enable-languages=c,c++ &&
make &&
make install