Skip to content

Commit 32dcc38

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

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

cibuildwheel/platforms/android.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import shutil
88
import subprocess
99
import sysconfig
10-
from collections.abc import Iterable, Iterator
10+
from collections.abc import Iterable, Iterator, MutableMapping
1111
from dataclasses import dataclass
1212
from os.path import relpath
1313
from pathlib import Path
@@ -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,25 @@ 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+
call("rustup", "target", "add", cargo_target)
401+
402+
# CARGO_BUILD_TARGET is the variable used by Cargo and setuptools_rust
403+
if env.get("CARGO_BUILD_TARGET"):
404+
if env["CARGO_BUILD_TARGET"] != cargo_target:
405+
log.notice("Not overriding CARGO_BUILD_TARGET as it has already been set")
406+
# No message if it was set to what we were planning to set it to
407+
elif cargo_target:
408+
log.notice(f"Setting CARGO_BUILD_TARGET={cargo_target} for cross-compilation")
409+
env["CARGO_BUILD_TARGET"] = cargo_target
410+
else:
411+
log.warning(f"Unable to configure Rust cross-compilation for architecture {cargo_target}")
412+
413+
393414
def before_build(state: BuildState) -> None:
394415
if state.options.before_build:
395416
log.step("Running before_build...")

0 commit comments

Comments
 (0)