Skip to content

Commit fd6a812

Browse files
committed
fix: struct matches target architecture
1 parent c7e7fe7 commit fd6a812

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

stm32-bindings-gen/src/lib.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
use bindgen::callbacks::{ItemInfo, ItemKind, ParseCallbacks};
12
use std::io::Write;
23
use std::{fs, path::PathBuf};
34
use tempfile::NamedTempFile;
45

6+
#[derive(Debug)]
7+
struct UppercaseCallbacks;
8+
9+
impl ParseCallbacks for UppercaseCallbacks {
10+
fn item_name(&self, item: ItemInfo<'_>) -> Option<String> {
11+
if matches!(item.kind, ItemKind::Var) {
12+
Some(item.name.to_ascii_uppercase())
13+
} else {
14+
None
15+
}
16+
}
17+
}
18+
519
pub struct Options {
620
pub out_dir: PathBuf,
721
pub sources_dir: PathBuf,
@@ -35,6 +49,9 @@ impl Gen {
3549
// to bindgen, and lets you build up options for
3650
// the resulting bindings.
3751
let bindings = bindgen::Builder::default()
52+
.parse_callbacks(Box::new(UppercaseCallbacks))
53+
// Force Clang to use the same 32-bit target layout as the firmware.
54+
.clang_args(["--target=thumbv8m.main-none-eabihf", "-mthumb"])
3855
.clang_arg(format!(
3956
"-I{}/Middlewares/ST/STM32_WPAN/mac_802_15_4/core/inc",
4057
self.opts.sources_dir.to_str().unwrap()
@@ -53,12 +70,30 @@ impl Gen {
5370
.write_to_file(&out_path)
5471
.expect("Couldn't write bindings!");
5572

56-
let file_contents = fs::read_to_string(&out_path).unwrap();
57-
let file_contents = file_contents
73+
let mut file_contents = fs::read_to_string(&out_path).unwrap();
74+
file_contents = file_contents
5875
.replace("::std::mem::", "::core::mem::")
5976
.replace("::std::os::raw::", "::core::ffi::")
6077
.replace("::std::option::", "::core::option::");
6178

79+
file_contents = file_contents
80+
.lines()
81+
.map(|line| {
82+
if let Some(rest) = line.strip_prefix("pub const ") {
83+
if let Some((name, tail)) = rest.split_once(':') {
84+
let upper = name.trim().to_ascii_uppercase();
85+
return format!("pub const {}:{}", upper, tail);
86+
}
87+
}
88+
line.to_owned()
89+
})
90+
.collect::<Vec<_>>()
91+
.join("\n");
92+
93+
if !file_contents.ends_with('\n') {
94+
file_contents.push('\n');
95+
}
96+
6297
fs::write(&out_path, file_contents).unwrap();
6398

6499
// copy misc files

0 commit comments

Comments
 (0)