From 41d7fa66049c0787b8ea26a5bc39445147060cff Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 1 Feb 2024 09:10:59 +0800 Subject: [PATCH 1/4] Add no-export in cbindgen.toml config Signed-off-by: tison --- docs.md | 6 ++++-- src/bindgen/bindings.rs | 10 ++++++++++ src/bindgen/builder.rs | 9 +++++++++ src/bindgen/config.rs | 2 ++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/docs.md b/docs.md index d8c863171..f490fb38b 100644 --- a/docs.md +++ b/docs.md @@ -267,8 +267,6 @@ pub mod my_uninteresting_mod; // This won't be scanned by cbindgen. cbindgen will usually emit all items it finds, as instructed by the parse and export config sections. This annotation will make cbindgen skip this item from the output, while still being aware of it. This is useful for a) suppressing "Can't find" errors and b) emitting `struct my_struct` for types in a different header (rather than a bare `my_struct`). -There is no equivalent config for this annotation - by comparison, the export exclude config will cause cbindgen to not be aware of the item at all. - Note that cbindgen will still traverse `no-export` structs that are `repr(C)` to emit types present in the fields. You will need to manually exclude those types in your config if desired. ``` @@ -579,6 +577,10 @@ include = ["MyOrphanStruct", "MyGreatTypeRename"] # default: [] exclude = ["Bad"] +# A list of items to not export in the generated bindings +# default: [] +no_export = ["Inner"] + # A prefix to add before the name of every item # default: no prefix is added prefix = "CAPI_" diff --git a/src/bindgen/bindings.rs b/src/bindgen/bindings.rs index 410b11f30..017bc51b2 100644 --- a/src/bindgen/bindings.rs +++ b/src/bindgen/bindings.rs @@ -363,6 +363,16 @@ impl Bindings { continue; } + if self + .config + .export + .no_export + .iter() + .any(|i| i == item.deref().export_name()) + { + continue; + } + out.new_line_if_not_start(); match *item { ItemContainer::Constant(..) => unreachable!(), diff --git a/src/bindgen/builder.rs b/src/bindgen/builder.rs index a0328b409..56fbde955 100644 --- a/src/bindgen/builder.rs +++ b/src/bindgen/builder.rs @@ -179,6 +179,15 @@ impl Builder { self } + #[allow(unused)] + pub fn no_export_item>(mut self, item_name: S) -> Builder { + self.config + .export + .no_export + .push(String::from(item_name.as_ref())); + self + } + #[allow(unused)] pub fn rename_item>(mut self, from: S, to: S) -> Builder { self.config diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index 5012414b7..622bae43f 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -327,6 +327,8 @@ pub struct ExportConfig { pub include: Vec, /// A list of items to not include in the generated bindings pub exclude: Vec, + /// A list of items to not export in the generated bindings + pub no_export: Vec, /// Table of name conversions to apply to item names pub rename: HashMap, /// Table of raw strings to prepend to the body of items. From ad87419b5dc972da17fe5ea0651b80be22fbd90f Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 28 Feb 2024 11:49:47 +0800 Subject: [PATCH 2/4] Address comment Signed-off-by: tison --- docs.md | 8 ++++++-- tests/rust/inner_no_export.rs | 12 ++++++++++++ tests/rust/inner_no_export.toml | 4 ++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/rust/inner_no_export.rs create mode 100644 tests/rust/inner_no_export.toml diff --git a/docs.md b/docs.md index f490fb38b..4e64c2b42 100644 --- a/docs.md +++ b/docs.md @@ -573,11 +573,15 @@ usize_is_size_t = true # default: [] include = ["MyOrphanStruct", "MyGreatTypeRename"] -# A list of items to not include in the generated bindings +# A list of items to not include in the generated bindings. The items won't get parsed +# at all. +# # default: [] exclude = ["Bad"] -# A list of items to not export in the generated bindings +# A list of items to not export in the generated bindings. The items will still get +# parsed. +# # default: [] no_export = ["Inner"] diff --git a/tests/rust/inner_no_export.rs b/tests/rust/inner_no_export.rs new file mode 100644 index 000000000..045d0727b --- /dev/null +++ b/tests/rust/inner_no_export.rs @@ -0,0 +1,12 @@ +pub mod inner { + /// Inner docs should not be exported. + pub struct Inner; +} + +pub struct Outer { + inner: inner::Inner, +} + +#[no_mangle] +pub extern "C" fn root(a: Outer) +{ } diff --git a/tests/rust/inner_no_export.toml b/tests/rust/inner_no_export.toml new file mode 100644 index 000000000..a38f0fc2f --- /dev/null +++ b/tests/rust/inner_no_export.toml @@ -0,0 +1,4 @@ +[export] +no_export = [ + "Inner", +] From e38386ad750bd049523b5323bde767cce70184b4 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 28 Feb 2024 11:53:07 +0800 Subject: [PATCH 3/4] Address comment Signed-off-by: tison --- tests/rust/inner_no_export.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/rust/inner_no_export.rs b/tests/rust/inner_no_export.rs index 045d0727b..f90cf2bf8 100644 --- a/tests/rust/inner_no_export.rs +++ b/tests/rust/inner_no_export.rs @@ -3,8 +3,9 @@ pub mod inner { pub struct Inner; } +#[repr(C)] pub struct Outer { - inner: inner::Inner, + inner: *mut inner::Inner, } #[no_mangle] From 606bf11936ca5e0a8263ed9d2027cde1a3c3b614 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 28 Feb 2024 12:35:30 +0800 Subject: [PATCH 4/4] more Signed-off-by: tison --- tests/expectations/inner_no_export.compat.c | 20 ++++++++++++++++++++ tests/rust/inner_no_export.rs | 11 ++++++----- tests/rust/inner_no_export.toml | 8 ++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 tests/expectations/inner_no_export.compat.c diff --git a/tests/expectations/inner_no_export.compat.c b/tests/expectations/inner_no_export.compat.c new file mode 100644 index 000000000..07c2eb9c7 --- /dev/null +++ b/tests/expectations/inner_no_export.compat.c @@ -0,0 +1,20 @@ +#include +#include +#include + +/** + * Outer docs should be exported. + */ +typedef struct { + Inner *inner; +} Outer; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void root(const Outer *a); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/rust/inner_no_export.rs b/tests/rust/inner_no_export.rs index f90cf2bf8..fe45a2b63 100644 --- a/tests/rust/inner_no_export.rs +++ b/tests/rust/inner_no_export.rs @@ -1,13 +1,14 @@ -pub mod inner { - /// Inner docs should not be exported. - pub struct Inner; +/// Inner docs should not be exported. +pub struct Inner { + a: i32, } +/// Outer docs should be exported. #[repr(C)] pub struct Outer { - inner: *mut inner::Inner, + inner: *mut Inner, } #[no_mangle] -pub extern "C" fn root(a: Outer) +pub extern "C" fn root(a: *const Outer) { } diff --git a/tests/rust/inner_no_export.toml b/tests/rust/inner_no_export.toml index a38f0fc2f..84b13ad8f 100644 --- a/tests/rust/inner_no_export.toml +++ b/tests/rust/inner_no_export.toml @@ -1,3 +1,11 @@ +cpp_compat = true +language = "C" +no_includes = true +sys_includes = ["stdint.h", "stddef.h", "stdbool.h"] + +[parse] +parse_deps = true + [export] no_export = [ "Inner",