snaphu-sys 0.1.2

Low-level Rust bindings to the SNAPHU C library
Documentation
use std::path::Path;

fn main() {
    let src_dir = Path::new("src/c")
        // Canonicalize the path as `rustc-link-search` requires an absolute
        // path.
        .canonicalize()
        .expect("cannot canonicalize path");

    let sources = [
        "snaphu.c",
        "snaphu_cost.c",
        "snaphu_cs2.c",
        "snaphu_io.c",
        "snaphu_solver.c",
        "snaphu_tile.c",
        "snaphu_util.c",
    ];

    let header = "snaphu.h";

    let mut build = cc::Build::new();

    build.include(&src_dir).warnings(false);

    for file in &sources {
        build.file(src_dir.join(file));
        println!("cargo:rerun-if-changed={}", src_dir.join(file).display());
    }

    println!("cargo:rerun-if-changed={}", src_dir.join(header).display());

    build.compile("snaphu-sys-cc");

    // libm is required on Unix-like platforms
    if cfg!(unix) {
        println!("cargo:rustc-link-lib=m");
    }

    // Generate bindings to SNAPHU
    let bindings = bindgen::Builder::default()
        .header(src_dir.join(header).to_str().unwrap())
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .generate()
        .expect("Unable to generate bindings");

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