Skip to content

Commit

Permalink
build: Use syn to pick out constants
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Feb 22, 2023
1 parent 6b4f5d4 commit 2af99de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pin-utils = "0.1"

[build-dependencies]
shlex = "0.1.1"
syn = { version = "1.0.107", features = [ "parsing" ] }

[features]
default = []
Expand Down
38 changes: 32 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,38 @@ fn main() {
let bindgen_output = std::fs::read_to_string(bindgen_output_file)
.expect("Failed to read BINDGEN_OUTPUT_FILE");

// Whether or not the extra space is there depends on whether or not rustfmt is installed.
// FIXME: Find a better way to extract that information
if bindgen_output.contains("pub const CONFIG_AUTO_INIT_ENABLE_DEBUG: u32 = 1;")
|| bindgen_output.contains("pub const CONFIG_AUTO_INIT_ENABLE_DEBUG : u32 = 1 ;")
{
println!("cargo:rustc-cfg=marker_config_auto_init_enable_debug");
const BOOLEAN_FLAGS: &[&str] = &[
// This decides whether or not some fields are populated ... and unlike with other
// structs, the zeroed default is not a good solution here. (It'd kind of work, but
// it'd produce incorrect debug output).
"CONFIG_AUTO_INIT_ENABLE_DEBUG",
];

let parsed = syn::parse_file(&bindgen_output).expect("Failed to parse bindgen output");
for item in &parsed.items {
if let syn::Item::Const(const_) = item {
// It's the easiest way to get something we can `contains`...
let ident = const_.ident.to_string();
if BOOLEAN_FLAGS.contains(&ident.as_str()) {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Int(litint),
..
}) = &*const_.expr
{
let value: usize = litint
.base10_parse()
.expect("Identifier is integer literal but not parsable");
if value != 0 {
println!("cargo:rustc-cfg=marker_{}", ident.to_lowercase());
}
continue;
}
panic!(
"Found {} but it's not the literal const it was expected to be",
ident
);
}
}
}
} else {
println!("cargo:warning=Old riot-sys did not provide BINDGEN_OUTPUT_FILE, assuming it's an old RIOT version");
Expand Down

0 comments on commit 2af99de

Please sign in to comment.