fn main() {
use std::env;
use std::path::{Path, PathBuf};
let nrfxlib_path = "./third_party/nordic/nrfxlib";
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{}", nrfxlib_path))
.clang_arg("-I./include")
.clang_arg("-I./third_party/nordic/nrfxlib/crypto/nrf_cc310_platform/include")
.clang_arg("-I./third_party/nordic/nrfxlib/crypto/nrf_oberon")
.clang_arg("-nostdinc")
.clang_arg("-target")
.clang_arg("arm")
.clang_arg("-mcpu=cortex-m33")
.clang_arg("-mfloat-abi=soft")
.use_core()
.ctypes_prefix("ctypes")
.whitelist_function("nrf_.*")
.whitelist_function("bsd_.*")
.whitelist_type("nrf_.*")
.whitelist_var("NRF_.*")
.whitelist_var("BSD_.*")
.rustfmt_bindings(true)
.generate()
.expect("Unable to generate bindings");
let mut rust_source = bindings.to_string();
rust_source = rust_source.replace("#[doc = \"@{*/\"]", "");
let re = regex::Regex::new("\" \\s+- ").unwrap();
rust_source = re.replace_all(&rust_source, "\" * ").into();
let re = regex::Regex::new(r"\s*@param\s+(?P<var>[A-Za-z0-9_]+)\s+").unwrap();
rust_source = re.replace_all(&rust_source, " * `$var` - ").into();
let re =
regex::Regex::new(r"\s*@param\[(out|in|inout|in,out)\](\\t|\s+)(?P<var>[A-Za-z0-9_]+)\s+")
.unwrap();
rust_source = re.replace_all(&rust_source, " * `$var` - ").into();
let re = regex::Regex::new(r"@[cp]\s+(?P<var>[A-Za-z0-9_\(\)]+)").unwrap();
rust_source = re.replace_all(&rust_source, " * `$var` - ").into();
let re = regex::Regex::new(r"\\\\[cp]\s+(?P<var>[A-Za-z0-9_\(\)]+)").unwrap();
rust_source = re.replace_all(&rust_source, "`$var`").into();
let re = regex::Regex::new(r"\\\\ref\s+(?P<var>[A-Za-z0-9_\(\)]+)").unwrap();
rust_source = re.replace_all(&rust_source, "`$var`").into();
rust_source = rust_source.replace("\" @remark", "\" NB: ");
rust_source = rust_source.replace("\"@brief", "\"");
rust_source = rust_source.replace("\" @brief", "\" ");
rust_source = rust_source.replace("\"@detail", "\"");
rust_source = rust_source.replace("\" @detail", "\" ");
rust_source = rust_source.replace("@name ", "# ");
rust_source = rust_source.replace("@return ", "Returns ");
rust_source = rust_source.replace("@retval ", "Returns ");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");
std::fs::write(out_path, rust_source).expect("Couldn't write updated bindgen output");
println!(
"cargo:rustc-link-search={}",
Path::new(&nrfxlib_path)
.join("bsdlib/lib/cortex-m33/soft-float")
.display()
);
println!(
"cargo:rustc-link-search={}",
Path::new(&nrfxlib_path)
.join("crypto/nrf_oberon/lib/cortex-m33/soft-float")
.display()
);
println!("cargo:rustc-link-lib=static=bsd_nrf9160_xxaa");
println!("cargo:rustc-link-lib=static=oberon_3.0.7");
}