Skip to content

Commit cbedc33

Browse files
committed
Use rustc's minimum Apple deployment targets when available
1 parent 9d84691 commit cbedc33

File tree

1 file changed

+80
-42
lines changed

1 file changed

+80
-42
lines changed

src/lib.rs

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,31 +2253,16 @@ impl Build {
22532253
Catalyst(&'static str),
22542254
}
22552255

2256-
enum Os {
2257-
MacOs,
2258-
Ios,
2259-
WatchOs,
2260-
}
2261-
impl Display for Os {
2262-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2263-
match self {
2264-
Os::MacOs => f.write_str("macOS"),
2265-
Os::Ios => f.write_str("iOS"),
2266-
Os::WatchOs => f.write_str("WatchOS"),
2267-
}
2268-
}
2269-
}
2270-
22712256
let target = self.get_target()?;
22722257
let os = if target.contains("-darwin") {
2273-
Os::MacOs
2258+
AppleOs::MacOs
22742259
} else if target.contains("-watchos") {
2275-
Os::WatchOs
2260+
AppleOs::WatchOs
22762261
} else {
2277-
Os::Ios
2262+
AppleOs::Ios
22782263
};
22792264
let is_mac = match os {
2280-
Os::MacOs => true,
2265+
AppleOs::MacOs => true,
22812266
_ => false,
22822267
};
22832268

@@ -2351,29 +2336,11 @@ impl Build {
23512336
}
23522337
};
23532338

2354-
let (sdk_prefix, sim_prefix, min_version) = match os {
2355-
Os::MacOs => (
2356-
"macosx",
2357-
"",
2358-
std::env::var("MACOSX_DEPLOYMENT_TARGET").unwrap_or_else(|_| {
2359-
(if arch_str == "aarch64" {
2360-
"11.0"
2361-
} else {
2362-
"10.7"
2363-
})
2364-
.into()
2365-
}),
2366-
),
2367-
Os::Ios => (
2368-
"iphone",
2369-
"ios-",
2370-
std::env::var("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "7.0".into()),
2371-
),
2372-
Os::WatchOs => (
2373-
"watch",
2374-
"watch",
2375-
std::env::var("WATCHOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "2.0".into()),
2376-
),
2339+
let min_version = self.apple_deployment_version(os, &target, arch_str);
2340+
let (sdk_prefix, sim_prefix) = match os {
2341+
AppleOs::MacOs => ("macosx", ""),
2342+
AppleOs::Ios => ("iphone", "ios-"),
2343+
AppleOs::WatchOs => ("watch", "watch"),
23772344
};
23782345

23792346
let sdk = match arch {
@@ -3323,6 +3290,61 @@ impl Build {
33233290
Ok(ret)
33243291
}
33253292

3293+
fn apple_deployment_version(&self, os: AppleOs, target: &str, arch_str: &str) -> String {
3294+
fn rustc_provided_target(rustc: Option<&str>, target: &str) -> Option<String> {
3295+
let rustc = rustc?;
3296+
let output = Command::new(rustc)
3297+
.stderr(Stdio::piped())
3298+
.stdout(Stdio::piped())
3299+
.args(&["--target", target])
3300+
.args(&["--print", "deployment-target"])
3301+
.output()
3302+
.ok()?;
3303+
3304+
if output.status.success() {
3305+
std::str::from_utf8(&output.stdout)
3306+
.unwrap()
3307+
.split('=')
3308+
.last()
3309+
.map(|v| v.trim())
3310+
.map(ToString::to_string)
3311+
} else {
3312+
// rustc is < 1.71
3313+
None
3314+
}
3315+
}
3316+
3317+
let rustc = self.getenv("RUSTC");
3318+
let rustc = rustc.as_deref();
3319+
// note the hardcoded minimums here are subject to change in a future compiler release,
3320+
// so the ones rustc knows about are preferred. For any compiler version that has bumped them
3321+
// though, `--print deployment-target` will be present and the fallbacks won't be used.
3322+
//
3323+
// the ordering of env -> rustc -> old defaults is intentional for performance when using
3324+
// an explicit target
3325+
match os {
3326+
AppleOs::MacOs => env::var("MACOSX_DEPLOYMENT_TARGET")
3327+
.ok()
3328+
.or_else(|| rustc_provided_target(rustc, target))
3329+
.unwrap_or_else(|| {
3330+
if arch_str == "aarch64" {
3331+
"11.0"
3332+
} else {
3333+
"10.7"
3334+
}
3335+
.into()
3336+
}),
3337+
AppleOs::Ios => env::var("IPHONEOS_DEPLOYMENT_TARGET")
3338+
.ok()
3339+
.or_else(|| rustc_provided_target(rustc, target))
3340+
.unwrap_or_else(|| "7.0".into()),
3341+
AppleOs::WatchOs => env::var("WATCHOS_DEPLOYMENT_TARGET")
3342+
.ok()
3343+
.or_else(|| rustc_provided_target(rustc, target))
3344+
.unwrap_or_else(|| "2.0".into()),
3345+
}
3346+
}
3347+
33263348
fn cuda_file_count(&self) -> usize {
33273349
self.files
33283350
.iter()
@@ -3676,6 +3698,22 @@ fn command_add_output_file(
36763698
}
36773699
}
36783700

3701+
#[derive(Clone, Copy)]
3702+
enum AppleOs {
3703+
MacOs,
3704+
Ios,
3705+
WatchOs,
3706+
}
3707+
impl Display for AppleOs {
3708+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
3709+
match self {
3710+
AppleOs::MacOs => f.write_str("macOS"),
3711+
AppleOs::Ios => f.write_str("iOS"),
3712+
AppleOs::WatchOs => f.write_str("WatchOS"),
3713+
}
3714+
}
3715+
}
3716+
36793717
// Use by default minimum available API level
36803718
// See note about naming here
36813719
// https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/docs/BuildSystemMaintainers.md#Clang

0 commit comments

Comments
 (0)