Skip to content

Commit f84b811

Browse files
committed
use MACOSX_DEPLOYMENT_TARGET when set
1 parent 53fb72c commit f84b811

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/lib.rs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,8 +1870,8 @@ impl Build {
18701870
}
18711871
}
18721872

1873-
if target.contains("apple-ios") || target.contains("apple-watchos") {
1874-
self.ios_watchos_flags(cmd)?;
1873+
if target.contains("-apple-") {
1874+
self.apple_flags(cmd)?;
18751875
}
18761876

18771877
if self.static_flag.unwrap_or(false) {
@@ -2064,32 +2064,40 @@ impl Build {
20642064
Ok(())
20652065
}
20662066

2067-
fn ios_watchos_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
2067+
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
20682068
enum ArchSpec {
20692069
Device(&'static str),
20702070
Simulator(&'static str),
20712071
Catalyst(&'static str),
20722072
}
20732073

20742074
enum Os {
2075+
MacOs,
20752076
Ios,
20762077
WatchOs,
20772078
}
20782079
impl Display for Os {
20792080
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
20802081
match self {
2082+
Os::MacOs => f.write_str("macOS"),
20812083
Os::Ios => f.write_str("iOS"),
20822084
Os::WatchOs => f.write_str("WatchOS"),
20832085
}
20842086
}
20852087
}
20862088

20872089
let target = self.get_target()?;
2088-
let os = if target.contains("-watchos") {
2090+
let os = if target.contains("-darwin") {
2091+
Os::MacOs
2092+
} else if target.contains("-watchos") {
20892093
Os::WatchOs
20902094
} else {
20912095
Os::Ios
20922096
};
2097+
let is_mac = match os {
2098+
Os::MacOs => true,
2099+
_ => false,
2100+
};
20932101

20942102
let arch = target.split('-').nth(0).ok_or_else(|| {
20952103
Error::new(
@@ -2108,7 +2116,18 @@ impl Build {
21082116
None => false,
21092117
};
21102118

2111-
let arch = if is_catalyst {
2119+
let arch = if is_mac {
2120+
match arch {
2121+
"i686" => ArchSpec::Device("-m32"),
2122+
"x86_64" | "aarch64" => ArchSpec::Device("-m64"),
2123+
_ => {
2124+
return Err(Error::new(
2125+
ErrorKind::ArchitectureInvalid,
2126+
"Unknown architecture for macOS target.",
2127+
));
2128+
}
2129+
}
2130+
} else if is_catalyst {
21122131
match arch {
21132132
"arm64e" => ArchSpec::Catalyst("arm64e"),
21142133
"arm64" | "aarch64" => ArchSpec::Catalyst("arm64"),
@@ -2151,6 +2170,12 @@ impl Build {
21512170
};
21522171

21532172
let (sdk_prefix, sim_prefix, min_version) = match os {
2173+
Os::MacOs => (
2174+
"macosx",
2175+
"",
2176+
std::env::var("MACOSX_DEPLOYMENT_TARGET")
2177+
.unwrap_or_else(|_| (if self.cpp { "10.9" } else { "10.4" }).into()),
2178+
),
21542179
Os::Ios => (
21552180
"iphone",
21562181
"ios-",
@@ -2164,6 +2189,11 @@ impl Build {
21642189
};
21652190

21662191
let sdk = match arch {
2192+
ArchSpec::Device(_) if is_mac => {
2193+
cmd.args
2194+
.push(format!("-mmacosx-version-min={}", min_version).into());
2195+
"macosx".to_owned()
2196+
}
21672197
ArchSpec::Device(arch) => {
21682198
cmd.args.push("-arch".into());
21692199
cmd.args.push(arch.into());
@@ -2180,11 +2210,13 @@ impl Build {
21802210
ArchSpec::Catalyst(_) => "macosx".to_owned(),
21812211
};
21822212

2183-
self.print(&format!("Detecting {} SDK path for {}", os, sdk));
2184-
let sdk_path = self.apple_sdk_root(sdk.as_str())?;
2185-
cmd.args.push("-isysroot".into());
2186-
cmd.args.push(sdk_path);
2187-
cmd.args.push("-fembed-bitcode".into());
2213+
if !is_mac {
2214+
self.print(&format!("Detecting {} SDK path for {}", os, sdk));
2215+
let sdk_path = self.apple_sdk_root(sdk.as_str())?;
2216+
cmd.args.push("-isysroot".into());
2217+
cmd.args.push(sdk_path);
2218+
cmd.args.push("-fembed-bitcode".into());
2219+
}
21882220
/*
21892221
* TODO we probably ultimately want the -fembed-bitcode-marker flag
21902222
* but can't have it now because of an issue in LLVM:

tests/test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,18 @@ fn msvc_no_dash_dash() {
427427

428428
test.cmd(0).must_not_have("--");
429429
}
430+
431+
#[test]
432+
fn gnu_apple_darwin() {
433+
for arch in &["x86_64", "aarch64"] {
434+
let target = format!("{}-apple-darwin", arch);
435+
let test = Test::gnu();
436+
test.gcc()
437+
.target(&target)
438+
.host(&target)
439+
.file("foo.c")
440+
.compile("foo");
441+
442+
test.cmd(0).must_have("-mmacosx-version-min=10.4");
443+
}
444+
}

0 commit comments

Comments
 (0)