cplex-rs-sys 0.1.3

FFI bindings to cplex
use glob::glob;
use std::env;
use std::path::PathBuf;

fn main() {
    #[cfg(docsrs)]
    let cplex_include_path = Path::from(env!("CARGO_MANIFEST_DIR"))
        .join("include")
        .join("22010000");

    #[cfg(not(docsrs))]
    let cplex_include_path = {
        let cplex_installation_path = env::var("CPLEX_PATH")
        .map(PathBuf::from)
        .unwrap_or_else(|_| {
            glob("/opt/ibm/ILOG/*/cplex")
                .expect("Invalid glob pattern")
                .filter_map(|path| path.ok())
                .next()
                .expect("No valid CPLEX installation path found. Please set the env variable 'CPLEX_PATH' with the CPLEX installation directory or install CPLEX in the default location.")
        });

        // For now we support only static linking on linux x86-64
        let cplex_lib_path = cplex_installation_path.join("lib/x86-64_linux/static_pic");

        // Tell cargo to look for shared libraries in the specified directory
        println!(
            "cargo:rustc-link-search={}",
            cplex_lib_path.as_os_str().to_string_lossy()
        );

        // Tell cargo to tell rustc to link the system cplex
        // static library.
        println!("cargo:rustc-link-lib=cplex");

        cplex_installation_path.join("include")
    };

    // The bindgen::Builder is the main entry point
    // to bindgen, and lets you build up options for
    // the resulting bindings.
    let bindings = bindgen::Builder::default()
        // The input header we would like to generate
        // bindings for.
        .header("wrapper.h")
        .clang_arg(format!(
            "-F{}",
            cplex_include_path.as_os_str().to_string_lossy()
        ))
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .allowlist_item("CPX.*")
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}