Skip to content

Commit 68d73ec

Browse files
committed
use MACOSX_DEPLOYMENT_TARGET when set
1 parent 8daff16 commit 68d73ec

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

src/lib.rs

+56-19
Original file line numberDiff line numberDiff line change
@@ -1887,8 +1887,8 @@ impl Build {
18871887
}
18881888
}
18891889

1890-
if target.contains("apple-ios") || target.contains("apple-watchos") {
1891-
self.ios_watchos_flags(cmd)?;
1890+
if target.contains("-apple-") {
1891+
self.apple_flags(cmd)?;
18921892
}
18931893

18941894
if self.static_flag.unwrap_or(false) {
@@ -2081,34 +2081,42 @@ impl Build {
20812081
Ok(())
20822082
}
20832083

2084-
fn ios_watchos_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
2084+
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
20852085
enum ArchSpec {
20862086
Device(&'static str),
20872087
Simulator(&'static str),
20882088
Catalyst(&'static str),
20892089
}
20902090

20912091
enum Os {
2092+
MacOs,
20922093
Ios,
20932094
WatchOs,
20942095
}
20952096
impl Display for Os {
20962097
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
20972098
match self {
2099+
Os::MacOs => f.write_str("macOS"),
20982100
Os::Ios => f.write_str("iOS"),
20992101
Os::WatchOs => f.write_str("WatchOS"),
21002102
}
21012103
}
21022104
}
21032105

21042106
let target = self.get_target()?;
2105-
let os = if target.contains("-watchos") {
2107+
let os = if target.contains("-darwin") {
2108+
Os::MacOs
2109+
} else if target.contains("-watchos") {
21062110
Os::WatchOs
21072111
} else {
21082112
Os::Ios
21092113
};
2114+
let is_mac = match os {
2115+
Os::MacOs => true,
2116+
_ => false,
2117+
};
21102118

2111-
let arch = target.split('-').nth(0).ok_or_else(|| {
2119+
let arch_str = target.split('-').nth(0).ok_or_else(|| {
21122120
Error::new(
21132121
ErrorKind::ArchitectureInvalid,
21142122
format!("Unknown architecture for {} target.", os).as_str(),
@@ -2125,8 +2133,19 @@ impl Build {
21252133
None => false,
21262134
};
21272135

2128-
let arch = if is_catalyst {
2129-
match arch {
2136+
let arch = if is_mac {
2137+
match arch_str {
2138+
"i686" => ArchSpec::Device("-m32"),
2139+
"x86_64" | "aarch64" => ArchSpec::Device("-m64"),
2140+
_ => {
2141+
return Err(Error::new(
2142+
ErrorKind::ArchitectureInvalid,
2143+
"Unknown architecture for macOS target.",
2144+
));
2145+
}
2146+
}
2147+
} else if is_catalyst {
2148+
match arch_str {
21302149
"arm64e" => ArchSpec::Catalyst("arm64e"),
21312150
"arm64" | "aarch64" => ArchSpec::Catalyst("arm64"),
21322151
"x86_64" => ArchSpec::Catalyst("-m64"),
@@ -2138,7 +2157,7 @@ impl Build {
21382157
}
21392158
}
21402159
} else if is_sim {
2141-
match arch {
2160+
match arch_str {
21422161
"arm64" | "aarch64" => ArchSpec::Simulator("-arch arm64"),
21432162
"x86_64" => ArchSpec::Simulator("-m64"),
21442163
_ => {
@@ -2149,7 +2168,7 @@ impl Build {
21492168
}
21502169
}
21512170
} else {
2152-
match arch {
2171+
match arch_str {
21532172
"arm" | "armv7" | "thumbv7" => ArchSpec::Device("armv7"),
21542173
"armv7k" => ArchSpec::Device("armv7k"),
21552174
"armv7s" | "thumbv7s" => ArchSpec::Device("armv7s"),
@@ -2168,6 +2187,18 @@ impl Build {
21682187
};
21692188

21702189
let (sdk_prefix, sim_prefix, min_version) = match os {
2190+
Os::MacOs => (
2191+
"macosx",
2192+
"",
2193+
std::env::var("MACOSX_DEPLOYMENT_TARGET").unwrap_or_else(|_| {
2194+
(if arch_str == "aarch64" {
2195+
"11.0"
2196+
} else {
2197+
"10.7"
2198+
})
2199+
.into()
2200+
}),
2201+
),
21712202
Os::Ios => (
21722203
"iphone",
21732204
"ios-",
@@ -2181,6 +2212,11 @@ impl Build {
21812212
};
21822213

21832214
let sdk = match arch {
2215+
ArchSpec::Device(_) if is_mac => {
2216+
cmd.args
2217+
.push(format!("-mmacosx-version-min={}", min_version).into());
2218+
"macosx".to_owned()
2219+
}
21842220
ArchSpec::Device(arch) => {
21852221
cmd.args.push("-arch".into());
21862222
cmd.args.push(arch.into());
@@ -2197,16 +2233,17 @@ impl Build {
21972233
ArchSpec::Catalyst(_) => "macosx".to_owned(),
21982234
};
21992235

2200-
self.print(&format!("Detecting {} SDK path for {}", os, sdk));
2201-
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
2202-
sdkroot
2203-
} else {
2204-
self.apple_sdk_root(sdk.as_str())?
2205-
};
2206-
2207-
cmd.args.push("-isysroot".into());
2208-
cmd.args.push(sdk_path);
2209-
cmd.args.push("-fembed-bitcode".into());
2236+
if !is_mac {
2237+
self.print(&format!("Detecting {} SDK path for {}", os, sdk));
2238+
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
2239+
sdkroot
2240+
} else {
2241+
self.apple_sdk_root(sdk.as_str())?
2242+
};
2243+
cmd.args.push("-isysroot".into());
2244+
cmd.args.push(sdk_path);
2245+
cmd.args.push("-fembed-bitcode".into());
2246+
}
22102247
/*
22112248
* TODO we probably ultimately want the -fembed-bitcode-marker flag
22122249
* but can't have it now because of an issue in LLVM:

tests/test.rs

+16
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,19 @@ 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, version) in &[("x86_64", "10.7"), ("aarch64", "11.0")] {
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)
443+
.must_have(format!("-mmacosx-version-min={}", version));
444+
}
445+
}

0 commit comments

Comments
 (0)