Cross Compiling
From RockWiki
Cross Compiling means that you compile a program or set of programs on one architecture for another. In this example we build a PowerPC system on an x86 machine.
Contents |
Cross Compiling ROCK Linux itself
Cross Compiling ROCK Linux from one architecture to another is pretty easy. Just follow the steps described in the Documentation/BUILD file and make sure that you selected the This is a cross-build between architectures configuration option.
Now, when you start './scripts/Build-Target it will first build a cross compiler and then cross compile a minimal ROCK Linux for you. This minimal system contains everything needed for a fully functional command-line system including init process, shell, and standard tools.
You can install this cross-compiled ROCK Linux system on your target architecture and make a full rebuild of ROCK Linux there if you need the more advanced packages (like X11).
Note that not all targets can be cross-compiled without errors - but the target generic should do fine.
Cross-building the kernel
The 2.6 Linux kernel is cross-compiled along with the other packages by default, so you can generally skip this section.
First extract the kernel sources somewhere. Do not cross-build the linux kernel in /usr/src/linux - this might screw up your system headers! Change to the linux kernel source directory (for example: /usr/src/linux-<architecture>).
Before we can build the kernel we need to tell the system where the cross compiler we built with ./scripts/Build-Target is installed. It's in the ROCK Linux base directory under build/<config-id>/ROCK/tools/crosscc. So we extend the PATH variable using a command like e.g. for PowerPC:
export PATH="/rock-linux/build/powerpc-1.7.0-DEV-powerpc-cross-generic/tools/crosscc:$PATH"
We also need to tell the kernel about the architecture and cross-compiler. Set the variables ARCH and CROSS_COMPILE e.g. for PowerPC:
export ARCH=ppc export CROSS_COMPILE=powerpc-unknown-linux-gnu-
Now you can configure and build a kernel as usual using make menuconfig and make vmlinux (or something like make bzImage).
Installing the results
This is the most complicated step and may be difficult for some architectures. You need to export the system in build/<config-id>/ using NFS and boot the cross-built kernel on the target architecture with nfs-root.
Let's say we want to install our PowerPC cross-built on an RS/6000 with OpenFirmware. In that case we need to copy the kernel chrp image to a floppy disk and boot the kernel with the Openfirmware command:
boot floppy:,zImage.prep root=/dev/nfs nfsroot=/ppc-nfs-root ip=192.168.0.2:192.168.0.1
Assuming that the nfs server has the ip 192.168.0.1, the client should use 192.168.0.2 and the exported directory is /ppc-nfs-root. Read Documentation/nfsroot.txt from the linux kernel sources for details.
Now you can use the standard Linux commands for creating a filesystem on a local partition and copy all system data.
Building a Cross Compiler
The ROCK Linux build system can also be used to build a cross compiler without cross building an entire distribution. Simply use "./scripts/Build-Pkg" or "rocket emerge". First you need to create some headers for the target architecture:
# rocket emerge linux26-header-cross-sparc == 16:40:11 =[9]=> Building base/linux=linux26-header-cross-sparc [2.6.15 0]. -> Reading build/.../tools.native/lib/pkg_linux_pre.conf -> No bootsplash in this config -> No mISDN driver in this config -> Reading package configuration from package directory. -> Preparing to build a linux kernel header package. -> Preparing to build a linux kernel sparc cross-headers package. -> Preparing build in src.linux26-header-cross-sparc.1137685172.4951.1211733481 -> Building. Writing output to $root/var/adm/logs/9-linux26-header-cross-sparc.out -> $root/var/adm/logs/9-linux26-header-cross-sparc.out -> 9-linux26-header-cross-sparc.log == 01/19/06 16:43:09 =[9]=> Finished building package linux26-header-cross-sparc.
# rocket emerge glibc23-cross-sparc == 15:33:51 =[9]=> Building base/glibc=glibc23-cross-sparc [2.3.6 0]. -> Reading package configuration from package directory. -> Preparing to build a cross-glibc package for sparc. -> Building only cross-headers because there is no cross-gcc (yet). -> Preparing build in src.glibc23-cross-sparc.1137681184.21010.3923905385 -> Building. Writing output to $root/var/adm/logs/9-glibc23-cross-sparc.out -> $root/var/adm/logs/9-glibc23-cross-sparc.out -> 9-glibc23-cross-sparc.log == 01/19/06 15:39:26 =[9]=> Finished building package glibc23-cross-sparc.
The glibc package auto-detects that there is no cross-compiler yet and only builds the headers.
Once the glibc headers are there you can build a cross binutils and gcc:
# rocket emerge binutils-cross-sparc == 14:03:45 =[9]=> Building base/binutils=binutils-cross-sparc [2.16.91.0.4 0]. -> Reading package configuration from package directory. -> Preparing to build a cross-binutils package for sparc. -> Preparing build in src.binutils-cross-sparc.1137675817.22756.1044747514 -> Building. Writing output to $root/var/adm/logs/9-binutils-cross-sparc.out -> $root/var/adm/logs/9-binutils-cross-sparc.out -> 9-binutils-cross-sparc.log == 01/19/06 14:15:08 =[9]=> Finished building package binutils-cross-sparc.
# rocket emerge gcc40-cross-sparc == 16:14:31 =[9]=> Building base/gcc=gcc40-cross-sparc [4.0.2 0]. -> Reading package configuration from package directory. -> Preparing to build a cross-gcc package for sparc. -> Preparing build in src.gcc40-cross-sparc.1137683636.29291.2095596055 -> Building. Writing output to $root/var/adm/logs/9-gcc40-cross-sparc.out -> $root/var/adm/logs/9-gcc40-cross-sparc.out -> 9-gcc40-cross-sparc.log == 01/19/06 16:28:01 =[9]=> Finished building package gcc40-cross-sparc.
At last you need to re-build the glibc package so it also contains the libraries and not just the headers:
# rocket emerge glibc23-cross-sparc == 18:13:02 =[9]=> Building base/glibc=glibc23-cross-sparc [2.3.6 0]. -> Reading package configuration from package directory. -> Preparing to build a cross-glibc package for sparc. -> Preparing build in src.glibc23-cross-sparc.1137690736.20511.420264516 -> Building. Writing output to $root/var/adm/logs/9-glibc23-cross-sparc.out -> $root/var/adm/logs/9-glibc23-cross-sparc.out -> 9-glibc23-cross-sparc.log == 01/19/06 18:30:12 =[9]=> Finished building package glibc23-cross-sparc.
