use std::env;
use std::path::PathBuf;
fn create_file_lib(out_dir: &PathBuf) {
let include_dir = PathBuf::from("include");
let out_path = out_dir.join("clist_bindings.rs");
let header_path = include_dir.join("clist.h");
let header_path_str = header_path.to_str().unwrap();
let obj_path = out_dir.join("clist.o");
let lib_path = out_dir.join("libclist.a");
let impl_path = include_dir.join("clist.c");
println!("cargo::rustc-link-search={}", out_dir.to_str().unwrap());
println!("cargo::rustc-link-search={}", include_dir.to_str().unwrap());
println!("cargo::rustc-link-lib={}", "clist");
if !std::process::Command::new("clang")
.arg("-c")
.arg("-o")
.arg(&obj_path)
.arg(format!("-I{}", include_dir.to_str().unwrap()))
.arg(&impl_path)
.output()
.expect("Could not Run clang succesfully")
.status
.success()
{
panic!("Could not Compile: {}, in {}", impl_path.to_str().unwrap(), include_dir.to_str().unwrap());
}
if !std::process::Command::new("ar")
.arg("rcs")
.arg(&lib_path)
.arg(&obj_path)
.output()
.expect("Could not create static library")
.status
.success()
{
panic!("Could not create static library: {}", lib_path.to_str().unwrap());
}
let bindings = bindgen::Builder::default()
.header(header_path_str)
.clang_arg("-lclist")
.allowlist_type("CList")
.allowlist_function("CList_init")
.allowlist_function("CList_free")
.allowlist_function("CList_append")
.allowlist_function("CList_extend")
.allowlist_function("CList_push")
.allowlist_function("CList_remove")
.allowlist_function("CList_pop")
.allowlist_function("CList_to_array")
.allowlist_function("CList_to_array_new")
.allowlist_function("CList_size")
.allowlist_function("CList_len")
.allowlist_function("CList_lock")
.allowlist_function("CList_unlock")
.allowlist_function("CList_is_locked")
.generate()
.expect("Could not generate bindings");
bindings.write_to_file(out_path).expect("Could not write bindings");
}
fn get_out_dir() -> PathBuf {
let out_dir = env::var("OUT_DIR").expect("OUT_DIR must be set");
if !out_dir.is_empty() {
let out_path_buf = PathBuf::from(out_dir);
return out_path_buf;
} else {
let out_dir_raw = env::temp_dir();
let out_dir_str = out_dir_raw.to_str().expect("Could not get out_dir");
env::set_var("OUT_DIR", out_dir_str);
let out_path_buf = PathBuf::from(out_dir_str);
return out_path_buf;
}
}
fn main() {
let out_dir = get_out_dir();
create_file_lib(&out_dir);
}