Skip to content

Commit c60ea04

Browse files
committed
Emit CLI flags for callbacks
1 parent 225b118 commit c60ea04

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

bindgen-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ path = "main.rs"
2121
name = "bindgen"
2222

2323
[dependencies]
24-
bindgen = { path = "../bindgen", version = "=0.63.0" }
24+
bindgen = { path = "../bindgen", version = "=0.63.0", features = ["cli"] }
2525
shlex = "1"
2626
clap = { version = "4", features = ["derive"] }
2727
env_logger = { version = "0.9.0", optional = true }

bindgen-cli/options.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -919,14 +919,36 @@ where
919919
}
920920

921921
impl bindgen::callbacks::ParseCallbacks for CustomDeriveCallback {
922+
fn cli_args(&self) -> Vec<String> {
923+
let mut args = vec![];
924+
925+
let flag = match &self.kind {
926+
None => "--with-derive-custom",
927+
Some(TypeKind::Struct) => "--with-derive-custom-struct",
928+
Some(TypeKind::Enum) => "--with-derive-custom-enum",
929+
Some(TypeKind::Union) => "--with-derive-custom-union",
930+
};
931+
932+
let derives = self.derives.join(",");
933+
934+
for item in self.regex_set.get_items() {
935+
args.extend_from_slice(&[
936+
flag.to_owned(),
937+
format!("{}={}", item, derives),
938+
]);
939+
}
940+
941+
args
942+
}
943+
922944
fn add_derives(
923945
&self,
924946
info: &bindgen::callbacks::DeriveInfo<'_>,
925947
) -> Vec<String> {
926-
if self.kind.map(|kind| kind == info.kind).unwrap_or(true) {
927-
if self.regex_set.matches(info.name) {
928-
return self.derives.clone();
929-
}
948+
if self.kind.map(|kind| kind == info.kind).unwrap_or(true)
949+
&& self.regex_set.matches(info.name)
950+
{
951+
return self.derives.clone();
930952
}
931953
vec![]
932954
}
@@ -940,9 +962,9 @@ where
940962
] {
941963
for custom_derive in custom_derives {
942964
let (regex, derives) = custom_derive
943-
.rsplit_once("=")
965+
.rsplit_once('=')
944966
.expect("Invalid custom derive argument: Missing `=`");
945-
let derives = derives.split(",").map(|s| s.to_owned()).collect();
967+
let derives = derives.split(',').map(|s| s.to_owned()).collect();
946968

947969
let mut regex_set = RegexSet::new();
948970
regex_set.insert(regex);

bindgen/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static = ["clang-sys/static"]
4747
runtime = ["clang-sys/runtime"]
4848
# Dynamically discover a `rustfmt` binary using the `which` crate
4949
which-rustfmt = ["which"]
50+
cli = []
5051

5152
# These features only exist for CI testing -- don't use them if you're not hacking
5253
# on bindgen!

bindgen/callbacks.rs

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ impl Default for MacroParsingBehavior {
2525
/// A trait to allow configuring different kinds of types in different
2626
/// situations.
2727
pub trait ParseCallbacks: fmt::Debug {
28+
#[cfg(feature = "cli")]
29+
#[doc(hidden)]
30+
fn cli_args(&self) -> Vec<String> {
31+
vec![]
32+
}
33+
2834
/// This function will be run on every macro that is identified.
2935
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
3036
MacroParsingBehavior::Default

bindgen/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,11 @@ impl Builder {
653653
output_vector.push("--wrap-unsafe-ops".into());
654654
}
655655

656+
#[cfg(feature = "cli")]
657+
for callbacks in &self.options.parse_callbacks {
658+
output_vector.extend(callbacks.cli_args());
659+
}
660+
656661
// Add clang arguments
657662

658663
output_vector.push("--".into());

bindgen/regex_set.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ pub struct RegexSet {
1818
impl RegexSet {
1919
/// Create a new RegexSet
2020
pub fn new() -> RegexSet {
21-
RegexSet { ..Default::default() }
21+
RegexSet {
22+
..Default::default()
23+
}
2224
}
2325

2426
/// Is this set empty?

0 commit comments

Comments
 (0)