Fox Kibble

Arthur Shipkowski's personal site

Cross-compiling Python With _ctypes Support

I found myself in need of a cross-compiled Python 2.6 to run some hardware test code on an MPC8315ERDB evaluation board. I found Paul Gibson’s guide and patches for cross-compiling Python useful, but they didn’t support the ctypes module, which was needed for the hardware interface code, based around a C shared library. In fact, further searching turned up that one should not expect ctypes to be available for cross-compilation.

I dug into the code, though, and discovered the issue is that _ctypes has its own configure call in setup.py. Pass in the right parameters, and all is well. In my case, my patch (atop of the pre-existing one) looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--- Python/setup.py 2010-04-07 15:00:28.183555632 -0400
+++ Python-xcompile/setup.py    2010-04-07 15:33:35.443567686 -0400
@@ -17,7 +17,7 @@
from distutils.command.install_lib import install_lib

# This global variable is used to hold the list of modules to be disabled.
-disabled_module_list = ['_ctypes']
+disabled_module_list = []

def add_dir_to_list(dirlist, dir):
"""Add the directory 'dir' to the list 'dirlist' (at the front) if
@@ -1705,7 +1705,7 @@
ffi_configfile):
from distutils.dir_util import mkpath
mkpath(ffi_builddir)
-                config_args = []
+                config_args =  sysconfig.get_config_var("CONFIG_ARGS").split(" ")

# Pass empty CFLAGS because we'll just append the resulting
# CFLAGS to Python's; -g or -O2 is to be avoided.

For the curious, below are the key steps for cross-compilation of Python outside LTIB (since it was for test, there was no need to incorporate it) for the MPC8315ERDB – it may work for MPC8313-based setups as well. You probably want to look at the original post for slimming the tree down once the build is done, and if you have modules for which support isn’t found, change the add_dir_to_list lines to point at your build system’s library and include directories rather than /usr/local.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tar xzf Python-2.6.4.tgz
cd Python-2.6.4
./configure
make python Parser/pgen
mv python hostpython
mv Parser/pgen Parser/hostpgen
make distclean
patch -p1 < ~/Python-2.6.4-xcompile.patch
#
# Insert my patch here
vim setup.py
#
# Put LTIB tools in $PATH
PATH=$PATH:/opt/freescale/usr/local/gcc-4.1.78-eglibc-2.5.78-1/powerpc-e300c3-linux-gnu/bin/
CROSS=powerpc-e300c3-linux-gnu- CC=${CROSS}gcc CXX=${CROSS}g++ AR=${CROSS}ar RANLIB=${CROSS}ranlib ./configure --host=powerpc-e300c3-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/python
make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED="powerpc-e300c3-linux-gnu-gcc -shared" CROSS_COMPILE=${CROSS} CROSS_COMPILE_TARGET=yes
make install HOSTPYTHON=./hostpython BLDSHARED="powerpc-e300c3-linux-gnu-gcc -shared" CROSS_COMPILE=${CROSS} CROSS_COMPILE_TARGET=yes prefix=~/Downloads/Python-2.6.4/_install