From 4278797665c3bcde1397d65a8fd122599e3ed594 Mon Sep 17 00:00:00 2001 From: messense Date: Mon, 29 Nov 2021 20:56:49 +0800 Subject: [PATCH 1/2] Make platform tags related functions fallible --- src/build_context.rs | 10 ++++++---- src/main.rs | 6 +++--- src/python_interpreter.rs | 17 ++++++++++------- src/target.rs | 36 ++++++++++++++++-------------------- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/build_context.rs b/src/build_context.rs index 28ba445dd..6d6ef5e8c 100644 --- a/src/build_context.rs +++ b/src/build_context.rs @@ -284,7 +284,9 @@ impl BuildContext { major: u8, min_minor: u8, ) -> Result { - let platform = self.target.get_platform_tag(platform_tag, self.universal2); + let platform = self + .target + .get_platform_tag(platform_tag, self.universal2)?; let tag = format!("cp{}{}-abi3-{}", major, min_minor, platform); let mut writer = WheelWriter::new(&tag, &self.out, &self.metadata21, &[tag.clone()])?; @@ -343,7 +345,7 @@ impl BuildContext { artifact: &Path, platform_tag: PlatformTag, ) -> Result { - let tag = python_interpreter.get_tag(platform_tag, self.universal2); + let tag = python_interpreter.get_tag(platform_tag, self.universal2)?; let mut writer = WheelWriter::new(&tag, &self.out, &self.metadata21, &[tag.clone()])?; @@ -437,7 +439,7 @@ impl BuildContext { ) -> Result { let (tag, tags) = self .target - .get_universal_tags(platform_tag, self.universal2); + .get_universal_tags(platform_tag, self.universal2)?; let mut writer = WheelWriter::new(&tag, &self.out, &self.metadata21, &tags)?; @@ -490,7 +492,7 @@ impl BuildContext { ) -> Result { let (tag, tags) = self .target - .get_universal_tags(platform_tag, self.universal2); + .get_universal_tags(platform_tag, self.universal2)?; if !self.metadata21.scripts.is_empty() { bail!("Defining entrypoints and working with a binary doesn't mix well"); diff --git a/src/main.rs b/src/main.rs index b8d234e48..a087d0058 100644 --- a/src/main.rs +++ b/src/main.rs @@ -379,18 +379,18 @@ fn pep517(subcommand: Pep517Command) -> Result<()> { // Since afaik all other PEP 517 backends also return linux tagged wheels, we do so too let tags = match context.bridge { BridgeModel::Bindings(_) => { - vec![context.interpreter[0].get_tag(PlatformTag::Linux, context.universal2)] + vec![context.interpreter[0].get_tag(PlatformTag::Linux, context.universal2)?] } BridgeModel::BindingsAbi3(major, minor) => { let platform = context .target - .get_platform_tag(PlatformTag::Linux, context.universal2); + .get_platform_tag(PlatformTag::Linux, context.universal2)?; vec![format!("cp{}{}-abi3-{}", major, minor, platform)] } BridgeModel::Bin | BridgeModel::Cffi => { context .target - .get_universal_tags(PlatformTag::Linux, context.universal2) + .get_universal_tags(PlatformTag::Linux, context.universal2)? .1 } }; diff --git a/src/python_interpreter.rs b/src/python_interpreter.rs index 52278f81a..78b3d3588 100644 --- a/src/python_interpreter.rs +++ b/src/python_interpreter.rs @@ -360,20 +360,22 @@ impl PythonInterpreter { /// Don't ask me why or how, this is just what setuptools uses so I'm also going to use /// /// If abi3 is true, cpython wheels use the generic abi3 with the given version as minimum - pub fn get_tag(&self, platform_tag: PlatformTag, universal2: bool) -> String { + pub fn get_tag(&self, platform_tag: PlatformTag, universal2: bool) -> Result { // Restrict `sysconfig.get_platform()` usage to Windows and non-portable Linux only for now // so we don't need to deal with macOS deployment target let use_sysconfig_platform = self.target.is_windows() || (self.target.is_linux() && !platform_tag.is_portable()) || self.target.is_illumos(); let platform = if use_sysconfig_platform { - self.platform - .clone() - .unwrap_or_else(|| self.target.get_platform_tag(platform_tag, universal2)) + if let Some(platform) = self.platform.clone() { + platform + } else { + self.target.get_platform_tag(platform_tag, universal2)? + } } else { - self.target.get_platform_tag(platform_tag, universal2) + self.target.get_platform_tag(platform_tag, universal2)? }; - match self.interpreter_kind { + let tag = match self.interpreter_kind { InterpreterKind::CPython => { if self.target.is_unix() { format!( @@ -408,7 +410,8 @@ impl PythonInterpreter { platform = platform, ) } - } + }; + Ok(tag) } /// Adds the ext_suffix we read from python or know (.pyd/.abi3.so) and adds it to the base name diff --git a/src/target.rs b/src/target.rs index 8c006a79f..0c0efe5fa 100644 --- a/src/target.rs +++ b/src/target.rs @@ -157,8 +157,8 @@ impl Target { } /// Returns the platform part of the tag for the wheel name - pub fn get_platform_tag(&self, platform_tag: PlatformTag, universal2: bool) -> String { - match (&self.os, &self.arch) { + pub fn get_platform_tag(&self, platform_tag: PlatformTag, universal2: bool) -> Result { + let tag = match (&self.os, &self.arch) { // FreeBSD (Os::FreeBsd, Arch::X86_64) | (Os::FreeBsd, Arch::Aarch64) @@ -172,10 +172,7 @@ impl Target { | (Os::OpenBsd, Arch::X86) | (Os::OpenBsd, Arch::X86_64) | (Os::OpenBsd, Arch::Aarch64) => { - let info = match PlatformInfo::new() { - Ok(info) => info, - Err(error) => panic!("{}", error), - }; + let info = PlatformInfo::new()?; let release = info.release().replace(".", "_").replace("-", "_"); let arch = match self.arch { Arch::X86_64 => "amd64", @@ -195,17 +192,14 @@ impl Target { ) } (Os::Illumos, Arch::X86_64) => { - let info = match PlatformInfo::new() { - Ok(info) => info, - Err(error) => panic!("{}", error), - }; + let info = PlatformInfo::new()?; let mut release = info.release().replace(".", "_").replace("-", "_"); let mut arch = info.machine().replace(' ', "_").replace('/', "_"); let mut os = self.os.to_string().to_ascii_lowercase(); // See https://github.com/python/cpython/blob/46c8d915715aa2bd4d697482aa051fe974d440e1/Lib/sysconfig.py#L722-L730 if let Some((major, other)) = release.split_once('_') { - let major_ver: u64 = major.parse().expect("illumos major version is not a number"); + let major_ver: u64 = major.parse().context("illumos major version is not a number")?; if major_ver >= 5 { // SunOS 5 == Solaris 2 os = "solaris".to_string(); @@ -252,7 +246,8 @@ impl Target { (Os::Windows, Arch::X86_64) => "win_amd64".to_string(), (Os::Windows, Arch::Aarch64) => "win_arm64".to_string(), (_, _) => panic!("unsupported target should not have reached get_platform_tag()"), - } + }; + Ok(tag) } /// Returns the name python uses in `sys.platform` for this os @@ -351,11 +346,12 @@ impl Target { } /// Returns the tags for the WHEEL file for cffi wheels - pub fn get_py3_tags(&self, platform_tag: PlatformTag, universal2: bool) -> Vec { - vec![format!( + pub fn get_py3_tags(&self, platform_tag: PlatformTag, universal2: bool) -> Result> { + let tags = vec![format!( "py3-none-{}", - self.get_platform_tag(platform_tag, universal2) - )] + self.get_platform_tag(platform_tag, universal2)? + )]; + Ok(tags) } /// Returns the path to the python executable inside a venv @@ -425,13 +421,13 @@ impl Target { &self, platform_tag: PlatformTag, universal2: bool, - ) -> (String, Vec) { + ) -> Result<(String, Vec)> { let tag = format!( "py3-none-{platform}", - platform = self.get_platform_tag(platform_tag, universal2) + platform = self.get_platform_tag(platform_tag, universal2)? ); - let tags = self.get_py3_tags(platform_tag, universal2); - (tag, tags) + let tags = self.get_py3_tags(platform_tag, universal2)?; + Ok((tag, tags)) } } From 81dc57850a41d535e1970c142a8246f0c4851c88 Mon Sep 17 00:00:00 2001 From: messense Date: Mon, 29 Nov 2021 21:22:04 +0800 Subject: [PATCH 2/2] Account for `MACOSX_DEPLOYMENT_TARGET` env var in wheel platform tag --- Changelog.md | 1 + src/target.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index ad5bcd39f..245aebb70 100644 --- a/Changelog.md +++ b/Changelog.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Revert back to Rust 2018 edition in [#710](https://github.com/PyO3/maturin/pull/710) * Warn missing `cffi` package dependency in [#711](https://github.com/PyO3/maturin/pull/711) * Add support for Illumos in [#712](https://github.com/PyO3/maturin/pull/712) +* Account for `MACOSX_DEPLOYMENT_TARGET` env var in wheel platform tag in [#716](https://github.com/PyO3/maturin/pull/716) ## [0.12.2] - 2021-11-26 diff --git a/src/target.rs b/src/target.rs index 0c0efe5fa..cd516e75d 100644 --- a/src/target.rs +++ b/src/target.rs @@ -229,17 +229,31 @@ impl Target { tags.join(".") } (Os::Macos, Arch::X86_64) => { + let ((x86_64_major, x86_64_minor), (arm64_major, arm64_minor)) = macosx_deployment_target(env::var("MACOSX_DEPLOYMENT_TARGET").ok().as_deref(), universal2)?; if universal2 { - "macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2".to_string() + format!( + "macosx_{x86_64_major}_{x86_64_minor}_x86_64.macosx_{arm64_major}_{arm64_minor}_arm64.macosx_{x86_64_major}_{x86_64_minor}_universal2", + x86_64_major = x86_64_major, + x86_64_minor = x86_64_minor, + arm64_major = arm64_major, + arm64_minor = arm64_minor + ) } else { - "macosx_10_7_x86_64".to_string() + format!("macosx_{}_{}_x86_64", x86_64_major, x86_64_minor) } } (Os::Macos, Arch::Aarch64) => { + let ((x86_64_major, x86_64_minor), (arm64_major, arm64_minor)) = macosx_deployment_target(env::var("MACOSX_DEPLOYMENT_TARGET").ok().as_deref(), universal2)?; if universal2 { - "macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2".to_string() + format!( + "macosx_{x86_64_major}_{x86_64_minor}_x86_64.macosx_{arm64_major}_{arm64_minor}_arm64.macosx_{x86_64_major}_{x86_64_minor}_universal2", + x86_64_major = x86_64_major, + x86_64_minor = x86_64_minor, + arm64_major = arm64_major, + arm64_minor = arm64_minor + ) } else { - "macosx_11_0_arm64".to_string() + format!("macosx_{}_{}_arm64", arm64_major, arm64_minor) } } (Os::Windows, Arch::X86) => "win32".to_string(), @@ -464,3 +478,65 @@ pub(crate) fn get_host_target() -> Result { .to_string(); Ok(host) } + +fn macosx_deployment_target( + deploy_target: Option<&str>, + universal2: bool, +) -> Result<((usize, usize), (usize, usize))> { + let x86_64_default = if universal2 { (10, 9) } else { (10, 7) }; + let arm64_default = (11, 0); + let mut x86_64_ver = x86_64_default; + let mut arm64_ver = arm64_default; + if let Some(deploy_target) = deploy_target { + let err_ctx = "MACOSX_DEPLOYMENT_TARGET is invalid"; + let mut parts = deploy_target.split('.'); + let major = parts.next().context(err_ctx)?; + let major: usize = major.parse().context(err_ctx)?; + let minor = parts.next().context(err_ctx)?; + let minor: usize = minor.parse().context(err_ctx)?; + if (major, minor) > x86_64_default { + x86_64_ver = (major, minor); + } + if (major, minor) > arm64_default { + arm64_ver = (major, minor); + } + } + Ok((x86_64_ver, arm64_ver)) +} + +#[cfg(test)] +mod test { + use super::macosx_deployment_target; + + #[test] + fn test_macosx_deployment_target() { + assert_eq!( + macosx_deployment_target(None, false).unwrap(), + (((10, 7), (11, 0))) + ); + assert_eq!( + macosx_deployment_target(None, true).unwrap(), + (((10, 9), (11, 0))) + ); + assert_eq!( + macosx_deployment_target(Some("10.6"), false).unwrap(), + (((10, 7), (11, 0))) + ); + assert_eq!( + macosx_deployment_target(Some("10.6"), true).unwrap(), + (((10, 9), (11, 0))) + ); + assert_eq!( + macosx_deployment_target(Some("10.9"), false).unwrap(), + (((10, 9), (11, 0))) + ); + assert_eq!( + macosx_deployment_target(Some("11.0.0"), false).unwrap(), + (((11, 0), (11, 0))) + ); + assert_eq!( + macosx_deployment_target(Some("11.1"), false).unwrap(), + (((11, 1), (11, 1))) + ); + } +}