Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect target based on interpreter for pep517 build-wheel #2088

Merged
merged 1 commit into from
May 19, 2024
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
11 changes: 10 additions & 1 deletion src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,16 @@ 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(interpreter) = self.interpreter.first() {
if let Some(detected_target) =
crate::target::detect_arch_from_python(interpreter, &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
}
Loading