From c74f81917d04213dad70ff2e0e777616dc90209d Mon Sep 17 00:00:00 2001 From: xoviat Date: Tue, 25 Nov 2025 19:56:10 -0600 Subject: [PATCH] fix ctypes --- .vscode/settings.json | 26 +++++++++ stm32-bindings-gen/Cargo.toml | 1 + stm32-bindings-gen/res/build.rs | 63 ++-------------------- stm32-bindings-gen/res/src/bindings/mod.rs | 1 + stm32-bindings-gen/res/src/lib.rs | 13 +---- stm32-bindings-gen/src/lib.rs | 36 ++++++++++++- 6 files changed, 67 insertions(+), 73 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 stm32-bindings-gen/res/src/bindings/mod.rs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..77ee51e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,26 @@ +{ + "[toml]": { + "editor.formatOnSave": false + }, + "[markdown]": { + "editor.formatOnSave": false + }, + "rust-analyzer.rustfmt.extraArgs": [ + "+nightly" + ], + "rust-analyzer.check.allTargets": false, + //"rust-analyzer.check.noDefaultFeatures": true, + //"rust-analyzer.cargo.noDefaultFeatures": true, + "rust-analyzer.showUnlinkedFileNotification": false, + // Uncomment the target of your chip. + // "rust-analyzer.cargo.target": "thumbv6m-none-eabi", + //"rust-analyzer.cargo.target": "thumbv7m-none-eabi", + "rust-analyzer.cargo.target": "thumbv7em-none-eabi", + //"rust-analyzer.cargo.target": "thumbv7em-none-eabihf", + // "rust-analyzer.cargo.target": "thumbv8m.main-none-eabihf", + "rust-analyzer.cargo.features": [], + "rust-analyzer.linkedProjects": [ + "stm32-bindings-gen/Cargo.toml", + "build/stm32-bindings/Cargo.toml" + ], +} \ No newline at end of file diff --git a/stm32-bindings-gen/Cargo.toml b/stm32-bindings-gen/Cargo.toml index 9ef409b..0f1398b 100644 --- a/stm32-bindings-gen/Cargo.toml +++ b/stm32-bindings-gen/Cargo.toml @@ -11,3 +11,4 @@ serde = { version = "1.0.157", features = [ "derive" ] } serde_json = "1.0.94" proc-macro2 = "1.0.52" bindgen = "0.72.1" +tempfile = "3.23.0" \ No newline at end of file diff --git a/stm32-bindings-gen/res/build.rs b/stm32-bindings-gen/res/build.rs index d9bdbad..b97aa27 100644 --- a/stm32-bindings-gen/res/build.rs +++ b/stm32-bindings-gen/res/build.rs @@ -1,69 +1,12 @@ -// use std::env; -// #[cfg(feature = "rt")] -// use std::path::PathBuf; -// -// enum GetOneError { -// None, -// Multiple, -// } -// -// trait IteratorExt: Iterator { -// fn get_one(self) -> Result; -// } -// -// impl IteratorExt for T { -// fn get_one(mut self) -> Result { -// match self.next() { -// None => Err(GetOneError::None), -// Some(res) => match self.next() { -// Some(_) => Err(GetOneError::Multiple), -// None => Ok(res), -// }, -// } -// } -// } +use std::env; +use std::path::PathBuf; fn main() { let crate_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); println!( "cargo:rustc-link-search=native={}", - crate_dir.join("src/lib") + crate_dir.join("src/lib").to_str().unwrap() ); println!("cargo:rustc-link-lib=static=wba_mac_lib"); - - // #[cfg(feature = "rt")] - // let crate_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); - // - // let chip_core_name = match env::vars() - // .map(|(a, _)| a) - // .filter(|x| x.starts_with("CARGO_FEATURE_STM32")) - // .get_one() - // { - // Ok(x) => x, - // Err(GetOneError::None) => panic!("No stm32xx Cargo feature enabled"), - // Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"), - // } - // .strip_prefix("CARGO_FEATURE_") - // .unwrap() - // .to_ascii_lowercase() - // .replace('_', "-"); - // - // #[cfg(feature = "rt")] - // println!( - // "cargo:rustc-link-search={}/src/chips/{}", - // crate_dir.display(), - // chip_core_name, - // ); - // - // println!( - // "cargo:rustc-env=STM32_METAPAC_PAC_PATH=chips/{}/pac.rs", - // chip_core_name - // ); - // println!( - // "cargo:rustc-env=STM32_METAPAC_METADATA_PATH=chips/{}/metadata.rs", - // chip_core_name - // ); - // - // println!("cargo:rerun-if-changed=build.rs"); } diff --git a/stm32-bindings-gen/res/src/bindings/mod.rs b/stm32-bindings-gen/res/src/bindings/mod.rs new file mode 100644 index 0000000..6443cd1 --- /dev/null +++ b/stm32-bindings-gen/res/src/bindings/mod.rs @@ -0,0 +1 @@ +mod wpan_wba; diff --git a/stm32-bindings-gen/res/src/lib.rs b/stm32-bindings-gen/res/src/lib.rs index d5d1248..1f12fe9 100644 --- a/stm32-bindings-gen/res/src/lib.rs +++ b/stm32-bindings-gen/res/src/lib.rs @@ -4,15 +4,4 @@ #![allow(non_camel_case_types)] #![doc(html_no_source)] -mod generated { - - mod std { - pub mod os { - pub mod raw { - pub type c_uint = u32; - } - } - } - - // core::include!(core::concat!(core::env!("OUT_DIR"), "/bindings.rs")); -} +mod bindings; diff --git a/stm32-bindings-gen/src/lib.rs b/stm32-bindings-gen/src/lib.rs index c3d4afb..12426f7 100644 --- a/stm32-bindings-gen/src/lib.rs +++ b/stm32-bindings-gen/src/lib.rs @@ -1,4 +1,6 @@ +use std::io::Write; use std::{fs, path::PathBuf}; +use tempfile::NamedTempFile; pub struct Options { pub out_dir: PathBuf, @@ -15,9 +17,20 @@ impl Gen { } pub fn run_gen(&mut self) { + let _ = fs::remove_dir_all(self.opts.out_dir.clone()); fs::create_dir_all(self.opts.out_dir.join("src/bindings")).unwrap(); fs::create_dir_all(self.opts.out_dir.join("src/lib")).unwrap(); + // Create a named temporary file + let mut header = NamedTempFile::new().unwrap(); + + // Write some data to the first handle + header + .write_all(include_bytes!("../inc/wpan-wba.h")) + .unwrap(); + + header.reopen().unwrap(); + // The bindgen::Builder is the main entry point // to bindgen, and lets you build up options for // the resulting bindings. @@ -34,10 +47,20 @@ impl Gen { // Unwrap the Result and panic on failure. .expect("Unable to generate bindings"); + let out_path = self.opts.out_dir.join("src/bindings/wpan_wba.rs"); + bindings - .write_to_file(self.opts.out_dir.join("src/bindings/wpan-wba.rs")) + .write_to_file(&out_path) .expect("Couldn't write bindings!"); + let file_contents = fs::read_to_string(&out_path).unwrap(); + let file_contents = file_contents + .replace("::std::mem::", "::core::mem::") + .replace("::std::os::raw::", "::core::ffi::") + .replace("::std::option::", "::core::option::"); + + fs::write(&out_path, file_contents).unwrap(); + // copy misc files fs::copy( self.opts @@ -51,6 +74,11 @@ impl Gen { include_bytes!("../res/README.md"), ) .unwrap(); + fs::write( + self.opts.out_dir.join("Cargo.toml"), + include_bytes!("../res/Cargo.toml"), + ) + .unwrap(); fs::write( self.opts.out_dir.join("build.rs"), include_bytes!("../res/build.rs"), @@ -61,5 +89,11 @@ impl Gen { include_bytes!("../res/src/lib.rs"), ) .unwrap(); + + fs::write( + self.opts.out_dir.join("src/bindings/mod.rs"), + include_bytes!("../res/src/bindings/mod.rs"), + ) + .unwrap(); } }