Skip to content

Commit c7e7fe7

Browse files
authored
Merge pull request #3 from xoviat/build
fix ctypes
2 parents 5a9d5a7 + c74f819 commit c7e7fe7

File tree

6 files changed

+67
-73
lines changed

6 files changed

+67
-73
lines changed

.vscode/settings.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"[toml]": {
3+
"editor.formatOnSave": false
4+
},
5+
"[markdown]": {
6+
"editor.formatOnSave": false
7+
},
8+
"rust-analyzer.rustfmt.extraArgs": [
9+
"+nightly"
10+
],
11+
"rust-analyzer.check.allTargets": false,
12+
//"rust-analyzer.check.noDefaultFeatures": true,
13+
//"rust-analyzer.cargo.noDefaultFeatures": true,
14+
"rust-analyzer.showUnlinkedFileNotification": false,
15+
// Uncomment the target of your chip.
16+
// "rust-analyzer.cargo.target": "thumbv6m-none-eabi",
17+
//"rust-analyzer.cargo.target": "thumbv7m-none-eabi",
18+
"rust-analyzer.cargo.target": "thumbv7em-none-eabi",
19+
//"rust-analyzer.cargo.target": "thumbv7em-none-eabihf",
20+
// "rust-analyzer.cargo.target": "thumbv8m.main-none-eabihf",
21+
"rust-analyzer.cargo.features": [],
22+
"rust-analyzer.linkedProjects": [
23+
"stm32-bindings-gen/Cargo.toml",
24+
"build/stm32-bindings/Cargo.toml"
25+
],
26+
}

stm32-bindings-gen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ serde = { version = "1.0.157", features = [ "derive" ] }
1111
serde_json = "1.0.94"
1212
proc-macro2 = "1.0.52"
1313
bindgen = "0.72.1"
14+
tempfile = "3.23.0"

stm32-bindings-gen/res/build.rs

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,12 @@
1-
// use std::env;
2-
// #[cfg(feature = "rt")]
3-
// use std::path::PathBuf;
4-
//
5-
// enum GetOneError {
6-
// None,
7-
// Multiple,
8-
// }
9-
//
10-
// trait IteratorExt: Iterator {
11-
// fn get_one(self) -> Result<Self::Item, GetOneError>;
12-
// }
13-
//
14-
// impl<T: Iterator> IteratorExt for T {
15-
// fn get_one(mut self) -> Result<Self::Item, GetOneError> {
16-
// match self.next() {
17-
// None => Err(GetOneError::None),
18-
// Some(res) => match self.next() {
19-
// Some(_) => Err(GetOneError::Multiple),
20-
// None => Ok(res),
21-
// },
22-
// }
23-
// }
24-
// }
1+
use std::env;
2+
use std::path::PathBuf;
253

264
fn main() {
275
let crate_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
286

297
println!(
308
"cargo:rustc-link-search=native={}",
31-
crate_dir.join("src/lib")
9+
crate_dir.join("src/lib").to_str().unwrap()
3210
);
3311
println!("cargo:rustc-link-lib=static=wba_mac_lib");
34-
35-
// #[cfg(feature = "rt")]
36-
// let crate_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
37-
//
38-
// let chip_core_name = match env::vars()
39-
// .map(|(a, _)| a)
40-
// .filter(|x| x.starts_with("CARGO_FEATURE_STM32"))
41-
// .get_one()
42-
// {
43-
// Ok(x) => x,
44-
// Err(GetOneError::None) => panic!("No stm32xx Cargo feature enabled"),
45-
// Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"),
46-
// }
47-
// .strip_prefix("CARGO_FEATURE_")
48-
// .unwrap()
49-
// .to_ascii_lowercase()
50-
// .replace('_', "-");
51-
//
52-
// #[cfg(feature = "rt")]
53-
// println!(
54-
// "cargo:rustc-link-search={}/src/chips/{}",
55-
// crate_dir.display(),
56-
// chip_core_name,
57-
// );
58-
//
59-
// println!(
60-
// "cargo:rustc-env=STM32_METAPAC_PAC_PATH=chips/{}/pac.rs",
61-
// chip_core_name
62-
// );
63-
// println!(
64-
// "cargo:rustc-env=STM32_METAPAC_METADATA_PATH=chips/{}/metadata.rs",
65-
// chip_core_name
66-
// );
67-
//
68-
// println!("cargo:rerun-if-changed=build.rs");
6912
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod wpan_wba;

stm32-bindings-gen/res/src/lib.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,4 @@
44
#![allow(non_camel_case_types)]
55
#![doc(html_no_source)]
66

7-
mod generated {
8-
9-
mod std {
10-
pub mod os {
11-
pub mod raw {
12-
pub type c_uint = u32;
13-
}
14-
}
15-
}
16-
17-
// core::include!(core::concat!(core::env!("OUT_DIR"), "/bindings.rs"));
18-
}
7+
mod bindings;

stm32-bindings-gen/src/lib.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use std::io::Write;
12
use std::{fs, path::PathBuf};
3+
use tempfile::NamedTempFile;
24

35
pub struct Options {
46
pub out_dir: PathBuf,
@@ -15,9 +17,20 @@ impl Gen {
1517
}
1618

1719
pub fn run_gen(&mut self) {
20+
let _ = fs::remove_dir_all(self.opts.out_dir.clone());
1821
fs::create_dir_all(self.opts.out_dir.join("src/bindings")).unwrap();
1922
fs::create_dir_all(self.opts.out_dir.join("src/lib")).unwrap();
2023

24+
// Create a named temporary file
25+
let mut header = NamedTempFile::new().unwrap();
26+
27+
// Write some data to the first handle
28+
header
29+
.write_all(include_bytes!("../inc/wpan-wba.h"))
30+
.unwrap();
31+
32+
header.reopen().unwrap();
33+
2134
// The bindgen::Builder is the main entry point
2235
// to bindgen, and lets you build up options for
2336
// the resulting bindings.
@@ -34,10 +47,20 @@ impl Gen {
3447
// Unwrap the Result and panic on failure.
3548
.expect("Unable to generate bindings");
3649

50+
let out_path = self.opts.out_dir.join("src/bindings/wpan_wba.rs");
51+
3752
bindings
38-
.write_to_file(self.opts.out_dir.join("src/bindings/wpan-wba.rs"))
53+
.write_to_file(&out_path)
3954
.expect("Couldn't write bindings!");
4055

56+
let file_contents = fs::read_to_string(&out_path).unwrap();
57+
let file_contents = file_contents
58+
.replace("::std::mem::", "::core::mem::")
59+
.replace("::std::os::raw::", "::core::ffi::")
60+
.replace("::std::option::", "::core::option::");
61+
62+
fs::write(&out_path, file_contents).unwrap();
63+
4164
// copy misc files
4265
fs::copy(
4366
self.opts
@@ -51,6 +74,11 @@ impl Gen {
5174
include_bytes!("../res/README.md"),
5275
)
5376
.unwrap();
77+
fs::write(
78+
self.opts.out_dir.join("Cargo.toml"),
79+
include_bytes!("../res/Cargo.toml"),
80+
)
81+
.unwrap();
5482
fs::write(
5583
self.opts.out_dir.join("build.rs"),
5684
include_bytes!("../res/build.rs"),
@@ -61,5 +89,11 @@ impl Gen {
6189
include_bytes!("../res/src/lib.rs"),
6290
)
6391
.unwrap();
92+
93+
fs::write(
94+
self.opts.out_dir.join("src/bindings/mod.rs"),
95+
include_bytes!("../res/src/bindings/mod.rs"),
96+
)
97+
.unwrap();
6498
}
6599
}

0 commit comments

Comments
 (0)