From 1a824c04f21f22d1488609cedf8d0c7b13bb83eb Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sun, 24 Nov 2019 11:28:40 +0100 Subject: [PATCH] Add support for verbatim content after includes Let the user add raw C code in the generated header after the include blocks. It matches what bindgen provides with the raw_lines option. --- docs.md | 2 ++ src/bindgen/bindings.rs | 6 ++++++ src/bindgen/builder.rs | 6 ++++++ src/bindgen/config.rs | 3 +++ template.toml | 1 + tests/expectations/both/raw_lines.c | 12 ++++++++++++ tests/expectations/both/raw_lines.compat.c | 20 ++++++++++++++++++++ tests/expectations/raw_lines.c | 12 ++++++++++++ tests/expectations/raw_lines.compat.c | 20 ++++++++++++++++++++ tests/expectations/raw_lines.cpp | 16 ++++++++++++++++ tests/expectations/tag/raw_lines.c | 12 ++++++++++++ tests/expectations/tag/raw_lines.compat.c | 20 ++++++++++++++++++++ tests/rust/raw_lines.rs | 3 +++ tests/rust/raw_lines.toml | 3 +++ 14 files changed, 136 insertions(+) create mode 100644 tests/expectations/both/raw_lines.c create mode 100644 tests/expectations/both/raw_lines.compat.c create mode 100644 tests/expectations/raw_lines.c create mode 100644 tests/expectations/raw_lines.compat.c create mode 100644 tests/expectations/raw_lines.cpp create mode 100644 tests/expectations/tag/raw_lines.c create mode 100644 tests/expectations/tag/raw_lines.compat.c create mode 100644 tests/rust/raw_lines.rs create mode 100644 tests/rust/raw_lines.toml diff --git a/docs.md b/docs.md index f324a15..608f7ae 100644 --- a/docs.md +++ b/docs.md @@ -396,6 +396,8 @@ no_includes = false # default: false cpp_compat = false +# A list of lines to add verbatim after the includes block +after_includes = "#define VERSION 1" diff --git a/src/bindgen/bindings.rs b/src/bindgen/bindings.rs index 63c43f8..36d0076 100644 --- a/src/bindgen/bindings.rs +++ b/src/bindgen/bindings.rs @@ -156,6 +156,7 @@ impl Bindings { if self.config.no_includes && self.config.sys_includes.is_empty() && self.config.includes.is_empty() + && self.config.after_includes.is_none() { return; } @@ -200,6 +201,11 @@ impl Bindings { write!(out, "#include \"{}\"", include); out.new_line(); } + + if let Some(ref line) = self.config.after_includes { + write!(out, "{}", line); + out.new_line(); + } } pub fn write(&self, file: F) { diff --git a/src/bindgen/builder.rs b/src/bindgen/builder.rs index 000d641..37bb5d8 100644 --- a/src/bindgen/builder.rs +++ b/src/bindgen/builder.rs @@ -60,6 +60,12 @@ impl Builder { self } + #[allow(unused)] + pub fn with_after_include>(mut self, line: S) -> Builder { + self.config.after_includes = Some(String::from(line.as_ref())); + self + } + #[allow(unused)] pub fn with_trailer>(mut self, trailer: S) -> Builder { self.config.trailer = Some(String::from(trailer.as_ref())); diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index c5e1230..580c9e0 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -701,6 +701,8 @@ pub struct Config { pub includes: Vec, /// A list of additional system includes to put at the beginning of the generated header pub sys_includes: Vec, + /// Optional verbatim code added after the include blocks + pub after_includes: Option, /// Optional text to output at the end of the file pub trailer: Option, /// Optional name to use for an include guard @@ -768,6 +770,7 @@ impl Default for Config { header: None, includes: Vec::new(), sys_includes: Vec::new(), + after_includes: None, trailer: None, include_guard: None, pragma_once: false, diff --git a/template.toml b/template.toml index 5d7dbb3..c663a28 100644 --- a/template.toml +++ b/template.toml @@ -24,6 +24,7 @@ using_namespaces = [] sys_includes = [] includes = [] no_includes = false +after_includes = "" diff --git a/tests/expectations/both/raw_lines.c b/tests/expectations/both/raw_lines.c new file mode 100644 index 0000000..f136292 --- /dev/null +++ b/tests/expectations/both/raw_lines.c @@ -0,0 +1,12 @@ +#ifndef INCLUDE_GUARD_H +#define INCLUDE_GUARD_H + +#include +#include +#include +#include +#define VERSION 1 + +void root(void); + +#endif /* INCLUDE_GUARD_H */ diff --git a/tests/expectations/both/raw_lines.compat.c b/tests/expectations/both/raw_lines.compat.c new file mode 100644 index 0000000..05f4493 --- /dev/null +++ b/tests/expectations/both/raw_lines.compat.c @@ -0,0 +1,20 @@ +#ifndef INCLUDE_GUARD_H +#define INCLUDE_GUARD_H + +#include +#include +#include +#include +#define VERSION 1 + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif /* INCLUDE_GUARD_H */ diff --git a/tests/expectations/raw_lines.c b/tests/expectations/raw_lines.c new file mode 100644 index 0000000..f136292 --- /dev/null +++ b/tests/expectations/raw_lines.c @@ -0,0 +1,12 @@ +#ifndef INCLUDE_GUARD_H +#define INCLUDE_GUARD_H + +#include +#include +#include +#include +#define VERSION 1 + +void root(void); + +#endif /* INCLUDE_GUARD_H */ diff --git a/tests/expectations/raw_lines.compat.c b/tests/expectations/raw_lines.compat.c new file mode 100644 index 0000000..05f4493 --- /dev/null +++ b/tests/expectations/raw_lines.compat.c @@ -0,0 +1,20 @@ +#ifndef INCLUDE_GUARD_H +#define INCLUDE_GUARD_H + +#include +#include +#include +#include +#define VERSION 1 + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif /* INCLUDE_GUARD_H */ diff --git a/tests/expectations/raw_lines.cpp b/tests/expectations/raw_lines.cpp new file mode 100644 index 0000000..1e6431b --- /dev/null +++ b/tests/expectations/raw_lines.cpp @@ -0,0 +1,16 @@ +#ifndef INCLUDE_GUARD_H +#define INCLUDE_GUARD_H + +#include +#include +#include +#include +#define VERSION 1 + +extern "C" { + +void root(); + +} // extern "C" + +#endif // INCLUDE_GUARD_H diff --git a/tests/expectations/tag/raw_lines.c b/tests/expectations/tag/raw_lines.c new file mode 100644 index 0000000..f136292 --- /dev/null +++ b/tests/expectations/tag/raw_lines.c @@ -0,0 +1,12 @@ +#ifndef INCLUDE_GUARD_H +#define INCLUDE_GUARD_H + +#include +#include +#include +#include +#define VERSION 1 + +void root(void); + +#endif /* INCLUDE_GUARD_H */ diff --git a/tests/expectations/tag/raw_lines.compat.c b/tests/expectations/tag/raw_lines.compat.c new file mode 100644 index 0000000..05f4493 --- /dev/null +++ b/tests/expectations/tag/raw_lines.compat.c @@ -0,0 +1,20 @@ +#ifndef INCLUDE_GUARD_H +#define INCLUDE_GUARD_H + +#include +#include +#include +#include +#define VERSION 1 + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif /* INCLUDE_GUARD_H */ diff --git a/tests/rust/raw_lines.rs b/tests/rust/raw_lines.rs new file mode 100644 index 0000000..03de69f --- /dev/null +++ b/tests/rust/raw_lines.rs @@ -0,0 +1,3 @@ +#[no_mangle] +pub extern "C" fn root() { +} diff --git a/tests/rust/raw_lines.toml b/tests/rust/raw_lines.toml new file mode 100644 index 0000000..13a2eba --- /dev/null +++ b/tests/rust/raw_lines.toml @@ -0,0 +1,3 @@ +include_guard = "INCLUDE_GUARD_H" +no_includes = false +after_includes = "#define VERSION 1"