Gentoo Logo

[ << ] [ < ] [ Home ] [ > ] [ >> ]


4. Cross-Compiling The Kernel

Content:

4.a. Sources

First install the relevant kernel sources. You can use a package from the Gentoo portage tree or fetch your own from The Linux Kernel Archive or some other random source. The method for actually compiling is all the same.

You should install the kernel into the sysroot so that if you want to cross-compile packages which include kernel modules, the process will be transparent. Otherwise, the actual place where you build the kernel does not matter. Some people build all their kernels in /usr/src/ for example.

4.b. Setup Cross-compiling

There are two fundamental variables that the kernel uses to select the target architecture. Normally these values are guessed based on your build environment, but of course that environment here does not match our target embedded system, so we'll need to override them. The variables in question are ARCH and CROSS_COMPILE. The default values for both are found in the toplevel Makefile and the values of both may be overriden on the command line.

The ARCH variable is the architecture you're targetting as the kernel knows it. So while portage and other people may use "x86", the kernel uses "i386". Peek in the arch/ subdirectory real quick to figure out what you want to use.

Hopefully the CROSS_COMPILE variable is pretty self-explanatory. Set this to the prefix of your toolchain (including the trailing dash "-"). So if your toolchain is invoked as say x86_64-pc-linux-gnu-gcc, just chop off that trailing gcc and that's what you use: x86_64-pc-linux-gnu-.

There is an additional variable, INSTALL_MOD_PATH, which defines where the /lib directory will be created, and all the modules stored. While you don't have to transfer the kernel sources to your target device, if you build any modules, you'll want this directory.

There are really two ways you can setup the system. You can modify the toplevel Makefile or you can override the relevant variables on the command line. How you do it is largely a matter of taste, so we'll cover both. Pick one of the following.

Code Listing 2.1: Editing Toplevel Makefile

# This is what the vanilla Makefile looks like
ARCH            ?= $(SUBARCH)
CROSS_COMPILE   ?= 

# Set the ARCH and CROSS_COMPILE default values
ARCH            ?= arm
CROSS_COMPILE   ?= arm-unknown-linux-gnu-

Code Listing 2.2: Overriding On The Commandline

# make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnu- ....

You can use a little helper script if you need to hop between different kernel trees at the sametime. We'll call this script xkmake.

Code Listing 2.3: xkmake

#!/bin/sh
exec make ARCH="arm" CROSS_COMPILE="arm-unknown-linux-gnu-" INSTALL_MOD_PATH="${SYSROOT}" "$@"

So now when you want to build a kernel or do anything else, you just execute xkmake in place of make.

4.c. Configure and Compile

At this point, configuring and compiling the kernel is like any other kernel, so we won't go into depth as there are plenty of HOWTOs and guides out there which can treat the subject in much greater detail.

Code Listing 3.1: Configure and Compile

# cd "${SYSROOT}/usr/src/linux"
# xkmake menuconfig
# xkmake

[ << ] [ < ] [ Home ] [ > ] [ >> ]


Print

View all

Page updated April 28, 2013

Summary: Cross-compile a kernel for your system with flair!

Mike Frysinger
Author

Ned Ludd
Author

Robin H. Johnson
Author

Alex Tarkovsky
Author

Alexey Shvetsov
Author

Raúl Porcel
Author

Joshua Saddler
Editor

Donate to support our development efforts.

Copyright 2001-2014 Gentoo Foundation, Inc. Questions, Comments? Contact us.