umka-sys 0.1.0

low level FFI bindings for umka
Documentation
#![allow(clippy::expect_used)]
use fs_extra::dir::{CopyOptions, copy};
use std::{env, path::PathBuf, process::Command};

fn main() {
    let umka_path = PathBuf::from("umka-lang")
        .canonicalize()
        .expect("failed to canonicalize umka path");

    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR should be set"));

    // we have to build inside OUT_DIR, so copy umka's source into place & build it
    let umka_copy = out_dir.join("umka-lang");
    if !umka_copy.exists() {
        let mut options = CopyOptions::new();
        options.copy_inside = true;
        copy(&umka_path, &umka_copy, &options)
            .expect("we should be able to copy umka-lang into OUT_DIR");
    }

    if !Command::new("make")
        .current_dir(&umka_copy)
        .arg("static")
        .status()
        .expect("failed to spawn make")
        .success()
    {
        panic!("make: failed to build umka");
    }

    // regular bindgen stuff
    let build = umka_copy.join("build");
    println!(
        "cargo:rustc-link-search={}",
        build
            .to_str()
            .expect("we should be able to render the build path as a string")
    );
    println!("cargo:rustc-link-lib=static=umka");

    let bindings = bindgen::Builder::default()
        .header(
            umka_copy
                .join("src/umka_api.h")
                .to_str()
                .expect("we should be able to render the header path as a string"),
        )
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .layout_tests(true)
        .generate()
        .expect("failed to generate umka bindings");

    bindings
        .write_to_file(out_dir.join("bindings.rs"))
        .expect("failed to write umka bindings");
}