Skip to content

Commit b1e566f

Browse files
youknowoneBlackHoleFox
authored andcommitted
use MACOSX_DEPLOYMENT_TARGET when set
1 parent 0d082a2 commit b1e566f

File tree

2 files changed

+81
-25
lines changed

2 files changed

+81
-25
lines changed

src/lib.rs

+63-25
Original file line numberDiff line numberDiff line change
@@ -2141,8 +2141,8 @@ impl Build {
21412141
}
21422142
}
21432143

2144-
if target.contains("apple-ios") || target.contains("apple-watchos") {
2145-
self.ios_watchos_flags(cmd)?;
2144+
if target.contains("-apple-") {
2145+
self.apple_flags(cmd)?;
21462146
}
21472147

21482148
if self.static_flag.unwrap_or(false) {
@@ -2360,37 +2360,45 @@ impl Build {
23602360
Ok(())
23612361
}
23622362

2363-
fn ios_watchos_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
2363+
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
23642364
enum ArchSpec {
23652365
Device(&'static str),
23662366
Simulator(&'static str),
23672367
Catalyst(&'static str),
23682368
}
23692369

23702370
enum Os {
2371+
MacOs,
23712372
Ios,
23722373
WatchOs,
23732374
}
2374-
impl Display for Os {
2375+
impl std::fmt::Debug for Os {
23752376
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
23762377
match self {
2378+
Os::MacOs => f.write_str("macOS"),
23772379
Os::Ios => f.write_str("iOS"),
23782380
Os::WatchOs => f.write_str("WatchOS"),
23792381
}
23802382
}
23812383
}
23822384

23832385
let target = self.get_target()?;
2384-
let os = if target.contains("-watchos") {
2386+
let os = if target.contains("-darwin") {
2387+
Os::MacOs
2388+
} else if target.contains("-watchos") {
23852389
Os::WatchOs
23862390
} else {
23872391
Os::Ios
23882392
};
2393+
let is_mac = match os {
2394+
Os::MacOs => true,
2395+
_ => false,
2396+
};
23892397

2390-
let arch = target.split('-').nth(0).ok_or_else(|| {
2398+
let arch_str = target.split('-').nth(0).ok_or_else(|| {
23912399
Error::new(
23922400
ErrorKind::ArchitectureInvalid,
2393-
format!("Unknown architecture for {} target.", os),
2401+
format!("Unknown architecture for {:?} target.", os),
23942402
)
23952403
})?;
23962404

@@ -2404,11 +2412,22 @@ impl Build {
24042412
None => false,
24052413
};
24062414

2407-
let arch = if is_catalyst {
2408-
match arch {
2415+
let arch = if is_mac {
2416+
match arch_str {
2417+
"i686" => ArchSpec::Device("-m32"),
2418+
"x86_64" | "x86_64h" | "aarch64" => ArchSpec::Device("-m64"),
2419+
_ => {
2420+
return Err(Error::new(
2421+
ErrorKind::ArchitectureInvalid,
2422+
"Unknown architecture for macOS target.",
2423+
));
2424+
}
2425+
}
2426+
} else if is_catalyst {
2427+
match arch_str {
24092428
"arm64e" => ArchSpec::Catalyst("arm64e"),
24102429
"arm64" | "aarch64" => ArchSpec::Catalyst("arm64"),
2411-
"x86_64" => ArchSpec::Catalyst("-m64"),
2430+
"x86_64" | "x86_64h" => ArchSpec::Catalyst("-m64"),
24122431
_ => {
24132432
return Err(Error::new(
24142433
ErrorKind::ArchitectureInvalid,
@@ -2417,9 +2436,9 @@ impl Build {
24172436
}
24182437
}
24192438
} else if is_sim {
2420-
match arch {
2439+
match arch_str {
24212440
"arm64" | "aarch64" => ArchSpec::Simulator("arm64"),
2422-
"x86_64" => ArchSpec::Simulator("-m64"),
2441+
"x86_64" | "x86_64h" => ArchSpec::Simulator("-m64"),
24232442
_ => {
24242443
return Err(Error::new(
24252444
ErrorKind::ArchitectureInvalid,
@@ -2428,25 +2447,37 @@ impl Build {
24282447
}
24292448
}
24302449
} else {
2431-
match arch {
2450+
match arch_str {
24322451
"arm" | "armv7" | "thumbv7" => ArchSpec::Device("armv7"),
24332452
"armv7k" => ArchSpec::Device("armv7k"),
24342453
"armv7s" | "thumbv7s" => ArchSpec::Device("armv7s"),
24352454
"arm64e" => ArchSpec::Device("arm64e"),
24362455
"arm64" | "aarch64" => ArchSpec::Device("arm64"),
24372456
"arm64_32" => ArchSpec::Device("arm64_32"),
24382457
"i386" | "i686" => ArchSpec::Simulator("-m32"),
2439-
"x86_64" => ArchSpec::Simulator("-m64"),
2458+
"x86_64" | "x86_64h" => ArchSpec::Simulator("-m64"),
24402459
_ => {
24412460
return Err(Error::new(
24422461
ErrorKind::ArchitectureInvalid,
2443-
format!("Unknown architecture for {} target.", os),
2462+
format!("Unknown architecture for {:?} target.", os),
24442463
));
24452464
}
24462465
}
24472466
};
24482467

24492468
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+
),
24502481
Os::Ios => (
24512482
"iphone",
24522483
"ios-",
@@ -2460,6 +2491,11 @@ impl Build {
24602491
};
24612492

24622493
let sdk = match arch {
2494+
ArchSpec::Device(_) if is_mac => {
2495+
cmd.args
2496+
.push(format!("-mmacosx-version-min={}", min_version).into());
2497+
"macosx".to_owned()
2498+
}
24632499
ArchSpec::Device(arch) => {
24642500
cmd.args.push("-arch".into());
24652501
cmd.args.push(arch.into());
@@ -2482,17 +2518,19 @@ impl Build {
24822518
ArchSpec::Catalyst(_) => "macosx".to_owned(),
24832519
};
24842520

2485-
self.print(&format_args!("Detecting {} SDK path for {}", os, sdk));
2486-
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
2487-
sdkroot
2488-
} else {
2489-
self.apple_sdk_root(sdk.as_str())?
2490-
};
2521+
if !is_mac {
2522+
self.print(&format_args!("Detecting {:?} SDK path for {}", os, sdk));
2523+
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
2524+
sdkroot
2525+
} else {
2526+
self.apple_sdk_root(sdk.as_str())?
2527+
};
24912528

2492-
cmd.args.push("-isysroot".into());
2493-
cmd.args.push(sdk_path);
2494-
// TODO: Remove this once Apple stops accepting apps built with Xcode 13
2495-
cmd.args.push("-fembed-bitcode".into());
2529+
cmd.args.push("-isysroot".into());
2530+
cmd.args.push(sdk_path);
2531+
// TODO: Remove this once Apple stops accepting apps built with Xcode 13
2532+
cmd.args.push("-fembed-bitcode".into());
2533+
}
24962534

24972535
Ok(())
24982536
}

tests/test.rs

+18
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,21 @@ fn asm_flags() {
475475
test.cmd(1).must_have("--abc");
476476
test.cmd(2).must_have("--abc");
477477
}
478+
479+
#[test]
480+
fn gnu_apple_darwin() {
481+
for (arch, version) in &[("x86_64", "10.7"), ("aarch64", "11.0")] {
482+
let target = format!("{}-apple-darwin", arch);
483+
let test = Test::gnu();
484+
test.gcc()
485+
.target(&target)
486+
.host(&target)
487+
// Avoid test maintainence when minimum supported OSes change.
488+
.__set_env("MACOSX_DEPLOYMENT_TARGET", version)
489+
.file("foo.c")
490+
.compile("foo");
491+
492+
test.cmd(0)
493+
.must_have(format!("-mmacosx-version-min={}", version));
494+
}
495+
}

0 commit comments

Comments
 (0)