unit-wasm-sys 0.1.0

Native bindings for the libunit-wasm C API
Documentation
// buildscript for the unit-wasm-sys crate.

// Check if the C-API was already build using make! If so DO NOT DOWNLOAD and build!
// build the C API first. Then, create the bindings and the crate

// 1. Check Pre-Req of the bindings if not present download

use std::env;
use std::path::{Path, PathBuf};

fn main() {
    // Is `WASI_SYSROOT` environment variable set?
    let wasi_sysroot = env::var("WASI_SYSROOT").map_or(false, |s| s != "0");

    if wasi_sysroot {
        // WASI SYSROOT Path provided! Checking..
        let path = env::var("WASI_SYSROOT").unwrap();
        println!("{}", path);

        if !Path::new(&path).exists() {
            // perform download
        }
    }
    // tell rustc where to find the libunit-wasm library.
    let libunit_wasm_dir = "libunit-wasm";

    let dir = env::var("CARGO_MANIFEST_DIR").unwrap();


    // Some generics
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=wrapper.h");

    // The rustc-link-search tells Cargo to pass the `-L` flag to the compiler to add a directory to the library
    // search plugin. The `native` keyword means "only looking for `native libraries` in this directory".
    // @todo: Build libunit-wasm.a and .o to OUTDIR from this buildscript.
    // println!("cargo:rustc-link-search=native={}", Path::new(&dir).join(libunit_wasm_dir).display());
    println!("cargo:rustc-link-search=native={}", Path::new(&dir).join(libunit_wasm_dir).display());

    // the rustc-link-lib tells Cargo to link the given library using the compiler's `-l` flag. This is needed to
    // start building our FFIs.
    println!("cargo:rustc-link-lib=static=unit-wasm");


    generate_bindings();
}

fn generate_bindings() {
    let bindings = bindgen::Builder::default()
        // The in put header file.
        .header("libunit-wasm/include/unit/unit-wasm.h")
        .allowlist_function("^luw_.*")
        .allowlist_var("^luw_.*")
        .allowlist_type("^luw_.*")
        .generate()
        .expect("Unable to generate bindings");

    let out_dir_env = env::var("OUT_DIR").expect("The required environment variable OUT_DIR was not set");
    let out_path = PathBuf::from(out_dir_env);

    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}