Skip to content

Conversation

@ririv
Copy link

@ririv ririv commented Nov 5, 2025

Like platform windows

def setup_rust_cross_compile(
tmp: Path, # noqa: ARG001
python_configuration: PythonConfiguration,
python_libs_base: Path, # noqa: ARG001
env: MutableMapping[str, str],
) -> None:
# Assume that MSVC will be used, because we already know that we are
# cross-compiling. MinGW users can set CARGO_BUILD_TARGET themselves
# and we will respect the existing value.
cargo_target = {
"64": "x86_64-pc-windows-msvc",
"32": "i686-pc-windows-msvc",
"ARM64": "aarch64-pc-windows-msvc",
}.get(python_configuration.arch)
# CARGO_BUILD_TARGET is the variable used by Cargo and setuptools_rust
if env.get("CARGO_BUILD_TARGET"):
if env["CARGO_BUILD_TARGET"] != cargo_target:
log.notice("Not overriding CARGO_BUILD_TARGET as it has already been set")
# No message if it was set to what we were planning to set it to
elif cargo_target:
log.notice(f"Setting CARGO_BUILD_TARGET={cargo_target} for cross-compilation")
env["CARGO_BUILD_TARGET"] = cargo_target
else:
log.warning(
f"Unable to configure Rust cross-compilation for architecture {python_configuration.arch}"
)

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

@ririv ririv requested a review from mhsmith as a code owner November 5, 2025 02:59
@ririv
Copy link
Author

ririv commented Nov 5, 2025

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 PyO3/maturin#2810

@joerick
Copy link
Contributor

joerick commented Nov 7, 2025

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

@ririv
Copy link
Author

ririv commented Nov 7, 2025

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

@joerick
Copy link
Contributor

joerick commented Nov 7, 2025

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

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

@ririv ririv force-pushed the main branch 10 times, most recently from 1384753 to 99ee18a Compare November 10, 2025 06:01
…_DIR to link against libpython3.x.so explicitly
@ririv
Copy link
Author

ririv commented Nov 10, 2025

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

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
Copy link
Author

ririv commented Nov 10, 2025

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:

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants