How to cross-compile Python Modules

Hi,
I cross-compiled python 3.9.4 for Android.
How do I cross-compile CPython modules?
I found one tool online: crossenv · PyPI
but it doesn’t seem to be compatible with Android clang?
Does CPython support cross-compiling modules?
Please advise.

Hi, I don’t think cross compilation is officially supported. Also a lot of modules use a custom setup.py that goes against standards of cross-compilation.

if your host python is clang built you may want to extend the replay method of pyodide project with pywasmcross.py pyodide/pyodide-build/pyodide_build at main · pyodide/pyodide · GitHub : it’s a very generic way to build extensions with clang but afaik it does not support ndk out of the box.

If you only need a handfull of tested android modules and thirdparties library support you can look at kivy https://pypi.org/project/python-for-android + pydk github.com/pmp-p/pydk ( very close to crossenv but geared toward ndk and emsdk )

I cross-compiled python 3.9.4 for Android.

Please share the patches and tools for running testsuites if you have any success in that matter.

ref Packaging and python-for-android - #22 by jtt

On conda-forge, we are using cross-env to create a virtual environment suitable for cross-compiling where also some of the Python modules from the host can be loaded in the setup.py for the to-be compiled package.

The most interesting details for this setup are encapsulated in the following script: cross-python-feedstock/activate-cross-python.sh at 41083676420cef1f476a0bde41632cdf7334279b · conda-forge/cross-python-feedstock · GitHub

For a conda(-build) user it is as simple as adding cross-python as a dependency and specifying their respective target_platform. Outside of the conda setup, it will probably be a bit more complicated.

This setup has enabled us to cross-compile nearly all of the major Python packages for the Apple M1.