Difference between revisions of "Raspberry Pi Kernel Compilation"

From eLinux.org
Jump to: navigation, search
(Common)
(46 intermediate revisions by 14 users not shown)
Line 1: Line 1:
 
{{Template:RPi_Software}}
 
{{Template:RPi_Software}}
 
 
 
= Overview =
 
= Overview =
 
 
First, you are going to get and build the linux kernel and its modules using a suitable compiler (a "cross-compiler" if you aren't building it on the same hardware you will be running it on) and then you are going to create a kernel image from the uncompressed kernel (Image) to place on the sd, along with the modules you build alongside it.
 
First, you are going to get and build the linux kernel and its modules using a suitable compiler (a "cross-compiler" if you aren't building it on the same hardware you will be running it on) and then you are going to create a kernel image from the uncompressed kernel (Image) to place on the sd, along with the modules you build alongside it.
 
+
= From the Raspberry pi =
See below for the various guides to get and compile a suitable kernel for your RPi, and then create a kernel.img according to the steps at the end.
+
== Firmware ==
 
+
Install git first, as below in [[RPi Kernel Compilation#Kernel compilation|Kernel compilation]].
= Raspberry PI kernel compilation =
+
cd /opt
 
+
git clone --depth 1 git://github.com/raspberrypi/firmware.git
You can compile the kernel on the board itself, but because of the limited resources it will take a lot of time. Alternatively you can crosscompile the kernel on another machine running Linux, Windows or OS X.
+
cd firmware/boot
 
+
cp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf /boot/
== Compiling on the Raspberry pi itself ==
+
cd /opt
 
+
rm -r firmware
=== Arch Linux ===
+
== Kernel compilation ==
 
+
=== Debian pre-build===
==== getting the compiler ====
+
apt-get update
You will need GIT to clone the kernel source tree from GitHub, compiler (gcc) and GNU Make:
+
apt-get -y dist-upgrade
  pacman -S git gcc make
+
apt-get -y install git gcc make tmux
 
+
=== Arch Linux pre-build===
(NOTE: git might be omitted if you decide to download sources in compressed format; this is far faster)
+
pacman -Syu
 
+
  pacman -S git gcc make tmux
==== getting the sources ====
+
=== Common ===
 
+
  cd /opt
create a directory where you can work on the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.
 
 
 
 
  mkdir raspberrypi
 
  mkdir raspberrypi
  cd raspberrypi  
+
  cd raspberrypi
  git clone https://github.com/raspberrypi/linux.git
+
  git clone --depth 1 git://github.com/raspberrypi/linux.git
(NOTE: git might fail due to memory constraints; in this case creation of swap file might help. Be warned - this takes ages! To omit the revision history and reduce the download, you can add "--depth 1" to the end of the git clone command.)
+
  cd linux
 
 
Alternatively, download ZIP or TAR.GZ version of the sources from:
 
https://github.com/raspberrypi/linux/downloads
 
unpack and enter the extracted directory (this is your kernel directory - its sources to be precise)
 
 
 
==== configuring the kernel ====
 
Next, the kernel options are configured. Either copy the cut down Raspberry Pi .config file from the kernel source configs directory:
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
 
 
Or alternatively, to use the configuration from a currently running Raspberry Pi image, connect to the target and extract the .config file.  Then copy the resultant .config file into the Linux kernel source root directory:
 
 
  zcat /proc/config.gz > .config
 
  zcat /proc/config.gz > .config
  cp .config <path to linux source root directory>
+
  #optional#make menuconfig # i.e. if you want to alter the configuration. You'll need to first run # apt-get install libncurses5-dev
 
+
  tmux new -s make
If needed - manual/additional configuration:
+
  nice make; nice make modules
  make menuconfig
+
  [Ctrl]+[B],[D]
 
+
  ##############… 5 to 11 hours later…
==== compile the kernel ====
+
  tmux a -t m
  make
+
[Ctrl]+[D]
 
+
  cp arch/arm/boot/Image /boot/kernel.img
(NOTE: this will take around 6h; You might find GNU Screen useful)
+
  make ARCH=arm modules_install INSTALL_MOD_PATH=/
 
+
  cd /opt
==== build kernel.img so your RPi can boot from it ====
+
  rm -r raspberrypi
 
+
  shutdown -r now;
Finally you need to build a kernel.img for your Pi to boot from. For this, you need the mkimage tool from the raspberrypi github repository:
 
 
 
  git clone https://github.com/raspberrypi/tools
 
 
 
Alternatively, download 'imagetool-uncompressed.py' and its dependencies from (this takes far less time and resources):
 
  https://github.com/raspberrypi/tools/tree/master/mkimage
 
 
 
Before you can use this script you need Python v2 to be installed:
 
  pacman -S python2
 
 
 
Once all above is set up you should have the following files (checklist):
 
* in the kernel folder you compiled a file: linux/arch/arm/boot/Image
 
* python2 executable (it should be located by default in /usr/bin/python2)
 
* imagetool-uncompressed.py script
 
 
 
 
 
If this is a case (you have all the above) convert your kernel image with the script:
 
 
 
  python2 imagetool-uncompressed.py path/to/linux/arch/arm/boot/Image
 
 
 
This will create a file called kernel.img. Transfer this file into /boot directory (make sure the existing kernel.img in /boot directory gets replaced).
 
 
 
The last thing is to install kernel modules. To do this navigate to your kernel folder and execute:
 
  make modules_install
 
 
 
This will install all compiled modules into /lib/modules and possibly some additional files into /lib/firmware folders.
 
 
 
Reboot your RPi and pray :)
 
  reboot
 
 
 
TODO: verify & consolidate
 
 
 
== Cross compiling on a foreign machine==
 
 
 
=== Ubuntu Linux ===
 
 
 
==== getting the compiler ====
 
 
 
On Ubuntu Oneiric getting the arm cross compiler can be as easy as:
 
 
 
  sudo apt-get install gcc-4.6-arm-linux-gnueabi
 
  sudo apt-get install git-core  #jhauser14905 -- might as well state the obvious, you need git installed! -- it's git-core on ubuntu. --REW
 
 
 
(TODO: Is this the right one? More packages required? I did this a while ago! TODO: Other distributions?)
 
 
 
==== getting the sources ====
 
 
 
create a directory where you can work on  the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.
 
  
 +
= From a foreign machine =
 +
== Firmware ==
 +
cd /opt
 +
git clone git://github.com/raspberrypi/firmware.git
 +
cd firmware/boot
 +
scp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf <user>@<host>:/boot/
 +
After the first time:
 +
cd /opt/firmware
 +
git pull
 +
cd boot
 +
scp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf <user>@<host>:/boot/
 +
== Kernel cross compilation ==
 +
You can use the "-j" flag to improve compilation time. If you have a dual core machine you can use "-j 3", for a quad core machine you can use "-j 6", and so on. Also if your trying to rebuild an existing kernel, the proper .config file can be obtained from the raspberry pi using zcat /proc/config.gz > .config
 +
=== Ubuntu ===
 +
apt-get install git gcc-arm-linux-gnueabi make ncurses-dev
 +
cd /opt
 
  mkdir raspberrypi
 
  mkdir raspberrypi
 
  cd raspberrypi  
 
  cd raspberrypi  
  git clone https://github.com/raspberrypi/linux.git
+
  git clone git://github.com/raspberrypi/linux.git
 
  cd linux
 
  cd linux
 
jhauser14905: on 2012-01-28, with all package updates applied, i had to add the following symlink in order to get the make commands to work. otherwise they would error out
 
 
sudo ln -s  /usr/bin/arm-linux-gnueabi-gcc-4.6 /usr/bin/arm-linux-gnueabi-gcc
 
 
==== compiling ====
 
 
Next, the kernel options are configured.  Either copy the cut down Raspberry Pi .config file from the kernel source configs directory:
 
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
Or alternatively, to use the configuration from a currently running Raspberry Pi image, connect to the target and extract the .config file.  Then copy the resultant .config file into the Linux kernel source root directory:
 
zcat /proc/config.gz > .config
 
cp .config <path to linux source root directory>
 
 
Configure the kernel with the copied .config file by running oldconfig:
 
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
 
+
  #optional#make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
If manual/additional configuration of kernel options are needed run menuconfig:
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
 
 
 
Then build the kernel:
 
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
 
You can use the "-j" flag to improve compilation time. If you have a dual core machine you can use "-j 3", for a quad core machine you can use "-j 6", and so on.
 
 
If you get the error messages that arm-linux-gnueabi-gcc cannot be found when running make, run the following command:
 
 
sudo ln -s /usr/bin/arm-linux-gnueabi-gcc-4.6 /usr/bin/arm-linux-gnueabi-gcc
 
 
this creates a symbolic link to the 4.6 gcc binary
 
  
 
=== Gentoo Linux ===
 
=== Gentoo Linux ===
 
==== getting the compiler ====
 
 
Build the cross toolchain:
 
 
  crossdev -S -v -t arm-unknown-linux-gnueabi
 
  crossdev -S -v -t arm-unknown-linux-gnueabi
 
theBuell: on 2012-05-06, cross -S -v -A gnueabi arm works just fine
 
 
This command should create a cross-toolchain using the latest stable versions of the required packages.  If it fails, you can specify exact versions by removing the "-S" flag and adding the "--b", "--g", "--k" and "--l" flags. For the exact usage refer to the crossdev manpage. A good starting point for figuring out the right versions are those which are stable for the arm architecture.
 
 
==== getting the sources ====
 
 
create a directory where you can work on  the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.
 
 
 
  mkdir raspberrypi
 
  mkdir raspberrypi
 
  cd raspberrypi  
 
  cd raspberrypi  
  git clone https://github.com/raspberrypi/linux.git
+
  git clone git://github.com/raspberrypi/linux.git
 
  cd linux
 
  cd linux
 
==== compiling ====
 
 
Next you have to configure the kernel:
 
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
+
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-unknown-linux-gnueabi- oldconfig
 +
#optional#make ARCH=arm CROSS_COMPILE=/usr/bin/arm-unknown-linux-gnueabi- menuconfig
 +
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-unknown-linux-gnueabi- -k
  
Then building the kernel:
+
crossdev should create a cross-toolchain using the latest stable versions of the required packages. If it fails, you can specify exact versions by removing the "-S" flag and adding the "--b", "--g", "--k" and "--l" flags.
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
 
  
You can use the "-j" flag to improve compilation time. If you have a dual core machine you can use "-j 3", for a quad core machine you can use "-j 6", and so on.
+
On 2012-05-06, cross -S -v -A gnueabi arm works just fine
  
 
=== Arch Linux ===
 
=== Arch Linux ===
 
==== getting the compiler ====
 
You will need GIT to clone the kernel source tree from GitHub:
 
 
  pacman -S git
 
  pacman -S git
 
Build the cross toolchain:
 
arm-linux-gnueabi-gcc is on the AUR. If you use yaourt:
 
 
 
  yaourt -S arm-linux-gnueabi-gcc
 
  yaourt -S arm-linux-gnueabi-gcc
 
+
  cd /opt
Yaourt is recommended as it will build all dependencies.
 
 
 
==== getting the sources ====
 
 
 
create a directory where you can work on the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.
 
 
 
 
  mkdir raspberrypi
 
  mkdir raspberrypi
 
  cd raspberrypi  
 
  cd raspberrypi  
  git clone https://github.com/raspberrypi/linux.git
+
  git clone git://github.com/raspberrypi/linux.git
 
  cd linux
 
  cd linux
 
==== compiling ====
 
 
Next you have to configure the kernel:
 
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
 
+
#optional#make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
Then building the kernel:
 
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
 
  make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
 
You can use the "-j" flag to improve compilation time. If you have a dual core machine you can use "-j 3", for a quad core machine you can use "-j 6", and so on.
 
 
=== Windows ===
 
 
TODO
 
  
 
=== OS X ===
 
=== OS X ===
 
+
The Kernel source requires a case-sensitive filesystem. If you do not have a HFS+ Case-sensitive partition that can be used, create a disk image with the appropriate format.
==== getting the compiler ====
+
Ensure latest Xcode and command line tools are installed from [http://developer.apple.com/downloads Apple Developer Connection]
Ensure latest Xcode and command line tools are installed from [http://developer.apple.com/downloads Apple Developer Connection] then
+
==== Macports ====
Downoad and install an GNU ARM toolchain such as [http://www.yagarto.de/#downloadmac yagarto]
+
Install [http://guide.macports.org/#installing macports]
 
+
port install arm-none-eabi-gcc
Another option is the MacPorts arm-none-eabi-*:
 
 
 
 
  port install arm-none-eabi-binutils
 
  port install arm-none-eabi-binutils
 
+
  cd /opt
==== getting the sources ====
 
 
 
create a directory where you can work on the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.
 
 
 
 
  mkdir raspberrypi
 
  mkdir raspberrypi
 
  cd raspberrypi  
 
  cd raspberrypi  
  git clone https://github.com/raspberrypi/linux.git
+
  git clone git://github.com/raspberrypi/linux.git
 
  cd linux
 
  cd linux
 
==== compiling ====
 
 
Next you have to configure the kernel: (the running kernel config can be found in <code>/proc/config.gz</code> on your RPi)
 
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- oldconfig
 
 
or if you used the MacPorts
 
 
  make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- oldconfig
 
  make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- oldconfig
 
Then building the kernel:
 
make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- -k
 
 
or if you used the MacPorts
 
 
  make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- -k
 
  make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- -k
 
You can use the "-j" flag to improve compilation time. If you have a dual core machine you can use "-j 3", for a quad core machine you can use "-j 6", and so on. (Don't use these for the oldconfig option because it messes up the input and output).
 
 
 
'''If you get an error message that elf.h is missing'''
 
'''If you get an error message that elf.h is missing'''
 
install [http://guide.macports.org/#installing macports]
 
install libelf and symlink to /usr/libelf:
 
 
  sudo port install libelf && sudo ln -s /opt/local/include/libelf /usr/include/libelf
 
  sudo port install libelf && sudo ln -s /opt/local/include/libelf /usr/include/libelf
copy [http://opensource.apple.com/source/dtrace/dtrace-48/sys/elf.h?txt elf.h] and [http://opensource.apple.com/source/dtrace/dtrace-48/sys/elftypes.h?txt elftypes.h] to /usr/include
+
From opensource.apple.com, download and copy [http://opensource.apple.com/source/dtrace/dtrace-48/sys/elf.h?txt elf.h] and [http://opensource.apple.com/source/dtrace/dtrace-48/sys/elftypes.h?txt elftypes.h] to /usr/include
  
 
Edit elf.h and add
 
Edit elf.h and add
Line 262: Line 125:
 
  #define R_MIPS_HI16      5
 
  #define R_MIPS_HI16      5
 
  #define R_MIPS_LO16      6
 
  #define R_MIPS_LO16      6
 +
'''If you get a "SEGMENT_SIZE is undeclared" error'''
 +
open the Makefile and change the line:
 +
NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
 +
to
 +
NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) -Dlinux
  
= Final step: Making the 'kernel.img' for your Pi =
+
==== Yagarto ====
 
+
Downoad and install an GNU ARM toolchain such as [http://www.yagarto.de/#downloadmac yagarto].
Finally you need to build a kernel.img for your Pi to boot from. The next two sections describe how to do this, depending on which firmware/bootloader version you're using.
+
cd /opt
 
+
mkdir raspberrypi
== Image Generation For Latest Firmware ==
+
cd raspberrypi  
 
+
  git clone git://github.com/raspberrypi/linux.git
With the latest firmware (available from https://github.com/raspberrypi/firmware), you no longer need to create an explicit kernel image; you can directly use Image or zImage from the kernel build process as /boot/kernel.img.
+
cd linux
 
+
  cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
== Image Generation For Older Firmware ==
+
  make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- oldconfig
 
+
make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- -k
For this, you need the mkimage tool from the raspberrypi github repository:
 
 
 
  git clone https://github.com/raspberrypi/tools
 
 
 
In tools/mkimage, you'll find a python script called 'imagetool-uncompressed.py':
 
 
 
  usage : imagetool-uncompressed.py <kernel image>
 
 
 
After building your linux kernel, you'll find the kernel image you require in 'arch/arm/boot/Image' of the linux directory. Convert your kernel image with the script:
 
 
 
  python imagetool-uncompressed.py path/to/linux/arch/arm/boot/Image
 
 
 
= Transferring The Image To The Raspberry Pi =
 
  
 +
== Transferring The Build ==
 
Then you have to transfer this img file to the /boot directory and install the compiled modules. Unfortunately the compiled modules are not in a single place, there are two options of installing them.
 
Then you have to transfer this img file to the /boot directory and install the compiled modules. Unfortunately the compiled modules are not in a single place, there are two options of installing them.
  
Line 295: Line 151:
 
  cd linux
 
  cd linux
 
  make modules_install
 
  make modules_install
 
+
If you got "Permission denied" when doing <code>cd linux</code>, try:
 +
sudo sh -c "cd linux ; make modules_install"
 
If that is not an option, you can also install the modules into a temporary folder:
 
If that is not an option, you can also install the modules into a temporary folder:
 
  mkdir /tmp/modules
 
  mkdir /tmp/modules
 
  make ARCH=arm modules_install INSTALL_MOD_PATH=/tmp/modules
 
  make ARCH=arm modules_install INSTALL_MOD_PATH=/tmp/modules
 
 
Now you have to copy the contents of that directory to /lib/modules on the SD card.
 
Now you have to copy the contents of that directory to /lib/modules on the SD card.
 +
scp linux/arch/arm/boot/Image <user>@<host>:/boot/kernel.img
 +
scp -r /tmp/modules/* <user>@<host>://lib/modules/
 +
Once you've done those two steps, you are ready to put the SD card in and try booting your new system!
  
Once you've done those two steps, you are ready to put the SD card in and try booting your new system!
+
Note: if /tmp/modules contains symlinks they will be followed and if they are recursive this is a problem. A safer way to copy is to use tar:
 +
  cd /tmp/modules/lib/modules; tar cJf - * | ssh <user>@<host> '(cd /lib/modules; tar xJf -)'
  
 +
=References=
 +
<references/>
 
{{Template:Raspberry Pi}}
 
{{Template:Raspberry Pi}}
[[Category: RaspberryPi]]
+
[[Category:RaspberryPi]]

Revision as of 13:23, 15 October 2012

Back to the Hub.


Software & Distributions:

Software - an overview.

Distributions - operating systems and development environments for the Raspberry Pi.

Kernel Compilation - advice on compiling a kernel.

Performance - measures of the Raspberry Pi's performance.

Programming - programming languages that might be used on the Raspberry Pi.

Overview

First, you are going to get and build the linux kernel and its modules using a suitable compiler (a "cross-compiler" if you aren't building it on the same hardware you will be running it on) and then you are going to create a kernel image from the uncompressed kernel (Image) to place on the sd, along with the modules you build alongside it.

From the Raspberry pi

Firmware

Install git first, as below in Kernel compilation.

cd /opt
git clone --depth 1 git://github.com/raspberrypi/firmware.git
cd firmware/boot
cp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf /boot/
cd /opt
rm -r firmware

Kernel compilation

Debian pre-build

apt-get update
apt-get -y dist-upgrade
apt-get -y install git gcc make tmux

Arch Linux pre-build

pacman -Syu
pacman -S git gcc make tmux

Common

cd /opt
mkdir raspberrypi
cd raspberrypi
git clone --depth 1 git://github.com/raspberrypi/linux.git
cd linux
zcat /proc/config.gz > .config
#optional#make menuconfig # i.e. if you want to alter the configuration. You'll need to first run # apt-get install libncurses5-dev
tmux new -s make
nice make; nice make modules
[Ctrl]+[B],[D]
##############… 5 to 11 hours later…
tmux a -t m
[Ctrl]+[D]
cp arch/arm/boot/Image /boot/kernel.img
make ARCH=arm modules_install INSTALL_MOD_PATH=/
cd /opt
rm -r raspberrypi
shutdown -r now;

From a foreign machine

Firmware

cd /opt
git clone git://github.com/raspberrypi/firmware.git
cd firmware/boot
scp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf <user>@<host>:/boot/

After the first time:

cd /opt/firmware
git pull
cd boot
scp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf <user>@<host>:/boot/

Kernel cross compilation

You can use the "-j" flag to improve compilation time. If you have a dual core machine you can use "-j 3", for a quad core machine you can use "-j 6", and so on. Also if your trying to rebuild an existing kernel, the proper .config file can be obtained from the raspberry pi using zcat /proc/config.gz > .config

Ubuntu

apt-get install git gcc-arm-linux-gnueabi make ncurses-dev
cd /opt
mkdir raspberrypi
cd raspberrypi 
git clone git://github.com/raspberrypi/linux.git
cd linux
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
#optional#make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k

Gentoo Linux

crossdev -S -v -t arm-unknown-linux-gnueabi
mkdir raspberrypi
cd raspberrypi 
git clone git://github.com/raspberrypi/linux.git
cd linux
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-unknown-linux-gnueabi- oldconfig
#optional#make ARCH=arm CROSS_COMPILE=/usr/bin/arm-unknown-linux-gnueabi- menuconfig
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-unknown-linux-gnueabi- -k

crossdev should create a cross-toolchain using the latest stable versions of the required packages. If it fails, you can specify exact versions by removing the "-S" flag and adding the "--b", "--g", "--k" and "--l" flags.

On 2012-05-06, cross -S -v -A gnueabi arm works just fine

Arch Linux

pacman -S git
yaourt -S arm-linux-gnueabi-gcc
cd /opt
mkdir raspberrypi
cd raspberrypi 
git clone git://github.com/raspberrypi/linux.git
cd linux
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
#optional#make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k

OS X

The Kernel source requires a case-sensitive filesystem. If you do not have a HFS+ Case-sensitive partition that can be used, create a disk image with the appropriate format. Ensure latest Xcode and command line tools are installed from Apple Developer Connection

Macports

Install macports

port install arm-none-eabi-gcc
port install arm-none-eabi-binutils
cd /opt
mkdir raspberrypi
cd raspberrypi 
git clone git://github.com/raspberrypi/linux.git
cd linux
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- oldconfig
make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- -k

If you get an error message that elf.h is missing

sudo port install libelf && sudo ln -s /opt/local/include/libelf /usr/include/libelf

From opensource.apple.com, download and copy elf.h and elftypes.h to /usr/include

Edit elf.h and add

#define R_386_NONE        0
#define R_386_32          1
#define R_386_PC32        2
#define R_ARM_NONE        0
#define R_ARM_PC24        1
#define R_ARM_ABS32       2
#define R_MIPS_NONE       0
#define R_MIPS_16         1
#define R_MIPS_32         2
#define R_MIPS_REL32      3
#define R_MIPS_26         4
#define R_MIPS_HI16       5
#define R_MIPS_LO16       6

If you get a "SEGMENT_SIZE is undeclared" error open the Makefile and change the line:

NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)

to

NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) -Dlinux

Yagarto

Downoad and install an GNU ARM toolchain such as yagarto.

cd /opt
mkdir raspberrypi
cd raspberrypi 
git clone git://github.com/raspberrypi/linux.git
cd linux
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- oldconfig
make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- -k

Transferring The Build

Then you have to transfer this img file to the /boot directory and install the compiled modules. Unfortunately the compiled modules are not in a single place, there are two options of installing them.

Boot your RaspberryPi and mount the linux directory over the network using sshfs:

cd /mnt
mkdir linux
sshfs <user>@<host>:<path/to/linux> linux
cd linux
make modules_install

If you got "Permission denied" when doing cd linux, try:

sudo sh -c "cd linux ; make modules_install"

If that is not an option, you can also install the modules into a temporary folder:

mkdir /tmp/modules
make ARCH=arm modules_install INSTALL_MOD_PATH=/tmp/modules

Now you have to copy the contents of that directory to /lib/modules on the SD card.

scp linux/arch/arm/boot/Image <user>@<host>:/boot/kernel.img
scp -r /tmp/modules/* <user>@<host>://lib/modules/

Once you've done those two steps, you are ready to put the SD card in and try booting your new system!

Note: if /tmp/modules contains symlinks they will be followed and if they are recursive this is a problem. A safer way to copy is to use tar:

 cd /tmp/modules/lib/modules; tar cJf - * | ssh <user>@<host> '(cd /lib/modules; tar xJf -)'

References