From 88bce88064404c1c3342e76ced704e4929cfc47f Mon Sep 17 00:00:00 2001 From: Kevin King <4kevinking@gmail.com> Date: Tue, 14 May 2024 12:52:25 -0700 Subject: [PATCH] Detect target based on interpreter for pep517 build-wheel --- src/build_options.rs | 11 ++++++++++- src/develop.rs | 20 +++----------------- src/target.rs | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/build_options.rs b/src/build_options.rs index 4258e3568..b30caf101 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -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(), diff --git a/src/develop.rs b/src/develop.rs index 2a948e5e1..19fc727ef 100644 --- a/src/develop.rs +++ b/src/develop.rs @@ -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; @@ -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); } } diff --git a/src/target.rs b/src/target.rs index 177b8453d..ab651714b 100644 --- a/src/target.rs +++ b/src/target.rs @@ -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; @@ -618,3 +619,24 @@ fn rustc_version_meta() -> Result { })?; Ok(meta) } + +pub(crate) fn detect_arch_from_python(python: &PathBuf, target: &Target) -> Option { + 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 +}