Skip to content

Commit ea97a6c

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

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

src/lib.rs

+41-10
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,11 @@ 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").unwrap_or_else(|_| "10.0".into()),
2177+
),
21542178
Os::Ios => (
21552179
"iphone",
21562180
"ios-",
@@ -2164,6 +2188,11 @@ impl Build {
21642188
};
21652189

21662190
let sdk = match arch {
2191+
ArchSpec::Device(_) if is_mac => {
2192+
cmd.args
2193+
.push(format!("-mmacosx-version-min={}", min_version).into());
2194+
"macosx".to_owned()
2195+
}
21672196
ArchSpec::Device(arch) => {
21682197
cmd.args.push("-arch".into());
21692198
cmd.args.push(arch.into());
@@ -2180,11 +2209,13 @@ impl Build {
21802209
ArchSpec::Catalyst(_) => "macosx".to_owned(),
21812210
};
21822211

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());
2212+
if !is_mac {
2213+
self.print(&format!("Detecting {} SDK path for {}", os, sdk));
2214+
let sdk_path = self.apple_sdk_root(sdk.as_str())?;
2215+
cmd.args.push("-isysroot".into());
2216+
cmd.args.push(sdk_path);
2217+
cmd.args.push("-fembed-bitcode".into());
2218+
}
21882219
/*
21892220
* TODO we probably ultimately want the -fembed-bitcode-marker flag
21902221
* but can't have it now because of an issue in LLVM:

tests/test.rs

+15
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.0");
443+
}
444+
}

0 commit comments

Comments
 (0)