How to Cross Compile Python a Step by Step Guide

Introduction

As you know, cross compiling Python is not an easy task. Some patches can be found in sourceforge http://sourceforge.net/tracker/index.php?func=detail&aid=1006238&group_id=5470&atid=305470, but they allow you to cross compile Python only in release 2.4.2.

The specifity of the Python compilation process is to execute things built for the target system on the build system. When cross compiling, if the target system is different from the build system (compiling an ARM Python on an intel platform for example), then you will sadly see your compilation process stop.

Let's assume one would like to compile a given release of Python on a PXA platform, then it should have access to an ARM linux filesystem providing applications such as GCC. We all know that our embedded linux file systems work on hardware based on few RAM and flash memory. And it is certainly not probable to see a compiler such as gcc available.

The solution is therefore to have such a file system emulated on your PC. The PC is not hardware limited, it has enough RAM, consequent hard disk, network card ...

This solution is called QEMU http://fabrice.bellard.free.fr/qemu/ developed by Fabrice Bellard. QEMU is a generic and open source processor emulator which can emulate i386, x86_64, ARM, mips, PowerPC and sparc systems.

Having such a file system emulated on your PC not only will allow you to compile any release of Python but will also help you to debug complete embedded system applications before the hardware is ready.

Then, it's up to you to decide how to proceed. Either you prefer to download a complete linux distribution compiled for ARM (such as Debian) or ARM kernel compiled with the right options. In the first case, you will have a complete file system that you will update according to your needs (gcc for example) with apt-get. And in the second case, through your kernel line command, you will decide to mount through NFS an already available ARM file system (such as Sargebook-lite for example).

This How-To will present you a way to cross compile Python 2.5.1 on a PXA270 (arm) platform using QEMU 0.9.0 on a i386 Ubuntu Linux distribution emulating an ARM 2.6.16 kernel configured for network and NFS mount. The used root file system is a slightly modified version of Sargebook-lite with a number of development packages (such as GCC) installed.

In the meantime, if someone succeeds to cross compile python on other platforms with the QEMU method, he is very encouraged to provide to the site the kernel/root filesystems he has used in order to share with others his findings.

Python 2.5.1 cross compilation

Once you have started your NFS ARM-Debian root file system (user:root, password:root) by following the procedure indicated in arm-debian-on-ubuntu, do the following preliminary actions:

  • create the /opt directory since it is not present (per default) in the Sargebook-lite root file sytem by type the command mkdir /opt

  • in your host machine, download Python-2.5.1.tgz by typing wget http://www.python.org/ftp/python/2.5.1/Python-2.5.1.tgz, I couldn't have wget work on the ARM-Debian emulated distribution. Move the downloaded file in the host NFS folder, in our case /opt/fs/openpsion. And then untar it on the emulated distro (under /opt/external_packages/python/2.5.1/source) typing the following commands:

mkdir /opt/external_packages
mkdir /opt/external_packages/python
mkdir /opt/external_packages/python/2.5.1
mkdir /opt/external_packages/python/2.5.1/source
mkdir /opt/external_packages/python/2.5.1/compiled
tar xvzf /Python-2.5.1.tgz -C /opt/external_packages/python/2.5.1/source
mv /opt/external_packages/python/2.5.1/source/Python-2.5.1/* /opt/external_packages/python/2.5.2/source
rm -rf /opt/external_packages/python/2.5.1/source/Python-2.5.1

And the launch the cross compilation by using the internal ARM gcc compiled of the Sargebook-lite distro by successively typing:

cd /opt/external_packages/python/2.5.1/source
./configure --prefix=/opt/external_packages/python/2.5.1/compiled
make
make install

The cross compilation takes some time on the emulated system.

And then, on your host Linux system, create a tarball you will use on any of your ARM embedded systems:

cd /opt/external_packages/python/2.5.1/compiled
tar -zvcf arm-Python-2.5.1.tar.gz *

Enjoy your newly compiled ARM Python 2.5.1 package!


Written by David Sayada.

Edit History Last Modified March 19, 2008