Skip to content

Instantly share code, notes, and snippets.

@ericzolf
Forked from phillipberndt/gist:ef25e95ef801707eb27a
Last active April 30, 2017 08:28
Show Gist options
  • Select an option

  • Save ericzolf/782c2a50469a3fa8fa9d7358cc619eb9 to your computer and use it in GitHub Desktop.

Select an option

Save ericzolf/782c2a50469a3fa8fa9d7358cc619eb9 to your computer and use it in GitHub Desktop.
Compile Python for Android
#!/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=~/Public/Android/Sdk/ndk-bundle
# Enter the correct Python version
PYVER=2.7.13
# 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-24/arch-arm
## The remainder shouldn't need any changes, unless you want a newer Python version:
# 1) Download Python
wget -N https://www.python.org/ftp/python/${PYVER}/Python-${PYVER}.tar.xz
rm -fr Python-${PYVER}
rm -fr prefix
tar xaf Python-${PYVER}.tar.xz
cd Python-${PYVER}
# 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
sed -i -re "s#ret = os.system\\('%s -E -v - </dev/null 2>%s 1>/dev/null' % \\(gcc, tmpfile\\)\\)#ret = os.system('%s %s -E -v - </dev/null 2>%s 1>/dev/null' % (gcc, sysconfig.get_config_var('CPPFLAGS'), tmpfile))#" setup.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 zlib; 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 CPPFLAGS="--sysroot $SYSROOT"
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.tgz
tar czf python.tgz *
mv python.tgz ../../../python4android-${PYVER}.tgz
# Done. You may remove the temporary directories now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment