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:
1 |
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:
1 2 3 4 5 |
./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:
1 2 |
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:
1 |
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:
1 |
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:
1 |
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:
1 |
python lib/Python2.7/test/test___all___.py |
Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.
Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress
hi,
what should I type for ARM instead of MIPS ?
thank you
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
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 ?
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:
@Jean-Luc Aufranc (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..
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..
@sai
Maybe it’s because of the compiler you use.
To cross compiling Python 3.5 for arm, what the patch i have to use ? or any other process to cross compile.
Suggest me
thq
Hi all,
when I’m executing this command “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” I’m getting the following error message:
“configure: error: invalid variable name: `–host'” Do you have any idea whats causing this?
Thank you
@Steven
Sorry there was some problems with the website platform (WordPress) .
where double hyphen
would show as a single hyphen
I’ve now updated the post to fix the issue. The instructions are old so they may not work with the latest version Python, but should still work with Python 2.7.
When i am running this command
make install HOSTPYTHON=./hostpython BLDSHARED=”arm-none-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-none-linux- CROSS_COMPILE_TARGET=yes prefix=/home/arm_install
getting the error as
/usr/bin/install -c -m 644 ./LICENSE /home/arm_install/lib/python2.7/LICENSE.txt
PYTHONPATH=/home/arm_install/lib/python2.7 \
./hostpython -Wi -tt /home/arm_install/lib/python2.7/compileall.py \
-d /home/arm_install/lib/python2.7 -f \
-x ‘bad_coding|badsyntax|site-packages|lib2to3/tests/data’ \
/home/arm_install/lib/python2.7
Traceback (most recent call last):
File “/home/arm_install/lib/python2.7/compileall.py”, line 17, in
import struct
File “/home/arm_install/lib/python2.7/struct.py”, line 1, in
from _struct import *
ImportError: /u02/python_crosscompile/Python-2.7.1/build/lib.linux-x86_64-2.7/_struct.so: wrong ELF class: ELFCLASS32
make: *** [libinstall] Error 1
what is this ? How to solve.
@Manikanta
It could be a 32-bit/64-bit issue. Try to look for “wrong ELF class: ELFCLASS32” error on the web.
ake install HOSTPYTHON=./hostpython BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=mipsel-linux- CROSS_COMPILE_TARGET=yes prefix=/home/liucong/Python-2.7.1/install Parser/pgen ./Grammar/Grammar ./Include/graminit.h ./Python/graminit.c Parser/pgen: Parser/pgen: cannot execute binary file make: [Include/graminit.h] Error 126 (ignored) Parser/pgen ./Grammar/Grammar ./Include/graminit.h ./Python/graminit.c Parser/pgen: Parser/pgen: cannot execute binary file make: [Python/graminit.c] Error 126 (ignored) /usr/bin/install -c python /home/liucong/Python-2.7.1/install/bin/python2.7 if test -f libpython2.7.a; then \ if test -n “” ; then \ /usr/bin/install -c -m 555 /home/liucong/Python-2.7.1/install/bin; \ else \ /usr/bin/install -c -m 555 libpython2.7.a /home/liucong/Python-2.7.1/install/lib/libpython2.7.a; \ if test libpython2.7.a != libpython2.7.a; then \ (cd /home/liucong/Python-2.7.1/install/lib; ln -sf libpython2.7.a libpython2.7.a) \ fi \ fi; \ else true; \ fi File “./setup.py”, line 316 self.announce(‘*** WARNING: renaming “%s”… Read more »
Linux 2.6.. you are using a really old Linux distribution.
But anyway, is it a MIPS system? I can see it fails to run Parser/pgen
> But anyway, is it a MIPS system?
x64 running CentOS 6.
Yes.. I should have paid more attention to the log.
The OP should look at Parser/pgen with file or other tools.
I want to cross compile Python for mips on x64 running centos 6, how to solve this problem or what should I do?thanks
You need to solve this issue:
“Parser/pgen: Parser/pgen: cannot execute binary file”
Analyze the file with “file” utility, and possibly run the make command in verbose mode:
today,I trying . I found this problem to occur after the command was executed.this problem stills exists make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=mipsel-linux- CROSS_COMPILE_TARGET=yes configure: WARNING: If you wanted to set the –build type, don’t use –host. If a cross compiler is detected then cross compile mode will be used. checking for –enable-universalsdk… no checking for –with-universal-archs… 32-bit checking MACHDEP… linux2 checking EXTRAPLATDIR… checking machine type as reported by uname -m… x86_64 checking for –without-gcc… no checking for mipsel-linux-gcc… mipsel-linux-gcc checking whether the C compiler works… yes checking for C compiler default output file name… a.out checking for suffix of… Read more »
I suspect that this “pgen” utility was built with the cross-compiler instead of being built with your local CC. I’m seeing a HOSTPGEN variable in your build command that makes me think that this utility ought to have been used instead. You should carefully review the makefile to be sure that your variables are properly named and that the resulting utility is usable.