use super::utils;
use bindgen;
use std::fs;
pub fn generate_bindings() -> Result<(), String> {
let header_path = utils::get_capi_path();
dbg!(&header_path);
println!("cargo:rerun-if-changed={}", header_path.display());
let content: String = fs::read_to_string(header_path.clone()).unwrap();
let bindings = bindgen::Builder::default()
.header_contents("CApi.hpp", &content) .clang_args(&[
format!("-I{}", utils::get_llvm_header_path().display())
])
.allowlist_type("CConcreteType")
.rustified_enum("CConcreteType")
.allowlist_type("CDerivativeMode")
.rustified_enum("CDerivativeMode")
.allowlist_type("CDIFFE_TYPE")
.rustified_enum("CDIFFE_TYPE")
.allowlist_type("LLVMContextRef")
.allowlist_type("CTypeTreeRef")
.allowlist_type("EnzymeTypeAnalysisRef")
.allowlist_function("EnzymeNewTypeTree")
.allowlist_function("EnzymeNewTypeTreeCT")
.allowlist_function("EnzymeFreeTypeTree")
.allowlist_function("EnzymeMergeTypeTree")
.allowlist_function("EnzymeTypeTreeOnlyEq")
.allowlist_function("EnzymeMergeTypeTree")
.allowlist_function("EnzymeTypeTreeShiftIndiciesEq")
.allowlist_function("EnzymeTypeTreeToString")
.allowlist_function("EnzymeTypeTreeToStringFree")
.allowlist_function("EnzymeSetCLBool")
.allowlist_function("EnzymeSetCLInteger")
.allowlist_function("CreateTypeAnalysis")
.allowlist_function("ClearTypeAnalysis")
.allowlist_function("FreeTypeAnalysis")
.allowlist_function("CreateEnzymeLogic")
.allowlist_function("ClearEnzymeLogic")
.allowlist_function("FreeEnzymeLogic")
.allowlist_type("LLVMOpaqueModule")
.allowlist_function("EnzymeCreatePrimalAndGradient")
.allowlist_function("EnzymeCreateAugmentedPrimal")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate();
let bindings = match bindings {
Ok(v) => v,
Err(_) => {
return Err(format!(
"Unable to generate bindings from {}.",
header_path.display()
))
}
};
let out_file = utils::get_bindings_string();
if out_file.exists() {
fs::remove_file(out_file.clone()).unwrap();
}
let result = bindings.write_to_file(out_file.clone());
match result {
Ok(_) => Ok(()),
Err(_) => Err(format!(
"Couldn't write bindings to {}.",
out_file.display()
)),
}
}