Skip to content

Commit

Permalink
boring-sys: Handle cross-compiling macOS targets
Browse files Browse the repository at this point in the history
...such as compiling for Apple Silicon on an Intel Mac.
  • Loading branch information
jrose-signal committed Oct 13, 2022
1 parent 3059ba6 commit 081b0c1
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions boring-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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",
&[
Expand All @@ -70,26 +71,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 = std::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 = std::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 the platform-specific output path for lib.
Expand Down Expand Up @@ -169,8 +185,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);
}
Expand Down Expand Up @@ -282,11 +305,11 @@ fn get_extra_clang_args_for_bindgen() -> Vec<String> {
// Add platform-specific parameters.
#[allow(clippy::single_match)]
match os.as_ref() {
"ios" => {
"ios" | "macos" => {
use std::io::Write;
// When cross-compiling for iOS, tell bindgen to use iOS sysroot,
// 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()
Expand Down

0 comments on commit 081b0c1

Please sign in to comment.