Created
May 7, 2015 10:12
-
-
Save phillipberndt/ef25e95ef801707eb27a to your computer and use it in GitHub Desktop.
Compile Python for Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# This script builds & bundles Python for Android | |
# You'll end up with a tar.bz2 file that contains a Python distribution | |
# | |
# Requires all prerequisites to build Android on the host, and the NDK | |
# installed. | |
# | |
# This script creates a file python4android.tbz2. Unpack it on your device | |
# (into a non-noexec partition!) and enjoy. | |
# | |
set -e | |
# Set the correct NDK path here! | |
NDK=~/android/android-ndk-r10d | |
# This shouldn't need to be updated often.. but it might need updating, so check this: | |
NDK_TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin | |
NDK_SYSROOT=$NDK/platforms/android-21/arch-arm | |
## The remainder shouldn't need any changes, unless you want a newer Python version: | |
# 1) Download Python | |
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz | |
tar xaf Python-2.7.9.tar.xz | |
cd Python-2.7.9 | |
# 2) Compile host pgen/python | |
( | |
./configure | |
make python Parser/pgen | |
mv python pythonh | |
mv Parser/pgen Parser/pgenh | |
) 2>&1 | tee host-build.log | |
# 3) Patch the Makefile to use the host versions | |
sed -i -re 's#^(\s+)\$\(PGEN\)#\1$(PGEN)h#m' Makefile.pre.in | |
sed -i -re 's#./\$\(BUILDPYTHON\)#./$(BUILDPYTHON)h#m' Makefile.pre.in | |
make distclean | |
# 4) Patch some usually misdetected functions, setup modules to compile and fix some other problems | |
sed -i -re 's# ftime # #' configure | |
sed -i -re 's#p->pw_gecos#""#' Modules/pwdmodule.c | |
sed -i -re "s#exit_status = .+#exit_status = 0#" Lib/compileall.py | |
for MOD in _socket array cmath math _struct time operator _random _collections _heapq itertools strop _functools _elementtree datetime _bisect unicodedata _locale fcntl "select" mmap _md5 _sha256 _sha512 _sha binascii cPickle cStringIO _io; do | |
grep "#$MOD" Modules/Setup.dist | grep "\.c" | sed -re 's%^#%%' >> Modules/Setup.local | |
done | |
# 5) Configure | |
export PATH=${PATH}:$NDK_TOOLCHAIN | |
export SYSROOT=$NDK_SYSROOT | |
export CFLAGS="-fPIC -fPIE --sysroot $SYSROOT -Dandroid -mandroid" | |
export LDFLAGS="--sysroot $SYSROOT" | |
export ac_cv_file__dev_ptmx=no | |
export ac_cv_file__dev_ptc=no | |
./configure --disable-ipv6 --host=arm-linux-androideabi --build=x86_64-linux-gnu ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no 2>&1 | tee dev-build.log | |
# 6) Fix some mistakes configure made | |
sed -i -re "s%^#define HAVE_GETHOSTBYNAME_.+%%" pyconfig.h | |
echo "#define HAVE_GETHOSTBYNAME 1" >> pyconfig.h | |
# 7) Compile python | |
make 2>&1 | tee -a dev-build.log | |
# 8) Recompile the python executable as a position independent executable | |
rm -f python | |
sed -i -re 's#^LDFLAGS=#LDFLAGS= -pie #' Makefile | |
make 2>&1 | tee deb-build-pie.log | |
# 9) Install into our prefix and prepare an archive | |
make install DESTDIR=`pwd`/../prefix 2>&1 | tee install.log | |
cd ../prefix/usr/local/lib | |
rm -f *.a | |
cd python* | |
rm -rf test | |
find -iname "*.pyc" -exec rm \{\} \; | |
cd ../../bin | |
arm-linux-androideabi-strip python2.7 | |
cd .. | |
rm -f python.tbz2 | |
tar cjf python.tbz2 * | |
mv python.tbz2 ../../../python4android.tbz2 | |
# Done. You may remove the temporary directories now. |
huhuang03
commented
Sep 18, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment