diff options
author | Travis Finkenauer <tmfinken@gmail.com> | 2020-06-28 21:14:48 -0400 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-06-29 13:25:00 +0200 |
commit | 3151087a3a3940aef33c4a3d23671a05683c6e85 (patch) | |
tree | 4b193276016eb66496472d528178ef41ee5c81f8 | |
parent | 30ac96231ad4d8af8a92a5f83043bd61682691d5 (diff) |
Handle multiple headers for command_line_flags()
Output from Builder::command_line_flags() would fail if more than one
header were provided. This adds extra headers via the '-include' clang
option.
-rw-r--r-- | src/lib.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 42 |
2 files changed, 49 insertions, 5 deletions
@@ -499,11 +499,13 @@ impl Builder { } if self.input_headers.len() > 1 { - output_vector.extend( - self.input_headers[..self.input_headers.len() - 1] - .iter() - .cloned(), - ); + // To pass more than one header, we need to pass all but the last + // header via the `-include` clang arg + output_vector.reserve(2 * self.input_headers.len() - 2); + for header in &self.input_headers[..self.input_headers.len() - 1] { + output_vector.push("-include".to_string()); + output_vector.push(header.clone()); + } } output_vector diff --git a/src/main.rs b/src/main.rs index c554ac50..168d6cc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,3 +86,45 @@ fn print_verbose_err() { https://github.com/rust-lang/rust-bindgen/issues/new" ); } + +#[cfg(test)] +mod test { + use std::path::PathBuf; + + fn build_flags_output_helper(builder: &bindgen::Builder) { + let mut command_line_flags = builder.command_line_flags(); + command_line_flags.insert(0, "bindgen".to_string()); + + let flags_quoted: Vec<String> = command_line_flags + .iter() + .map(|x| format!("'{}'", x)) + .collect(); + let flags_str = flags_quoted.join(" "); + println!("{}", flags_str); + + crate::options::builder_from_flags(command_line_flags.into_iter()) + .unwrap(); + } + + #[test] + fn commandline_flag_roundtrip() { + // test1: various options + let bindings = bindgen::Builder::default() + .header("tests/headers/char.h") + .record_matches(false) + .size_t_is_usize(true) + .rustfmt_bindings(false) + .rustfmt_configuration_file(Some(PathBuf::from("/dev/null"))) + .no_partialeq(".") + .no_copy(".") + .no_hash("."); + build_flags_output_helper(&bindings); + + // test2: multiple headers + let bindings = bindgen::Builder::default() + .header("tests/headers/char.h") + .header("tests/headers/func_ptr.h") + .header("tests/headers/16-byte-alignment.h"); + build_flags_output_helper(&bindings); + } +} |