From 8faf7011ae63850e46377250d1d9a721648d6001 Mon Sep 17 00:00:00 2001 From: konstin Date: Thu, 30 Aug 2018 19:39:47 +0200 Subject: [PATCH] Add no_includes option --- src/bindgen/bindings.rs | 12 +++++++++--- src/bindgen/builder.rs | 6 ++++++ src/bindgen/config.rs | 6 ++++++ tests/expectations/both/no_includes.c | 1 + tests/expectations/no_includes.c | 1 + tests/expectations/no_includes.cpp | 5 +++++ tests/expectations/tag/no_includes.c | 1 + tests/rust/no_includes.rs | 3 +++ tests/rust/no_includes.toml | 1 + 9 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/expectations/both/no_includes.c create mode 100644 tests/expectations/no_includes.c create mode 100644 tests/expectations/no_includes.cpp create mode 100644 tests/expectations/tag/no_includes.c create mode 100644 tests/rust/no_includes.rs create mode 100644 tests/rust/no_includes.toml diff --git a/src/bindgen/bindings.rs b/src/bindgen/bindings.rs index 0711f0f..ee05761 100644 --- a/src/bindgen/bindings.rs +++ b/src/bindgen/bindings.rs @@ -65,9 +65,7 @@ impl Bindings { } } - pub fn write(&self, file: F) { - let mut out = SourceWriter::new(file, &self.config); - + pub fn write_headers(&self, out: &mut SourceWriter) { if let Some(ref f) = self.config.header { out.new_line_if_not_start(); write!(out, "{}", f); @@ -119,6 +117,14 @@ impl Bindings { write!(out, "#include \"{}\"", include); out.new_line(); } + } + + pub fn write(&self, file: F) { + let mut out = SourceWriter::new(file, &self.config); + + if !self.config.no_includes { + self.write_headers(&mut out); + } for constant in &self.constants { if constant.ty.is_primitive_or_ptr_primitive() { diff --git a/src/bindgen/builder.rs b/src/bindgen/builder.rs index 75618d2..0cf93eb 100644 --- a/src/bindgen/builder.rs +++ b/src/bindgen/builder.rs @@ -40,6 +40,12 @@ impl Builder { self } + #[allow(unused)] + pub fn with_no_includes(mut self) -> Builder { + self.config.no_includes = true; + self + } + #[allow(unused)] pub fn with_include>(mut self, include: S) -> Builder { self.config.includes.push(String::from(include.as_ref())); diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index 94cee19..41679f1 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -534,6 +534,11 @@ pub struct Config { pub trailer: Option, /// Optional name to use for an include guard pub include_guard: Option, + /// Generates no includes at all. Overrides all other include options + /// + /// This option is useful when using cbindgen with tools such as python's cffi which + /// doesn't understand include directives + pub no_includes: bool, /// Optional text to output at major sections to deter manual editing pub autogen_warning: Option, /// Include a comment with the version of cbindgen used to generate the file @@ -584,6 +589,7 @@ impl Default for Config { include_guard: None, autogen_warning: None, include_version: false, + no_includes: false, namespace: None, namespaces: None, braces: Braces::SameLine, diff --git a/tests/expectations/both/no_includes.c b/tests/expectations/both/no_includes.c new file mode 100644 index 0000000..34194d0 --- /dev/null +++ b/tests/expectations/both/no_includes.c @@ -0,0 +1 @@ +void root(void); diff --git a/tests/expectations/no_includes.c b/tests/expectations/no_includes.c new file mode 100644 index 0000000..34194d0 --- /dev/null +++ b/tests/expectations/no_includes.c @@ -0,0 +1 @@ +void root(void); diff --git a/tests/expectations/no_includes.cpp b/tests/expectations/no_includes.cpp new file mode 100644 index 0000000..3c06638 --- /dev/null +++ b/tests/expectations/no_includes.cpp @@ -0,0 +1,5 @@ +extern "C" { + +void root(); + +} // extern "C" diff --git a/tests/expectations/tag/no_includes.c b/tests/expectations/tag/no_includes.c new file mode 100644 index 0000000..34194d0 --- /dev/null +++ b/tests/expectations/tag/no_includes.c @@ -0,0 +1 @@ +void root(void); diff --git a/tests/rust/no_includes.rs b/tests/rust/no_includes.rs new file mode 100644 index 0000000..03de69f --- /dev/null +++ b/tests/rust/no_includes.rs @@ -0,0 +1,3 @@ +#[no_mangle] +pub extern "C" fn root() { +} diff --git a/tests/rust/no_includes.toml b/tests/rust/no_includes.toml new file mode 100644 index 0000000..eed30dc --- /dev/null +++ b/tests/rust/no_includes.toml @@ -0,0 +1 @@ +no_includes = true