Skip to content

Commit 4dd4523

Browse files
christianheussyemilio
authored andcommitted
feat: add headers option
Problem: It's cumbersome to define multiple input headers using the existing `header` API. It's difficult for the user to configure the `Builder` with a list of input headers. Solution: Add `headers` method that permits adding multiple headers via an iterable of Strings. Testing: Added `test_headers_call_in_builder`. Ran `cargo test` in `bindgen-tests/tests/expectations`. Issue: #2738
1 parent 9e004e9 commit 4dd4523

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

bindgen-tests/tests/tests.rs

+36
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,42 @@ fn test_multiple_header_calls_in_builder() {
472472
}
473473
}
474474

475+
#[test]
476+
fn test_headers_call_in_builder() {
477+
let actual = builder()
478+
.headers([
479+
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/func_ptr.h"),
480+
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/char.h"),
481+
])
482+
.clang_arg("--target=x86_64-unknown-linux")
483+
.generate()
484+
.unwrap()
485+
.to_string();
486+
487+
let actual = format_code(actual).unwrap();
488+
489+
let expected_filename = concat!(
490+
env!("CARGO_MANIFEST_DIR"),
491+
"/tests/expectations/tests/test_multiple_header_calls_in_builder.rs"
492+
);
493+
let expected = include_str!(concat!(
494+
env!("CARGO_MANIFEST_DIR"),
495+
"/tests/expectations/tests/test_multiple_header_calls_in_builder.rs"
496+
));
497+
let expected = format_code(expected).unwrap();
498+
499+
if actual != expected {
500+
println!("Generated bindings differ from expected!");
501+
error_diff_mismatch(
502+
&actual,
503+
&expected,
504+
None,
505+
Path::new(expected_filename),
506+
)
507+
.unwrap();
508+
}
509+
}
510+
475511
#[test]
476512
fn test_multiple_header_contents() {
477513
let actual = builder()

bindgen/options/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,35 @@ options! {
11791179
self.options.input_headers.push(header.into().into_boxed_str());
11801180
self
11811181
}
1182+
1183+
/// Add input C/C++ header(s) to generate bindings for.
1184+
///
1185+
/// This can be used to generate bindings for a single header:
1186+
///
1187+
/// ```ignore
1188+
/// let bindings = bindgen::Builder::default()
1189+
/// .headers(["input.h"])
1190+
/// .generate()
1191+
/// .unwrap();
1192+
/// ```
1193+
///
1194+
/// Or for multiple headers:
1195+
///
1196+
/// ```ignore
1197+
/// let bindings = bindgen::Builder::default()
1198+
/// .headers(["first.h", "second.h", "third.h"])
1199+
/// .generate()
1200+
/// .unwrap();
1201+
/// ```
1202+
pub fn headers<I: IntoIterator>(mut self, headers: I) -> Builder
1203+
where
1204+
I::Item: Into<String>,
1205+
{
1206+
self.options
1207+
.input_headers
1208+
.extend(headers.into_iter().map(Into::into).map(Into::into));
1209+
self
1210+
}
11821211
},
11831212
// This field is handled specially inside the macro.
11841213
as_args: ignore,

0 commit comments

Comments
 (0)