commit 4b560fe79047893f90d3e51a22b304ad27a19e59 Author: Moritz Bitsch Date: Tue Nov 21 18:22:49 2023 +0100 initial import diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..36cc42e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "zephyr-config" +version = "0.1.0" +edition = "2021" + +[build-dependencies] +bindgen = "0.69.1" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..743f589 --- /dev/null +++ b/build.rs @@ -0,0 +1,42 @@ +extern crate bindgen; + +use std::{env, path::PathBuf}; + +fn main() { + let zephyr_sdk_install_dir = PathBuf::from(env::var("ZEPHYR_SDK_INSTALL_DIR").unwrap()); + + let bindings = bindgen::Builder::default() + // Only use core, as we are in embedded land + .use_core() + // The input header we would like to generate + // bindings for. + .header("src/wrapper.h") + .clang_args( + env::var("TARGET_CFLAGS") + .unwrap() + .split(' ') + .filter(|&el| el != "-mfp16-format=ieee" && el != "-fno-reorder-functions"), + ) + .clang_arg(format!( + "-I{}", + zephyr_sdk_install_dir + .join("arm-zephyr-eabi/arm-zephyr-eabi/sys-include") + .to_str() + .unwrap() + )) + // only allow CONFIG_ defines + .allowlist_item("^CONFIG_.*") + // Tell cargo to invalidate the built crate whenever any of the + // included header files changed. + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) + // Finish the builder and generate the bindings. + .generate() + // Unwrap the Result and panic on failure. + .expect("Unable to generate bindings"); + + // Write the bindings to the $OUT_DIR/bindings.rs file. + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4d04108 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,13 @@ +#![no_std] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(dead_code)] +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + +pub fn config_str<'a>(var: &'a [u8]) -> &'a str { + core::ffi::CStr::from_bytes_until_nul(var) + .unwrap() + .to_str() + .unwrap() +} diff --git a/src/wrapper.h b/src/wrapper.h new file mode 100644 index 0000000..91657d7 --- /dev/null +++ b/src/wrapper.h @@ -0,0 +1 @@ +#include \ No newline at end of file