mirror of
https://gitee.com/openharmony/third_party_rust_bindgen
synced 2025-03-04 12:47:22 +00:00
Generate bindings to all headers passed to Builder::header
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.
This commit is contained in:
parent
159dd69a01
commit
65bab6fb75
28
src/lib.rs
28
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
|
||||
|
@ -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, }
|
@ -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!();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user