Skip to content

Instantly share code, notes, and snippets.

@ChronoMonochrome
Last active April 23, 2025 11:34
Show Gist options
  • Save ChronoMonochrome/dfedd83ab85f94ad8ed63bc4446bcfeb to your computer and use it in GitHub Desktop.
Save ChronoMonochrome/dfedd83ab85f94ad8ed63bc4446bcfeb to your computer and use it in GitHub Desktop.
Build script to cross compile Python 3.9 for Android

Setup environment

wget https://dl.google.com/android/repository/android-ndk-r23c-linux.zip
unzip android-ndk-r23c-linux.zip

export NDK=$(realpath android-ndk-r23c)
$NDK/build/tools/make_standalone_toolchain.py  --arch arm --api 21 --install-dir  $(realpath toolchain)

export PATH=$PATH:$(realpath toolchain/bin)


export PATH=$(realpath android-ndk-r23c/toolchains/llvm/prebuilt/linux-x86_64/bin):$PATH

# Tell configure what tools to use.
target_host=arm-linux-androideabi
export AR=$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar
export AS=$target_host-clang
export CC=$target_host-clang
export CXX=$target_host-clang++
export LD=$target_host-ld
export RANLIB=$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ranlib
export READELF=$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-readobj
export STRIP=$target_host-strip

# Tell configure what flags Android requires.
export CFLAGS="-fPIE -fPIC"
export LDFLAGS="-pie"

export PYTHON_PREFIX=$(realpath ./python_install)

Build Zlib

git clone https://github.com/madler/zlib
cd zlib
./configure  --prefix=$PYTHON_PREFIX

make
make install

Build OpenSSL

git clone https://github.com/openssl/openssl
cd openssl
./Configure linux-generic32 shared -DL_ENDIAN --prefix=$PYTHON_PREFIX --openssldir=$PYTHON_PREFIX

make MAKEDEPPROG=arm-linux-androideabi-clang PROCESSOR=ARM -j8
make install

Build libffi

git clone https://github.com/libffi/libffi
cd libffi
CC=arm-linux-androideabi-gcc LD=arm-linux-androideabi-ld ./configure --host=arm-linux-androideabi \
 --prefix=$(realpath ./python_install)  --disable-multi-os-directory
make -j8
make install

Build python interpreter

git clone https://github.com/python/cpython -b 3.9

cd cpython

./configure --host=arm-linux-androideabi --target=arm-linux-androideabi --build=x86_64-linux-gnu \
 --prefix=$PYTHON_PREFIX CPPFLAGS="-I$PYTHON_PREFIX/include" LDFLAGS="-L$PYTHON_PREFIX/lib"      \
 --disable-ipv6 ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no ac_cv_have_long_long_format=yes   \
 --enable-shared --libdir=/system/lib --with-platlibdir=/system/lib

#sudo mkdir /system/lib

make -j8
make install

wd=/media/system/dev/ARM
ctypes=cpython/Modules/_ctypes

arm-linux-androideabi-clang -shared -L$wd/python_install/lib -march=armv7-a -Wl,--fix-cortex-a8        \ 
-pie -pie -fPIE -fPIC build/temp.linux-arm-3.9$wd/cpython/Modules/_ctypes/_ctypes.o                    \
build/temp.linux-arm-3.9$wd/$ctypes/callbacks.o build/temp.linux-arm-3.9$wd/$ctypes/callproc.o         \
build/temp.linux-arm-3.9$wd/$ctypes/cfield.o build/temp.linux-arm-3.9$wd/$ctypes/stgdict.o             \
$wd/libffi/arm-unknown-linux-androideabi/.libs/libffi.a  -L. -L$wd/python_install/lib -ldl -lpython3.9 \
-o build/lib.linux-arm-3.9/_ctypes.cpython-39.so

cp build/lib.linux-arm-3.9/_ctypes.cpython-39.so $PYTHON_PREFIX/system/lib/python3.9/lib-dynload

Links

make install doesn't respect configure --with-platlibdir=lib64
Standalone toolchains
Cross compiling python for ARM from source
Cross-Compile Python with zlib support cross compiling of openssl for linux arm-v5te-linux-gnueabi toolchain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment