Respect MACOSX_DEPLOYMENT_TARGET in --python-platform (#3470)

## Summary

This is universal environment variable used to determine the mac OS
deployment target. We now respect it in `--python-platform` -- so we
default to 12.0, but users can override it as needed.
This commit is contained in:
Charlie Marsh 2024-05-08 16:03:41 -04:00 committed by GitHub
parent ca809addf8
commit 7d41e7d260
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 45 additions and 19 deletions

View file

@ -27,6 +27,7 @@ rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
serde_json = { workspace = true }
tracing = { workspace = true }
[features]
default = []

View file

@ -1,3 +1,5 @@
use tracing::debug;
use pep508_rs::MarkerEnvironment;
use platform_tags::{Arch, Os, Platform};
@ -29,13 +31,18 @@ pub enum TargetTriple {
#[cfg_attr(feature = "schemars", schemars(rename = "x86_64-unknown-linux-gnu"))]
X8664UnknownLinuxGnu,
/// An ARM-based macOS target, as seen on Apple Silicon devices, with support for the
/// least-recent, non-EOL macOS version (12.0).
/// An ARM-based macOS target, as seen on Apple Silicon devices
///
/// By default, assumes the least-recent, non-EOL macOS version (12.0), but respects
/// the `MACOSX_DEPLOYMENT_TARGET` environment variable if set.
#[cfg_attr(feature = "clap", value(name = "aarch64-apple-darwin"))]
#[cfg_attr(feature = "schemars", schemars(rename = "aarch64-apple-darwin"))]
Aarch64AppleDarwin,
/// An x86 macOS target, with support for the least-recent, non-EOL macOS version (12.0).
/// An x86 macOS target.
///
/// By default, assumes the least-recent, non-EOL macOS version (12.0), but respects
/// the `MACOSX_DEPLOYMENT_TARGET` environment variable if set.
#[cfg_attr(feature = "clap", value(name = "x86_64-apple-darwin"))]
#[cfg_attr(feature = "schemars", schemars(rename = "x86_64-apple-darwin"))]
X8664AppleDarwin,
@ -88,20 +95,20 @@ impl TargetTriple {
},
Arch::X86_64,
),
Self::Macos | Self::Aarch64AppleDarwin => Platform::new(
Os::Macos {
major: 12,
minor: 0,
},
Arch::Aarch64,
),
Self::X8664AppleDarwin => Platform::new(
Os::Macos {
major: 12,
minor: 0,
},
Arch::X86_64,
),
Self::Macos | Self::Aarch64AppleDarwin => {
let (major, minor) = macos_deployment_target().map_or((12, 0), |(major, minor)| {
debug!("Found macOS deployment target: {}.{}", major, minor);
(major, minor)
});
Platform::new(Os::Macos { major, minor }, Arch::Aarch64)
}
Self::X8664AppleDarwin => {
let (major, minor) = macos_deployment_target().map_or((12, 0), |(major, minor)| {
debug!("Found macOS deployment target: {}.{}", major, minor);
(major, minor)
});
Platform::new(Os::Macos { major, minor }, Arch::X86_64)
}
Self::Aarch64UnknownLinuxGnu => Platform::new(
Os::Manylinux {
major: 2,
@ -271,3 +278,17 @@ impl TargetTriple {
}
}
}
/// Return the macOS deployment target as parsed from the environment.
fn macos_deployment_target() -> Option<(u16, u16)> {
let version = std::env::var("MACOSX_DEPLOYMENT_TARGET").ok()?;
let mut parts = version.split('.');
// Parse the major version (e.g., `12` in `12.0`).
let major = parts.next()?.parse::<u16>().ok()?;
// Parse the minor version (e.g., `0` in `12.0`), with a default of `0`.
let minor = parts.next().unwrap_or("0").parse::<u16>().ok()?;
Some((major, minor))
}