Skip to content

Commit 2a6798e

Browse files
committed
feat(android): add PYO3 cross-compilation setup to set PYO3_CROSS_LIB_DIR to link against libpython3.x.so explicitly
1 parent 22fe90b commit 2a6798e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

cibuildwheel/platforms/android.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ def setup_android_env(
385385
# Cargo target linker need to be specified after CC is set
386386
setup_rust_cross_compile(config, android_env)
387387

388+
setup_PYO3_cross_compile(python_dir, android_env)
389+
388390
# Format the environment so it can be pasted into a shell when debugging.
389391
for key, value in sorted(android_env.items()):
390392
if os.environ.get(key) != value:
@@ -419,6 +421,21 @@ def setup_rust_cross_compile(
419421
log.warning(f"Unable to configure Rust cross-compilation for architecture {cargo_target}")
420422

421423

424+
def setup_PYO3_cross_compile(
425+
python_dir: Path,
426+
env: MutableMapping[str, str],
427+
) -> None:
428+
# All Python extension modules must therefore be explicitly linked against libpython3.x.so when building for Android.
429+
# See: https://peps.python.org/pep-0738/#linkage
430+
# For projects using PyO3, this requires setting PYO3_CROSS_LIB_DIR to the directory containing libpython3.x.so.
431+
# See: https://pyo3.rs/v0.27.1/building-and-distribution.html#cross-compiling
432+
if env.get("PYO3_CROSS_LIB_DIR"):
433+
log.notice("Not overriding PYO3_CROSS_LIB_DIR as it has already been set")
434+
else:
435+
env["PYO3_CROSS_LIB_DIR"] = str(python_dir / "prefix" / "lib")
436+
log.notice("Setting PYO3_CROSS_LIB_DIR for PyO3 cross-compilation")
437+
438+
422439
def before_build(state: BuildState) -> None:
423440
if state.options.before_build:
424441
log.step("Running before_build...")

0 commit comments

Comments
 (0)