Skip to content

Commit

Permalink
Fix AppleClang darwin build & AppleTVOS support
Browse files Browse the repository at this point in the history
- Fix AppleClang build for -apple-darwin to add -isysroot for MacOSX sdk correctly
- Add AppleTVOS support
  • Loading branch information
youknowone committed Jul 26, 2022
1 parent 53fb72c commit f55cce3
Showing 1 changed file with 86 additions and 9 deletions.
95 changes: 86 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,20 @@ impl Build {
.into(),
);
}
} else if target.contains("tvos-sim") {
if let Some(arch) =
map_darwin_target_from_rust_to_compiler_architecture(target)
{
let deployment_target =
env::var("TVOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "9.0".into());
cmd.args.push(
format!(
"--target={}-apple-tvos{}-simulator",
arch, deployment_target
)
.into(),
);
}
} else if target.starts_with("riscv64gc-") {
cmd.args.push(
format!("--target={}", target.replace("riscv64gc", "riscv64")).into(),
Expand Down Expand Up @@ -1870,8 +1884,11 @@ impl Build {
}
}

if target.contains("apple-ios") || target.contains("apple-watchos") {
self.ios_watchos_flags(cmd)?;
if target.contains("-apple-") {
// AppleClang requires apple flags even for darwin
if cmd.check_apple_clang() || !target.ends_with("-darwin") {
self.apple_flags(cmd)?;
}
}

if self.static_flag.unwrap_or(false) {
Expand Down Expand Up @@ -2064,32 +2081,44 @@ 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,
TvOs,
}
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"),
Os::TvOs => f.write_str("tvOS"),
}
}
}

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 if target.contains("-tvos") {
Os::TvOs
} else {
Os::Ios
};
let is_mac = match os {
Os::MacOs => true,
_ => false,
};

let arch = target.split('-').nth(0).ok_or_else(|| {
Error::new(
Expand All @@ -2103,12 +2132,23 @@ impl Build {
None => false,
};

let is_sim = match target.split('-').nth(3) {
let is_arm_sim = match target.split('-').nth(3) {
Some(v) => v == "sim",
None => false,
};

let arch = if is_catalyst {
let arch = if is_mac {
match arch {
"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 {
"arm64e" => ArchSpec::Catalyst("arm64e"),
"arm64" | "aarch64" => ArchSpec::Catalyst("arm64"),
Expand All @@ -2120,14 +2160,14 @@ impl Build {
));
}
}
} else if is_sim {
} else if is_arm_sim {
match arch {
"arm64" | "aarch64" => ArchSpec::Simulator("-arch arm64"),
"x86_64" => ArchSpec::Simulator("-m64"),
_ => {
return Err(Error::new(
ErrorKind::ArchitectureInvalid,
"Unknown architecture for iOS simulator target.",
"Unknown architecture for simulator target.",
));
}
}
Expand All @@ -2151,6 +2191,11 @@ impl Build {
};

let (sdk_prefix, sim_prefix, min_version) = match os {
Os::MacOs => (
"macosx",
"",
std::env::var("MACOSX_DEPLOYMENT_TARGET").unwrap_or_else(|_| "10.0".into()),
),
Os::Ios => (
"iphone",
"ios-",
Expand All @@ -2161,9 +2206,20 @@ impl Build {
"watch",
std::env::var("WATCHOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "2.0".into()),
),
Os::TvOs => (
"tv",
"tv",
std::env::var("TVOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "9.0".into()),
),
};

let sdk = match arch {
ArchSpec::Device(arch) if is_mac => {
cmd.args.push(arch.into());
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 @@ -2184,7 +2240,9 @@ impl Build {
let sdk_path = self.apple_sdk_root(sdk.as_str())?;
cmd.args.push("-isysroot".into());
cmd.args.push(sdk_path);
cmd.args.push("-fembed-bitcode".into());
if !is_mac {
cmd.args.push("-fembed-bitcode".into());
}
/*
* TODO we probably ultimately want the -fembed-bitcode-marker flag
* but can't have it now because of an issue in LLVM:
Expand Down Expand Up @@ -3080,6 +3138,25 @@ impl Tool {
self.family == ToolFamily::Clang
}

/// Whether the tool is AppleClang.
#[cfg(target_os = "macos")]
fn check_apple_clang(&self) -> bool {
if self.family != ToolFamily::Clang {
return false;
}
let output = std::process::Command::new(&self.path)
.arg("--version")
.output();
match output {
Ok(output) => String::from_utf8_lossy(&output.stdout).contains("Apple clang"),
Err(_) => false,
}
}
#[cfg(not(target_os = "macos"))]
fn check_apple_clang(&self) -> bool {
false
}

/// Whether the tool is MSVC-like.
pub fn is_like_msvc(&self) -> bool {
match self.family {
Expand Down

0 comments on commit f55cce3

Please sign in to comment.