#[cfg(not(any(
feature = "nrf9160",
feature = "nrf9120",
feature = "nrf9151",
feature = "nrf9161",
)))]
compile_error!(
"No chip feature selected. You must selected exactly one of the following features:
nrf9160,
nrf9151,
nrf9161,
"
);
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_cc310_mbedcrypto/include")
.clang_arg("-I./third_party/nordic/nrfxlib/crypto/nrf_oberon")
.clang_arg("-I./third_party/nordic/nrfxlib/nrf_modem/include")
.clang_arg("-nostdinc")
.clang_arg("-target")
.clang_arg("arm")
.clang_arg("-mcpu=cortex-m33")
.clang_arg("-mfloat-abi=soft")
.use_core()
.allowlist_function("nrf_.*")
.allowlist_function("ocrypto_.*")
.allowlist_function("bsd_.*")
.allowlist_type("nrf_.*")
.allowlist_type("ocrypto_.*")
.allowlist_var("NRF_.*")
.allowlist_var("BSD_.*")
.allowlist_var("OCRYPTO_.*")
.formatter(bindgen::Formatter::Rustfmt)
.layout_tests(false)
.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 bindings_out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");
std::fs::write(bindings_out_path, rust_source).expect("Couldn't write updated bindgen output");
#[cfg(feature = "nrf9160")]
let libmodem_original_path = if cfg!(feature = "log") {
Path::new(&nrfxlib_path).join("nrf_modem/lib/cellular/nrf9160/hard-float/libmodem_log.a")
} else {
Path::new(&nrfxlib_path).join("nrf_modem/lib/cellular/nrf9160/hard-float/libmodem.a")
};
#[cfg(feature = "nrf9120")]
let libmodem_original_path = if cfg!(feature = "log") {
Path::new(&nrfxlib_path).join("nrf_modem/lib/cellular/nrf9120/hard-float/libmodem_log.a")
} else {
Path::new(&nrfxlib_path).join("nrf_modem/lib/cellular/nrf9120/hard-float/libmodem.a")
};
let libmodem_changed_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("libmodem.a");
#[cfg(feature = "arm-none-eabi-objcopy")]
{
let child = std::process::Command::new("arm-none-eabi-objcopy")
.arg("--decompress-debug-sections")
.arg(&libmodem_original_path)
.arg(&libmodem_changed_path)
.spawn()
.expect("Could not start `arm-none-eabi-objcopy`. Is it installed and available in your path?");
let child_result = child.wait_with_output().unwrap();
if !child_result.status.success() {
panic!("Something went wrong with `arm-none-eabi-objcopy`.");
}
}
#[cfg(feature = "llvm-objcopy")]
{
let tool_error = "Could not find `llvm-objcopy`. Is it installed? Use `rustup component add llvm-tools` to install it or select the `arm-none-eabi-objcopy` feature if you have that tool installed.";
let path = llvm_tools::LlvmTools::new()
.expect(tool_error)
.tool(&llvm_tools::exe("llvm-objcopy"))
.expect(tool_error);
let child = std::process::Command::new(path)
.arg("--strip-debug")
.arg(&libmodem_original_path)
.arg(&libmodem_changed_path)
.spawn()
.expect(tool_error);
let child_result = child.wait_with_output().unwrap();
if !child_result.status.success() {
panic!("Something went wrong with `llvm-objcopy`.");
}
}
println!(
"cargo:rustc-link-search={}",
libmodem_changed_path.parent().unwrap().display()
);
println!(
"cargo:rustc-link-search={}",
Path::new(&nrfxlib_path)
.join("crypto/nrf_oberon/lib/cortex-m33/hard-float")
.display()
);
println!(
"cargo:rustc-link-search={}",
Path::new(&nrfxlib_path)
.join("crypto/nrf_cc310_platform/lib/cortex-m33/hard-float/no-interrupts")
.display()
);
println!("cargo:rustc-link-lib=static=modem");
println!("cargo:rustc-link-lib=static=oberon_3.0.15");
println!("cargo:rustc-link-lib=static=nrf_cc310_platform_0.9.19");
}