libxml2-rs 0.1.1

Rust bindings for the libxml2 C library
Documentation
use cmake::Config;
use std::env;
use std::path::{Path, PathBuf};


fn fetch_library_path() -> Option<String> {
    if let Ok(lib_path) = env::var("LIBXML2_LIBRARY_DIR") && Path::new(&lib_path).exists() {
            return Some(lib_path.to_string());
    }

    None
}

fn fetch_include_path() -> Option<String> {
    if let Ok(lib_path) = env::var("LIBXML2_INCLUDE_DIR") && Path::new(&lib_path).exists() {
            return Some(lib_path.to_string());
    }

    let lib_path = "/usr/include/libxml2";
    if Path::new(lib_path).exists() {
        return Some(lib_path.to_string());
    }

    None
}

fn generate_config() -> Config {
    let mut config = Config::new("libxml2_interface");

    if let Ok(cmake_toolchain_file) = env::var("CMAKE_TOOLCHAIN_FILE") {
        config.define("CMAKE_TOOLCHAIN_FILE", cmake_toolchain_file);
    }

    if let Ok(out_dir) = env::var("OUT_DIR") {
        config.out_dir(out_dir);
    }

    config
}

fn main() {
    let path = generate_config().build(); 

    println!("cargo:rustc-link-search=native={:?}", path);
    if let Some(lib_path) = fetch_library_path() {
        println!("cargo:rustc-link-search={}", lib_path);
    }

    
    #[cfg(not(target_os = "windows"))]
    println!("cargo:rustc-link-lib=xml2");
    #[cfg(target_os =  "windows")]
    println!("cargo:rustc-link-lib=libxml2");
    

    let callbacks = bindgen::CargoCallbacks::new();

    let builder = bindgen::builder()
        .header("libxml2_interface/wrapper.h")
        .parse_callbacks(Box::new(callbacks));

    let bindings = match fetch_include_path() {
        Some(lib_path) => builder.clang_arg(format!("-I{}", lib_path)),
        None => builder,
    }
    .generate()
    .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!");
}