Skip to content

Commit 795d900

Browse files
authored
Introduce ParseCallbacks::header_file (#2653)
* Add `ParseCallbacks::input_file` * Make `CargoCallbacks` configurable * Use the `input_file` callback with every header * Fix docs * Rename callback to `header_file` for clarity * Update CHANGELOG.md * Run rustfmt
1 parent b72bf15 commit 795d900

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,19 @@
178178
# Unreleased
179179

180180
## Added
181+
- Added the `ParseCallbacks::header_file` callback which runs on every filename passed to `Builder::header`.
182+
- Added the `CargoCallbacks::new` constructor which emits a cargo-rerun line
183+
for every input header file by default.
184+
- Added the `CargoCallbacks::rerun_on_header_files` method to configure whether
185+
a cargo-rerun line should be emitted for every input header file.
181186
## Changed
182187
- The `--wrap-static-fns` feature was updated so function types that has no
183188
argument use `void` as its sole argument.
189+
- `CargoCallbacks` is no longer a [unit-like
190+
struct](https://doc.rust-lang.org/reference/items/structs.html) and the
191+
`CargoCallbacks` constant was added to mitigate the breaking nature of this
192+
change. This constant has been marked as deprecated and users will have to
193+
use the new `CargoCallbacks::new` method in the future.
184194
## Removed
185195
## Fixed
186196
- Allow compiling `bindgen-cli` with a static libclang.

bindgen/callbacks.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ pub trait ParseCallbacks: fmt::Debug {
9999
None
100100
}
101101

102+
/// This will be called on every header filename passed to (`Builder::header`)[`crate::Builder::header`].
103+
fn header_file(&self, _filename: &str) {}
104+
102105
/// This will be called on every file inclusion, with the full path of the included file.
103106
fn include_file(&self, _filename: &str) {}
104107

bindgen/lib.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ impl Builder {
325325
.map(String::into_boxed_str),
326326
);
327327

328+
for header in &self.options.input_headers {
329+
self.options
330+
.for_each_callback(|cb| cb.header_file(header.as_ref()));
331+
}
332+
328333
// Transform input headers to arguments on the clang command line.
329334
self.options.clang_args.extend(
330335
self.options.input_headers
@@ -566,6 +571,10 @@ impl BindgenOptions {
566571
.collect()
567572
}
568573

574+
fn for_each_callback(&self, f: impl Fn(&dyn callbacks::ParseCallbacks)) {
575+
self.parse_callbacks.iter().for_each(|cb| f(cb.as_ref()));
576+
}
577+
569578
fn process_comment(&self, comment: &str) -> String {
570579
let comment = comment::preprocess(comment);
571580
self.parse_callbacks
@@ -1232,9 +1241,50 @@ fn get_target_dependent_env_var(
12321241
/// .generate();
12331242
/// ```
12341243
#[derive(Debug)]
1235-
pub struct CargoCallbacks;
1244+
pub struct CargoCallbacks {
1245+
rerun_on_header_files: bool,
1246+
}
1247+
1248+
/// Create a new `CargoCallbacks` value with [`CargoCallbacks::rerun_on_header_files`] disabled.
1249+
///
1250+
/// This constructor has been deprecated in favor of [`CargoCallbacks::new`] where
1251+
/// [`CargoCallbacks::rerun_on_header_files`] is enabled by default.
1252+
#[deprecated = "Use `CargoCallbacks::new()` instead. Please, check the documentation for further information."]
1253+
pub const CargoCallbacks: CargoCallbacks = CargoCallbacks {
1254+
rerun_on_header_files: false,
1255+
};
1256+
1257+
impl CargoCallbacks {
1258+
/// Create a new `CargoCallbacks` value.
1259+
pub fn new() -> Self {
1260+
Self {
1261+
rerun_on_header_files: true,
1262+
}
1263+
}
1264+
1265+
/// Whether Cargo should re-run the build script if any of the input header files has changed.
1266+
///
1267+
/// This option is enabled by default unless the deprecated [`const@CargoCallbacks`]
1268+
/// constructor is used.
1269+
pub fn rerun_on_header_files(mut self, doit: bool) -> Self {
1270+
self.rerun_on_header_files = doit;
1271+
self
1272+
}
1273+
}
1274+
1275+
impl Default for CargoCallbacks {
1276+
fn default() -> Self {
1277+
Self::new()
1278+
}
1279+
}
12361280

12371281
impl callbacks::ParseCallbacks for CargoCallbacks {
1282+
fn header_file(&self, filename: &str) {
1283+
if self.rerun_on_header_files {
1284+
println!("cargo:rerun-if-changed={}", filename);
1285+
}
1286+
}
1287+
12381288
fn include_file(&self, filename: &str) {
12391289
println!("cargo:rerun-if-changed={}", filename);
12401290
}

0 commit comments

Comments
 (0)