boring-sys: Handle cross-compiling macOS targets

...such as compiling for Apple Silicon on an Intel Mac.
This commit is contained in:
Jordan Rose 2022-07-29 10:57:17 -07:00
parent 3478bc2668
commit a39f0c78ee
1 changed files with 34 additions and 11 deletions

View File

@ -53,7 +53,8 @@ fn cmake_params_android() -> &'static [(&'static str, &'static str)] {
&[]
}
const CMAKE_PARAMS_IOS: &[(&str, &[(&str, &str)])] = &[
const CMAKE_PARAMS_APPLE: &[(&str, &[(&str, &str)])] = &[
// iOS
(
"aarch64-apple-ios",
&[
@ -75,26 +76,41 @@ const CMAKE_PARAMS_IOS: &[(&str, &[(&str, &str)])] = &[
("CMAKE_OSX_SYSROOT", "iphonesimulator"),
],
),
// macOS
(
"aarch64-apple-darwin",
&[
("CMAKE_OSX_ARCHITECTURES", "arm64"),
("CMAKE_OSX_SYSROOT", "macosx"),
],
),
(
"x86_64-apple-darwin",
&[
("CMAKE_OSX_ARCHITECTURES", "x86_64"),
("CMAKE_OSX_SYSROOT", "macosx"),
],
),
];
fn cmake_params_ios() -> &'static [(&'static str, &'static str)] {
fn cmake_params_apple() -> &'static [(&'static str, &'static str)] {
let target = env::var("TARGET").unwrap();
for (ios_target, params) in CMAKE_PARAMS_IOS {
if *ios_target == target {
for (next_target, params) in CMAKE_PARAMS_APPLE {
if *next_target == target {
return params;
}
}
&[]
}
fn get_ios_sdk_name() -> &'static str {
for (name, value) in cmake_params_ios() {
fn get_apple_sdk_name() -> &'static str {
for (name, value) in cmake_params_apple() {
if *name == "CMAKE_OSX_SYSROOT" {
return value;
}
}
let target = env::var("TARGET").unwrap();
panic!("cannot find iOS SDK for {} in CMAKE_PARAMS_IOS", target);
panic!("cannot find SDK for {} in CMAKE_PARAMS_APPLE", target);
}
/// Returns an absolute path to the BoringSSL source.
@ -180,8 +196,15 @@ fn get_boringssl_cmake_config() -> cmake::Config {
boringssl_cmake.define("ANDROID_STL", "c++_shared");
}
"macos" => {
for (name, value) in cmake_params_apple() {
eprintln!("macos arch={} add {}={}", arch, name, value);
boringssl_cmake.define(name, value);
}
}
"ios" => {
for (name, value) in cmake_params_ios() {
for (name, value) in cmake_params_apple() {
eprintln!("ios arch={} add {}={}", arch, name, value);
boringssl_cmake.define(name, value);
}
@ -295,10 +318,10 @@ fn get_extra_clang_args_for_bindgen() -> Vec<String> {
// Add platform-specific parameters.
#[allow(clippy::single_match)]
match os.as_ref() {
"ios" => {
// When cross-compiling for iOS, tell bindgen to use iOS sysroot,
"ios" | "macos" => {
// When cross-compiling for Apple targets, tell bindgen to use SDK sysroot,
// and *don't* use system headers of the host macOS.
let sdk = get_ios_sdk_name();
let sdk = get_apple_sdk_name();
let output = std::process::Command::new("xcrun")
.args(["--show-sdk-path", "--sdk", sdk])
.output()