Skip to content

Commit

Permalink
use MACOSX_DEPLOYMENT_TARGET when set
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone authored and BlackHoleFox committed Aug 4, 2023
1 parent 5710ce5 commit 9d84691
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
76 changes: 57 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2027,8 +2027,8 @@ impl Build {
}
}

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

if self.static_flag.unwrap_or(false) {
Expand Down Expand Up @@ -2246,34 +2246,42 @@ impl Build {
Ok(())
}

fn ios_watchos_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
enum ArchSpec {
Device(&'static str),
Simulator(&'static str),
Catalyst(&'static str),
}

enum Os {
MacOs,
Ios,
WatchOs,
}
impl Display for Os {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Os::MacOs => f.write_str("macOS"),
Os::Ios => f.write_str("iOS"),
Os::WatchOs => f.write_str("WatchOS"),
}
}
}

let target = self.get_target()?;
let os = if target.contains("-watchos") {
let os = if target.contains("-darwin") {
Os::MacOs
} else if target.contains("-watchos") {
Os::WatchOs
} else {
Os::Ios
};
let is_mac = match os {
Os::MacOs => true,
_ => false,
};

let arch = target.split('-').nth(0).ok_or_else(|| {
let arch_str = target.split('-').nth(0).ok_or_else(|| {
Error::new(
ErrorKind::ArchitectureInvalid,
format!("Unknown architecture for {} target.", os),
Expand All @@ -2290,8 +2298,19 @@ impl Build {
None => false,
};

let arch = if is_catalyst {
match arch {
let arch = if is_mac {
match arch_str {
"i686" => ArchSpec::Device("-m32"),
"x86_64" | "aarch64" => ArchSpec::Device("-m64"),
_ => {
return Err(Error::new(
ErrorKind::ArchitectureInvalid,
"Unknown architecture for macOS target.",
));
}
}
} else if is_catalyst {
match arch_str {
"arm64e" => ArchSpec::Catalyst("arm64e"),
"arm64" | "aarch64" => ArchSpec::Catalyst("arm64"),
"x86_64" => ArchSpec::Catalyst("-m64"),
Expand All @@ -2303,7 +2322,7 @@ impl Build {
}
}
} else if is_sim {
match arch {
match arch_str {
"arm64" | "aarch64" => ArchSpec::Simulator("arm64"),
"x86_64" => ArchSpec::Simulator("-m64"),
_ => {
Expand All @@ -2314,7 +2333,7 @@ impl Build {
}
}
} else {
match arch {
match arch_str {
"arm" | "armv7" | "thumbv7" => ArchSpec::Device("armv7"),
"armv7k" => ArchSpec::Device("armv7k"),
"armv7s" | "thumbv7s" => ArchSpec::Device("armv7s"),
Expand All @@ -2333,6 +2352,18 @@ impl Build {
};

let (sdk_prefix, sim_prefix, min_version) = match os {
Os::MacOs => (
"macosx",
"",
std::env::var("MACOSX_DEPLOYMENT_TARGET").unwrap_or_else(|_| {
(if arch_str == "aarch64" {
"11.0"
} else {
"10.7"
})
.into()
}),
),
Os::Ios => (
"iphone",
"ios-",
Expand All @@ -2346,6 +2377,11 @@ impl Build {
};

let sdk = match arch {
ArchSpec::Device(_) if is_mac => {
cmd.args
.push(format!("-mmacosx-version-min={}", min_version).into());
"macosx".to_owned()
}
ArchSpec::Device(arch) => {
cmd.args.push("-arch".into());
cmd.args.push(arch.into());
Expand All @@ -2368,17 +2404,19 @@ impl Build {
ArchSpec::Catalyst(_) => "macosx".to_owned(),
};

self.print(&format_args!("Detecting {} SDK path for {}", os, sdk));
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
sdkroot
} else {
self.apple_sdk_root(sdk.as_str())?
};
if !is_mac {
self.print(&format_args!("Detecting {} SDK path for {}", os, sdk));
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
sdkroot
} else {
self.apple_sdk_root(sdk.as_str())?
};

cmd.args.push("-isysroot".into());
cmd.args.push(sdk_path);
// TODO: Remove this once Apple stops accepting apps built with Xcode 13
cmd.args.push("-fembed-bitcode".into());
cmd.args.push("-isysroot".into());
cmd.args.push(sdk_path);
// TODO: Remove this once Apple stops accepting apps built with Xcode 13
cmd.args.push("-fembed-bitcode".into());
}

Ok(())
}
Expand Down
16 changes: 16 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,19 @@ fn asm_flags() {
test.cmd(1).must_have("--abc");
test.cmd(2).must_have("--abc");
}

#[test]
fn gnu_apple_darwin() {
for (arch, version) in &[("x86_64", "10.7"), ("aarch64", "11.0")] {
let target = format!("{}-apple-darwin", arch);
let test = Test::gnu();
test.gcc()
.target(&target)
.host(&target)
.file("foo.c")
.compile("foo");

test.cmd(0)
.must_have(format!("-mmacosx-version-min={}", version));
}
}

0 comments on commit 9d84691

Please sign in to comment.