Vous êtes sur la page 1sur 3

Cross Compiling Python for Embedded Linux

, 6 8 2013, 11:04

Cross Compiling Python for Embedded Linux


Posted by Paul Gibson on 16 November 2011, 2:48 pm

This article details how to cross compile Python for the ARM and PowerPC platforms. It should apply equally to other platforms as well, just plug-in the correct cross-compiler. The article supports Python versions, 2.6.6 (Thanks to evadeflow), 2.7.2, 2.7.3 (thanks to Lothsahn) and 3.1.1, 3.1.2, 3.1.3, 3.2.2. This page has been translated into <a href=http://www.webhostinghub.com/support/es/misc/cruzada-para-linux>Spanish</a> language by Maria Ramos from <a href=http://www.webhostinghub.com/support/edu>Webhostinghub.com/support/edu</a>. Firstly, download the Python that you want to use from http://www.python.org/ Unpack the Python package using tar:
t a rx v z fP y t h o n 2 . 7 . 2 . t g z

This will create a directory called Python-2.7.2.


c dP y t h o n 2 . 7 . 2

Goto the directory:

Then run these commands to build the host components:


. / c o n f i g u r e m a k ep y t h o nP a r s e r / p g e n m vp y t h o nh o s t p y t h o n m vP a r s e r / p g e nP a r s e r / h o s t p g e n m a k ed i s t c l e a n

Download the correct patch for your version of Python: Python-2.6.6-xcompile.patch Python-2.7.2-xcompile.patch (With ctypes thanks to Jaux (jaux.net) Python-2.7.3-xcompile.patch ( Thanks to Lothsahn) Python-3.1.1-xcompile.patch Python-3.1.2-xcompile.patch Python-3.1.3-xcompile.patch

Python-3.2.2-xcompile.patch Then apply the patch:


p a t c hp 1<P y t h o n 2 . 7 . 2 x c o m p i l e . p a t c h

Then run this (where ~/Python-2.7.2/_install/ is your desired installation path). Note also that you must replace all instances of the cross compiler and the host build system in the lines below. If you are on an x86_64 machine, then you should use x86_64-linux-gnu as the host.
C C = p p c _ 6 x x g c cC X X = p p c _ 6 x x g + +A R = p p c _ 6 x x a rR A N L I B = p p c _ 6 x x r a n l i b. / c o n f i g u r eh o s t

m a k eH O S T P Y T H O N = . / h o s t p y t h o nH O S T P G E N = . / P a r s e r / h o s t p g e nB L D S H A R E D = " p p c _ 6 x x g c cs h a r e d "

m a k ei n s t a l lH O S T P Y T H O N = . / h o s t p y t h o nB L D S H A R E D = " p p c _ 6 x x g c cs h a r e d "C R O S S _ C O M P I L E = p p c _

This will install all your python binaries and libraries in ~/Python-2.7.2/_install. Copy the entire _install directory to the device, setup the PATH environment variable to include the path the Python executable and run:
p y t h o nl i b / p y t h o n 2 . 7 / t e s t / t e s t _ _ _ a l l _ _ . p y

and hopefully all the tests will run correctly. To speed up directory to Python files that depends the importing of Python modules on the target, I recommend to zip up the lib make a file called python27.zip. This means that we do not have to copy all the to the target, just the one zip file. This technique might also save space, but on your file system. On the host machine:

c d_ i n s t a l l / l i b / p y t h o n 2 . 7 z i pryp y t h o n 2 7 . z i p.

Delete libpythonxxx.a site-packages, lib-dynload, config and anything else you do not need from the python27.zip file. Copy the _install/bin/python to the /usr/bin directory on the target: Copy the python27.zip file to the /usr/lib directory on the target: Create a directory on the target called python2.7 in the /usr/lib directory and copy the following directories to that directory: ./lib/python2.7/config ./lib/python2.7/lib-dynload ./lib/python2.7/site-packages Your directory structure on the target must be as follows:
/ u s r / l i b#l s

p y t h o n 2 . 7p y t h o n 2 7 . z i p / p y t h o n / l i b / p y t h o n 2 . 7#l s c o n f i gl i b d y n l o a ds i t e p a c k a g e s

Set the PYTHONHOME environment variable to /usr/ and you are ready to run Python on the target.

Building SQLLite and other dependancies


Thanks to Lothsahns 2.7.3 patch, it is now possible to build sqlite3, bz2, gz, ssl and ctypes. Lothsahn provides the following instructions: To do the compilation, the dependencies must be in the include and lib folder one path up from the python install. For example: <base directory>/include < header files go here <base directory>/lib < shared object files go here /Python-2.7.3 <Python sources go here during compile Dependencies include libffi, sqlite, openssl, bzip2, zlib, etc.

Credits
The above patches are based on Chris Lambachers patches for Python 2.5 here: http://whatschrisdoing.com/blog/2006/10/06/howto-cross-compile-python-25/

Other links and credits:


http://www.ailis.de/~k/archives/19-ARM-cross-compiling-howto.html
Filed under arm, Cross-Compiling, Embedded Linux, Linux, PowerPC, Python Linux, PowerPC, Python Tagged Cross-Compiling, Embedded,