Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Allow iOS cross-platform virtual environments, such as those used by cibuildwheel, to imply an iOS target.
* Fix iOS wheel naming to be compliant with PEP 730.
* Fix generated WHEEL Tag metadata to be spec compliant.
* Remove `add_directory()` from ModuleWriter and make it an implementation detail for the specific impl
Expand Down
21 changes: 19 additions & 2 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::cross_compile::{find_sysconfigdata, parse_sysconfigdata};
use crate::project_layout::ProjectResolver;
use crate::pyproject_toml::ToolMaturin;
use crate::python_interpreter::{InterpreterConfig, InterpreterKind};
use crate::target::{detect_arch_from_python, is_arch_supported_by_pypi};
use crate::target::{
detect_arch_from_python, detect_target_from_cross_python, is_arch_supported_by_pypi,
};
use crate::{BridgeModel, BuildContext, PyO3, PythonInterpreter, Target};
use anyhow::{bail, format_err, Context, Result};
use cargo_metadata::{CrateType, PackageId, TargetKind};
Expand Down Expand Up @@ -665,7 +667,22 @@ impl BuildContextBuilder {
let mut target = Target::from_target_triple(target_triple.as_ref())?;
if !target.user_specified && !universal2 {
if let Some(interpreter) = build_options.interpreter.first() {
if let Some(detected_target) = detect_arch_from_python(interpreter, &target) {
// If there's an explicitly provided interpreter, check to see
// if it's a cross-compiling interpreter; otherwise, check to
// see if an target change is required.
if let Some(detected_target) = detect_target_from_cross_python(interpreter) {
target = Target::from_target_triple(Some(&detected_target))?;
} else if let Some(detected_target) = detect_arch_from_python(interpreter, &target)
{
target = Target::from_target_triple(Some(&detected_target))?;
}
} else {
// If there's no explicit user-provided target or interpreter,
// check the interpreter; if the interpreter identifies as a
// cross compiler, set the target based on the platform reported
// by the interpreter.
if let Some(detected_target) = detect_target_from_cross_python(&target.get_python())
{
target = Target::from_target_triple(Some(&detected_target))?;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/cross_compile.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::target::Os;
use crate::{PythonInterpreter, Target};
use anyhow::{bail, Result};
use fs_err::{self as fs, DirEntry};
Expand Down Expand Up @@ -32,6 +33,13 @@ pub fn is_cross_compiling(target: &Target) -> Result<bool> {
return Ok(false);
}

if target.target_os() == Os::Ios {
// Not cross-compiling to compile for iOS. There's no on-device compilation,
// so compilation will always be in a "fake" cross-platform venv with a
// working python/sysconfig that can interrogated.
return Ok(false);
}

Ok(true)
}

Expand Down
21 changes: 21 additions & 0 deletions src/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,24 @@ pub(crate) fn detect_arch_from_python(python: &PathBuf, target: &Target) -> Opti
}
None
}

pub(crate) fn detect_target_from_cross_python(python: &PathBuf) -> Option<TargetTriple> {
match Command::new(python)
.arg("-c")
.arg("import sys, sysconfig; print(sysconfig.get_platform(), end='') if getattr(sys, 'cross_compiling', False) else ''")
.output()
{
Ok(output) if output.status.success() => {
let platform = String::from_utf8_lossy(&output.stdout);
if platform.ends_with("-arm64-iphoneos") {
return Some(TargetTriple::Regular("aarch64-apple-ios".to_string()));
} else if platform.ends_with("-arm64-iphonesimulator") {
return Some(TargetTriple::Regular("aarch64-apple-ios-sim".to_string()));
} else if platform.ends_with("-x86_64-iphonesimulator") {
return Some(TargetTriple::Regular("x86_64-apple-ios".to_string()));
}
}
_ => eprintln!("⚠️ Warning: Failed to determine python platform"),
}
None
}
Loading