Skip to content

Commit 4ca9210

Browse files
committed
use MACOSX_DEPLOYMENT_TARGET when set
1 parent 57853c4 commit 4ca9210

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

src/lib.rs

+57-19
Original file line numberDiff line numberDiff line change
@@ -1994,8 +1994,8 @@ impl Build {
19941994
}
19951995
}
19961996

1997-
if target.contains("apple-ios") || target.contains("apple-watchos") {
1998-
self.ios_watchos_flags(cmd)?;
1997+
if target.contains("-apple-") {
1998+
self.apple_flags(cmd)?;
19991999
}
20002000

20012001
if self.static_flag.unwrap_or(false) {
@@ -2205,34 +2205,42 @@ impl Build {
22052205
Ok(())
22062206
}
22072207

2208-
fn ios_watchos_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
2208+
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
22092209
enum ArchSpec {
22102210
Device(&'static str),
22112211
Simulator(&'static str),
22122212
Catalyst(&'static str),
22132213
}
22142214

22152215
enum Os {
2216+
MacOs,
22162217
Ios,
22172218
WatchOs,
22182219
}
22192220
impl Display for Os {
22202221
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
22212222
match self {
2223+
Os::MacOs => f.write_str("macOS"),
22222224
Os::Ios => f.write_str("iOS"),
22232225
Os::WatchOs => f.write_str("WatchOS"),
22242226
}
22252227
}
22262228
}
22272229

22282230
let target = self.get_target()?;
2229-
let os = if target.contains("-watchos") {
2231+
let os = if target.contains("-darwin") {
2232+
Os::MacOs
2233+
} else if target.contains("-watchos") {
22302234
Os::WatchOs
22312235
} else {
22322236
Os::Ios
22332237
};
2238+
let is_mac = match os {
2239+
Os::MacOs => true,
2240+
_ => false,
2241+
};
22342242

2235-
let arch = target.split('-').nth(0).ok_or_else(|| {
2243+
let arch_str = target.split('-').nth(0).ok_or_else(|| {
22362244
Error::new(
22372245
ErrorKind::ArchitectureInvalid,
22382246
format!("Unknown architecture for {} target.", os).as_str(),
@@ -2249,8 +2257,19 @@ impl Build {
22492257
None => false,
22502258
};
22512259

2252-
let arch = if is_catalyst {
2253-
match arch {
2260+
let arch = if is_mac {
2261+
match arch_str {
2262+
"i686" => ArchSpec::Device("-m32"),
2263+
"x86_64" | "aarch64" => ArchSpec::Device("-m64"),
2264+
_ => {
2265+
return Err(Error::new(
2266+
ErrorKind::ArchitectureInvalid,
2267+
"Unknown architecture for macOS target.",
2268+
));
2269+
}
2270+
}
2271+
} else if is_catalyst {
2272+
match arch_str {
22542273
"arm64e" => ArchSpec::Catalyst("arm64e"),
22552274
"arm64" | "aarch64" => ArchSpec::Catalyst("arm64"),
22562275
"x86_64" => ArchSpec::Catalyst("-m64"),
@@ -2262,7 +2281,7 @@ impl Build {
22622281
}
22632282
}
22642283
} else if is_sim {
2265-
match arch {
2284+
match arch_str {
22662285
"arm64" | "aarch64" => ArchSpec::Simulator("arm64"),
22672286
"x86_64" => ArchSpec::Simulator("-m64"),
22682287
_ => {
@@ -2273,7 +2292,7 @@ impl Build {
22732292
}
22742293
}
22752294
} else {
2276-
match arch {
2295+
match arch_str {
22772296
"arm" | "armv7" | "thumbv7" => ArchSpec::Device("armv7"),
22782297
"armv7k" => ArchSpec::Device("armv7k"),
22792298
"armv7s" | "thumbv7s" => ArchSpec::Device("armv7s"),
@@ -2292,6 +2311,18 @@ impl Build {
22922311
};
22932312

22942313
let (sdk_prefix, sim_prefix, min_version) = match os {
2314+
Os::MacOs => (
2315+
"macosx",
2316+
"",
2317+
std::env::var("MACOSX_DEPLOYMENT_TARGET").unwrap_or_else(|_| {
2318+
(if arch_str == "aarch64" {
2319+
"11.0"
2320+
} else {
2321+
"10.7"
2322+
})
2323+
.into()
2324+
}),
2325+
),
22952326
Os::Ios => (
22962327
"iphone",
22972328
"ios-",
@@ -2305,6 +2336,11 @@ impl Build {
23052336
};
23062337

23072338
let sdk = match arch {
2339+
ArchSpec::Device(_) if is_mac => {
2340+
cmd.args
2341+
.push(format!("-mmacosx-version-min={}", min_version).into());
2342+
"macosx".to_owned()
2343+
}
23082344
ArchSpec::Device(arch) => {
23092345
cmd.args.push("-arch".into());
23102346
cmd.args.push(arch.into());
@@ -2327,17 +2363,19 @@ impl Build {
23272363
ArchSpec::Catalyst(_) => "macosx".to_owned(),
23282364
};
23292365

2330-
self.print(&format!("Detecting {} SDK path for {}", os, sdk));
2331-
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
2332-
sdkroot
2333-
} else {
2334-
self.apple_sdk_root(sdk.as_str())?
2335-
};
2366+
if !is_mac {
2367+
self.print(&format!("Detecting {} SDK path for {}", os, sdk));
2368+
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
2369+
sdkroot
2370+
} else {
2371+
self.apple_sdk_root(sdk.as_str())?
2372+
};
23362373

2337-
cmd.args.push("-isysroot".into());
2338-
cmd.args.push(sdk_path);
2339-
// TODO: Remove this once Apple stops accepting apps built with Xcode 13
2340-
cmd.args.push("-fembed-bitcode".into());
2374+
cmd.args.push("-isysroot".into());
2375+
cmd.args.push(sdk_path);
2376+
// TODO: Remove this once Apple stops accepting apps built with Xcode 13
2377+
cmd.args.push("-fembed-bitcode".into());
2378+
}
23412379

23422380
Ok(())
23432381
}

tests/test.rs

+16
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,19 @@ fn asm_flags() {
459459
test.cmd(1).must_have("--abc");
460460
test.cmd(2).must_have("--abc");
461461
}
462+
463+
#[test]
464+
fn gnu_apple_darwin() {
465+
for (arch, version) in &[("x86_64", "10.7"), ("aarch64", "11.0")] {
466+
let target = format!("{}-apple-darwin", arch);
467+
let test = Test::gnu();
468+
test.gcc()
469+
.target(&target)
470+
.host(&target)
471+
.file("foo.c")
472+
.compile("foo");
473+
474+
test.cmd(0)
475+
.must_have(format!("-mmacosx-version-min={}", version));
476+
}
477+
}

0 commit comments

Comments
 (0)