extern crate bindgen;
extern crate metadeps;
use env::VarError;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn format_write(builder: bindgen::Builder) -> String {
builder
.generate()
.unwrap()
.to_string()
.replace("/**", "/*")
.replace("/*!", "/*")
}
fn main() {
let use_static = env::var("CARGO_FEATURE_STATIC").is_ok();
let headers = env::var("FFMS_INCLUDE_DIR").map(|value| {
let include_dir = PathBuf::from(value.as_str());
if !include_dir.is_dir() {
panic!("The specified include directory '{}' in FFMS_INCLUDE_DIR is not valid.", value);
}
let lib_dir = env::var("FFMS_LIB_DIR").and_then(|lib_dir| {
match PathBuf::from(lib_dir) {
lib_dir if lib_dir.is_dir() => Ok(lib_dir),
_ => Err(VarError::NotPresent)
}
}).expect("FFMS_LIB_DIR is not set or the specified directory is not valid.");
#[cfg(windows)]
if !use_static {
let cargo_output_dir = env::var("OUT_DIR").expect("Unable to get OUT_DIR");
let linkable_dll: PathBuf = [cargo_output_dir.as_ref(), "..", "..", "..", "ffms2.dll"].iter().collect();
if !linkable_dll.is_file() {
let dll_file = lib_dir.as_path().join("ffms2.dll");
if !dll_file.is_file() {
panic!("Unable to find the 'ffms2.dll' in 'FFMS_LIB_DIR' ('{}').", lib_dir.display());
}
std::fs::copy(dll_file, linkable_dll).expect("Copying DLL failed");
}
}
if use_static {
println!("cargo:rustc-link-lib=static=ffms2");
} else {
println!("cargo:rustc-link-lib=dylib=ffms2");
}
println!("cargo:rustc-link-search=native={}", lib_dir.to_string_lossy());
vec![include_dir]
}).unwrap_or_else(|_| {
let libs = metadeps::probe().expect("Unable to query include paths using pkg-config. Consider setting the environment variable FFMS_INCLUDE_DIR and FFMS_LIB_DIR explicitely.");
libs.get("ffms2").unwrap().include_paths.clone()
});
let mut builder = bindgen::builder().header("data/ffms.h");
for header in headers {
builder = builder.clang_arg("-I").clang_arg(header.to_str().unwrap());
}
builder = builder.default_enum_style(bindgen::EnumVariation::Rust {
non_exhaustive: false,
});
let s = format_write(builder);
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut file = File::create(out_path.join("ffms2.rs")).unwrap();
let _ = file.write(s.as_bytes());
}