From fbf1e91f7e468d8b16bd79c77cea2a96f44ee511 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Mon, 1 Jul 2019 13:58:22 -0500 Subject: [PATCH 1/2] Support Linux Cross Compiling Add extra configuration through environment variables to allow compiling for MacOS on Linux. iOS might be possible as well, but it is untested. --- build.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/build.rs b/build.rs index 2974293..350761d 100644 --- a/build.rs +++ b/build.rs @@ -30,6 +30,11 @@ fn frameworks_path() -> Result { // for actually linking against them (especially for cross-compilation) once // has to refer to the frameworks as found within "Xcode.app/Contents/Developer/…". + // Use environment variable if set + if let Ok(path) = std::env::var("COREAUDIO_FRAMEWORKS_PATH") { + return Ok(path) + } + if osx_version() .and_then(|version| Ok(parse_version(&version).map(|v| v >= 13).unwrap_or(false))) .unwrap_or(false) @@ -40,9 +45,10 @@ fn frameworks_path() -> Result { let prefix_str = std::str::from_utf8(&output).expect("invalid output from `xcode-select`"); let prefix = prefix_str.trim_right(); - let platform = if cfg!(target_os = "macos") { + let target = std::env::var("TARGET").unwrap(); + let platform = if target.contains("apple-darwin") { "MacOSX" - } else if cfg!(target_os = "ios") { + } else if target.contains("apple-ios") { "iPhoneOS" } else { unreachable!(); @@ -111,11 +117,13 @@ fn build(frameworks_path: &str) { headers.push("OpenAL.framework/Headers/alc.h"); } - #[cfg(all(feature = "core_midi", target_os = "macos"))] + #[cfg(all(feature = "core_midi"))] { - println!("cargo:rustc-link-lib=framework=CoreMIDI"); - frameworks.push("CoreMIDI"); - headers.push("CoreMIDI.framework/Headers/CoreMIDI.h"); + if std::env::var("TARGET").unwrap().contains("apple-darwin") { + println!("cargo:rustc-link-lib=framework=CoreMIDI"); + frameworks.push("CoreMIDI"); + headers.push("CoreMIDI.framework/Headers/CoreMIDI.h"); + } } // Get the cargo out directory. @@ -139,10 +147,16 @@ fn build(frameworks_path: &str) { } // Generate the bindings. - let bindings = builder + builder = builder .trust_clang_mangling(false) .derive_default(true) - .rustfmt_bindings(false) + .rustfmt_bindings(false); + + if let Ok(cflags) = std::env::var("COREAUDIO_CFLAGS") { + builder = builder.clang_args(cflags.split(" ")); + } + + let bindings = builder .generate() .expect("unable to generate bindings"); @@ -152,8 +166,12 @@ fn build(frameworks_path: &str) { .expect("could not write bindings"); } -#[cfg(any(target_os = "macos", target_os = "ios"))] fn main() { + let target = std::env::var("TARGET").unwrap(); + if ! (target.contains("apple-darwin") || target.contains("apple-ios")) { + eprintln!("coreaudio-sys requires macos or ios target"); + } + if let Ok(directory) = frameworks_path() { build(&directory); } else { @@ -161,7 +179,3 @@ fn main() { } } -#[cfg(not(any(target_os = "macos", target_os = "ios")))] -fn main() { - eprintln!("coreaudio-sys requires macos or ios target"); -} From e012a49a4532fabaf25964f07842bdb6d7cc2040 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Sun, 7 Jul 2019 19:20:39 -0500 Subject: [PATCH 2/2] Add Instructions For Cross Compiling to README --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 396a4e5..34ebc55 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,27 @@ # coreaudio-sys [![Build Status](https://travis-ci.org/RustAudio/coreaudio-sys.svg?branch=master)](https://travis-ci.org/RustAudio/coreaudio-sys) [![Crates.io](https://img.shields.io/crates/v/coreaudio-sys.svg)](https://crates.io/crates/coreaudio-sys) [![Crates.io](https://img.shields.io/crates/l/coreaudio-sys.svg)](https://github.com/RustAudio/coreaudio-sys/blob/master/LICENSE) Raw bindings to Apple's Core Audio API for macos and iOS generated using [rust-bindgen](https://github.com/rust-lang-nursery/rust-bindgen). [coreaudio-rs](https://github.com/RustAudio/coreaudio-rs) is an attempt at offering a higher level API around this crate. + +## Cross Compiling + +[Rust Cross](https://github.com/japaric/rust-cross) has a good explanation of how cross-compiling Rust works in general. While the author of Rust Cross advises against it, it is perfectly possible to cross-compile Rust for MacOS on Linux. [OSXCross](https://github.com/tpoechtrager/osxcross) can be used to create a compiler toolchain that can compile for MacOS on Linux. + +### Environment Variables + +When cross-compiling for MacOS on Linux there are two environment variables that are used to configure how `coreaudio-sys` finds the required headers and libraries. The following examples assume that you have OSXCross installed at `/build/osxcross`. + +#### `COREAUDIO_CFLAGS` + +This allows you to add arbitrary flags to the `clang` call that is made when auto-generating the Rust bindings to coreaudio. This will need to be set to include some headers used by coreaudio: + +```bash +export COREAUDIO_CFLAGS=-I/build/osxcross/target/SDK/MacOSX10.11.sdk/System/Library/Frameworks/Kernel.framework/Headers -I/build/osxcross/target/SDK/MacOSX10.11.sdk/usr/include +``` + +#### `COREAUDIO_FRAMEWORKS_PATH` + +This tell `coreaudio-sys` where to find the Frameworks path of the MacOS SDK: + +```bash +export COREAUDIO_FRAMEWORKS_PATH=/build/osxcross/target/SDK/MacOSX10.11.sdk/System/Library/Frameworks +```