How to cross-compile python to the arm-Linux platform

Source: Internet
Author: User
Preface

I need to run python on the S3C2410 CPU of ARM9. The following is my compilation process.

Host compiling environment:

Ubuntu 7.04, GCC 4.2.1, arm-Linux-GCC 3.3.2

Program to cross compile:

  • SQLite 3.5.6,
  • Python 2.5.1
Compile SQLite

First go to The http://www.sqlite.org/download.html to download the latest SQLite source code, I use 3.5.6 here.

I recommend that you use the source code of the amalgamation version. This Code only contains a few files, which is easy to compile. It is said that if the compiler is good, more efficient code may be compiled. I downloaded

http://www.sqlite.org/sqlite-amalgamation-3.5.6.tar.gz

Run the following steps:

tar zxf sqlite-amalgamation-3.5.6.tar.gzcd sqlite-3.5.6./configure --host=arm-linux --prefix=/usr/local/arm/3.3.2 --enable-shared --disable-readline --disable-dynamic-extensions

The above is to decompress SQLite and make some configuration. Here, I want SQLite to be installed in/usr/local/ARM/3.3.2 at that time to generate a dynamic link library instead of Readline, do not use dynamic expansion of SQLite.

Edit makefile and remove-g from cflag and cxxflag. We do not need to debug SQLite.

Next we can compile and install SQLite:

makemake install


This step completes the compilation and installation of SQLite.

Compile Python

Go to http://www.python.org/download/to download the latest Python source code. Here I download:

http://www.python.org/ftp/python/2.5.1/Python-2.5.1.tar.bz2

First decompress Python:

tar jxf Python-2.5.1.tar.bz2cd Python-2.5.1

Compile the PC version of the syntax parser

When compiling python, You need to compile a program named pgen to generate a syntax parser. Therefore, we need to generate a PC version of pgen:

mkdir build.pccd build.pc../configuremake Parser/pgen

Then ls parser, we should be able to see the pgen.

Modify configure

When configure detects whether the printf of the compiler supports % ZD, it will not work if it finds that it is in cross compile. Why?

Remove this part of detection code. This code starts

echo "$as_me:$LINENO: checking for %zd printf() format support" >&5echo $ECHO_N "checking for %zd printf() format support... $ECHO_C" >&6if test "$cross_compiling" = yes; then

End

cat >>confdefs.h <<\_ACEOF#define PY_FORMAT_SIZE_T "z"_ACEOFelse  echo "$as_me: program exited with status $ac_status" >&5echo "$as_me: failed program was:" >&5sed 's/^/| /' conftest.$ac_ext >&5( exit $ac_status )echo "$as_me:$LINENO: result: no" >&5echo "${ECHO_T}no" >&6firm -f core *.core gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_extfi

Delete the two sections and the intermediate content.

Compile python for ARM

With the syntax parser, you can start compiling python of the arm version.

mkdir ../build.armcd ../build.arm../configure --prefix=/home/leojay/test/arm-system-working/rootfs --disable-ipv6 --host=arm-linux --enable-shared

First, create a directory build. Arm for compilation, and then configure python, such as the installation directory. Do not use IPv6. Use the arm-Linux compiler to generate a dynamic link library.

Modify makefile

Make some modifications to the makefile.

OPT=            -DNDEBUG -g -O3 -Wall -Wstrict-prototypes

In a row, remove-G. Do not debug python. Change-O3 to-O2, so that the space is tight.

In

PGEN=           Parser/pgen$(EXE)

Add

PGEN_HOST=      ../build.pc/Parser/pgen$(EXE)

Indicates that the pgen we run on the host

Change pgen to pgen_host:

$(GRAMMAR_H) $(GRAMMAR_C): $(PGEN) $(GRAMMAR_INPUT)                -$(PGEN) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)

Changed:

$(GRAMMAR_H) $(GRAMMAR_C): $(PGEN) $(GRAMMAR_INPUT)                -$(PGEN_HOST) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)

Modify allUseNew python.

All places such as./$ (buildpython) are changed to Python

For example:

platform: $(BUILDPYTHON)        $(RUNSHARED) ./$(BUILDPYTHON) -E -c 'import sys ; from distutils.util import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform

Change

platform: $(BUILDPYTHON)        $(RUNSHARED)  python  -E -c 'import sys ; from distutils.util import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform

There are many such places, so you should be careful to modify them.

Modify setup. py

Setup. py is responsible for compiling various Python extension modules. However, since Python does not consider cross compile at all, some modifications are required.

  • Pybuildext class:

    1. Build_extension function:

      • After compiling all the extension functions, this function will load the extension just compiled, but we obviously cannot load the extension on the i686 computer, so skip these operations. Write a return directly after build_ext.build_extension (self, ext) without load.
    2. Detect_modules function:
      • The first two lines of the function are to add/usr/local to the search directory. Our cross compiler is generally not directly installed in/usr/local, so these two lines are removed:
      • add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
      • In the settings of lib_dirs and inc_dirs, remove all the braces. Do not use any of the following modules:
    3. Cmath, ctypes, _ testcapi, PWD, GRP, spwd, MMAP, audioop, imageop, rgbimg, Readline,
    4. ssl, openssl, bdb, dbm, termios, nsl, ncurses, bz2, linuxaudiodev, ossaudiodev, tkinter

      Due to Python problems, currently, ctypes cannot run on machines other than i386, so ctypes also removes the compiling of SQLite:

    5. for d in inc_dirs + sqlite_inc_paths:

      Changed:

    6. for d in ['/usr/local/arm/3.3.2/include']

Because my sqlite3 is installed here, if it is not changed here, setup. py will find SQLite on my computer

  • Main function:

    • When the setup function is called, remove the part of the scripts to be installed.

Then you can make & make install.

The modified makefile and setup. py are attached for your reference.

Reduce Python

After python is fully installed, it is very large. Therefore, you need to remove some libraries that are definitely not usable. So, attach my cut script cleanpy. Sh.

Note that because all my python programs run under the python-oo parameter, all. py and. PyC do not need it, as long as there is. Pyo. Therefore, this script will delete all. py and. PyC.

You can make adjustments as needed.

For more information about how to generate the. Pyo file for all libraries, see my article: http://wiki.woodpecker.org.cn/moin/LeoJay/PyPackage

Note:

Please respect the original author's labor results, please note the source of the extraction: http://wiki.woodpecker.org.cn/moin/LeoJay/HOWTOCrossCompilePythonForARM

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.