nosv-sys 0.1.0

Low-level Rust FFI bindings to the nOS-V runtime API
use std::{env, path::PathBuf};

const MIN_NOSV_VERSION: &str = "4.1.0";

fn main() {
    println!("cargo:rerun-if-changed=wrapper.h");
    println!("cargo:rerun-if-env-changed=PKG_CONFIG_PATH");
    println!("cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR");
    println!("cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR");
    println!("cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC");
    println!("cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC");

    let nosv = pkg_config::Config::new()
        .atleast_version(MIN_NOSV_VERSION)
        .cargo_metadata(true)
        .probe("nos-v")
        .expect("nOS-V was not found; set PKG_CONFIG_PATH to the directory containing nos-v.pc");

    println!("cargo:metadata=nosv_version={}", nosv.version);
    println!("cargo:metadata=nosv_min_version={MIN_NOSV_VERSION}");

    let mut builder = bindgen::Builder::default()
        .header("wrapper.h")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .allowlist_function("^nosv_.*")
        .allowlist_type("^nosv_.*")
        .allowlist_var("^(NOSV_|SIZEOF_NOSV_).*")
        .derive_default(true)
        .layout_tests(true)
        .generate_comments(true);

    for include_path in &nosv.include_paths {
        builder = builder.clang_arg(format!("-I{}", include_path.display()));
    }

    let bindings = builder
        .generate()
        .expect("bindgen failed to generate nOS-V bindings");

    let output = PathBuf::from(env::var_os("OUT_DIR").expect("Cargo did not define OUT_DIR"));

    bindings
        .write_to_file(output.join("bindings.rs"))
        .expect("failed to write nOS-V bindings");
}