Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Use syn to pick out constants #49

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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