From 792131cb1f3994665db952da6b8c112aaa6baaa4 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 14 Jun 2022 16:59:51 +0800 Subject: [PATCH] Infer target triple from `ARCHFLAGS` for macOS --- src/build_options.rs | 55 ++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/src/build_options.rs b/src/build_options.rs index f70206349..494e2ce94 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -170,10 +170,10 @@ impl BuildOptions { &self, bridge: &BridgeModel, interpreter: &[PathBuf], + target: &Target, min_python_minor: Option, generate_import_lib: bool, ) -> Result> { - let target = &Target::from_target_triple(self.target.clone())?; match bridge { BridgeModel::Bindings(binding_name, _) | BridgeModel::Bin(Some((binding_name, _))) => { let mut native_interpreters = false; @@ -533,7 +533,36 @@ impl BuildOptions { ); } - let target = Target::from_target_triple(self.target.clone())?; + let mut target_triple = self.target.clone(); + + let mut universal2 = self.universal2; + // Also try to determine universal2 from ARCHFLAGS environment variable + if let Ok(arch_flags) = env::var("ARCHFLAGS") { + let arches: HashSet<&str> = arch_flags + .split("-arch") + .filter_map(|x| { + let x = x.trim(); + if x.is_empty() { + None + } else { + Some(x) + } + }) + .collect(); + match (arches.contains("x86_64"), arches.contains("arm64")) { + (true, true) => universal2 = true, + (true, false) if target_triple.is_none() => { + target_triple = Some("x86_64-apple-darwin".to_string()) + } + (false, true) if target_triple.is_none() => { + target_triple = Some("aarch64-apple-darwin".to_string()) + } + _ => {} + } + }; + let strip = pyproject.map(|x| x.strip()).unwrap_or_default() || strip; + + let target = Target::from_target_triple(target_triple)?; let wheel_dir = match self.out { Some(ref dir) => dir.clone(), @@ -546,6 +575,7 @@ impl BuildOptions { self.find_interpreters( &bridge, &[], + &target, get_min_python_minor(&metadata21), generate_import_lib, )? @@ -556,7 +586,7 @@ impl BuildOptions { } else { self.interpreter.clone() }; - self.find_interpreters(&bridge, &interpreter, None, generate_import_lib)? + self.find_interpreters(&bridge, &interpreter, &target, None, generate_import_lib)? }; let mut rustc_extra_args = self.rustc_extra_args.clone(); @@ -569,25 +599,6 @@ impl BuildOptions { } rustc_extra_args = split_extra_args(&rustc_extra_args)?; - let mut universal2 = self.universal2; - // Also try to determine universal2 from ARCHFLAGS environment variable - if let Ok(arch_flags) = env::var("ARCHFLAGS") { - let arches: HashSet<&str> = arch_flags - .split("-arch") - .filter_map(|x| { - let x = x.trim(); - if x.is_empty() { - None - } else { - Some(x) - } - }) - .collect(); - if arches.contains("x86_64") && arches.contains("arm64") { - universal2 = true; - } - }; - let strip = pyproject.map(|x| x.strip()).unwrap_or_default() || strip; let skip_auditwheel = pyproject.map(|x| x.skip_auditwheel()).unwrap_or_default() || self.skip_auditwheel; let platform_tags = if self.platform_tag.is_empty() {