Search code examples
pythonlinuxpython-2.7cross-compilingembedded-linux

How to cross-compile Python?


I have an SoC with Cortex A9 ARMv7 architecture. I need to be able to run python scripts on a board with this SoC. So I need to cross-compile Python for this platform.

The SoC has ARMv7 architecture, so I've installed the arm-linux-gnueabihf cross-compiler for Ubuntu 20.04 running in the VirtualBox.

I am following this instruction:

  1. First I've downloaded Python 2.7.1 sources and extracted it to the /home/user/python/Python-2.7.1 directory.

  2. Then I've downloaded the patch from the instruction I am following.

  3. Then I've applied the patch:

    patch -p1 < python-2.7.1-cross-compile.patch
    
  4. Then I've compiled some tools for host:

    ./configure
    make python Parser/pgen
    mv python hostpython
    mv Parser/pgen Parser/hostpgen
    make distclean
    
  5. Then I've configured Python for cross-compilation:

    readonly CROSS_COMPILER=arm-linux-gnueabihf
    
    CC=${CROSS_COMPILER}-gcc \
    CXX=${CROSS_COMPILER}-g++ \
    AR=${CROSS_COMPILER}-ar \
    RANLIB=${CROSS_COMPILER}-ranlib \
    ./configure \
    --host=${CROSS_COMPILER} \
    --target=${CROSS_COMPILER} \
    --prefix=/python
    
  6. And then I finally cross-compile it:

    make \
    HOSTPYTHON=./hostpython \
    HOSTPGEN=./Parser/hostpgen \
    BLDSHARED="${CROSS_COMPILER}-gcc -shared" \
    CROSS_COMPILE=${CROSS_COMPILER}- \
    CROSS_COMPILE_TARGET=yes
    

    But eventually I have got an IndentationError:

    /usr/lib/gcc-cross/arm-linux-gnueabihf/9/../../../../arm-linux-gnueabihf/bin/ld: libpython2.7.a(posixmodule.o): in function `posix_tmpnam':
    /home/user/python/Python-2.7.1/./Modules/posixmodule.c:7346: warning: the use of `tmpnam_r' is dangerous, better use `mkstemp'
    /usr/lib/gcc-cross/arm-linux-gnueabihf/9/../../../../arm-linux-gnueabihf/bin/ld: libpython2.7.a(posixmodule.o): in function `posix_tempnam':
    /home/user/python/Python-2.7.1/./Modules/posixmodule.c:7301: warning: the use of `tempnam' is dangerous, better use `mkstemp'
    File "./setup.py", line 316
      self.announce('*** WARNING: renaming "%s" since importing it'
        ^
    IndentationError: expected an indented block
    make: *** [Makefile:425: sharedmods] Error 1
    

What I do wrong and how to solve this problem?


It seems that Python itself is successfully compiled and linked because after all this process I get the python file in the build directory:

$ file python
python: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=1a757bca3295fe062ffbee5cf2d791eb36861524, for GNU/Linux 3.2.0, with debug_info, not stripped

Solution

  • I did python 2.7.3 a while ago. The summary is here. All other files are in the same directory as this txt summary. Not sure whether it still works or not on ubuntu 20. Though it was for arm v5, you'll need to do some adjustment.

    Any version after 2.7.3 uses a dramatically different build system. The same was not working. I'm looking for instructions to build 3.8.


    Update: If you want to build python 3.8.6, I saw someone mentioned on SO about using buildroot. Here is a note that works. I've created a virtual env using it on the target device.