Skip to content
 

Cross Compiling Python for Embedded Linux

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:

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:

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.

 

Building SQLLite and other dependancies

Thanks to Lothsahn’s 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 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

162 Comments

  1. TS says:

    When I tried Dave L’s 2.7.2 patch i got this:
    ]# patch -p1 < 2.7.2.patch
    patching file configure
    patching file Makefile.pre.in
    Hunk #1 FAILED at 182.
    Hunk #2 FAILED at 215.
    Hunk #3 FAILED at 407.
    Hunk #4 FAILED at 542.
    Hunk #5 FAILED at 925.
    Hunk #6 FAILED at 1049.
    6 out of 6 hunks FAILED — saving rejects to file Makefile.pre.in.rej
    patching file setup.py

  2. Paul Gibson says:

    Hi TS,

    Yes, I have the same problem. I have a new patch that I will try and provide tonight.

    Paul

  3. TS says:

    Thank you very much. Your how-to helped me alot to get Python onto an Embedded Linux system for the company I work for. So far I have been able to successfull get Python 2.7.1 running on the system.

    Again, thanks a lot for taking your time to do all of this.

  4. TS says:

    I forgot to mention that using the 2.7.1 patch disables the ctypes module. Here is the lines that I think cause this problem:
    +# _ctypes fails to cross-compile due to the libffi configure script.
    +if os.environ.has_key(‘PYTHONXCPREFIX’):
    + disabled_module_list.append(‘_ctypes’)
    +

    When I used the patch and cross compiled it, I have ctypes in my library files, but not as a .so module in lib-dynload. Is there any way to fix the patch to allow for ctypes? What I tried so far to get around this problem was downloading and installing Python version 2.6.6, patching it with the 2.6.6 patch and then cross compiling it. Then I took the _ctypes.so from the 2.6.6 lib-dynload and then put it into my 2.7.1 cross compiled version. So far it seems to be working.

  5. CLaymore says:

    Hello all!

    I have this task, which is to make a build of Python 2.7 for the PowerPC CPU.
    I was able to follow this tutorial but i notice that my lib/python2.7/lib-dynload is empty…

    Did you guys had the same problem? If you did, how did you solve it?

    I noticed in the make install output that the make, copies the files to the right place but after that, it cleans the lib-dynload directory :S

  6. Paul Gibson says:

    Hi Claymore,

    It is working for me and the lib-dynload directory is here:
    lib/python2.7/lib-dynload/

    Paul

  7. Paul Gibson says:

    Patch for 2.7.2 and 3.2.2 has been posted.

  8. JaneS says:

    Hi all, please can you help me??? I just cross compile Python 3.2.2 and I have some problems with ELF. Python is normally cross compiles, but not modules.
    Make’s output is something like this :

    *** WARNING: renaming “_struct” since importing it failed: build/lib.linux-x86_64-3.2/_struct.cpython-32m.so: wrong ELF class: ELFCLASS32

    and in /build/tmp.linux-x86_64-3.2/libffi/config.log have this output log:

    /home/jane/libs/Python-3.2.2/Modules/_ctypes/libffi/configure: line 3699: ./conftest: cannot execute binary file
    configure:3701: $? = 126
    configure:3708: error: in `/home/jane/libs/Python-3.2.2/build/temp.linux-x86_64-3.2/libffi’:
    configure:3712: error: cannot run C compiled programs.

    P.S. I understand what the problem is in ./conftest binary file, but how can I solve this.
    in Crosscompiling I use the patch 3.2.2-xcompile.diff but without changesin setup.py.
    P.P.s. I have this problem and in python 2.2.1.
    PLEASE HELP!!!

  9. jaux says:

    Cross-compiling Python 2.7 without ctypes is not cool, I’ll try to work it out next few days, stay turned ;)

  10. jaux says:

    @paul, please contact me, I have the patch for 2.7.2 with ctypes enabled.

  11. apl says:

    @jaux: your 2.7.2 ctypes patch did not work for me :(
    Compiling for arm-unknown-linux-gnueabi-. Does anybody have the solution?
    Thanks!

    --- snip ---
    /home/apl/python/Python-2.7.2/Modules/_ctypes/cfield.c: In function 'PyCField_FromDesc':
    /home/apl/python/Python-2.7.2/Modules/_ctypes/cfield.c:50:29: warning: variable 'length' set but not used [-Wunused-but-set-variable]
    /home/apl/python/Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c: In function 'mmap_resize':
    /home/apl/python/Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c:3193:5: warning: implicit declaration of function 'mremap' [-Wimplicit-function-declaration]
    /home/apl/python/Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c: In function 'sys_trim':
    /home/apl/python/Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c:3612:62: warning: comparison between pointer and integer [enabled by default]
    /tmp/ccgf4gVM.s: Assembler messages:
    /tmp/ccgf4gVM.s:1388: Error: bad instruction `icbi 0,r0'
    /tmp/ccgf4gVM.s:1388: Error: bad instruction `dcbf 0,ip'
    /tmp/ccgf4gVM.s:1397: Error: bad instruction `icbi 0,r4'
    /tmp/ccgf4gVM.s:1397: Error: bad instruction `dcbf 0,r5'
    /tmp/ccgf4gVM.s:1406: Error: bad instruction `icbi 0,r4'
    /tmp/ccgf4gVM.s:1406: Error: bad instruction `dcbf 0,r5'
    /tmp/ccgf4gVM.s:1415: Error: bad instruction `icbi 0,r4'
    /tmp/ccgf4gVM.s:1415: Error: bad instruction `dcbf 0,r5'
    /tmp/ccgf4gVM.s:1424: Error: bad instruction `icbi 0,r4'
    /tmp/ccgf4gVM.s:1424: Error: bad instruction `dcbf 0,r5'
    /tmp/ccgf4gVM.s:1433: Error: bad instruction `icbi 0,r0'
    /tmp/ccgf4gVM.s:1433: Error: bad instruction `dcbf 0,r4'
    /tmp/ccgf4gVM.s:1433: Error: bad instruction `sync'
    /tmp/ccgf4gVM.s:1433: Error: bad instruction `isync'
    --- snip ---

    --- snip ---
    Failed to build these modules:
    _ctypes
    --- snip ---
  12. Jack says:

    HI paul
    i’m from china and my english is pool~ i’m a newer and i tried to follow you instruction .unfortrunately i fail make as i got the error: arm-none-linux-gnueabi-gcc: directory”: No such file or directory
    : warning: missing terminating ” character
    ./Modules/getbuildinfo.c: In function ‘_Py_svnversion’:
    ./Modules/getbuildinfo.c:63: error: missing terminating ” character
    ./Modules/getbuildinfo.c:63: error: expected expression before ‘;’ token

    i have no idea how to fix this . could you help me please thanks !!!

  13. Jack says:

    @Paul Gibson – I just follow the commands you posted and when i finishing patching i made the setting :
    CC=arm-linux-gcc CXX=arm-linux-g++ AR=arm-linux-ar RANLIB=arm-linux-ranlib ./configure –host=arm-linux –build=i386-linux –prefix=$PREFIX

    i successfully passed the configure step but failed in the make step:

    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-linux-gcc -shared” CROSS_COMPILE=arm-linux- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=i386-linux

    the error imformation was shown above.

    do you have any ideas ? thanks!

  14. Michael says:

    Hi,
    I could crosscompile and easily install this on my ARM9 System.
    Python runs – so thanks for the great work. Anyway I’m missung some modules.

    The cross compiling said:

    Python build finished, but the necessary bits to build these modules were not
    found:
    _bsddb _curses _curses_panel
    _sqlite3 _ssl _tkinter
    bsddb185 bz2 dbm
    dl gdbm imageop
    nis readline sunaudiodev
    zlib
    To find the necessary bits, look in setup.py in detect_modules() for the
    module’s name.

    Failed to build these modules:
    _ctypes

    How can I get sqlite3? I need it for my app, is there anything I can do?

  15. William Ward says:

    Thanks for posting this cross-compilation tutorial – one device came with Python preloaded, but I have three others (two BugLabs Bugs) and a rooted Nook Color that I’d like to give a go at compiling newer versions of Python for, and subsequent modules.

    A clear tutorial helps a great deal!

    :)

  16. Paul Gibson says:

    I noticed today that the 2.6 and 2.7 patches above are not working on Linux 3.0 host machines such as Ubuntu 11.10. I have updated version 2.6.6 and 2.7.2 to include a patch that works on Linux 3.0 hosts. Python 3 patches are not affected by this.

    I have also removed all previous 2.6 and 2.7 patches to make the site cleaner.

  17. yesuraj says:

    Hi,
    After following the above steps, i successfully compiled python for arm architecture. But at the end i don’t find the libpython2.7.so.1.0 anywhere.
    Can some one help me in this regard

  18. Paul Gibson says:

    Hi yesuraj,

    We don’t currently build a shared library, only the static libpython2.7.a is generated. You should link against this instead. The shared library isn’t required for running python on the target.

    Paul

  19. yesuraj says:

    Hi,
    I compiled successfully, but i am not able to use ncurses modules in my python scripts. I hope ncurses modules are not build along. I installed ncurses in my ubuntu (The host machine OS that i use, it is “2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux” machine) host machine.
    When i do host configuration, that is before applying patches if i do ./configure
    the ncurses libs status shows as follows,

    checking ncurses.h usability… yes
    checking ncurses.h presence… yes
    checking for ncurses.h… yes

    But after applying patches if i do the follow configure command

    CC=/usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-gcc CXX=/usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-g++ AR=/usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-ar RANLIB=/usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-ranlib ./configure –host=arm-linux –build=x86_64-linux-gnu –prefix=/python

    The ncurses libs status shows as folows,
    checking ncurses.h usability… no
    checking ncurses.h presence… no
    checking for ncurses.h… no

    Can some one help me to build my python with ncurses modules

    Thanks in advance

    • Paul Gibson says:

      Hi yesuarj,

      I would suggest cross-compiling ncurses individually and then pass in CFLAGS and LDFLAGS to the configure to point to the ncurses that you have cross-compiled.

      eg ./configure CFLAGS=/home/yesuarj/myncurses/include LDFLAGS=/home/yesuarj/myncurses/lib

      Paul

  20. [...] [1]:??Cross Compiling Python for Embedded Linux Linux, Python ← Python ??? /* */ [...]

  21. Hiro says:

    hi,Pual.I can’t build _ctypes. Could you give me some help?

    Python build finished, but the necessary bits to build these modules were not found:
    _bsddb _curses _curses_panel
    _sqlite3 _ssl _tkinter
    bsddb185 bz2 dbm
    dl gdbm imageop
    nis readline sunaudiodev
    zlib
    To find the necessary bits, look in setup.py in detect_modules() for the module’s name.

    Failed to build these modules:
    _ctypes

  22. JKN says:

    Thanks for this useful page BTW. A couple of things:

    1) I think you have a typo in the line which runs the python tests. Instead of

    python lib/Python-2.7/test/test___all___.py

    I ran

    python lib/python-2.7/test/test___all___.py # (lower case ‘p’ in ‘python-2.7′)

    Also note that the test filename has three underscores, then two: test_ _ _all_ _.py

    2) I have got Python built for the ARM with sqlite now ;-) . Briefly, the steps are:

    i) download and cross-compile sqlite3 for your target (ie. before building Python)
    ii) make this cross-compiled sqlite3 accessible to Python 2.7.2, by editing setup.py to look for the sqlite3 header in the location you’ve just built it. You seem to have to add it to be the first entry in the list BTW
    iii) build python as per the page above.
    iv) download to target and test. You also have to download the sqlite3 libraries to the target and eg. set LD_LIBRARY_PATH so that these are found.

    BTW, for my Arm target I used

    CC=arm-none-linux-gnueabi-gcc

    instead of

    CC=ppc_6xx-gcc

    you need to make similar changes to the rest of the commands, pretty obviously.

    HTH
    J^n

  23. Bernhard Renz says:

    Ubuntu 11.04 64Bit distribution has the gcc-arm-linux packages. With this packages and the following commands, i got it to work:

    CC=arm-linux-gnueabi-gcc-4.6 CXX=arm-linux-gnueabi-g++ AR=arm-linux-gnueabi-ar RANLIB=arm-linux-gnueabi-ranlib ./configure –host=arm-linux –build=x86_64-linux-gnu –prefix=/python

    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-linux-gnueabi-gcc-4.6 -shared” CROSS_COMPILE=arm-linux-gnueabi- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=x86_64-linux-gnu

    make install HOSTPYTHON=./hostpython BLDSHARED=”arm-linux-gnueabi-gcc-4.6 -shared” CROSS_COMPILE=arm-linux-gnueabi- CROSS_COMPILE_TARGET=yes prefix=~/Python-3.2.2/_install

  24. Justin says:

    Hi Paul. What would be the easiest way to get an embedded Linux on an ARM Cortex M3 so that I can run through this tutorial? Thanks.

  25. Devika Nair says:

    Thanks for the write-up.

    My target is an AM3517 board which needs to run a node.js based webserver.As per the information from the internet, node.js has got dependency with python and libssl-dev.
    Your post helped to cross compile python for arm. Now my problem is to cross compile libssl-dev and node.js for arm.

    Could you help me with this?

  26. Shekhar says:

    Dear Paul,

    AS per the instructoins provided I tried to build the Python 2.7.2 for my ARM target.
    I reached till the applying the patch…

    When i tried for the next command, i,e
    CC=arm-eabi-gcc-4.4.0 CXX=arm-eabi-g++ AR=arm-eabi-ar RANLIB=arm-eabi-ranlib ./configure -–host=arm-linux -–build=x86_32-linux-gnu –-prefix=/python

    I’m getting the below error. Can you please let me know how to resolve this ?

    checking for –enable-universalsdk… no
    checking for –with-universal-archs… 32-bit
    checking MACHDEP… linux3
    checking EXTRAPLATDIR…
    checking machine type as reported by uname -m… i686
    checking for –without-gcc… no
    checking for arm-linux-gcc… arm-eabi-gcc-4.4.0
    checking whether the C compiler works… no
    configure: error: in `/home/rvs/Projects/pythonbuild/Python-2.7.2′:
    configure: error: C compiler cannot create executables
    See `config.log’ for more details

    In my UBUNTU machine I can find all the CC, CXX, AR, RANLIB under this path, but still i’m unable to procced. Please help on this.
    home/rvs/Projects/trunk/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin

    Regards,
    Shekhar

  27. Paul Gibson says:

    Hi Shekhar,

    Try this:

    CC=arm-eabi-gcc-4.4.0 CXX=arm-eabi-g++ AR=arm-eabi-ar RANLIB=arm-eabi-ranlib ./configure -–host=arm-eabi-–build=i386-linux-gnu –-prefix=/python

    Thanks,
    Paul

  28. Shekhar says:

    Hi Paul, I tried the above command, but once again unsuccessful.
    I’m getting the same error i,e

    checking for arm-linux-gcc… arm-eabi-gcc-4.4.0
    checking whether the C compiler works… no
    configure: error: in `/home/rvenkatachar/Projects/pythonbuild/Python-2.7.2?:
    configure: error: C compiler cannot create executables
    See `config.log’ for more details

    #############################################
    for the information I have pasted below the details of the config.log also.

    config log details
    +++++++++++++++++++++++++++++++++

    ## ———– ##
    ## Core tests. ##
    ## ———– ##

    configure:2764: checking for –enable-universalsdk
    configure:2805: result: no
    configure:2814: checking for –with-universal-archs
    configure:2831: result: 32-bit
    configure:2988: checking MACHDEP
    configure:3136: result: linux3
    configure:3142: checking EXTRAPLATDIR
    configure:3157: result:
    configure:3168: checking machine type as reported by uname -m
    configure:3171: result: i686
    configure:3184: checking for –without-gcc
    configure:3228: result: no
    configure:3249: checking for arm-eabi-gcc
    configure:3276: result: arm-eabi-gcc-4.4.0
    configure:3545: checking for C compiler version
    configure:3554: arm-eabi-gcc-4.4.0 –version >&5
    arm-eabi-gcc-4.4.0 (GCC) 4.4.0
    Copyright (C) 2009 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    configure:3565: $? = 0
    configure:3554: arm-eabi-gcc-4.4.0 -v >&5
    Using built-in specs.
    Target: arm-eabi
    Configured with: /home/jingyu/projects/gcc/android-toolchain/gcc-4.4.0/configure –prefix=/usr/local –target=arm-eabi –host=i686-unknown-linux-gnu –build=i686-unknown-linux-gnu –enable-languages=c,c++ –with-gmp=/home/jingyu/projects/gcc/toolchain_build/obj/temp-install –with-mpfr=/home/jingyu/projects/gcc/toolchain_build/obj/temp-install –disable-libssp –enable-threads –disable-nls –disable-libmudflap –disable-libgomp –disable-libstdc__-v3 –disable-sjlj-exceptions –disable-shared –disable-tls –with-float=soft –with-fpu=vfp –with-arch=armv5te –enable-target-optspace –with-abi=aapcs –with-gcc-version=4.4.0 –with-binutils-version=2.19 –with-gmp-version=4.2.4 –with-mpfr-version=2.4.1 –with-gdb-version=6.6 –with-arch=armv5te –with-multilib-list=mthumb-interwork,mandroid –with-sysroot=/g/users/jingyu/toolchain/cupcake_rel_root –program-transform-name=’s&^&arm-eabi-&’
    Thread model: single
    gcc version 4.4.0 (GCC)
    configure:3565: $? = 0
    configure:3554: arm-eabi-gcc-4.4.0 -V >&5
    arm-eabi-gcc-4.4.0: ‘-V’ option must have argument
    configure:3565: $? = 1
    configure:3554: arm-eabi-gcc-4.4.0 -qversion >&5
    arm-eabi-gcc-4.4.0: unrecognized option ‘-qversion’
    arm-eabi-gcc-4.4.0: no input files
    configure:3565: $? = 1
    configure:3585: checking whether the C compiler works
    configure:3607: arm-eabi-gcc-4.4.0 conftest.c >&5
    /home/rvenkatachar/Projects/trunk/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/bin/ld: crt0.o: No such file: No such file or directory
    collect2: ld returned 1 exit status
    configure:3611: $? = 1
    configure:3649: result: no
    configure: failed program was:
    | /* confdefs.h */
    | #define _GNU_SOURCE 1
    | #define _NETBSD_SOURCE 1
    | #define __BSD_VISIBLE 1
    | #define _BSD_TYPES 1
    | #define _DARWIN_C_SOURCE 1
    | #define _XOPEN_SOURCE 600
    | #define _XOPEN_SOURCE_EXTENDED 1
    | #define _POSIX_C_SOURCE 200112L
    | /* end confdefs.h. */
    |
    | int
    | main ()
    | {
    |
    | ;
    | return 0;
    | }
    configure:3654: error: in `/home/rvenkatachar/Projects/pythonbuild/Python-2.7.2′:
    configure:3656: error: C compiler cannot create executables
    See `config.log’ for more details

    ## —————- ##
    ## Cache variables. ##
    ## —————- ##

    ac_cv_env_CC_set=set
    ac_cv_env_CC_value=arm-eabi-gcc-4.4.0
    ac_cv_env_CFLAGS_set=
    ac_cv_env_CFLAGS_value=
    ac_cv_env_CPPFLAGS_set=
    ac_cv_env_CPPFLAGS_value=
    ac_cv_env_CPP_set=
    ac_cv_env_CPP_value=
    ac_cv_env_LDFLAGS_set=
    ac_cv_env_LDFLAGS_value=
    ac_cv_env_LIBS_set=
    ac_cv_env_LIBS_value=
    ac_cv_env_build_alias_set=set
    ac_cv_env_build_alias_value=i386-linux-gnu
    ac_cv_env_host_alias_set=set
    ac_cv_env_host_alias_value=arm-eabi
    ac_cv_env_target_alias_set=
    ac_cv_env_target_alias_value=
    ac_cv_prog_CC=arm-eabi-gcc-4.4.0

    ## —————– ##
    ## Output variables. ##
    ## —————– ##

    AR=’arm-eabi-ar’
    ARCH_RUN_32BIT=”
    ARFLAGS=”
    BASECFLAGS=”
    BLDLIBRARY=”
    BLDSHARED=”
    BUILDEXEEXT=”
    CC=’arm-eabi-gcc-4.4.0′
    CCSHARED=”
    CFLAGS=”
    CFLAGSFORSHARED=”
    CONFIGURE_MACOSX_DEPLOYMENT_TARGET=”
    CONFIG_ARGS=’ ‘\”–host=arm-eabi’\” ‘\”–build=i386-linux-gnu’\” ‘\”-prefix=/scartch/python’\” ‘\”build_alias=i386-linux-gnu’\” ‘\”host_alias=arm-eabi’\” ‘\”CC=arm-eabi-gcc-4.4.0′\”’
    CPP=”
    CPPFLAGS=”
    CXX=’arm-eabi-g++’
    DEFS=”
    DLINCLDIR=”
    DLLLIBRARY=”
    DYNLOADFILE=”
    ECHO_C=”
    ECHO_N=’-n’
    ECHO_T=”
    EGREP=”
    EXEEXT=”
    EXPORT_MACOSX_DEPLOYMENT_TARGET=’#’
    EXTRAMACHDEPPATH=”
    EXTRAPLATDIR=”
    FRAMEWORKALTINSTALLFIRST=”
    FRAMEWORKALTINSTALLLAST=”
    FRAMEWORKINSTALLAPPSPREFIX=”
    FRAMEWORKINSTALLFIRST=”
    FRAMEWORKINSTALLLAST=”
    FRAMEWORKUNIXTOOLSPREFIX=’/scartch/python’
    GNULD=”
    GREP=”
    HAS_HG=”
    HAVE_GETHOSTBYNAME=”
    HAVE_GETHOSTBYNAME_R=”
    HAVE_GETHOSTBYNAME_R_3_ARG=”
    HAVE_GETHOSTBYNAME_R_5_ARG=”
    HAVE_GETHOSTBYNAME_R_6_ARG=”
    HGBRANCH=”
    HGTAG=”
    HGVERSION=”
    INSTALL_DATA=”
    INSTALL_PROGRAM=”
    INSTALL_SCRIPT=”
    INSTSONAME=”
    LDCXXSHARED=”
    LDFLAGS=”
    LDLAST=”
    LDLIBRARY=”
    LDLIBRARYDIR=”
    LDSHARED=”
    LIBC=”
    LIBFFI_INCLUDEDIR=”
    LIBM=”
    LIBOBJS=”
    LIBRARY=”
    LIBS=”
    LIBTOOL_CRUFT=”
    LINKCC=”
    LINKFORSHARED=”
    LIPO_32BIT_FLAGS=”
    LN=”
    LTLIBOBJS=”
    MACHDEP=’linux3′
    MACHDEP_OBJS=”
    MAINCC=”
    OBJEXT=”
    OPT=”
    OTHER_LIBTOOL_OPT=”
    PACKAGE_BUGREPORT=’http://bugs.python.org/’
    PACKAGE_NAME=’python’
    PACKAGE_STRING=’python 2.7′
    PACKAGE_TARNAME=’python’
    PACKAGE_URL=”
    PACKAGE_VERSION=’2.7′
    PATH_SEPARATOR=’:’
    PKG_CONFIG=”
    PYTHONFRAMEWORK=”
    PYTHONFRAMEWORKDIR=’no-framework’
    PYTHONFRAMEWORKIDENTIFIER=’org.python.python’
    PYTHONFRAMEWORKINSTALLDIR=”
    PYTHONFRAMEWORKPREFIX=”
    RANLIB=’arm-eabi-ranlib’
    RUNSHARED=”
    SGI_ABI=”
    SHELL=’/bin/bash’
    SHLIBS=”
    SIGNAL_OBJS=”
    SO=”
    SOVERSION=’1.0′
    SRCDIRS=”
    SVNVERSION=”
    THREADHEADERS=”
    THREADOBJ=”
    TRUE=”
    UNICODE_OBJS=”
    UNIVERSALSDK=”
    UNIVERSAL_ARCH_FLAGS=”
    USE_SIGNAL_MODULE=”
    USE_THREAD_MODULE=”
    VERSION=’2.7′
    ac_ct_CC=”
    bindir=’${exec_prefix}/bin’
    build_alias=’i386-linux-gnu’
    datadir=’${datarootdir}’
    datarootdir=’${prefix}/share’
    docdir=’${datarootdir}/doc/${PACKAGE_TARNAME}’
    dvidir=’${docdir}’
    exec_prefix=’NONE’
    host_alias=’arm-eabi’
    htmldir=’${docdir}’
    includedir=’${prefix}/include’
    infodir=’${datarootdir}/info’
    libdir=’${exec_prefix}/lib’
    libexecdir=’${exec_prefix}/libexec’
    localedir=’${datarootdir}/locale’
    localstatedir=’${prefix}/var’
    mandir=’${datarootdir}/man’
    oldincludedir=’/usr/include’
    pdfdir=’${docdir}’
    prefix=’/scartch/python’
    program_transform_name=’s,x,x,’
    psdir=’${docdir}’
    sbindir=’${exec_prefix}/sbin’
    sharedstatedir=’${prefix}/com’
    sysconfdir=’${prefix}/etc’
    target_alias=”

    ## ———– ##
    ## confdefs.h. ##
    ## ———– ##

    /* confdefs.h */
    #define _GNU_SOURCE 1
    #define _NETBSD_SOURCE 1
    #define __BSD_VISIBLE 1
    #define _BSD_TYPES 1
    #define _DARWIN_C_SOURCE 1
    #define _XOPEN_SOURCE 600
    #define _XOPEN_SOURCE_EXTENDED 1
    #define _POSIX_C_SOURCE 200112L

    configure: exit 77
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Please let me know how to proceed ?

    Regards,
    Shekhar

    • Paul Gibson says:

      Hi Shekhar,

      Thanks for the detailed log file. The problem appears to be this line:
      /home/rvenkatachar/Projects/trunk/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/bin/ld: crt0.o: No such file: No such file or directory

      There seems to be something wrong with your toolchain as ld isn’t finding crt0.o. Can you try running ld manually and see if it works?

      Paul

      • Shekhar says:

        Hi Paul,

        As suggested by you , there was problem in the earlier toolchain what I was using.
        So I successfully built the python 2.7.2 with the different toolchain by using the below different commands.

        CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++ AR=arm-none-linux-gnueabi-ar RANLIB=arm-none-linux-gnueabi-ranlib ./configure -–host=arm-none-linux -–build=x86_64-linux-gnu –prefix=/python

        make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-none-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-none-linux-gnueabi- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-none-linux BUILDARCH=x86_64-linux-gnu

        make install HOSTPYTHON=./hostpython BLDSHARED=”arm-none-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-none-linux-gnueabi- CROSS_COMPILE_TARGET=yes prefix=~/raj/_install

        I copied the entire _install directory to the target. (in my case the space was not an issue -as we boot the device using nfs mounted).

        In the target, I set the path of the python to /_install/bin.
        Also, I set the PYTHONHOME environment variable to nfs mounted drive i,e — someip:/home//_install

        When I booted the device, and try to execute python on target — it says ==> python not found.
        Can you please let me know how to proceed ?

        Regards,
        shekhar

        • Paul Gibson says:

          Hi Shekhar,

          Could you show me how you are running python? Are you running python from /_install/bin? Is it on the PATH?

          Paul

          • Shekhar says:

            hi Paul,

            Thanks for the reply.

            I have copied the entire built python directory to my target device under /system/usr
            I have set the path environment variable like this …
            export PATH=$PATH:/system/usr

            My target is connected to my Ubuntu machine(10.04) and able to communicate to target over serial port using minicom. Using minicom, i have opened the shell prompt of the device.
            In shell prompt, I’m trying to execute python.
            If I execute the python command, I’m getting python not found !!!

            Below is the snapshot of the error
            ==========================================
            root@android:/# echo $PATH
            /sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin:/system/usr
            root@android:/# python
            /system/bin/sh: python: not found
            127|root@android:/# cd /system/usr/_install/bin
            root@android:/system/usr/_install/bin# python
            /system/bin/sh: python: not found
            =============================================

            Do I need set any other environment variables, or do I need to copy any other lib files from host to target.
            Please let me know.

            For your info : My target device has the Android’s Ice Cream Sandwitch OS running.

            Regards,
            Shekhar

            • Paul Gibson says:

              Hi Shekhar,

              My first guess is that you are still missing libraries that python is looking for:
              /system/bin/sh: python: not found

              The Android root filesystem is not standard Linux and therefore I cannot help. You may want to look at other articles such as:
              http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/

              Paul

              • Shekhar says:

                hi Paul,

                Thanks for the help provided….
                Now I’m looking into the link provided by you [http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/]. I have done good progress in compiling the Python for the Android.

                Thanks for the help ….

                Regards,
                Shekhar

                • Shekhar says:

                  hi Paul,

                  Thanks for the link , [http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/]. I successfully built python for Android. I ported into my target board….I can execute few python commands like, python -V and python –help.

                  But, python prompt is not coming on target..and idea why it is so.. any tricks for working ??
                  Also, I wrote simple python script which imports os, random and time modules….
                  Seems only os module is working but others are not working,,,can you please suggest some ideas what do i need to do to make it work??

                  I have set the environment variables too..
                  PYHTHONHOME = /system/lib/python2.7
                  PYHTHONPATH = /system/lib/python2.7

                  Regards,
                  Rajashekhar

  29. sudhesh says:

    Hi Paul,

    Thanks for you cross-compiling-python-for-embedded-linux blog.
    I want to run the Python on ARM QEMU(Hardware emulator).

    I have successfully cross compiled the Python2.7.2 for ARM QEMU and I have got the below build directories
    /bin /include /lib /share

    and I have copied all the Library files from /lib/python2.7/* to target(QEMU) /usr/lib/* directory.
    and /bin/python to target /usr/bin/python
    and i ran below commands on target:

    # export PYTHONPATH=/usr/bin/python
    # export PYTHONHOME=/usr/lib/

    # python -V
    python: error while loading shared libraries: libpthread.so.0: cannot open shared object file: No such file or directory

    “then I have copied libpthread.so.0 file from HOST arm-tool-chain “arm-none-linux-gnueabi/libc/lib/libpthread.so.0″ to /usr/lib/* on target.

    Now am getting the below error:

    # python -V
    python: relocation error: /usr/lib/libpthread.so.0: symbol __default_sa_restorer_v2, version GLIBC_PRIVATE not defined in file libc.so.6 with link time reference

    Could you please guide me the steps to bring the build files to the target and running the Python on the target.

    Regards,
    Sudhesh

  30. Paul Gibson says:

    Hi Sudhesh,

    It could be one of two things:
    – an issue with how you have copied your toolchain libraries to the target device.
    – Your Python build is built against a different version of libpthread than what is installed on the device.

    For copying the files to target:
    – Copy all the libraries from arm-none-linux-gnueabi/libc/lib/ to /lib on the target.
    – Copy all the libraries from arm-none-linux-gnueabi/libc/usr/lib/ to /usr/lib on the target.

    When building Python, make sure that you are building against the same libraries as provided by the toolchain.

    Paul

    • sudhesh says:

      Hi Paul,

      Thank you for advise, I have copied all the lib file from arm-none-linux-gnueabi/libc/lib/ to /lib
      and host _install/bin/* //usr/bin/*
      I have not found arm-none-linux-gnueabi/libc/usr/lib/ to copy /usr/lib on the target.

      Now Python is running on the target, but getting error while executing the sample scripts.

      # python -V
      Python 2.6.6

      # python
      ‘import site’ failed; use -v for traceback
      Python 2.6.6 (r266:84292, Apr 1 2012, 12:06:25)
      [GCC 4.4.1] on linux2
      Type “help”, “copyright”, “credits” or “license” for more information.
      >>>

      # cat test.py
      import os
      print “Well here we are again”
      os.system(‘ls -lt’)

      # python2.6 test.py
      ‘import site’ failed; use -v for traceback
      Traceback (most recent call last):
      File “test.py”, line 1, in
      import os
      ImportError: No module named os

      # python2.6 -v test.py
      # installing zipimport hook
      import zipimport # builtin
      # installed zipimport hook
      ‘import site’ failed; traceback:
      ImportError: No module named site
      Python 2.6.6 (r266:84292, Apr 1 2012, 12:06:25)
      [GCC 4.4.1] on linux2
      Type “help”, “copyright”, “credits” or “license” for more information.
      Traceback (most recent call last):
      File “test.py”, line 1, in
      import os
      ImportError: No module named os
      # clear __builtin__._
      # clear sys.path
      # clear sys.argv
      # clear sys.ps1
      # clear sys.ps2
      # clear sys.exitfunc
      # clear sys.exc_type
      # clear sys.exc_value
      # clear sys.exc_traceback
      # clear sys.last_type
      # clear sys.last_value
      # clear sys.last_traceback
      # clear sys.path_hooks
      # clear sys.path_importer_cache
      # clear sys.meta_path
      # clear sys.flags
      # clear sys.float_info
      # restore sys.stdin
      # restore sys.stdout
      # restore sys.stderr# python2.6 -v test.py
      # installing zipimport hook
      import zipimport # builtin
      # installed zipimport hook
      ‘import site’ failed; traceback:
      ImportError: No module named site
      Python 2.6.6 (r266:84292, Apr 1 2012, 12:06:25)
      [GCC 4.4.1] on linux2
      Type “help”, “copyright”, “credits” or “license” for more information.
      Traceback (most recent call last):
      File “test.py”, line 1, in
      import os
      ImportError: No module named os
      # clear __builtin__._
      # clear sys.path
      # clear sys.argv
      # clear sys.ps1
      # clear sys.ps2
      # clear sys.exitfunc
      # clear sys.exc_type
      # clear sys.exc_value
      # clear sys.exc_traceback
      # clear sys.last_type
      # clear sys.last_value
      # clear sys.last_traceback
      # clear sys.path_hooks
      # clear sys.path_importer_cache
      # clear sys.meta_path
      # clear sys.flags
      # clear sys.float_info
      # restore sys.stdin
      # restore sys.stdout
      # restore sys.stderr
      # cleanup __main__
      # cleanup[1] zipimport
      # cleanup[1] signal
      # cleanup[1] exceptions
      # cleanup[1] _warnings
      # cleanup sys
      # cleanup __builtin__
      # cleanup ints: 3 unfreed ints
      # cleanup floats
      #

      # cleanup __main__
      # cleanup[1] zipimport
      # cleanup[1] signal
      # cleanup[1] exceptions
      # cleanup[1] _warnings
      # cleanup sys
      # cleanup __builtin__
      # cleanup ints: 3 unfreed ints
      # cleanup floats
      #

      Could please guide me what’s went wrong ?

  31. Rudolf says:

    Hi, all

    Trying to follow the tutorial. Compile on Ubuntu 10.04 32-bit for ARM CPU (not for android)
    Config part works:
    CC=arm-cdcs-linux-gnueabi-gcc CXX=arm-cdcs-linux-gnueabi-g++ AR=arm-cdcs-linux-gnueabi-ranlib ./configure –host=arm-linux –build=x86-linux-gnu –prefix=/python

    Now for building:
    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-cdcs-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-cdcs-linux-gnueabi- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=x86-linux-gnu

    And I am getting an error (including few lines just before the error):
    arm-cdcs-linux-gnueabi-gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -c ./Modules/xxsubtype.c -o Modules/xxsubtype.o
    arm-cdcs-linux-gnueabi-gcc -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE \
    -DSVNVERSION=”\”`LC_ALL=C svnversion .`\”" \
    -DHGVERSION=”\”`LC_ALL=C `\”" \
    -DHGTAG=”\”`LC_ALL=C `\”" \
    -DHGBRANCH=”\”`LC_ALL=C `\”" \
    -o Modules/getbuildinfo.o ./Modules/getbuildinfo.c
    rm -f libpython2.7.a
    arm-cdcs-linux-gnueabi-ranlib rc libpython2.7.a Modules/getbuildinfo.o
    arm-cdcs-linux-gnueabi-ranlib: ‘rc’: No such file
    arm-cdcs-linux-gnueabi-ranlib: ‘libpython2.7.a’: No such file
    arm-cdcs-linux-gnueabi-ranlib: Modules/getbuildinfo.o: File format not recognized

    Modules/getbuildinfo.o is there and is of ARM ELF file format.
    It also looks like libpython2,7.a is removed before being used????

    Thanks,
    RUdolf
    make: *** [libpython2.7.a] Error 1

  32. Paul Jay says:

    i get the following error:

    paul@debian:~/Python-3.2.2$ patch -pl < Python-3.2.2-xcompile.patch
    patch: **** strip count l is not a number

    can anyone help me please?

  33. Hello, Paul!
    Your post was very helpful for me when embedding Python as static library to iOS application.
    I used slightly modified script from here: https://github.com/cobbal/python-for-iphone , your patches, and got library compiled for device. Need to do the same trick for iOS Simulator, but configure fails. Not necessary how I’ll find solution, anybody, if You know how to fix it, please post working solution here. Any hack will be accepted.

    my iOS-build272.sh: http://pastebin.com/KJfpUzxf
    used with patch from Paul for 2.7.2, Xcode 4.3.2 Mac OS X 10.7.4

    You can find some dirty hack for device on line 88. That’s it, I guess, I need apply the same (but not exactly) for Simulator (line 54). Currently it fails with “checking size of int… 0
    checking size of long… 0
    checking size of void *… 0
    checking size of short… 0
    checking size of float… 0
    checking size of double… 0
    checking size of fpos_t… 0
    checking size of size_t… configure: error: in `/Users/mac_user/Downloads/python27try/Python-2.7.2′:
    configure: error: cannot compute sizeof (size_t)
    See `config.log’ for more details”.
    Heelp

  34. claire says:

    Hello Paul!
    Need to cross-compile Python for arm-linux with module ctypes…Have you fixed the issue?
    Many thanks on reply!
    And I still have a problem on compiling on x86 linux. When i use python to execute .py files everything works fine. However, if i compile it with “python -m py_compile xxx.py” to .pyc file and run it, the result appears with unreadable codes. Sometimes my system may display in unreadable codes too.
    Could you give me a hand?

  35. Tibs says:

    Thanks for this, it has been directly useful this week.

    For reference, I’ve blogged about what I’ve been doing at http://kynesim.blogspot.co.uk/2012/06/cross-compiling-python-for-arm-with.html, with a link back to this blog post.

  36. Hi Paul, thanks for this blog! I’m interested in getting Py3k working on my Jailbroken Kindle Touch, which runs an ARM v8 processor.

    I’m trying to build on Ubuntu machine (uname = Linux 2.6.38-15-generic #64-Ubuntu SMP Fri Jul 6 17:18:17 UTC 2012 i686 i686 i386 GNU/Linux), and the kernel patch fails with the following:
    patching file configure
    Hunk #1 FAILED at 12160.
    Hunk #2 FAILED at 13625.
    2 out of 2 hunks FAILED — saving rejects to file configure.rej
    patching file Makefile.pre.in
    Hunk #1 FAILED at 435.
    Hunk #2 FAILED at 587.
    Hunk #3 FAILED at 1102.
    3 out of 3 hunks FAILED — saving rejects to file Makefile.pre.in.rej
    patching file setup.py
    Hunk #1 FAILED at 292.
    Hunk #2 FAILED at 304.
    Hunk #3 FAILED at 1603.
    3 out of 3 hunks FAILED — saving rejects to file setup.py.rej

    Any thoughts? I imagine this is only the first of many stupid problems I’ll have, I’ve never cross-compiled anything before now! :)

    • Paul Gibson says:

      Hi Cathal,

      What version of python are you trying to cross compile? Are you using exactly the same version patch file?

      What is the command you are running when the patch does not apply?

      Paul

      • I downloaded 3.2.2 from here: http://www.python.org/download/releases/3.2.2/
        It’s not even in the “releases” page anymore but I just changed the URL from 3.2.3 and found the old page still there. :)

        The specific command and output is:
        —————————————————————
        user@host:~/Applications/Python3.2_Kindle$ patch -p1 < Python-3.2.2-xcompile.patch
        patching file configure
        Hunk #1 FAILED at 12160.
        Hunk #2 FAILED at 13625.
        2 out of 2 hunks FAILED — saving rejects to file configure.rej
        patching file Makefile.pre.in
        Hunk #1 FAILED at 435.
        Hunk #2 FAILED at 587.
        Hunk #3 FAILED at 1102.
        3 out of 3 hunks FAILED — saving rejects to file Makefile.pre.in.rej
        patching file setup.py
        Hunk #1 FAILED at 292.
        Hunk #2 FAILED at 304.
        Hunk #3 FAILED at 1603.
        3 out of 3 hunks FAILED — saving rejects to file setup.py.rej
        —————————————————————

        I haven't even tried going further manually.. this is my first experience of a manual kernel patch and cross-compile, so I'm pretty sure "Here be dragons".

        I'm pretty surprised that there's no repo out there with precompiled binaries for ARMv7 to match the i.MX508 / Cortex-A8 processor on the Kindle! Well, there are ARM binaries in the ipkg repositories, but ipkg hangs on installation of Python 3 to the Kindle. It installs other packages correctly, such as Nano and xTerm etc., but not python. Initially, it had issues installing dependencies, but manually installing dependencies one-by-one without Python itself worked out OK.

        I tried compiling *on* the kindle too (I'm patient!), but it hangs some ways through. I was using SSH at the time and eventually I gave up wondering if it was just very busy, or if the shell session had lapsed due to a proper crash.

        I'm really hoping to get this working, because I just got Xterm installed on the Kindle and would love to get cracking on some fun scripts. :)

  37. Guolei Lu says:

    According to this artical, I made a Click and Run auto cross compiling script package that contains useful extended libraries such as sqlite3, berkeleyDB and openssl for python 2.6.6.
    Here is the instruction and download link:

    http://blog.sina.com.cn/s/blog_81a58dbf01013sre.html

  38. Lothsahn says:

    Tried to convert the 2.7.2 patch to 2.7.3, but failed with a lot of errors. It looks like 2.7.3′s installer is trying to use the hostpython to import the cross-compiled modules, which are then all failing.

    I’m stumped at this point… don’t really want to dig that much into Python’s build process to figure out what’s going on wrong…

    Anyone have a working version of the patch for 2.7.3 (with ctypes)?

  39. BaHko says:

    Same here. I’m compiling it for mini2440 on Ubuntu.

    I’m getting a lot of errors after executing the second line no matter if it is Python-2.7.2 or Python-2.7.3

    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-none-linux-gnueabi-gcc-shared” CROSS_COMPILE=arm-none-linux-gnueabi-CROSS_COMPILE_TARGET=yes HOSTARCH=arm-none-linux BUILDARCH=x86_64-linux-gnu

    and the error:
    checking whether we are cross compiling… configure: error: in `/home/admin1/mini2440/mini2440/Python-2.7.3/build/temp.linux-i686-2.7/libffi’:
    configure: error: cannot run C compiled programs.
    If you meant to cross compile, use `–host’.
    See `config.log’ for more details.
    Failed to configure _ctypes module

    Python build finished, but the necessary bits to build these modules were not found:
    _bsddb _curses _curses_panel
    _sqlite3 _ssl _tkinter
    bsddb185 bz2 dbm
    gdbm readline sunaudiodev
    zlib
    To find the necessary bits, look in setup.py in detect_modules() for the module’s name.

    Failed to build these modules:
    _bisect _codecs_cn _codecs_hk
    _codecs_iso2022 _codecs_jp _codecs_kr
    _codecs_tw _collections _csv
    _ctypes_test _elementtree _functools
    _heapq _hotshot _io
    _json _locale _lsprof
    _md5 _multibytecodec _multiprocessing
    _random _sha _socket
    _struct _testcapi array
    audioop binascii cmath
    cPickle crypt cStringIO
    datetime dl fcntl
    future_builtins grp imageop
    itertools linuxaudiodev math
    mmap nis operator
    ossaudiodev parser pyexpat
    resource select spwd
    strop syslog termios
    time unicodedata

  40. IndigoFire says:

    Thank you very much for posting these instructions and patches! I found a couple of things that needed changing to make it work with 3.2.3 to build the modules correctly on my Mac for an ARM platform. I also found that I had to set CPPFLAGS explicitly when calling configure. Here’s my patch:

    Index: configure
    ===================================================================
    — configure (revision 1137)
    +++ configure (working copy)
    @@ -12205,7 +12205,7 @@
    if ${ac_cv_broken_sem_getvalue+:} false; then :
    $as_echo_n “(cached) ” >&6
    else
    - if test “$cross_compiling” = yes; then :
    + if test “$cross_compiling” = no; then :
    ac_cv_broken_sem_getvalue=yes
    else
    cat confdefs.h – <conftest.$ac_ext
    @@ -13670,7 +13670,7 @@
    $as_echo_n “(cached) ” >&6
    else
    if test “$cross_compiling” = yes; then :
    - ac_cv_have_long_long_format=no
    + ac_cv_have_long_long_format=”yes”
    else
    cat confdefs.h – <conftest.$ac_ext
    /* end confdefs.h. */
    Index: setup.py
    ===================================================================
    — setup.py (revision 1137)
    +++ setup.py (working copy)
    @@ -323,6 +323,8 @@
    self.announce(‘WARNING: skipping import check for Cygwin-based “%s”‘
    % ext.name)
    return
    + if os.environ.get(‘CROSS_COMPILE_TARGET’) == ‘yes’:
    + return
    ext_filename = os.path.join(
    self.build_lib,
    self.get_ext_filename(self.get_ext_fullname(ext.name)))
    @@ -335,27 +337,31 @@
    try:
    imp.load_dynamic(ext.name, ext_filename)
    except ImportError as why:
    - self.failed.append(ext.name)
    - self.announce(‘*** WARNING: renaming “%s” since importing it’
    - ‘ failed: %s’ % (ext.name, why), level=3)
    - assert not self.inplace
    - basename, tail = os.path.splitext(ext_filename)
    - newname = basename + “_failed” + tail
    - if os.path.exists(newname):
    - os.remove(newname)
    - os.rename(ext_filename, newname)
    + if os.environ.get(‘CROSS_COMPILE_TARGET’) != “yes”:
    + self.announce(‘*** WARNING: renaming “%s” since importing it’
    + ‘ failed: %s’ % (ext.name, why), level=3)
    + assert not self.inplace
    + basename, tail = os.path.splitext(ext_filename)
    + newname = basename + “_failed” + tail
    + if os.path.exists(newname):
    + os.remove(newname)
    + os.rename(ext_filename, newname)

    - # XXX — This relies on a Vile HACK in
    - # distutils.command.build_ext.build_extension(). The
    - # _built_objects attribute is stored there strictly for
    - # use here.
    - # If there is a failure, _built_objects may not be there,
    - # so catch the AttributeError and move on.
    - try:
    - for filename in self._built_objects:
    - os.remove(filename)
    - except AttributeError:
    - self.announce(‘unable to remove files (ignored)’)
    + # XXX — This relies on a Vile HACK in
    + # distutils.command.build_ext.build_extension(). The
    + # _built_objects attribute is stored there strictly for
    + # use here.
    + # If there is a failure, _built_objects may not be there,
    + # so catch the AttributeError and move on.
    + try:
    + for filename in self._built_objects:
    + os.remove(filename)
    + except AttributeError:
    + self.announce(‘unable to remove files (ignored)’)
    + else:
    + self.announce(‘WARNING: “%s” failed importing, but we leave it ‘
    + ‘because we are cross-compiling’ %
    + ext.name)
    except:
    exc_type, why, tb = sys.exc_info()
    self.announce(‘*** WARNING: importing extension “%s” ‘
    @@ -396,10 +402,11 @@
    # Ensure that /usr/local is always used, but the local build
    # directories (i.e. ‘.’ and ‘Include’) must be first. See issue
    # 10520.
    - add_dir_to_list(self.compiler.library_dirs, ‘/usr/local/lib’)
    - add_dir_to_list(self.compiler.include_dirs, ‘/usr/local/include’)
    + if os.environ.get(‘CROSS_COMPILE_TARGET’) != ‘yes’:
    + add_dir_to_list(self.compiler.library_dirs, ‘/usr/local/lib’)
    + add_dir_to_list(self.compiler.include_dirs, ‘/usr/local/include’)
    self.add_multiarch_paths()
    -
    +
    # Add paths specified in the environment variables LDFLAGS and
    # CPPFLAGS for header and library files.
    # We must get the values from the Makefile and not the environment
    @@ -435,7 +442,8 @@
    add_dir_to_list(dir_list, directory)

    if os.path.normpath(sys.prefix) != ‘/usr’ \
    - and not sysconfig.get_config_var(‘PYTHONFRAMEWORK’):
    + and not sysconfig.get_config_var(‘PYTHONFRAMEWORK’) \
    + and os.environ.get(‘CROSS_COMPILE_TARGET’) != ‘yes’:
    # OSX note: Don’t add LIBDIR and INCLUDEDIR to building a framework
    # (PYTHONFRAMEWORK is set) to avoid # linking problems when
    # building a framework with different architectures than
    @@ -448,11 +456,16 @@
    # lib_dirs and inc_dirs are used to search for files;
    # if a file is found in one of those directories, it can
    # be assumed that no additional -I,-L directives are needed.
    - lib_dirs = self.compiler.library_dirs + [
    - '/lib64', '/usr/lib64',
    - '/lib', '/usr/lib',
    - ]
    - inc_dirs = self.compiler.include_dirs + ['/usr/include']
    + lib_dirs = self.compiler.library_dirs
    + if os.environ.get(‘CROSS_COMPILE_TARGET’) != ‘yes’:
    + lib_dirs = lib_dirs + [
    + '/lib64', '/usr/lib64',
    + '/lib', '/usr/lib',
    + ]
    + inc_dirs = self.compiler.include_dirs
    + if os.environ.get(‘CROSS_COMPILE_TARGET’) != ‘yes’:
    + inc_dirs = inc_dirs + ['/usr/include']
    + print(“inc_dirs is ” + str(inc_dirs))
    exts = []
    missing = []

    @@ -1663,7 +1676,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.
    Index: Makefile.pre.in
    ===================================================================
    — Makefile.pre.in (revision 1137)
    +++ Makefile.pre.in (working copy)
    @@ -437,8 +437,8 @@
    # Build the shared modules
    sharedmods: $(BUILDPYTHON)
    @case $$MAKEFLAGS in \
    - *s*) $(RUNSHARED) CC=’$(CC)’ LDSHARED=’$(BLDSHARED)’ OPT=’$(OPT)’ ./$(BUILDPYTHON) -E $(srcdir)/setup.py -q build;; \
    - *) $(RUNSHARED) CC=’$(CC)’ LDSHARED=’$(BLDSHARED)’ OPT=’$(OPT)’ ./$(BUILDPYTHON) -E $(srcdir)/setup.py build;; \
    + *s*) $(RUNSHARED) CC=’$(CC)’ LDSHARED=’$(BLDSHARED)’ OPT=’$(OPT)’ $(HOSTPYTHON) -E $(srcdir)/setup.py -q build;; \
    + *) $(RUNSHARED) CC=’$(CC)’ LDSHARED=’$(BLDSHARED)’ OPT=’$(OPT)’ $(HOSTPYTHON) -E $(srcdir)/setup.py build;; \
    esac

    # Build static library
    @@ -589,7 +589,7 @@
    $(GRAMMAR_H) $(GRAMMAR_C): Parser/pgen.stamp
    Parser/pgen.stamp: $(PGEN) $(GRAMMAR_INPUT)
    -@$(INSTALL) -d Include
    - $(PGEN) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)
    + $(HOSTPGEN) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)
    -touch Parser/pgen.stamp

    $(PGEN): $(PGENOBJS)
    @@ -1108,7 +1108,8 @@
    # Install the dynamically loadable modules
    # This goes into $(exec_prefix)
    sharedinstall: sharedmods
    - $(RUNSHARED) ./$(BUILDPYTHON) -E $(srcdir)/setup.py install \
    + CC=’$(CC)’ LDSHARED=’$(BLDSHARED)’ LDFLAGS=’$(LDFLAGS)’ OPT=’$(OPT)’ CROSS_COMPILE=’$(CROSS_COMPILE)’ \
    + $(RUNSHARED) ./$(HOSTPYTHON) -E $(srcdir)/setup.py install \
    –prefix=$(prefix) \
    –install-scripts=$(BINDIR) \
    –install-platlib=$(DESTSHARED) \

  41. Lothsahn says:

    Paul,

    Python 2.7.3 came out, and your 2.7.2 patch does not apply cleanly. In addition, I have patches that enable building not only ctypes but sqlite as well.

    I’d like to send you this patch so you can include it with your others. Please contact me so I can send you the file.

    Also, Python *almost* builds with Scratchbox 2. I ran into some minor problems, but that might be a good option for people to try as well.

    Thanks,
    Lothsahn

    • IndigoFire says:

      Lothsahn: You can apply the patch by doing -F20 when applying. I’ve updated the patch to be clean, but it’s easier to just do that. I’ll let you know what happens in my testing.

      • Lothsahn says:

        IndigoFire:

        There are other issues besides just getting it to apply. SQLLite3 not working, being one of them. I already sent Paul the patch for 2.7.3, so he should hopefully be uploading it to the site soon.

        I wouldn’t spend much time on it unless it’s an emergency–My coworker and I have already done most of the work. :)

        Lothsahn

  42. Dom says:

    Dear Paul:

    Thanks to your article, and now I am on make step,there is some wrong when i execute make command:
    “make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-linux-gcc -shared” CROSS_COMPILE=arm-linux- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=x86_64-linux-gnu”

    then error follows:

    arm-linux-gcc -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Modules/python.o ./Modules/python.c
    In file included from Include/Python.h:58,
    from ./Modules/python.c:3:
    Include/pyport.h:147: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘Py_intptr_t’
    make: *** [Modules/python.o] error 1

    my configure information is:
    “CC=arm-linux-gcc CXX=arm-linux-g++ AR=arm-linux-ar RANLIB=arm-linux-ranlib ./configure –host=arm-linux –build=x86_64-linux-gnu –prefix=/armTest/python”

    I checked the source file pyport.h , but it has nothing wrong.

    And my compile environment:
    Operating system:red-hat-enterprise-5 32bit and debian 32bit
    cross compiler: arm-linux-gcc-4.3.3
    Python version: Python-2.7.2 and Python-2.7.3
    whether I use redhat or debian system, and whether i use Python-2.7.2 and Python-2.7.3,I got the same wrong information.

    I don’t know what is the matter, could you give some suggestions, that would be very grateful,thank you!

    • Paul Gibson says:

      Hi Dom,

      The problem could be that you are compiling on a 32-bit platform, but you did not change the BUILDARCH (which is set to 64-bit). What happens if you change the x86_64- to your 32-bit platform type?

      Paul

      • Dom says:

        Hi, Paul

        I am glad to see your reply, and I have tried to change the parameter of BUILDARCH from”x86_64-linux-gnu” to “i386-redhat-linux-gnu” in make step, and also changed the build parameter to”i386-redhat-linux-gnu”.
        and even i changed it to “i686-redhat-linux-gnu”.

        I was doubt if my cross-compiler is bad, and i replaced it by arm-linux-gcc4.4.3

        but I get the same error information like before. So i really get confused about this now.

        Regards
        Dom

        • Paul Gibson says:

          Hi Dom,

          Are you sure they are the correct machine names?

          To get your correct machine name type:
          gcc -v

          Then get the name of the –build parameter. For example:

          Configured with: ../src/configure -v –with-pkgversion=’Ubuntu/Linaro 4.6.3-1ubuntu5′ –with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs –enable-languages=c,c++,fortran,objc,obj-c++ –prefix=/usr –program-suffix=-4.6 –enable-shared –enable-linker-build-id –with-system-zlib –libexecdir=/usr/lib –without-included-gettext –enable-threads=posix –with-gxx-include-dir=/usr/include/c++/4.6 –libdir=/usr/lib –enable-nls –with-sysroot=/ –enable-clocale=gnu –enable-libstdcxx-debug –enable-libstdcxx-time=yes –enable-gnu-unique-object –enable-plugin –enable-objc-gc –enable-targets=all –disable-werror –with-arch-32=i686 –with-tune=generic –enable-checking=release –build=i686-linux-gnu –host=i686-linux-gnu –target=i686-linux-gnu
          Thread model: posix
          gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

          Also, please send me the results of arm-linux-gcc -v

          Please try this and let me know the results?
          Paul

          • Dom says:

            Hi Paul

            I have checked my ggc version, but there’s nothing about build information,so I used the value of host parameter, then the same error cames again.
            gcc -v:”
            Configured with: ../configure –prefix=/usr –mandir=/usr/share/man –infodir=/usr/share/info –enable-shared –enable-threads=posix –enable-checking=release –with-system-zlib –enable-__cxa_atexit –disable-libunwind-exceptions –enable-libgcj-multifile –enable-languages=c,c++,objc,obj-c++,java,fortran,ada –enable-java-awt=gtk –disable-dssi –enable-plugin –with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre –with-cpu=generic –host=i386-redhat-linux
            Thread model?posix
            gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)

            and the infomation of arm-linux-gcc -v is:
            “Using built-in specs.
            Target: arm-none-linux-gnueabi
            Configured with: /scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/src/gcc-4.3/configure –build=i686-pc-linux-gnu –host=i686-pc-linux-gnu –target=arm-none-linux-gnueabi –enable-threads –disable-libmudflap –disable-libssp –disable-libstdcxx-pch –with-gnu-as –with-gnu-ld –with-specs=’%{funwind-tables|fno-unwind-tables|mabi=*|ffreestanding|nostdlib:;:-funwind-tables}’ –enable-languages=c,c++ –enable-shared –enable-symvers=gnu –enable-__cxa_atexit –with-pkgversion=’Sourcery G++ Lite 2009q1-203′ –with-bugurl=https://support.codesourcery.com/GNUToolchain/ –disable-nls –prefix=/opt/codesourcery –with-sysroot=/opt/codesourcery/arm-none-linux-gnueabi/libc –with-build-sysroot=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/install/arm-none-linux-gnueabi/libc –with-gmp=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/obj/host-libs-2009q1-203-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr –with-mpfr=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/obj/host-libs-2009q1-203-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr –disable-libgomp –enable-poison-system-directories –with-build-time-tools=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/install/arm-none-linux-gnueabi/bin –with-build-time-tools=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/install/arm-none-linux-gnueabi/bin
            Thread model: posix
            gcc version 4.3.3 (Sourcery G++ Lite 2009q1-203)

            Regards
            Dom

            • Dom says:

              Hi Paul!

              I just try it on OS:CentOS6.3-X86_64 with Python2.7.2:
              and i get the build parameter value like below gcc -v: -build=x86_64-redhat-linux
              so i changed configure and make info,but I still got the same wrong error information

              configure: “CC=arm-linux-gcc CXX=arm-linux-g++ AR=arm-linux-ar RANLIB=arm-linux-ranlib ./configure –host=arm-linux –build=x86_64-redhat-linux –prefix=/python”

              make:”make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-linux-gcc -shared” CROSS_COMPILE=arm-linux- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=x86_64-redhat-linux”

              gcc -v:
              Configured with?../configure –prefix=/usr –mandir=/usr/share/man –infodir=/usr/share/info –with-bugurl=http://bugzilla.redhat.com/bugzilla –enable-bootstrap –enable-shared –enable-threads=posix –enable-checking=release –with-system-zlib –enable-__cxa_atexit –disable-libunwind-exceptions –enable-gnu-unique-object –enable-languages=c,c++,objc,obj-c++,java,fortran,ada –enable-java-awt=gtk –disable-dssi –with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre –enable-libgcj-multifile –enable-java-maintainer-mode –with-ecj-jar=/usr/share/java/eclipse-ecj.jar –disable-libjava-multilib –with-ppl –with-cloog –with-tune=generic –with-arch_32=i686 –build=x86_64-redhat-linux
              Thread model?posix
              gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)

              arm-linux-gcc -v:
              Using built-in specs.
              Target: arm-none-linux-gnueabi
              Configured with: /scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/src/gcc-4.3/configure –build=i686-pc-linux-gnu –host=i686-pc-linux-gnu –target=arm-none-linux-gnueabi –enable-threads –disable-libmudflap –disable-libssp –disable-libstdcxx-pch –with-gnu-as –with-gnu-ld –with-specs=’%{funwind-tables|fno-unwind-tables|mabi=*|ffreestanding|nostdlib:;:-funwind-tables}’ –enable-languages=c,c++ –enable-shared –enable-symvers=gnu –enable-__cxa_atexit –with-pkgversion=’Sourcery G++ Lite 2009q1-203′ –with-bugurl=https://support.codesourcery.com/GNUToolchain/ –disable-nls –prefix=/opt/codesourcery –with-sysroot=/opt/codesourcery/arm-none-linux-gnueabi/libc –with-build-sysroot=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/install/arm-none-linux-gnueabi/libc –with-gmp=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/obj/host-libs-2009q1-203-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr –with-mpfr=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/obj/host-libs-2009q1-203-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr –disable-libgomp –enable-poison-system-directories –with-build-time-tools=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/install/arm-none-linux-gnueabi/bin –with-build-time-tools=/scratch/mitchell/builds/4.3-arm-none-linux-gnueabi-respin/lite/install/arm-none-linux-gnueabi/bin
              Thread model: posix
              gcc version 4.3.3 (Sourcery G++ Lite 2009q1-203)

              Dom

              • Paul Gibson says:

                Hi Dom,

                I don’t really know what is going on. I think you should look in Include/pyport.h, on line 147 and see what the asm command is. You should evaluate if this is the right command for your platform – eg If it is a x86 asm instruction and it is trying to compile using arm, then it won’t work and probably indicates an error in your build environment or some of the configure settings.

                Look around line 147 for any #defines that might be triggered on your platform, due to a bad environment or configure step.

                Paul

                • Dom says:

                  Hi Paul

                  I have tried every way I can thought, but still can’t find any solutions. at last we have to give it up, now we use routers built-in python environment to run our program, and it now goes well.

                  Thanks for your help, you are so kind

                  Best wishes!
                  Dom

  43. Hardcorefs says:

    patch -p1 < Python-2.7.3-xcompile.patch

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

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

    make install HOSTPYTHON=./hostpython BLDSHARED="arm-linux-gnueabi-gcc -shared" CROSS_COMPILE=arm-linux-gnueabi- CROSS_COMPILE_TARGET=yes prefix=~/Python-2.7.3/_install

    Results in:

    /home/Python-2.7.3/_install/bin # ./python
    Segmentation fault
    /home/Python-2.7.3/_install/bin #

    Checking the build

    if test -d /home/bob/Python-2.7.3/_install/lib/python2.7/distutils/tests; then \
    /usr/bin/install -c -m 644 ./Modules/xxmodule.c \
    /home/bob/Python-2.7.3/_install/lib/python2.7/distutils/tests ; \
    fi
    PYTHONPATH=/home/bob/Python-2.7.3/_install/lib/python2.7 \
    ././hostpython -Wi -tt /home/bob/Python-2.7.3/_install/lib/python2.7/compileall.py \
    -d /home/bob/Python-2.7.3/_install/lib/python2.7 -f \
    -x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
    /home/bob/Python-2.7.3/_install/lib/python2.7
    Traceback (most recent call last):
    File "/home/bob/Python-2.7.3/_install/lib/python2.7/compileall.py", line 16, in
    import struct
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/struct.py”, line 1, in
    from _struct import *
    ImportError: /home/bob/nfs/python/Python-2.7.3/build/lib.linux-x86_64-2.7/_struct.so: wrong ELF class: ELFCLASS32
    make: [libinstall] Error 1 (ignored)
    PYTHONPATH=/home/bob/Python-2.7.3/_install/lib/python2.7 \
    ././hostpython -Wi -tt -O /home/bob/Python-2.7.3/_install/lib/python2.7/compileall.py \
    -d /home/bob/Python-2.7.3/_install/lib/python2.7 -f \
    -x ‘bad_coding|badsyntax|site-packages|lib2to3/tests/data’ \
    /home/bob/Python-2.7.3/_install/lib/python2.7
    Traceback (most recent call last):
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/compileall.py”, line 16, in
    import struct
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/struct.py”, line 1, in
    from _struct import *
    ImportError: /home/bob/nfs/python/Python-2.7.3/build/lib.linux-x86_64-2.7/_struct.so: wrong ELF class: ELFCLASS32
    make: [libinstall] Error 1 (ignored)
    PYTHONPATH=/home/bob/Python-2.7.3/_install/lib/python2.7 \
    ././hostpython -Wi -t /home/bob/Python-2.7.3/_install/lib/python2.7/compileall.py \
    -d /home/bob/Python-2.7.3/_install/lib/python2.7/site-packages -f \
    -x badsyntax /home/bob/Python-2.7.3/_install/lib/python2.7/site-packages
    Traceback (most recent call last):
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/compileall.py”, line 16, in
    import struct
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/struct.py”, line 1, in
    from _struct import *
    ImportError: /home/bob/nfs/python/Python-2.7.3/build/lib.linux-x86_64-2.7/_struct.so: wrong ELF class: ELFCLASS32
    make: [libinstall] Error 1 (ignored)
    PYTHONPATH=/home/bob/Python-2.7.3/_install/lib/python2.7 \
    ././hostpython -Wi -t -O /home/bob/Python-2.7.3/_install/lib/python2.7/compileall.py \
    -d /home/bob/Python-2.7.3/_install/lib/python2.7/site-packages -f \
    -x badsyntax /home/bob/Python-2.7.3/_install/lib/python2.7/site-packages
    Traceback (most recent call last):
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/compileall.py”, line 16, in
    import struct
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/struct.py”, line 1, in
    from _struct import *
    ImportError: /home/bob/nfs/python/Python-2.7.3/build/lib.linux-x86_64-2.7/_struct.so: wrong ELF class: ELFCLASS32
    make: [libinstall] Error 1 (ignored)
    PYTHONPATH=/home/bob/Python-2.7.3/_install/lib/python2.7 \
    ././hostpython -Wi -t -c “import lib2to3.pygram, lib2to3.patcomp;lib2to3.patcomp.PatternCompiler()”
    Traceback (most recent call last):
    File “”, line 1, in
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/lib2to3/pygram.py”, line 11, in
    from .pgen2 import driver
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/lib2to3/pgen2/driver.py”, line 21, in
    import logging
    File “/home/bob/Python-2.7.3/_install/lib/python2.7/logging/__init__.py”, line 26, in
    import sys, os, time, cStringIO, traceback, warnings, weakref
    ImportError: /home/bob/nfs/python/Python-2.7.3/build/lib.linux-x86_64-2.7/time.so: wrong ELF class: ELFCLASS32
    make: [libinstall] Error 1 (ignored)

  44. Lothsahn says:

    Hardcorefs:

    You’re compiling on a 64-bit host system. Use a 32-bit host system and the problem goes away.

    It’d be nice if you could compile with a 64-bit host system, but I couldn’t get it to easily work so I just build a 32-bit VM.

    If someone gets that working, a patch would be wonderful…

    • Hardcorefs says:

      You know……
      Sometimes I really hate linux………

      So I guess its time to time to crack out that 32 bit VM.

      Thanks for your help.

      • Lothsahn says:

        Hardcorefs:

        I understand your frustration. Just so that you can put blame where blame is due…

        This is actually completely a Python problem. They don’t support cross-compiling at all (at least in Python 2), and so a bunch of 3rd party people like Paul and I have to try to hack around their build process to make it work.

        The root cause of this problem is that Python is building and running Python at the same time. During cross-compile, that means you’re attempting to run the cross-compiled Python on the host platform. While we’ve fixed this in the build process, Python also builds modules. Based on the way it does this, it attempts to use these modules in both the host and cross-compiled Python. As long as the modules are the same format (ELFCLASS32), you’re fine. However, if you’re running a 32-bit crosscompile (arm) and a 64-bit host OS (x86-64), then they won’t, and you’ll get this error.

        Ideally, Python should build the modules twice–once for the host OS and once for the cross-compiled OS. Unfortunately, due to the way Python detects the platform, this is quite non-trivial, and so I skipped trying to make this work after a few hours. Instead, I just built a 32-bit VM as the fastest way.

        I apologize I wasn’t able to get this fixed in my minimal time window, and I totally understand your frustration with Linux. Unfortunately, if Python would just realize that there are a lot of people out there that cross compile and officially support it–that’d solve your entire problem.

        • Hardcorefs says:

          Hi,
          A far better solution to all these problems is to just use “buildroot”.

          Not only does it build your file system, kernel ,boot image but it will also build python for the arm , without all this messing about with patching and trying to compile on the right system…

  45. vijayender says:

    On a 32 bit machine I’m getting the following error
    and running test___all__py results in segfault.

    The error
    PYTHONPATH=/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7 \
    ././hostpython -Wi -tt /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/compileall.py \
    -d /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7 -f \
    -x ‘bad_coding|badsyntax|site-packages|lib2to3/tests/data’ \
    /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7
    Traceback (most recent call last):
    File “/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/compileall.py”, line 16, in
    import struct
    File “/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/struct.py”, line 1, in
    from _struct import *
    ImportError: /home/vkarnaty/py/Python-2.7.3/build/lib.linux-i686-2.7/_struct.so: ELF file data encoding not little-endian
    make: [libinstall] Error 1 (ignored)
    PYTHONPATH=/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7 \
    ././hostpython -Wi -tt -O /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/compileall.py \
    -d /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7 -f \
    -x ‘bad_coding|badsyntax|site-packages|lib2to3/tests/data’ \
    /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7
    Traceback (most recent call last):
    File “/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/compileall.py”, line 16, in
    import struct
    File “/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/struct.py”, line 1, in
    from _struct import *
    ImportError: /home/vkarnaty/py/Python-2.7.3/build/lib.linux-i686-2.7/_struct.so: ELF file data encoding not little-endian
    make: [libinstall] Error 1 (ignored)
    PYTHONPATH=/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7 \
    ././hostpython -Wi -t /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/compileall.py \
    -d /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/site-packages -f \
    -x badsyntax /home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/site-packages
    Traceback (most recent call last):
    File “/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/compileall.py”, line 16, in
    import struct
    File “/home/vkarnaty/py/Python-2.7.3/_install/lib/python2.7/struct.py”, line 1, in
    from _struct import *

    • IndigoFire says:

      Perhaps I’m mistaken, but I didn’t think you could unit tests on a cross compiled build from the host machine. Hence the error “ELF file data encoding not little-endian” it’s trying to run code that cannot be executed on your platform. Perhaps try installing your build on the target system and then running the tests? Or just ignore the tests…

      • Lothsahn says:

        vijayender:

        This is likely happening because the modules are compiled for the host environment, but you’re using them on the client environment. Just like the 32-bit/64-bit mismatch, the difference in endianness is likely causing the ELF data format to be incorrect on the client system.

        The root cause really need to be addressed–we should generate the modules once for the host environment and store them somewhere. We should then regenerate the modules again for the cross-compiled environment. This would solve the 32/64 bit problem as well as the endianness problem.

        I never saw this as my arm platform is little endian, just like my x64 development platform.

  46. Elliot says:

    Just a note to anybody who is attempting to use the 2.7.3 patch from Lothsahn with any of the extra modules, you need to make sure that you set the where the include and lib folders are in your environment as PYTHON_XCOMPILE_DEPENDENCIES_PREFIX. I didn’t see that spelled out explicitly on this page.

  47. Chris says:

    Should this work for MIPSeL, too?
    Has someone tried this?

  48. Sachin says:

    Hi
    I am trying to compile python2.7 for mips target and followed the above step and am able to compile successfully but the step “Copy the entire _install directory to the device, setup the PATH environment variable to include the path the Python executable and run:” — is not possible for me since memory available on target 48 MB while __install directory is of 52.8 MB.

    Also i had tried to run python executable directly from /tmp directory on target with libutil.so.0 and libpython*(including links) but it is giving error “./python: can’t resolve symbol ‘fstat64′ ”

    Has someone faced such issue ?? help me on this issue

  49. Parry says:

    Hi Paul,

    Many thanks for sharing this tutorial. I have been able to do everything except getting SSL, SQLITE and other regular Python modules compiled on my target. I have the shared libraries and header files for these libraries compiled for my target. Where should I put them so that build process picks them up. In the section “Building SQLLite and other dependancies”, you mentioned that the patch for 2.7.3 should allow one to compile these packages if we put the header and shared libraries in the include and lib folder respectively under the python base folder. I did that but I still get the “Python build finished, but the necessary bits to build these modules were not found: ……” error. Can you please suggest what I should do to make sure that these modules also get added?

    Regards,
    Parry

Leave a Reply

(required)