summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2017-05-01 16:24:49 -0500
committerGitHub <noreply@github.com>2017-05-01 16:24:49 -0500
commit647d9203dafd66c117698eddbf922e6ff53423a2 (patch)
treed117245924850e9b89d9db369d0697a1e7cab52c
parent6cfb0ec4b8e304732b56618f386d4557ae8ff4cb (diff)
parent65bab6fb75ac79c4c3f3a7adc65023082b2254a9 (diff)
Auto merge of #681 - fitzgen:allow-multiple-headers, r=emilio
Generate bindings to all headers passed to `Builder::header` Inspired by real misconfiguration from inside stylo. The previous behavior was that it would reconfigure the builder to generate bindings to the last header `Builder::header` was invoked with. This was not what people would expect: they expected it to generate bindings for all of the headers, and were accidentally misconfiguring their builders. This is a breaking change, but moves us inline with users' expectations. r? @emilio
-rw-r--r--src/lib.rs28
-rw-r--r--tests/expectations/tests/test_multiple_header_calls_in_builder.rs17
-rw-r--r--tests/tests.rs26
3 files changed, 70 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index ca92d842..5d8237fb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -416,8 +416,34 @@ impl Builder {
output_vector
}
- /// Set the input C/C++ header.
+ /// Add an input C/C++ header to generate bindings for.
+ ///
+ /// This can be used to generate bindings to a single header:
+ ///
+ /// ```ignore
+ /// let bindings = bindgen::Builder::default()
+ /// .header("input.h")
+ /// .generate()
+ /// .unwrap();
+ /// ```
+ ///
+ /// Or you can invoke it multiple times to generate bindings to multiple
+ /// headers:
+ ///
+ /// ```ignore
+ /// let bindings = bindgen::Builder::default()
+ /// .header("first.h")
+ /// .header("second.h")
+ /// .header("third.h")
+ /// .generate()
+ /// .unwrap();
+ /// ```
pub fn header<T: Into<String>>(mut self, header: T) -> Builder {
+ if let Some(prev_header) = self.options.input_header.take() {
+ self.options.clang_args.push("-include".into());
+ self.options.clang_args.push(prev_header);
+ }
+
let header = header.into();
self.options.input_header = Some(header);
self
diff --git a/tests/expectations/tests/test_multiple_header_calls_in_builder.rs b/tests/expectations/tests/test_multiple_header_calls_in_builder.rs
new file mode 100644
index 00000000..58ec6fff
--- /dev/null
+++ b/tests/expectations/tests/test_multiple_header_calls_in_builder.rs
@@ -0,0 +1,17 @@
+/* automatically generated by rust-bindgen */
+
+extern "C" {
+ #[link_name = "foo"]
+ pub static mut foo:
+ ::std::option::Option<unsafe extern "C" fn(x:
+ ::std::os::raw::c_int,
+ y:
+ ::std::os::raw::c_int)
+ -> ::std::os::raw::c_int>;
+}
+#[repr(u32)]
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+pub enum Foo { Bar = 0, Qux = 1, }
+#[repr(i32)]
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+pub enum Neg { MinusOne = -1, One = 1, }
diff --git a/tests/tests.rs b/tests/tests.rs
index bb965bd7..c7e03cb5 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -160,3 +160,29 @@ extern \"C\" {
}
");
}
+
+#[test]
+fn test_multiple_header_calls_in_builder() {
+ let actual = builder()
+ .header(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/func_ptr.h"))
+ .header(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/enum.h"))
+ .generate()
+ .unwrap()
+ .to_string();
+
+ let expected = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/expectations/tests/test_multiple_header_calls_in_builder.rs"));
+
+ if actual != expected {
+ println!("Generated bindings differ from expected!");
+
+ for diff in diff::lines(&actual, &expected) {
+ match diff {
+ diff::Result::Left(l) => println!("-{}", l),
+ diff::Result::Both(l, _) => println!(" {}", l),
+ diff::Result::Right(r) => println!("+{}", r),
+ }
+ }
+
+ panic!();
+ }
+}