extern crate pkg_config;
use std::path::Path;
use std::env;
#[cfg(all(unix, not(target_os = "linux")))]
use unix_platform as platform;
fn main() {
if env::var("PORTAUDIO_ONLY_STATIC").is_err() {
if pkg_config::Config::new().atleast_version("19").find("portaudio-2.0").is_ok() {
return;
}
}
build();
}
fn build() {
let out_dir_str = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir_str);
let static_lib = out_dir.join("lib/libportaudio.a");
if let Err(_) = ::std::fs::metadata(static_lib) {
platform::download();
platform::build(out_dir);
}
platform::print_libs(out_dir);
}
#[allow(dead_code)]
mod unix_platform {
use std::process::Command;
use std::path::Path;
use std::env;
pub const PORTAUDIO_URL: &'static str = "http://www.portaudio.com/archives/pa_stable_v19_20140130.tgz";
pub const PORTAUDIO_TAR: &'static str = "pa_stable_v19_20140130.tgz";
pub const PORTAUDIO_FOLDER: &'static str = "portaudio";
pub fn download() {
match Command::new("curl").arg(PORTAUDIO_URL).arg("-O").output() {
Ok(_) => {},
Err(e) => panic!("{}", e)
}
}
pub fn build(out_dir: &Path) {
match Command::new("tar").arg("xvf").arg(PORTAUDIO_TAR).output() {
Ok(_) => {},
Err(e) => panic!("{}", e)
}
match env::set_current_dir(PORTAUDIO_FOLDER) {
Ok(_) => {},
Err(e) => panic!("{}", e)
}
Command::new("./configure")
.args(&["--disable-shared", "--enable-static"]) .args(&["--prefix", out_dir.to_str().unwrap()]) .arg("--with-pic") .output()
.unwrap();
match Command::new("make").output() {
Ok(_) => {},
Err(e) => panic!("{}", e)
}
match Command::new("make").arg("install").output() {
Ok(_) => {},
Err(e) => panic!("{}", e)
}
match env::set_current_dir("..") {
Ok(_) => {},
Err(e) => panic!("{}", e)
}
match Command::new("rm").arg("-rf").args(&[PORTAUDIO_TAR, PORTAUDIO_FOLDER]).output() {
Ok(_) => {},
Err(e) => panic!("{}", e)
}
}
pub fn print_libs(out_dir: &Path) {
let out_str = out_dir.to_str().unwrap();
println!("cargo:rustc-flags=-L native={}/lib -l static=portaudio", out_str);
}
}
#[cfg(target_os = "linux")]
mod platform {
use pkg_config;
use std::process::Command;
use super::unix_platform;
use std::path::Path;
pub fn download() {
match Command::new("wget").arg(unix_platform::PORTAUDIO_URL).output() {
Ok(_) => {},
Err(e) => panic!("{}", e)
}
}
pub fn build(out_dir: &Path) {
unix_platform::build(out_dir);
}
pub fn print_libs(out_dir: &Path) {
let portaudio_pc_file = out_dir.join("lib/pkgconfig/portaudio-2.0.pc");
let portaudio_pc_file = portaudio_pc_file.to_str().unwrap();
pkg_config::Config::new().statik(true).find(portaudio_pc_file).unwrap();
}
}
#[cfg(windows)]
mod platform {
use std::path::Path;
use std::io::{self, Write};
const PORTAUDIO_DOWNLOAD_URL: &'static str = "http://www.portaudio.com";
fn print_lib_url() {
let msg = format!("Don't know how to build portaudio on Windows yet. Sources and build instructions available at: {}", PORTAUDIO_DOWNLOAD_URL);
io::stderr().write(msg.as_bytes()).unwrap();
}
pub fn download() {
print_lib_url();
}
pub fn build(_: &Path) {
print_lib_url();
}
pub fn print_libs(_: &Path) {
print_lib_url();
}
}