Skip to content

Commit 6d9597a

Browse files
authored
Merge pull request #6 from xoviat/build
abstract lib building
2 parents 4d834d2 + 2fd69c5 commit 6d9597a

File tree

9 files changed

+125
-98
lines changed

9 files changed

+125
-98
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ jobs:
2929
- name: Load sources
3030
run: ./d download-all
3131
- name: Run build
32-
run: cargo run --release --bin stm32-bindings-gen -- --target=thumbv8m.main-none-eabihf
32+
run: cargo run --release --bin stm32-bindings-gen
3333
- name: Run package build
3434
run: cd build/stm32-bindings && cargo build --target=thumbv8m.main-none-eabihf

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// Uncomment the target of your chip.
1616
// "rust-analyzer.cargo.target": "thumbv6m-none-eabi",
1717
//"rust-analyzer.cargo.target": "thumbv7m-none-eabi",
18-
"rust-analyzer.cargo.target": "thumbv7em-none-eabi",
18+
// "rust-analyzer.cargo.target": "thumbv7em-none-eabi",
1919
//"rust-analyzer.cargo.target": "thumbv7em-none-eabihf",
2020
// "rust-analyzer.cargo.target": "thumbv8m.main-none-eabihf",
2121
"rust-analyzer.cargo.features": [],

d

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ set -e
44
cd $(dirname $0)
55

66
CMD=$1
7-
REV=8382ecbcc976ab5e91070b386700dbfd1e654275
7+
REV=v1.8.0
88
shift
99

1010
case "$CMD" in
1111
gen)
12-
cargo run --release stm32-bindings-gen -- --target=thumbv8m.main-none-eabihf
12+
cargo run --release stm32-bindings-gen
1313
;;
1414
download-all)
1515
rm -rf ./sources/
16-
git clone https://github.com/STMicroelectronics/STM32CubeWBA.git ./sources/ -q
16+
git clone --branch $REV https://github.com/STMicroelectronics/STM32CubeWBA.git ./sources/STM32CubeWBA/ --depth 1 -q
1717
cd ./sources/
18-
git checkout $REV
1918
;;
2019
*)
2120
echo "unknown command"

d.ps1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ param (
66
[string]$peri
77
)
88

9-
$REV = "8382ecbcc976ab5e91070b386700dbfd1e654275"
9+
$REV = "v1.8.0"
1010

1111
Switch ($CMD) {
1212
"gen" {
13-
cargo run --release stm32-bindings-gen -- --target=thumbv8m.main-none-eabihf
13+
cargo run --release stm32-bindings-gen
1414
}
1515
"download-all" {
1616
rm -r -Force ./sources/ -ErrorAction SilentlyContinue
17-
git clone https://github.com/STMicroelectronics/STM32CubeWBA.git ./sources/
18-
cd ./sources/
19-
git checkout $REV
17+
git clone --branch $REV https://github.com/STMicroelectronics/STM32CubeWBA.git ./sources/STM32CubeWBA/ --depth 1 -q
2018
cd ..
2119
}
2220
default {
File renamed without changes.

stm32-bindings-gen/res/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "stm32-bindings"
3-
version = "18.0.0"
3+
version = "0.1.0"
44
edition = "2024"
55
license = "MIT OR Apache-2.0"
66
repository = "https://github.com/embassy-rs/stm32-data"
@@ -15,8 +15,8 @@ include = [
1515
]
1616

1717
[package.metadata.docs.rs]
18-
features = ["stm32h755zi-cm7", "pac", "metadata"]
19-
default-target = "thumbv7em-none-eabihf"
18+
features = []
19+
default-target = "thumbv8m.main-none-eabihf"
2020
targets = []
2121
rustdoc-args = ["--cfg", "docsrs"]
2222

@@ -53,6 +53,10 @@ defmt = { version = "0.3.0", optional = true }
5353
[features]
5454
default = ["pac"]
5555

56+
wba_wpan_mac = []
57+
wba_wpan_ble = []
58+
n6_ai_runtime = []
59+
5660
# Build the actual PAC. Set by default.
5761
# If you just want the metadata, unset it with `default-features = false`.
5862
pac = []
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
mod wpan_wba;
1+
#[cfg(feature = "wba_wpan_mac")]
2+
mod wba_wpan_mac;

stm32-bindings-gen/src/lib.rs

Lines changed: 95 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,103 +19,119 @@ impl ParseCallbacks for UppercaseCallbacks {
1919
pub struct Options {
2020
pub out_dir: PathBuf,
2121
pub sources_dir: PathBuf,
22+
}
23+
24+
pub struct Library {
25+
pub sources_dir: PathBuf,
2226
pub target_triple: String,
27+
pub header: &'static [u8],
28+
pub module: &'static str,
29+
pub includes: Vec<PathBuf>,
30+
pub library: PathBuf,
2331
}
2432

2533
pub struct Gen {
2634
opts: Options,
35+
libs: Vec<Library>,
2736
}
2837

2938
impl Gen {
30-
pub fn new(opts: Options) -> Self {
31-
Self { opts }
39+
pub fn new(opts: Options, libs: Vec<Library>) -> Self {
40+
Self { opts, libs }
3241
}
3342

3443
pub fn run_gen(&mut self) {
3544
let _ = fs::remove_dir_all(self.opts.out_dir.clone());
3645
fs::create_dir_all(self.opts.out_dir.join("src/bindings")).unwrap();
3746
fs::create_dir_all(self.opts.out_dir.join("src/lib")).unwrap();
3847

39-
// Create a named temporary file
40-
let mut header = NamedTempFile::new().unwrap();
41-
42-
// Write some data to the first handle
43-
header
44-
.write_all(include_bytes!("../inc/wpan-wba.h"))
45-
.unwrap();
46-
47-
header.reopen().unwrap();
48-
49-
// The bindgen::Builder is the main entry point
50-
// to bindgen, and lets you build up options for
51-
// the resulting bindings.
52-
let target_flag = format!("--target={}", self.opts.target_triple);
53-
let include_arg = format!(
54-
"-I{}/Middlewares/ST/STM32_WPAN/mac_802_15_4/core/inc",
55-
self.opts.sources_dir.to_str().unwrap()
56-
);
57-
let mut builder = bindgen::Builder::default()
58-
.parse_callbacks(Box::new(UppercaseCallbacks))
59-
// Force Clang to use the same layout as the selected target.
60-
.clang_arg(&target_flag)
61-
.clang_arg(&include_arg);
62-
if self
63-
.opts
64-
.target_triple
65-
.to_ascii_lowercase()
66-
.starts_with("thumb")
67-
{
68-
builder = builder.clang_arg("-mthumb");
69-
}
70-
let bindings = builder
71-
// The input header we would like to generate
72-
// bindings for.
73-
.header("stm32-bindings-gen/inc/wpan-wba.h")
74-
// Finish the builder and generate the bindings.
75-
.generate()
76-
// Unwrap the Result and panic on failure.
77-
.expect("Unable to generate bindings");
78-
79-
let out_path = self.opts.out_dir.join("src/bindings/wpan_wba.rs");
80-
81-
bindings
82-
.write_to_file(&out_path)
83-
.expect("Couldn't write bindings!");
84-
85-
let mut file_contents = fs::read_to_string(&out_path).unwrap();
86-
file_contents = file_contents
87-
.replace("::std::mem::", "::core::mem::")
88-
.replace("::std::os::raw::", "::core::ffi::")
89-
.replace("::std::option::", "::core::option::");
90-
91-
file_contents = file_contents
92-
.lines()
93-
.map(|line| {
94-
if let Some(rest) = line.strip_prefix("pub const ") {
95-
if let Some((name, tail)) = rest.split_once(':') {
96-
let upper = name.trim().to_ascii_uppercase();
97-
return format!("pub const {}:{}", upper, tail);
48+
for lib in &self.libs {
49+
let sources_dir = self.opts.sources_dir.join(&lib.sources_dir);
50+
51+
// Create a named temporary file
52+
let mut header = NamedTempFile::with_suffix(".h").unwrap();
53+
54+
// Write some data to the first handle
55+
header.write_all(lib.header).unwrap();
56+
57+
// The bindgen::Builder is the main entry point
58+
// to bindgen, and lets you build up options for
59+
// the resulting bindings.
60+
let target_flag = format!("--target={}", lib.target_triple);
61+
62+
let mut builder = bindgen::Builder::default()
63+
.parse_callbacks(Box::new(UppercaseCallbacks))
64+
// Force Clang to use the same layout as the selected target.
65+
.clang_arg(&target_flag);
66+
67+
for include_arg in &lib.includes {
68+
builder = builder.clang_arg(&format!(
69+
"-I{}",
70+
sources_dir.join(include_arg).to_str().unwrap()
71+
));
72+
}
73+
74+
if lib.target_triple.to_ascii_lowercase().starts_with("thumb") {
75+
builder = builder.clang_arg("-mthumb");
76+
}
77+
let bindings = builder
78+
// The input header we would like to generate
79+
// bindings for.
80+
.header(header.path().to_str().unwrap())
81+
// Finish the builder and generate the bindings.
82+
.generate()
83+
// Unwrap the Result and panic on failure.
84+
.expect("Unable to generate bindings");
85+
86+
let out_path = self
87+
.opts
88+
.out_dir
89+
.join("src")
90+
.join("bindings")
91+
.join(format!("{}.rs", lib.module));
92+
93+
bindings
94+
.write_to_file(&out_path)
95+
.expect("Couldn't write bindings!");
96+
97+
let mut file_contents = fs::read_to_string(&out_path).unwrap();
98+
file_contents = file_contents
99+
.replace("::std::mem::", "::core::mem::")
100+
.replace("::std::os::raw::", "::core::ffi::")
101+
.replace("::std::option::", "::core::option::");
102+
103+
file_contents = file_contents
104+
.lines()
105+
.map(|line| {
106+
if let Some(rest) = line.strip_prefix("pub const ") {
107+
if let Some((name, tail)) = rest.split_once(':') {
108+
let upper = name.trim().to_ascii_uppercase();
109+
return format!("pub const {}:{}", upper, tail);
110+
}
98111
}
99-
}
100-
line.to_owned()
101-
})
102-
.collect::<Vec<_>>()
103-
.join("\n");
104-
105-
if !file_contents.ends_with('\n') {
106-
file_contents.push('\n');
112+
line.to_owned()
113+
})
114+
.collect::<Vec<_>>()
115+
.join("\n");
116+
117+
if !file_contents.ends_with('\n') {
118+
file_contents.push('\n');
119+
}
120+
121+
fs::write(&out_path, file_contents).unwrap();
122+
123+
// copy misc files
124+
fs::copy(
125+
sources_dir.join(&lib.library),
126+
self.opts
127+
.out_dir
128+
.join("src")
129+
.join("lib")
130+
.join(format!("{}.a", lib.module)),
131+
)
132+
.unwrap();
107133
}
108134

109-
fs::write(&out_path, file_contents).unwrap();
110-
111-
// copy misc files
112-
fs::copy(
113-
self.opts
114-
.sources_dir
115-
.join("Middlewares/ST/STM32_WPAN/mac_802_15_4/lib/wba_mac_lib.a"),
116-
self.opts.out_dir.join("src/lib/wba_mac_lib.a"),
117-
)
118-
.unwrap();
119135
fs::write(
120136
self.opts.out_dir.join("README.md"),
121137
include_bytes!("../res/README.md"),

stm32-bindings-gen/src/main.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
use std::{env, path::PathBuf, process};
22

3-
use stm32_bindings_gen::{Gen, Options};
3+
use stm32_bindings_gen::{Gen, Library, Options};
44

55
fn main() {
66
let out_dir = PathBuf::from("build/stm32-bindings");
77
let sources_dir = PathBuf::from("sources");
8-
let target_triple = resolve_target_triple();
98

109
let opts = Options {
1110
out_dir,
1211
sources_dir,
13-
target_triple,
1412
};
15-
Gen::new(opts).run_gen();
13+
14+
let libs = Vec::from([Library {
15+
target_triple: String::from("thumbv8m.main-none-eabihf"),
16+
sources_dir: "STM32CubeWBA".into(),
17+
header: include_bytes!("../inc/wba_wpan_mac.h"),
18+
module: "wba_wpan_mac",
19+
includes: Vec::from(["Middlewares/ST/STM32_WPAN/mac_802_15_4/core/inc".into()]),
20+
library: "Middlewares/ST/STM32_WPAN/mac_802_15_4/lib/wba_mac_lib.a".into(),
21+
}]);
22+
23+
Gen::new(opts, libs).run_gen();
1624
}
1725

26+
#[allow(dead_code)]
1827
fn resolve_target_triple() -> String {
1928
let mut args = env::args().skip(1);
2029
let mut positional: Option<String> = None;

0 commit comments

Comments
 (0)