Compile Python 3.2 with Android ndk R6

Source: Internet
Author: User

Reprinted please indicate the source is klayge Game Engine

 

The purpose of this article is not to completely port Python 3.2 to Android, but to compile a link library that can be used in your own program.

After porting boost 1.47, the next target is Python 3.2. Currently, Python only has 2.6.2 unofficially ported to Android (see p4a), and they are delayed in Porting 3.xbecause they think 3. X is useless -_-. It seems that this task can only be done by yourself. The difficulty of Python 3.x is completely unpredictable because it is significantly different from the previous version.

Preparations

Download required

  • Python 3.2.0
  • Crystax's ndk R6
  • Cygwin
  • P4a
Configure

According to the old habits of the Linux platform, many configurations are written in the. In file. You need to use configure to generate the corresponding. C or. h. Note that configure must use the ndk tool chain:

. /Configure-host = arm-Linux-androideabi cc = arm-Linux-androideabi-GCC cppflags = "-I $ android_ndk/platforms/Android-9/arch-arm/usr/include -I $ android_ndk/sources/cxx-STL/gnu-libstdc ++/include-I $ android_ndk/sources/cxx-STL/gnu-libstdc ++/libs/armeabi/include -I $ android_ndk/sources/crystax/include "cflags ="-nostdlib "ldflags ="-wl, -rpath-link = $ android_ndk/platforms/Android-9/arch-arm/usr/lib-L $ android_ndk/platforms/Android-9/arch-arm/usr/lib" libs = "-LC"

$ Android_ndk is an environment variable pointing to the root directory of the ndk.

After configure, pyconfig. H, config. C, and other files are generated. You need to manually copy pyconfig. H to the include directory.

Engineering documents

Unlike boost, Python does not have a compilation tool such as bjam, so you have to build an android. mk by yourself. I modified Android. mk of p4a and added some new. c files in 3.2.

Patch

P4a provides a Python-2.6.2-android.patch, but only applies to 2.6.2, After all 3. X and 2. x differ too much. Here, you need to modify the source code of Python 3.2 and manually add patches one by one. Note that pyconfig. h and config. c do not need to be changed.

Compile

Ndk-build was compiled. Everything went smoothly at the beginning, and then the problem occurred. The first compilation error is objects/unicodeobject. C. In the case of wchar, it requires the definition of py_format_long_long. But somehow, configure does not generate the correct py_format_long_long. So I added a line.

# Define py_format_long_long "ll"

Next, the modules/posixmodule. c compilation error occurs. After careful check, I found that because the pyconfig. h generated by configure contains

# Define have_dev_ptmx 1

But in pyconfig. h of p4a, here is

# UNDEF have_dev_ptmx

The possible cause is that cygwin supports/dev/ptmx, but Android does not. Therefore, configure incorrectly defines have_dev_ptmx Based on cygwin. In many cases, have_killpg also needs to be replaced by # UNDEF.

The most tricky problem is that the fdatasync compilation error cannot be found in modules/posixmodule. C. This function is defined in $ android_ndk/platforms/Android-9/arch-arm/usr/include/unistd. h, the directory has been added to Android. mk. By using ndk-build ndk_log = 1 V = 1 to carefully check each step, we found that ndk uses the Android-3 API by default, even if you have defined local_c_javasdes, it is useless. Therefore, it actually contains unistd. h under Android-3, while Android-3 does not support fdatasync, and the function is placed in # If 0. So I have to add an application. mk with only one line of content:

App_platform: = Android-9

Now, Python 3.2 can be compiled using ndk to generate libpython3.2.so.

Can I use VC for compilation?

I still hope that the same source code can be compiled using VC to generate python for Windows. There is basically only one problem. The pyconfig. h of Android is different from the pyconfig. h of windows. So I named the former pyconfig. android. h. The latter is named pyconfig. win. h, and respectively put the corresponding pyconfig in the compilation script and pre-build event. xxx. h overwrite and copy to pyconfig. h. In this way, different compilation systems can be compiled smoothly.

Together

In the attachment, you can find all the files I have modified. Before compilation, you must set an environment variable named "android_ndk" and only want your ndk root directory.

Python_for_android_patches.7z

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.