Skip to content

Commit d5a711e

Browse files
committed
[watchos] Make a single flags function for ios and watchos
1 parent 73f8fe4 commit d5a711e

File tree

1 file changed

+38
-80
lines changed

1 file changed

+38
-80
lines changed

src/lib.rs

Lines changed: 38 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
use std::collections::HashMap;
6060
use std::env;
6161
use std::ffi::{OsStr, OsString};
62-
use std::fmt::{self, Display};
62+
use std::fmt::{self, Display, Formatter};
6363
use std::fs;
6464
use std::io::{self, BufRead, BufReader, Read, Write};
6565
use std::path::{Component, Path, PathBuf};
@@ -1864,11 +1864,11 @@ impl Build {
18641864
}
18651865

18661866
if target.contains("apple-ios") {
1867-
self.ios_flags(cmd)?;
1867+
self.ios_watchos_flags(cmd)?;
18681868
}
18691869

18701870
if target.contains("apple-watchos") {
1871-
self.watchos_flags(cmd)?;
1871+
self.ios_watchos_flags(cmd)?;
18721872
}
18731873

18741874
if self.static_flag.unwrap_or(false) {
@@ -2061,18 +2061,37 @@ impl Build {
20612061
Ok(())
20622062
}
20632063

2064-
fn ios_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
2064+
fn ios_watchos_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
20652065
enum ArchSpec {
20662066
Device(&'static str),
20672067
Simulator(&'static str),
20682068
Catalyst(&'static str),
20692069
}
20702070

2071+
enum Os {
2072+
Ios,
2073+
WatchOs,
2074+
}
2075+
impl Display for Os {
2076+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2077+
match self {
2078+
Os::Ios => f.write_str("iOS"),
2079+
Os::WatchOs => f.write_str("WatchOS"),
2080+
}
2081+
}
2082+
}
2083+
20712084
let target = self.get_target()?;
2085+
let os = if target.contains("-watchos") {
2086+
Os::WatchOs
2087+
} else {
2088+
Os::Ios
2089+
};
2090+
20722091
let arch = target.split('-').nth(0).ok_or_else(|| {
20732092
Error::new(
20742093
ErrorKind::ArchitectureInvalid,
2075-
"Unknown architecture for iOS target.",
2094+
format!("Unknown architecture for {} target.", os).as_str(),
20762095
)
20772096
})?;
20782097

@@ -2101,6 +2120,7 @@ impl Build {
21012120
} else if is_sim {
21022121
match arch {
21032122
"arm64" | "aarch64" => ArchSpec::Simulator("-arch arm64"),
2123+
"x86_64" => ArchSpec::Simulator("-m64"),
21042124
_ => {
21052125
return Err(Error::new(
21062126
ErrorKind::ArchitectureInvalid,
@@ -2111,108 +2131,46 @@ impl Build {
21112131
} else {
21122132
match arch {
21132133
"arm" | "armv7" | "thumbv7" => ArchSpec::Device("armv7"),
2134+
"armv7k" => ArchSpec::Device("armv7k"),
21142135
"armv7s" | "thumbv7s" => ArchSpec::Device("armv7s"),
21152136
"arm64e" => ArchSpec::Device("arm64e"),
21162137
"arm64" | "aarch64" => ArchSpec::Device("arm64"),
2138+
"arm64_32" => ArchSpec::Device("arm64_32"),
21172139
"i386" | "i686" => ArchSpec::Simulator("-m32"),
21182140
"x86_64" => ArchSpec::Simulator("-m64"),
21192141
_ => {
21202142
return Err(Error::new(
21212143
ErrorKind::ArchitectureInvalid,
2122-
"Unknown architecture for iOS target.",
2144+
format!("Unknown architecture for {} target.", os).as_str(),
21232145
));
21242146
}
21252147
}
21262148
};
21272149

2128-
let min_version =
2129-
std::env::var("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "7.0".into());
2130-
2131-
let sdk = match arch {
2132-
ArchSpec::Device(arch) => {
2133-
cmd.args.push("-arch".into());
2134-
cmd.args.push(arch.into());
2135-
cmd.args
2136-
.push(format!("-miphoneos-version-min={}", min_version).into());
2137-
"iphoneos"
2138-
}
2139-
ArchSpec::Simulator(arch) => {
2140-
cmd.args.push(arch.into());
2141-
cmd.args
2142-
.push(format!("-mios-simulator-version-min={}", min_version).into());
2143-
"iphonesimulator"
2144-
}
2145-
ArchSpec::Catalyst(_) => "macosx",
2146-
};
2147-
2148-
self.print(&format!("Detecting iOS SDK path for {}", sdk));
2149-
let sdk_path = self.apple_sdk_root(sdk)?;
2150-
cmd.args.push("-isysroot".into());
2151-
cmd.args.push(sdk_path);
2152-
cmd.args.push("-fembed-bitcode".into());
2153-
/*
2154-
* TODO we probably ultimately want the -fembed-bitcode-marker flag
2155-
* but can't have it now because of an issue in LLVM:
2156-
* https://github.com/alexcrichton/cc-rs/issues/301
2157-
* https://github.com/rust-lang/rust/pull/48896#comment-372192660
2158-
*/
2159-
/*
2160-
if self.get_opt_level()? == "0" {
2161-
cmd.args.push("-fembed-bitcode-marker".into());
2162-
}
2163-
*/
2164-
2165-
Ok(())
2166-
}
2167-
2168-
fn watchos_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
2169-
enum ArchSpec {
2170-
Device(&'static str),
2171-
Simulator(&'static str),
2172-
}
2173-
2174-
let target = self.get_target()?;
2175-
let arch = target.split('-').nth(0).ok_or_else(|| {
2176-
Error::new(
2177-
ErrorKind::ArchitectureInvalid,
2178-
"Unknown architecture for watchOS target.",
2179-
)
2180-
})?;
2181-
2182-
let arch = match arch {
2183-
"armv7k" => ArchSpec::Device("armv7k"),
2184-
"arm64_32" => ArchSpec::Device("arm64_32"),
2185-
"i386" | "i686" => ArchSpec::Simulator("-m32"),
2186-
"x86_64" => ArchSpec::Simulator("-m64"),
2187-
_ => {
2188-
return Err(Error::new(
2189-
ErrorKind::ArchitectureInvalid,
2190-
"Unknown architecture for watchOS target.",
2191-
));
2192-
}
2150+
let (sdk_prefix, sim_prefix, min_version) = match os {
2151+
Os::Ios => ("iphone", "ios-", std::env::var("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "7.0".into())),
2152+
Os::WatchOs => ("watch", "watch", std::env::var("WATCHOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "2.0".into())),
21932153
};
21942154

2195-
let min_version =
2196-
std::env::var("WATCHOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "2.0".into());
2197-
21982155
let sdk = match arch {
21992156
ArchSpec::Device(arch) => {
22002157
cmd.args.push("-arch".into());
22012158
cmd.args.push(arch.into());
22022159
cmd.args
2203-
.push(format!("-mwatchos-version-min={}", min_version).into());
2204-
"watchos"
2160+
.push(format!("-m{}os-version-min={}", sdk_prefix, min_version).into());
2161+
format!("{}os", sdk_prefix)
22052162
}
22062163
ArchSpec::Simulator(arch) => {
22072164
cmd.args.push(arch.into());
22082165
cmd.args
2209-
.push(format!("-mwatchsimulator-version-min={}", min_version).into());
2210-
"watchsimulator"
2166+
.push(format!("-m{}simulator-version-min={}", sim_prefix, min_version).into());
2167+
format!("{}simulator", sdk_prefix)
22112168
}
2169+
ArchSpec::Catalyst(_) => "macosx".to_owned(),
22122170
};
22132171

2214-
self.print(&format!("Detecting watchOS SDK path for {}", sdk));
2215-
let sdk_path = self.apple_sdk_root(sdk)?;
2172+
self.print(&format!("Detecting {} SDK path for {}", os, sdk));
2173+
let sdk_path = self.apple_sdk_root(sdk.as_str())?;
22162174
cmd.args.push("-isysroot".into());
22172175
cmd.args.push(sdk_path);
22182176
cmd.args.push("-fembed-bitcode".into());

0 commit comments

Comments
 (0)