Compiling for Raspberry Pi from Odroid U2/U3

Created by: jgmdev
Date: 06/13/2015 @ 7:07:54 pm
Views: 380

When I bought my raspberry pi I got the B model first version. This version suffers a lot from power and usb issues even with an external powered usb hub. As a consequence my pi started to lock down so I couldn't build wxPHP for the Pi. I was preparing to put it to rest, but I noticed the arm deb for raspbian of previous version was downloaded more than 100 times. I decided to give it a go and try to compile for the pi using Qemu, for those that may be using wxPHP on their Pi's.

After searching the Internet for howto's about "raspbian on qemu", I found I needed a special kernel compiled for qemu, as the binary (qemu-system-arm)  and parameters required to make it boot. I found this project rpiqemuwindows which is for raspbian qemu emulation on Windows, but I use Linux. With no previous experience with qemu I decided to download the provided zip files and inspected the commands used to launch the qemu instance with raspbian. The zip file includes a kernel and raspbian image from 2012.

So on my ArchLinux workstation I installed the qemu package and was ready to roll. On the way I noticed that qemu has arm support for specific cpus and you need a kernel for each of them. The kernel distributed by the rpiqemuwindows file was for a CPU that is even slower than that of the PI. Also you can't increase the CPU clock rate and with the rapsbian image you are limited on the amount of ram you can use. In conclusion..., running the raspbian image on qemu was even slower than running it on the actual Pi.

So I kept searching and found about binfmt-support and qemu-user-static, which basically lets you run arm binaries under other cpu architectures like x86_64. Then I found some nice helper scripts (raspbian-tools) by Jeffrey Fernandez which will mount the raspbian image for you using kpartx and chroot to it. With some confidence I installed binfmt-support and qemu-user-static archlinux packages from the aur project (since archlinux doesn't support them officially), but to my dismiss it wasn't working properly and the binfmt wasn't registering qemu as a valid arm interpreter on arch linux :(. So I decided to launch a debian virtual machine and try it there. I assigned the VM some juicy resources (4GB of ram, 4 CPU Threads at 100%), to my surprise the raspbian-tools worked perfectly under Debian, finally I got chroot access to a raspbian image, yayyy!! :D.

After getting chroot access to rapsbian I started compiling wxPHP, but... I also noticed it was darn slow, what a party pooper :(. The qemu emulation/translation for the arm architecture is slow or it is limiting the cpu clock rate some how. In my despair I stopped the compilation process and finally, I gave up! Then I looked back and saw my Odroid U2 idling on a special furniture prepared just for it, and I thought, "wait a second, this thing has an arm cpu on it maybe I can chroot to the raspbian image and compile for the raspberry pi with it :D". Took the raspbian-tools and stripped the qemu bits, placed them on my Odroid U2 and bang!!! the chroot worked perfectly, I could even use all 4 cores of the odroid and 2 gigs of ram for compiling.

Conclusion, I build wxPHP in about 1 hour with the Odroid U2 which would have taken almost 1 day if building on the raspberry pi or 2-3 days with qemu :D. Thanks to Jeffrey Fernandez for sharing the raspbian-tools and everyone who shares knowledge on the Internet. And just like that there are now raspbian binaries of wxPHP even if just used by 2 or 3 of those 100 hundred downloads :D.