Skip to content

Commit e251e13

Browse files
committed
Use rustc's minimum Apple deployment targets when available
1 parent b1e566f commit e251e13

File tree

1 file changed

+77
-42
lines changed

1 file changed

+77
-42
lines changed

src/lib.rs

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,31 +2367,16 @@ impl Build {
23672367
Catalyst(&'static str),
23682368
}
23692369

2370-
enum Os {
2371-
MacOs,
2372-
Ios,
2373-
WatchOs,
2374-
}
2375-
impl std::fmt::Debug for Os {
2376-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2377-
match self {
2378-
Os::MacOs => f.write_str("macOS"),
2379-
Os::Ios => f.write_str("iOS"),
2380-
Os::WatchOs => f.write_str("WatchOS"),
2381-
}
2382-
}
2383-
}
2384-
23852370
let target = self.get_target()?;
23862371
let os = if target.contains("-darwin") {
2387-
Os::MacOs
2372+
AppleOs::MacOs
23882373
} else if target.contains("-watchos") {
2389-
Os::WatchOs
2374+
AppleOs::WatchOs
23902375
} else {
2391-
Os::Ios
2376+
AppleOs::Ios
23922377
};
23932378
let is_mac = match os {
2394-
Os::MacOs => true,
2379+
AppleOs::MacOs => true,
23952380
_ => false,
23962381
};
23972382

@@ -2465,29 +2450,11 @@ impl Build {
24652450
}
24662451
};
24672452

2468-
let (sdk_prefix, sim_prefix, min_version) = match os {
2469-
Os::MacOs => (
2470-
"macosx",
2471-
"",
2472-
std::env::var("MACOSX_DEPLOYMENT_TARGET").unwrap_or_else(|_| {
2473-
(if arch_str == "aarch64" {
2474-
"11.0"
2475-
} else {
2476-
"10.7"
2477-
})
2478-
.into()
2479-
}),
2480-
),
2481-
Os::Ios => (
2482-
"iphone",
2483-
"ios-",
2484-
std::env::var("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "7.0".into()),
2485-
),
2486-
Os::WatchOs => (
2487-
"watch",
2488-
"watch",
2489-
std::env::var("WATCHOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "2.0".into()),
2490-
),
2453+
let min_version = self.apple_deployment_version(os, &target, arch_str);
2454+
let (sdk_prefix, sim_prefix) = match os {
2455+
AppleOs::MacOs => ("macosx", ""),
2456+
AppleOs::Ios => ("iphone", "ios-"),
2457+
AppleOs::WatchOs => ("watch", "watch"),
24912458
};
24922459

24932460
let sdk = match arch {
@@ -3437,6 +3404,58 @@ impl Build {
34373404
Ok(ret)
34383405
}
34393406

3407+
fn apple_deployment_version(&self, os: AppleOs, target: &str, arch_str: &str) -> String {
3408+
fn rustc_provided_target(rustc: Option<&str>, target: &str) -> Option<String> {
3409+
let rustc = rustc?;
3410+
let output = Command::new(rustc)
3411+
.args(&["--target", target])
3412+
.args(&["--print", "deployment-target"])
3413+
.output()
3414+
.ok()?;
3415+
3416+
if output.status.success() {
3417+
std::str::from_utf8(&output.stdout)
3418+
.unwrap()
3419+
.strip_prefix("deployment_target=")
3420+
.map(|v| v.trim())
3421+
.map(ToString::to_string)
3422+
} else {
3423+
// rustc is < 1.71
3424+
None
3425+
}
3426+
}
3427+
3428+
let rustc = self.getenv("RUSTC");
3429+
let rustc = rustc.as_deref();
3430+
// note the hardcoded minimums here are subject to change in a future compiler release,
3431+
// so the ones rustc knows about are preferred. For any compiler version that has bumped them
3432+
// though, `--print deployment-target` will be present and the fallbacks won't be used.
3433+
//
3434+
// the ordering of env -> rustc -> old defaults is intentional for performance when using
3435+
// an explicit target
3436+
match os {
3437+
AppleOs::MacOs => env::var("MACOSX_DEPLOYMENT_TARGET")
3438+
.ok()
3439+
.or_else(|| rustc_provided_target(rustc, target))
3440+
.unwrap_or_else(|| {
3441+
if arch_str == "aarch64" {
3442+
"11.0"
3443+
} else {
3444+
"10.7"
3445+
}
3446+
.into()
3447+
}),
3448+
AppleOs::Ios => env::var("IPHONEOS_DEPLOYMENT_TARGET")
3449+
.ok()
3450+
.or_else(|| rustc_provided_target(rustc, target))
3451+
.unwrap_or_else(|| "7.0".into()),
3452+
AppleOs::WatchOs => env::var("WATCHOS_DEPLOYMENT_TARGET")
3453+
.ok()
3454+
.or_else(|| rustc_provided_target(rustc, target))
3455+
.unwrap_or_else(|| "2.0".into()),
3456+
}
3457+
}
3458+
34403459
fn cuda_file_count(&self) -> usize {
34413460
self.files
34423461
.iter()
@@ -3824,6 +3843,22 @@ fn command_add_output_file(
38243843
}
38253844
}
38263845

3846+
#[derive(Clone, Copy)]
3847+
enum AppleOs {
3848+
MacOs,
3849+
Ios,
3850+
WatchOs,
3851+
}
3852+
impl std::fmt::Debug for AppleOs {
3853+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
3854+
match self {
3855+
AppleOs::MacOs => f.write_str("macOS"),
3856+
AppleOs::Ios => f.write_str("iOS"),
3857+
AppleOs::WatchOs => f.write_str("WatchOS"),
3858+
}
3859+
}
3860+
}
3861+
38273862
// Use by default minimum available API level
38283863
// See note about naming here
38293864
// https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/docs/BuildSystemMaintainers.md#Clang

0 commit comments

Comments
 (0)