Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Linux Cross Compiling #27

Merged
merged 2 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
40 changes: 27 additions & 13 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn frameworks_path() -> Result<String, std::io::Error> {
// 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)
Expand All @@ -40,9 +45,10 @@ fn frameworks_path() -> Result<String, std::io::Error> {
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!();
Expand Down Expand Up @@ -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.
Expand All @@ -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");

Expand All @@ -152,16 +166,16 @@ 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 {
eprintln!("coreaudio-sys could not find frameworks path");
}
}

#[cfg(not(any(target_os = "macos", target_os = "ios")))]
fn main() {
eprintln!("coreaudio-sys requires macos or ios target");
}