Skip to content

Commit 2b7b97e

Browse files
committed
DRY Apple SDK setting
1 parent a602213 commit 2b7b97e

1 file changed

Lines changed: 21 additions & 44 deletions

File tree

boring-sys/build/main.rs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ fn cmake_params_android(config: &Config) -> &'static [(&'static str, &'static st
4444
}
4545

4646
const CMAKE_PARAMS_APPLE: &[(&str, &[(&str, &str)])] = &[
47-
// iOS
4847
(
4948
"aarch64-apple-ios",
5049
&[
5150
("CMAKE_OSX_ARCHITECTURES", "arm64"),
5251
("CMAKE_OSX_SYSROOT", "iphoneos"),
5352
("CMAKE_MACOSX_BUNDLE", "OFF"),
53+
("CMAKE_ASM_FLAGS", "-fembed-bitcode"),
5454
],
5555
),
5656
(
@@ -59,6 +59,7 @@ const CMAKE_PARAMS_APPLE: &[(&str, &[(&str, &str)])] = &[
5959
("CMAKE_OSX_ARCHITECTURES", "arm64"),
6060
("CMAKE_OSX_SYSROOT", "iphonesimulator"),
6161
("CMAKE_MACOSX_BUNDLE", "OFF"),
62+
("CMAKE_ASM_FLAGS", "-fembed-bitcode"),
6263
],
6364
),
6465
(
@@ -67,9 +68,12 @@ const CMAKE_PARAMS_APPLE: &[(&str, &[(&str, &str)])] = &[
6768
("CMAKE_OSX_ARCHITECTURES", "x86_64"),
6869
("CMAKE_OSX_SYSROOT", "iphonesimulator"),
6970
("CMAKE_MACOSX_BUNDLE", "OFF"),
71+
(
72+
"CMAKE_ASM_FLAGS",
73+
"-fembed-bitcode -target x86_64-apple-ios-simulator",
74+
),
7075
],
7176
),
72-
// macOS
7377
(
7478
"aarch64-apple-darwin",
7579
&[
@@ -87,25 +91,16 @@ const CMAKE_PARAMS_APPLE: &[(&str, &[(&str, &str)])] = &[
8791
];
8892

8993
fn cmake_params_apple(config: &Config) -> &'static [(&'static str, &'static str)] {
90-
for (next_target, params) in CMAKE_PARAMS_APPLE {
91-
if *next_target == config.target {
92-
return params;
93-
}
94-
}
95-
&[]
94+
CMAKE_PARAMS_APPLE
95+
.iter()
96+
.find_map(|&(target, params)| (target == config.target).then_some(params))
97+
.unwrap_or_default()
9698
}
9799

98-
fn get_apple_sdk_name(config: &Config) -> &'static str {
99-
for (name, value) in cmake_params_apple(config) {
100-
if *name == "CMAKE_OSX_SYSROOT" {
101-
return value;
102-
}
103-
}
104-
105-
panic!(
106-
"cannot find SDK for {} in CMAKE_PARAMS_APPLE",
107-
config.target
108-
);
100+
fn get_apple_sdk_name(config: &Config) -> Option<&'static str> {
101+
cmake_params_apple(config)
102+
.iter()
103+
.find_map(|&(name, value)| (name == "CMAKE_OSX_SYSROOT").then_some(value))
109104
}
110105

111106
/// Returns an absolute path to the BoringSSL source.
@@ -280,34 +275,13 @@ fn get_boringssl_cmake_config(config: &Config) -> cmake::Config {
280275
boringssl_cmake.define("CMAKE_ANDROID_STL_TYPE", "c++_shared");
281276
}
282277

283-
"macos" => {
278+
os @ ("macos" | "ios") => {
284279
for (name, value) in cmake_params_apple(config) {
285-
eprintln!("macos arch={} add {}={}", config.target_arch, name, value);
280+
eprintln!("{os} arch={} add {}={}", config.target_arch, name, value);
286281
boringssl_cmake.define(name, value);
287282
}
288283
}
289284

290-
"ios" => {
291-
for (name, value) in cmake_params_apple(config) {
292-
eprintln!("ios arch={} add {}={}", config.target_arch, name, value);
293-
boringssl_cmake.define(name, value);
294-
}
295-
296-
// Bitcode is always on.
297-
let bitcode_cflag = "-fembed-bitcode";
298-
299-
// Hack for Xcode 10.1.
300-
let target_cflag = if config.target_arch == "x86_64" {
301-
"-target x86_64-apple-ios-simulator"
302-
} else {
303-
""
304-
};
305-
306-
let cflag = format!("{bitcode_cflag} {target_cflag}");
307-
boringssl_cmake.define("CMAKE_ASM_FLAGS", &cflag);
308-
boringssl_cmake.cflag(&cflag);
309-
}
310-
311285
"windows" if config.host.contains("windows") => {
312286
// BoringSSL's CMakeLists.txt isn't set up for cross-compiling using Visual Studio.
313287
// Disable assembly support so that it at least builds.
@@ -394,8 +368,11 @@ fn get_extra_clang_args_for_bindgen(config: &Config) -> Vec<String> {
394368
"ios" | "macos" => {
395369
// When cross-compiling for Apple targets, tell bindgen to use SDK sysroot,
396370
// and *don't* use system headers of the host macOS.
397-
let sdk = get_apple_sdk_name(config);
398-
match run_command(Command::new("xcrun").args(["--show-sdk-path", "--sdk", sdk])) {
371+
match get_apple_sdk_name(config)
372+
.ok_or_else(|| io::Error::other(format!("can't find SDK for {}", config.target)))
373+
.and_then(|sdk| {
374+
run_command(Command::new("xcrun").args(["--show-sdk-path", "--sdk", sdk]))
375+
}) {
399376
Ok(output) => {
400377
let sysroot = std::str::from_utf8(&output.stdout).expect("xcrun output");
401378
params.push("-isysroot".to_string());

0 commit comments

Comments
 (0)