extern crate cc;
extern crate cmake;
extern crate pkg_config;
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::process::Command;
macro_rules! t {
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}
fn main() {
let has_pkgconfig = Command::new("pkg-config").output().is_ok();
if env::var("LIBSOUNDIO_SYS_USE_PKG_CONFIG").is_ok() {
assert!(has_pkgconfig, "pkg-config required");
assert!(
pkg_config::find_library("libsoundio").is_ok(),
"Can't run pkg-config"
);
return;
}
if !Path::new("libsoundio/.git").exists() {
let _ = Command::new("git")
.args(&["submodule", "update", "--init"])
.status();
}
let target = env::var("TARGET").unwrap();
let host = env::var("HOST").unwrap();
let windows = target.contains("windows");
let mut cfg = cmake::Config::new("libsoundio");
if windows && !host.contains("windows") {
let c_compiler = cc::Build::new().cargo_metadata(false).get_compiler();
let exe = c_compiler.path();
let path = env::var_os("PATH").unwrap_or(OsString::new());
let exe = env::split_paths(&path)
.map(|p| p.join(&exe))
.find(|p| p.exists());
if let Some(exe) = exe {
if let Some(name) = exe.file_name().and_then(|e| e.to_str()) {
let name = name.replace("gcc", "dlltool");
let dlltool = exe.with_file_name(name);
cfg.define("DLLTOOL", &dlltool);
}
}
}
let _ = fs::remove_dir_all(env::var("OUT_DIR").unwrap());
t!(fs::create_dir_all(env::var("OUT_DIR").unwrap()));
let dst = cfg
.define("BUILD_DYNAMIC_LIBS", "OFF")
.define("BUILD_STATIC_LIBS", "ON")
.define("BUILD_EXAMPLE_PROGRAMS", "OFF")
.define("BUILD_TESTS", "OFF")
.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
if target.contains("windows") {
println!("cargo:rustc-link-lib=ole32");
}
println!("cargo:rustc-link-lib=static=soundio");
if target.contains("apple") {
println!("cargo:rustc-link-lib=framework=AudioToolbox");
println!("cargo:rustc-link-lib=framework=CoreAudio");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
}
}