use std::env;
use std::ffi::OsString;
const MINIMUM_MEOS_VERSION: &str = "1.1.0";
fn detect_meos_via_pkg_config() -> Result<pkg_config::Library, pkg_config::Error> {
use pkg_config::Config;
Config::new()
.atleast_version(MINIMUM_MEOS_VERSION)
.probe("meos")
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let include_path = if cfg!(feature = "bundled") {
let meos_path = std::env::var("DEP_MEOSSRC_SEARCH").unwrap();
println!("cargo:rustc-link-search={meos_path}/lib");
println!("cargo:rustc-link-lib=meos");
format!("{meos_path}/include")
} else {
let pk = detect_meos_via_pkg_config();
match pk {
Ok(meos) => meos.include_paths[0].clone().display().to_string(),
Err(pkg_config_err) => {
if matches!(pkg_config_err, pkg_config::Error::Command { cause, .. } if cause.kind() == std::io::ErrorKind::NotFound)
{
panic!("Could not find `pkg-config` in your path. Please install it before running meos-sys-bind.");
}
if cfg!(feature = "v1_1") {
let default_include_path = String::from("/usr/local/lib/");
let lib_dir_env = env::var_os("MEOS_LIB_DIR")
.map(OsString::into_string)
.transpose()
.ok()
.flatten()
.unwrap_or(default_include_path.clone());
println!("cargo:rustc-link-search={lib_dir_env}");
println!("cargo:rustc-link-lib=dylib=meos");
default_include_path
} else {
panic!("Could not detect MEOS using pkg-config.");
}
}
}
};
#[cfg(feature = "bindgen")]
generate_bindings(include_path.into()).unwrap();
#[cfg(not(feature = "bindgen"))]
let _ = include_path;
}
#[cfg(feature = "bindgen")]
fn generate_bindings(include_path: std::path::PathBuf) -> Result<(), Box<dyn std::error::Error>> {
let bindings = bindgen::Builder::default()
.clang_arg(format!("-I{}", include_path.to_string_lossy()))
.header("wrapper.h")
.generate()
.expect("Unable to generate bindings");
let out_path = std::path::PathBuf::from(env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path.join("bindings.rs"))?;
Ok(())
}