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 +``` 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"); -}