Skip to content

Commit dd76a27

Browse files
committed
feat(android): add Rust cross-compilation setup for Android environment
1 parent c53e541 commit dd76a27

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cibuildwheel/platforms/android.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pprint import pprint
1515
from runpy import run_path
1616
from textwrap import dedent
17-
from typing import Any
17+
from typing import Any, MutableMapping
1818

1919
from build import ProjectBuilder
2020
from build.env import IsolatedEnv
@@ -340,6 +340,8 @@ def localized_vars(
340340
def setup_android_env(
341341
config: PythonConfiguration, python_dir: Path, venv_dir: Path, build_env: dict[str, str]
342342
) -> dict[str, str]:
343+
setup_rust_cross_compile(config, build_env)
344+
343345
site_packages = next(venv_dir.glob("lib/python*/site-packages"))
344346
for suffix in ["pth", "py"]:
345347
shutil.copy(resources.PATH / f"_cross_venv.{suffix}", site_packages)
@@ -390,6 +392,26 @@ def setup_android_env(
390392
return android_env
391393

392394

395+
def setup_rust_cross_compile(
396+
python_configuration: PythonConfiguration,
397+
env: MutableMapping[str, str],
398+
) -> None:
399+
cargo_target = android_triplet(python_configuration.identifier)
400+
401+
# CARGO_BUILD_TARGET is the variable used by Cargo and setuptools_rust
402+
if env.get("CARGO_BUILD_TARGET"):
403+
if env["CARGO_BUILD_TARGET"] != cargo_target:
404+
log.notice("Not overriding CARGO_BUILD_TARGET as it has already been set")
405+
# No message if it was set to what we were planning to set it to
406+
elif cargo_target:
407+
log.notice(f"Setting CARGO_BUILD_TARGET={cargo_target} for cross-compilation")
408+
env["CARGO_BUILD_TARGET"] = cargo_target
409+
else:
410+
log.warning(
411+
f"Unable to configure Rust cross-compilation for architecture {cargo_target}"
412+
)
413+
414+
393415
def before_build(state: BuildState) -> None:
394416
if state.options.before_build:
395417
log.step("Running before_build...")

0 commit comments

Comments
 (0)