Cross Compile Python For Embedded Linux
Cross Compile Python For Embedded Linux
, 6 8 2013, 11:04
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
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
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.
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/