use std::env;
use std::path::PathBuf;
#[cfg(feature = "download-lib")]
#[path = "build-utils/downloader.rs"]
mod downloader;
fn main() {
println!("cargo:rerun-if-changed=include/aic.h");
println!("cargo:rerun-if-env-changed=AIC_LIB_PATH");
generate_bindings();
if env::var("DOCS_RS").is_ok() {
return;
}
#[cfg(feature = "download-lib")]
let lib_path = {
let downloaded_path = download_lib();
downloaded_path.join("lib")
};
#[cfg(not(feature = "download-lib"))]
let lib_path = PathBuf::from(
env::var("AIC_LIB_PATH").expect("Enable feature `download-lib` or use a local library by setting the environment variable `AIC_LIB_PATH`"),
);
let lib_name = "aic";
println!("cargo:rustc-link-search=native={}", lib_path.display());
println!("cargo:rustc-link-lib=static={lib_name}");
add_platform_specific_libs();
}
fn add_platform_specific_libs() {
if cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=framework=CoreFoundation");
println!("cargo:rustc-link-lib=framework=Security");
} else if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=advapi32");
println!("cargo:rustc-link-lib=bcrypt");
println!("cargo:rustc-link-lib=kernel32");
println!("cargo:rustc-link-lib=ws2_32");
println!("cargo:rustc-link-lib=oleaut32");
println!("cargo:rustc-link-lib=crypt32");
} else if cfg!(target_os = "linux") {
println!("cargo:rustc-link-lib=pthread");
println!("cargo:rustc-link-lib=dl");
println!("cargo:rustc-link-lib=rt");
}
}
#[cfg(feature = "download-lib")]
fn download_lib() -> PathBuf {
use downloader::Downloader;
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let downloader = Downloader::new(&out_dir);
downloader.download()
}
fn generate_bindings() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let header_path = manifest_dir.join("include").join("aic.h");
let bindings = bindgen::Builder::default()
.header(header_path.to_str().unwrap())
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.constified_enum_module("AicErrorCode")
.constified_enum_module("AicProcessorParameter")
.constified_enum_module("AicVadParameter")
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
println!("cargo:rerun-if-changed={}", header_path.display());
}