Skip to content

Commit

Permalink
Detect target based on interpreter for pep517 build-wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
kcking committed May 14, 2024
1 parent d81bf10 commit 59b8474
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
10 changes: 9 additions & 1 deletion src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,15 @@ impl BuildOptions {
target_triple = None;
}

let target = Target::from_target_triple(target_triple)?;
let mut target = Target::from_target_triple(target_triple)?;
if !target.user_specified && !universal2 {
if let Some(detected_target) = crate::target::detect_arch_from_python(
self.interpreter.first().unwrap(),
&target,
) {
target = Target::from_target_triple(Some(detected_target))?;
}
}

let wheel_dir = match self.out {
Some(ref dir) => dir.clone(),
Expand Down
20 changes: 3 additions & 17 deletions src/develop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::build_options::CargoOptions;
use crate::target::Arch;
use crate::target::detect_arch_from_python;
use crate::BuildContext;
use crate::BuildOptions;
use crate::PlatformTag;
Expand Down Expand Up @@ -307,22 +307,8 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {

// check python platform and architecture
if !target.user_specified {
match Command::new(&python)
.arg("-c")
.arg("import sysconfig; print(sysconfig.get_platform(), end='')")
.output()
{
Ok(output) if output.status.success() => {
let platform = String::from_utf8_lossy(&output.stdout);
if platform.contains("macos") {
if platform.contains("x86_64") && target.target_arch() != Arch::X86_64 {
target_triple = Some("x86_64-apple-darwin".to_string());
} else if platform.contains("arm64") && target.target_arch() != Arch::Aarch64 {
target_triple = Some("aarch64-apple-darwin".to_string());
}
}
}
_ => eprintln!("⚠️ Warning: Failed to determine python platform"),
if let Some(detected_target) = detect_arch_from_python(&python, &target) {
target_triple = Some(detected_target);
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::env;
use std::fmt;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::str;
use target_lexicon::{Architecture, Environment, Triple};
use tracing::error;
Expand Down Expand Up @@ -618,3 +619,24 @@ fn rustc_version_meta() -> Result<VersionMeta> {
})?;
Ok(meta)
}

pub(crate) fn detect_arch_from_python(python: &PathBuf, target: &Target) -> Option<String> {
match Command::new(python)
.arg("-c")
.arg("import sysconfig; print(sysconfig.get_platform(), end='')")
.output()
{
Ok(output) if output.status.success() => {
let platform = String::from_utf8_lossy(&output.stdout);
if platform.contains("macos") {
if platform.contains("x86_64") && target.target_arch() != Arch::X86_64 {
return Some("x86_64-apple-darwin".to_string());
} else if platform.contains("arm64") && target.target_arch() != Arch::Aarch64 {
return Some("aarch64-apple-darwin".to_string());
}
}
}
_ => eprintln!("⚠️ Warning: Failed to determine python platform"),
}
None
}

0 comments on commit 59b8474

Please sign in to comment.