Illusory stillness

mumble mumble techblog

(Not) Compiling a Raspberry Pi Kernel on OSX

One thing leads to another and suddenly you want to compile a new Raspberry Pi kernel, and your most convenient host is a Mac.

I started with some existing notes about this — but I found they needed some tweaking and some updates have happened which make things a little easier.

I’m using crosstool-ng from Homebrew on OS X 10.9.

Instructions

Install packages

These are the packages I needed from Homebrew.

1
2
3
4
5
6
7
8
9
brew tap homebrew/versions
brew install gcc48
brew install crosstool-ng
brew tap homebrew/dupes
brew install grep
brew install gettext
brew link --force gettext
brew install ncurses
brew link --force ncurses

Note that link --force is not a particularly nice thing to do, and will (e.g.) prevent you from reinstalling crosstool-ng without uninstalling gettext first. But it appears to be necessary, as eglibc build fails without it (and I don’t understand if there’s an option that will let me specity gettext’s location to eglibc)

Make crosstools use GNU grep

1
chmod u+w /usr/local/Cellar/crosstool-ng/1.19.0/lib/ct-ng.1.19.0/paths.sh

Edit paths.sh, changing the line about grep to read:

1
export grep="/usr/local/bin/ggrep"

(Is there a user-level config way to do this w/o poking around in the Homebrew cellar?)

Create disk images

Crosstools needs case-sensitive file systems to work on, we’ll create two images, one to compile in and one to host the binaries.

1
2
3
4
5
6
7
8
9
cd ~
mkdir xtools-images
cd xtools-images
hdiutil create -size 6g \
   -attach -type SPARSE -fs 'Case-sensitive Journaled HFS+' \
   -volname xtools-build-env xtools-build-env
hdiutil create -size 500m \
   -attach -type SPARSE -fs 'Case-sensitive Journaled HFS+' \
   -volname xtools xtools

Set up crosstools-ng

Thanks to Kentzo there is a Raspberry Pi configuration already in crosstools-ng in Homebrew, and there only need minor adjustments.

1
2
cd /Volumes/xtools-build-env/
ct-ng armv6-rpi-linux-gnueabi

Now extract this repository onto the drive image. It contains a .config file and a couple of patches.

1
git clone homeGit/peter/raspberry-pi-osx-crosstools-config .

Then inspect the configuration:

1
ct-ng menuconfig

Check that all the paths under “Paths” point to somewhere logical (mounted on your disk images)

For extra speed, set “Paths::Build behavior::number of parallel jobs” to twice your number of cores. This also means you will need to also up the ulimit before building. (If there are weird errors about no rules to make targets related to language localization, you probably forgot to do this.) So now do

1
2
ulimit -n 1024
ct-ng build

Hopefully this will work.

Test the compiler

1
2
3
cat > test.c
#include <stdio.h>
int main() { printf("Hello, world!\n"); return 0; }

(press ctrl-d to finish)

1
/Volumes/xtools/armv6-rpi-linux-gnueabi/bin/armv6-rpi-linux-gnueabi-gcc -o test test.c

Copy test over to your RPi and try running it.

1
2
scp test pi@raspberrypi.local:
ssh pi@raspberrypi.local /home/pi/test

Compiling a Kernel

Well this is what I signed up for.

I wanted to build the kernel described here.

I’m cribbing from these notes.

1
2
cd /Volumes/xtools-build-env/
git clone https://github.com/petli/linux.git -b rpi-3.10-usb-sound-backport

Copy the existing config.

1
2
3
scp pi@raspberrypi.local:/proc/config.gz ./linux-config.gz
gunzip linux-config.gz
cp linux-config linux/.config
1
make ARCH=arm CROSS_COMPILE=/Volumes/xtools/armv6-rpi-linux-gnueabi/bin/armv6-rpi-linux-gnueabi- oldconfig

menuconfig doersn’t work for me because of some ncurses problem but there is make nconfig

1
make ARCH=arm CROSS_COMPILE=/Volumes/xtools/armv6-rpi-linux-gnueabi/bin/armv6-rpi-linux-gnueabi- nconfig
1
make ARCH=arm CROSS_COMPILE=/Volumes/xtools/armv6-rpi-linux-gnueabi/bin/armv6-rpi-linux-gnueabi- nconfig

Now it is complaining about lack of “elf.h” Where is it looking for elf.h?

1
2
3
4
sudo cp ../elf.h ../elftypes.h /usr/include
 
brew install libelf
sudo ln -s /usr/local/include/libelf /usr/include/libelf

… and some other advice from the internet, which didn’t work either. It’s not the ‘echo’ problem, as that’s been long fixed.

Giving up threshold reached

…. oh but adjusting Linux enough to compile in an OSX environment is going to be bad. gonna investigate just installing an Ubuntu VM under VirtualBox and compiling there

honestly a kernel compile on the Pi itself would have finished by now