| 1 |
extern crate bindgen; |
| 2 |
|
| 3 |
fn sdk_path(target: &str) -> Result<String, std::io::Error> { |
| 4 |
// Use environment variable if set |
| 5 |
println!("cargo:rerun-if-env-changed=COREAUDIO_SDK_PATH"); |
| 6 |
if let Ok(path) = std::env::var("COREAUDIO_SDK_PATH") { |
| 7 |
return Ok(path); |
| 8 |
} |
| 9 |
|
| 10 |
use std::process::Command; |
| 11 |
|
| 12 |
let sdk = if target.contains("apple-darwin") { |
| 13 |
"macosx" |
| 14 |
} else if target == "x86_64-apple-ios" |
| 15 |
|| target == "i386-apple-ios" |
| 16 |
|| target == "aarch64-apple-ios-sim" |
| 17 |
{ |
| 18 |
"iphonesimulator" |
| 19 |
} else if target == "aarch64-apple-ios" |
| 20 |
|| target == "armv7-apple-ios" |
| 21 |
|| target == "armv7s-apple-ios" |
| 22 |
{ |
| 23 |
"iphoneos" |
| 24 |
} else { |
| 25 |
unreachable!(); |
| 26 |
}; |
| 27 |
let output = Command::new("xcrun") |
| 28 |
.args(&["--sdk", sdk, "--show-sdk-path"]) |
| 29 |
.output()? |
| 30 |
.stdout; |
| 31 |
let prefix_str = std::str::from_utf8(&output).expect("invalid output from `xcrun`"); |
| 32 |
Ok(prefix_str.trim_end().to_string()) |
| 33 |
} |
| 34 |
|
| 35 |
fn build(sdk_path: Option<&str>, target: &str) { |
| 36 |
// Generate one large set of bindings for all frameworks. |
| 37 |
// |
| 38 |
// We do this rather than generating a module per framework as some frameworks depend on other |
| 39 |
// frameworks and in turn share types. To ensure all types are compatible across each |
| 40 |
// framework, we feed all headers to bindgen at once. |
| 41 |
// |
| 42 |
// Only link to each framework and include their headers if their features are enabled and they |
| 43 |
// are available on the target os. |
| 44 |
|
| 45 |
use std::env; |
| 46 |
use std::path::PathBuf; |
| 47 |
|
| 48 |
let mut headers: Vec<&'static str> = vec![]; |
| 49 |
|
| 50 |
#[cfg(feature = "audio_unit")] |
| 51 |
{ |
| 52 |
// Since iOS 10.0 and macOS 10.12, all the functionality in AudioUnit |
| 53 |
// moved to AudioToolbox, and the AudioUnit headers have been simple |
| 54 |
// wrappers ever since. |
| 55 |
if target.contains("apple-ios") { |
| 56 |
// On iOS, the AudioUnit framework does not have (and never had) an |
| 57 |
// actual dylib to link to, it is just a few header files. |
| 58 |
// The AudioToolbox framework contains the symbols instead. |
| 59 |
println!("cargo:rustc-link-lib=framework=AudioToolbox"); |
| 60 |
} else { |
| 61 |
// On macOS, the symbols are present in the AudioToolbox framework, |
| 62 |
// but only on macOS 10.12 and above. |
| 63 |
// |
| 64 |
// However, unlike on iOS, the AudioUnit framework on macOS |
| 65 |
// contains a dylib with the desired symbols, that we can link to |
| 66 |
// (in later versions just re-exports from AudioToolbox). |
| 67 |
println!("cargo:rustc-link-lib=framework=AudioUnit"); |
| 68 |
} |
| 69 |
headers.push("AudioUnit/AudioUnit.h"); |
| 70 |
} |
| 71 |
|
| 72 |
#[cfg(feature = "audio_toolbox")] |
| 73 |
{ |
| 74 |
println!("cargo:rustc-link-lib=framework=AudioToolbox"); |
| 75 |
headers.push("AudioToolbox/AudioToolbox.h"); |
| 76 |
} |
| 77 |
|
| 78 |
#[cfg(feature = "core_audio")] |
| 79 |
{ |
| 80 |
println!("cargo:rustc-link-lib=framework=CoreAudio"); |
| 81 |
|
| 82 |
if target.contains("apple-ios") { |
| 83 |
headers.push("CoreAudio/CoreAudioTypes.h"); |
| 84 |
} else { |
| 85 |
headers.push("CoreAudio/CoreAudio.h"); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#[cfg(feature = "io_kit_audio")] |
| 90 |
{ |
| 91 |
assert!(target.contains("apple-darwin")); |
| 92 |
println!("cargo:rustc-link-lib=framework=IOKit"); |
| 93 |
headers.push("IOKit/audio/IOAudioTypes.h"); |
| 94 |
} |
| 95 |
|
| 96 |
#[cfg(feature = "open_al")] |
| 97 |
{ |
| 98 |
println!("cargo:rustc-link-lib=framework=OpenAL"); |
| 99 |
headers.push("OpenAL/al.h"); |
| 100 |
headers.push("OpenAL/alc.h"); |
| 101 |
} |
| 102 |
|
| 103 |
#[cfg(all(feature = "core_midi"))] |
| 104 |
{ |
| 105 |
if target.contains("apple-darwin") { |
| 106 |
println!("cargo:rustc-link-lib=framework=CoreMIDI"); |
| 107 |
headers.push("CoreMIDI/CoreMIDI.h"); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
println!("cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS"); |
| 112 |
// Get the cargo out directory. |
| 113 |
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("env variable OUT_DIR not found")); |
| 114 |
|
| 115 |
// Begin building the bindgen params. |
| 116 |
let mut builder = bindgen::Builder::default(); |
| 117 |
|
| 118 |
// See https://github.com/rust-lang/rust-bindgen/issues/1211 |
| 119 |
// Technically according to the llvm mailing list, the argument to clang here should be |
| 120 |
// -arch arm64 but it looks cleaner to just change the target. |
| 121 |
let target = if target == "aarch64-apple-ios" { |
| 122 |
"arm64-apple-ios" |
| 123 |
} else if target == "aarch64-apple-darwin" { |
| 124 |
"arm64-apple-darwin" |
| 125 |
} else { |
| 126 |
target |
| 127 |
}; |
| 128 |
builder = builder.size_t_is_usize(true); |
| 129 |
|
| 130 |
builder = builder.clang_args(&[&format!("--target={}", target)]); |
| 131 |
|
| 132 |
if let Some(sdk_path) = sdk_path { |
| 133 |
builder = builder.clang_args(&["-isysroot", sdk_path]); |
| 134 |
} |
| 135 |
if target.contains("apple-ios") { |
| 136 |
// time.h as has a variable called timezone that conflicts with some of the objective-c |
| 137 |
// calls from NSCalendar.h in the Foundation framework. This removes that one variable. |
| 138 |
builder = builder.blocklist_item("timezone"); |
| 139 |
builder = builder.blocklist_item("objc_object"); |
| 140 |
} |
| 141 |
|
| 142 |
// bindgen produces alignment tests that cause undefined behavior in some cases. |
| 143 |
// This seems to happen across all apple target tripples :/. |
| 144 |
// https://github.com/rust-lang/rust-bindgen/issues/1651 |
| 145 |
builder = builder.layout_tests(false); |
| 146 |
|
| 147 |
let meta_header: Vec<_> = headers |
| 148 |
.iter() |
| 149 |
.map(|h| format!("#include <{}>\n", h)) |
| 150 |
.collect(); |
| 151 |
|
| 152 |
builder = builder.header_contents("coreaudio.h", &meta_header.concat()); |
| 153 |
|
| 154 |
// Generate the bindings. |
| 155 |
builder = builder.trust_clang_mangling(false).derive_default(true); |
| 156 |
|
| 157 |
let bindings = builder.generate().expect("unable to generate bindings"); |
| 158 |
|
| 159 |
// Write them to the crate root. |
| 160 |
bindings |
| 161 |
.write_to_file(out_dir.join("coreaudio.rs")) |
| 162 |
.expect("could not write bindings"); |
| 163 |
} |
| 164 |
|
| 165 |
fn main() { |
| 166 |
let target = std::env::var("TARGET").unwrap(); |
| 167 |
if !(target.contains("apple-darwin") || target.contains("apple-ios")) { |
| 168 |
panic!("coreaudio-sys requires macos or ios target"); |
| 169 |
} |
| 170 |
|
| 171 |
let directory = sdk_path(&target).ok(); |
| 172 |
build(directory.as_ref().map(String::as_ref), &target); |
| 173 |
} |