转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=1474

This article is not about completely porting the Python 3.2 to Android, but just compiling a linking library that can be used in your own program.

After building boost 1.47 with NDK, my next target is Python 3.2. Currently, only Python 2.6.2 is unofficially ported to Android (See P4A), and they haven’t start to work on 3.x. The main reason is that they don’t think 3.x is necessary-_-. As a result, I have to do it my own. Because Python 3.x is quite different with its previous versions, the difficulties is not predictable.

Preparation

Some packages must be downloaded

configure

According to the habit in linux, many configurable items are inside the .in file. You need to run configure script to generate the corresponding .c or .h. Note that you need to let configure uses the NDK’s toolchain:

./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”

where $ANDROID_NDK is a environment variable point to your NDK’s root.

After this step, files such as pyconfig.h and config.c are generated. You need to copy pyconfig.h to Include folder manually.

Project files

Different with boost, python don’t have a selft compiling tool like bjam. We have to create a Android.mk. I took the P4A’s Android.mk with some modification, some .c files newly inside 3.2 are added.

Patches

P4A provides a Python-2.6.2-android.patch, but it’s for 2.6.2 only. After all, the gap between 3.x and 2.x is too huge. Here you need to apply patches by hand. Note that don’t modify pyconfig.h and config.c any more.

Compiling

Run ndk-build to compile Python 3.2. It’s good at the beginning, but not for long. The first compiling error is from Objects/unicodeobject.c. When wchar is available, PY_FORMAT_LONG_LONG is required to be defined. However, configure doesn’t generate the write PY_FORMAT_LONG_LONG. So I add:

1
#define PY_FORMAT_LONG_LONG "ll"

Then, Modules/posixmodule.c gets error. After a carefully check, I found that inside the pyconfig.h by configure, there’s

1
#define HAVE_DEV_PTMX 1

But in P4A’s pyconfig.h, it’s

1
#undef HAVE_DEV_PTMX

One possible reason is that Cygwin supports /dev/ptmx, but Android doesn’t. So configure detects the Cygwin’s /dev/ptmx and wrongly defines HAVE_DEV_PTMX. The same situation happens in HAVE_KILLPG. It should be #undef, too.

The most challenging one is a fdatasync undefined error in Modules/posixmodule.c. This function is inside $ANDROID_NDK /platforms/android-9/arch-arm/usr/include/unistd.h. Since the folder have already in the Android.mk as include folder, it would be found. So I have to use ndk-build NDK_LOG=1 V=1 to look at every step it runs.  It shows that NDK uses android-3′s API by default, the folders in LOCAL_C_INCLUDES is after the android-3′s. What is really included is android-3′s unistd.h, and fdatasync is not supported in android-3′s (its in the #if 0). Finally I have to add an Application.mk with only one line:

1
APP_PLATFORM := android-9

Now, you can use NDK to compile Python 3.2.  A libpython3.2.so is generated.

Build by VC again?

I require that the same source code can be compiled by VC to generate the Python for Windows. There’s only one problem, Android’s pyconfig.h and Windows’ pyconfig.h is different. So the former one is renamed to pyconfig.android.h, and the latter one to pyconfig.win.h. Add a copy command in both building script and Pre-Build Event, use pyconfig.xxx.h to overwrite pyconfig.h. As a result, all compilers work fine.

All in one

You can find my modified files in the attachment. Before compiling, you need to set an environment variable name “ANDROID_NDK”, which is point to your NDK’s root.

Python_for_android_patches.7z

分享到: