cibuildwheel icon indicating copy to clipboard operation
cibuildwheel copied to clipboard

feat(android): add Rust cross-compilation setup for Android environment

Open ririv opened this issue 1 month ago • 8 comments

Like platform windows

https://github.com/pypa/cibuildwheel/blob/c53e541c2de29c8bb0a8f6e3ff13d96046e8000c/cibuildwheel/platforms/windows.py#L206-L232

Add Rust cross-compilation setup for Android environment, it can fix the cross-compilation problem for the rust project built with setuptools-rust

ririv avatar Nov 05 '25 02:11 ririv

For Rust projects built with Maturin, it currently does not work. Maturin has some cross-compilation issues on Android that still need to be fixed.

For details, please see https://github.com/PyO3/maturin/issues/2810

ririv avatar Nov 05 '25 03:11 ririv

This looks nice and neat! Is there an example of it working in a real project that you've been testing it with?

joerick avatar Nov 07 '25 12:11 joerick

I want to test, but I did not know how to run this without the github action,if you know how to do, you can tell me,and I will test it.

This commit is a modification I made based on the actual missing environment configuration built on Android using cbuildwheel.

Of course, I manually configured it externally in a non-isolated environment, and after the configuration, I built the wheel correctly and it can run on Android

ririv avatar Nov 07 '25 12:11 ririv

You can test a project using something like this in a github workflow-

      - name: Build wheels
        uses: ririv/cibuildwheel@main

joerick avatar Nov 07 '25 14:11 joerick

Thanks, I have tested it, and made some new commits. Now, it works.

  • The CARGO_TARGET_<triple>_LINKER must be set. Otherwise, it will use the host's linker cc, which will result in an error.
  • The PYO3_CROSS_LIB_DIR must be set to link against libpython3.x.so explicitly. See: https://peps.python.org/pep-0738/#linkage and https://pyo3.rs/v0.27.1/building-and-distribution.html#cross-compiling

The PYO3_CROSS_LIB_DIR environment variable should be set here rather than requiring users to specify it manually, for the following reasons:

  • PYO3 has become the de facto standard for Python extension modules built with Rust. Both setuptools-rust and maturin are tools developed by the PYO3 team specifically to serve PYO3.
  • The value set for PYO3_CROSS_LIB_DIR requires the directory path of target_python. This path is already configured within cbuildwheel's android.py but cannot be directly accessed externally. External configuration would necessitate manually constructing the path.
  • PYO3's documentation for Android cross-compilation is insufficiently detailed. If users attempt to set this variable manually, they are likely to become confused and unsure how to proceed.

ririv avatar Nov 10 '25 07:11 ririv

https://github.com/ririv/android-wheels/blob/main/.github/workflows/cbuildweel-repair-test.yml This is the workflow file. It performed some conversions. For the maturin project, it was converted to a setuptools-rust project. This is because maturin has some bugs in Android cross-compilation. I've submitted a PR to fix it, but the maturin maintainer hasn't responded yet.

Here are some logs:

  • pydantic-core | maturin convert to setuptools-rust https://github.com/ririv/android-wheels/actions/runs/19223626338/job/54946144941
  • tiktoken | based on setuptools-rust https://github.com/ririv/android-wheels/actions/runs/19223649349/job/54946212372

ririv avatar Nov 10 '25 07:11 ririv

Looks good to me! @mhsmith what do you think?

joerick avatar Nov 28 '25 15:11 joerick

Sorry for the delay, I'll look at this as soon as I can.

mhsmith avatar Dec 02 '25 13:12 mhsmith