Home > Linux, Linux 2.6, Programming > Cross-compiling Python for MIPS and ARM Platforms

Cross-compiling Python for MIPS and ARM Platforms

Python programming language is used in several open source projects such as Sugar OS and Xibo. Let’s see if we can cross-compile it in Ubuntu 10.10 using a mips compiler.

I’ll use the instructions given at http://randomsplat.com/id5-cross-compiling-python-for-embedded-linux.html.

Let’s download Python 2.7.1 first and extract the source code:

tar xjvf Python-2.7.1.tar.bz2

Then run the following command in Python-2.7.1 in order to build some tools for the host:

./configure
make python Parser/pgen
mv python hostpython
mv Parser/pgen Parser/hostpgen
make distclean

There is no patch for Python 2.7.1 cross-compilation in the link above, so let’s just try to configure and build it:

CC=mipsel-linux-gcc CXX=mipsel-linux-g++ AR=mipsel-linux-ar RANLIB=mipsel-linux-ranlib ./configure –host=mipsel-linux –target=mipsel-linux –prefix=/python

make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=mipsel-linux- CROSS_COMPILE_TARGET=yes

If we don’t use a patch the first error is:

Include/pyport.h:243:13: error: #error “This platform’s pyconfig.h needs to define PY_FORMAT_LONG_LONG”

So I used some older patch to create a new patch: http://www.cnx-software.com/patch/python-2.7.1-cross-compile.patch. You can download it an apply it as follows:

patch -p1 < python-2.7.1-cross-compile.patch

And repeat the step above to configure and cross-compile Python for mips.

Finally install Python in ~/Python-2.7.1/install for example:

make install HOSTPYTHON=./hostpython BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=mipsel-linux- CROSS_COMPILE_TARGET=yes prefix=~/Python-2.7.1/install

After that copy all necessary files in ~/Python-2.7.1/install to your target board and run the python test:

python lib/Python2.7/test/test___all___.py

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter

  1. novita
    August 25th, 2011 at 12:53 | #1

    hi,

    what should I type for ARM instead of MIPS ?

    thank you

    • August 25th, 2011 at 12:58 | #2

      You need to change that part:

      CC=mipsel-linux-gcc CXX=mipsel-linux-g++ AR=mipsel-linux-ar RANLIB=mipsel-linux-ranlib ./configure –host=mipsel-linux –target=mipsel-linux –prefix=/python

      make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=mipsel-linux- CROSS_COMPILE_TARGET=yes

      But it depends on the name of your ARM cross-compiler. If you are using Linaro gcc: arm-linux-gnueabi-gcc, then it would be something like:

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

      make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=arm-linux-gnueabi- CROSS_COMPILE_TARGET=yes

  2. maciej walczak
    October 8th, 2011 at 00:04 | #3

    And how add support for dbus and glib to python running on arm ? I made this procedure and everything works just fine but I need also dbus python module, do you have any guide how to do it ?

    • October 8th, 2011 at 08:17 | #4

      Sorry, I haven’t done this. But the code for dbus and glib can be downloaded so I suppose it should not be a problem.
      I’ve checked the code at http://dbus.freedesktop.org/releases/dbus-python/
      It seems you can just do something like:

      CC=mipsel-linux-gcc CXX=mipsel-linux-g++ AR=mipsel-linux-ar RANLIB=mipsel-linux-ranlib ./configure –host=mipsel-linux –target=mipsel-linux –prefix=/python
      make
      make install

  3. sai
    March 23rd, 2016 at 11:33 | #5

    @cnxsoft
    for arm, make file created successfully without errors. But when i make by using the command
    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-none-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-none-linux- CROSS_COMPILE_TARGET=yes

    i got the error as

    arm-none-linux-gnueabi-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:0,
    from ./Modules/python.c:3:
    Include/pyport.h:243:13: error: #error “This platform’s pyconfig.h needs to define PY_FORMAT_LONG_LONG”
    make: *** [Modules/python.o] Error 1

    Help me to solve the issue..

  4. sai
    March 23rd, 2016 at 11:43 | #6

    It successfully created make file for arm, But while make by using the command

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

    i get the error as

    arm-none-linux-gnueabi-gcc -Xlinker -export-dynamic -o python \
    Modules/python.o \
    libpython2.7.a -lpthread -ldl -lpthread -lutil -lm
    File “./setup.py”, line 316
    self.announce(‘*** WARNING: renaming “%s” since importing it’
    ^
    IndentationError: expected an indented block
    make: *** [sharedmods] Error 1

    Help me to solve..

  5. March 23rd, 2016 at 11:47 | #7

    @sai
    Maybe it’s because of the compiler you use.

  6. sai
    March 24th, 2016 at 18:26 | #8

    To cross compiling Python 3.5 for arm, what the patch i have to use ? or any other process to cross compile.
    Suggest me

  7. sai
    March 25th, 2016 at 10:53 | #9

    cnxsoft :
    @sai
    Maybe it’s because of the compiler you use.

    thq

  1. March 4th, 2012 at 14:31 | #1
  2. April 16th, 2013 at 22:29 | #2