您的位置:首页 > 运维架构 > Linux

Cross Compiling Python for Embedded Linux

2012-04-17 09:54 351 查看

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 and
3.1.1, 3.1.2, 3.1.3, 3.2.2.

Firstly, download the Python that you want to use from http://www.python.org/
Unpack the Python package using tar:

tar -xvzf Python-2.7.2.tgz

This will create a directory called Python-2.7.2. Goto the directory:

cd Python-2.7.2

Then run these commands to build the host components:

./configure

make python Parser/pgen

mv python hostpython

mv Parser/pgen Parser/hostpgen

make distclean

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-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:

patch -p1 < Python-2.7.2-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.

CC=ppc_6xx-gcc
CXX=ppc_6xx-g++ AR=ppc_6xx-ar RANLIB=ppc_6xx-ranlib ./configure
--host=ppc-linux --build=x86_64-linux-gnu
--prefix=/python

make HOSTPYTHON=./hostpython
HOSTPGEN=./Parser/hostpgen
BLDSHARED="ppc_6xx-gcc -shared"
CROSS_COMPILE=ppc_6xx- CROSS_COMPILE_TARGET=yes
HOSTARCH=ppc-linux BUILDARCH=x86_64-linux-gnu

make install
HOSTPYTHON=./hostpython BLDSHARED="ppc_6xx-gcc -shared"
CROSS_COMPILE=ppc_6xx- CROSS_COMPILE_TARGET=yes
prefix=~/Python-2.7.2/_install

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:

python lib/python-2.7/test/test___all__.py

…and hopefully all the tests will run correctly.

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

cd _install/lib/python2.7

zip -r -y python27.zip .

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:

/usr/lib
# ls

python2.7 python27.zip

/python/lib/python2.7
# ls

config lib-dynload site-packages

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

The above patches are based on Chris Lambacher’s 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

Python-2.6.2-xcompile.patch

Filed under arm,

Cross-Compiling,
Embedded Linux,
Linux,
PowerPC,
PythonTagged Cross-Compiling,
Embedded,
Linux, PowerPC,
Python
Comment (RSS) | Trackback
| Permalink
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: