Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-export in cbindgen.toml config #924

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```
Expand Down Expand Up @@ -579,6 +577,10 @@ include = ["MyOrphanStruct", "MyGreatTypeRename"]
# default: []
exclude = ["Bad"]

# A list of items to not export in the generated bindings
# default: []
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
no_export = ["Inner"]

# A prefix to add before the name of every item
# default: no prefix is added
prefix = "CAPI_"
Expand Down
10 changes: 10 additions & 0 deletions src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ impl Bindings {
continue;
}

if self
.config
.export
.no_export
.iter()
.any(|i| i == item.deref().export_name())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very sure if export_name is desired here but it works for me locally.

{
continue;
}

out.new_line_if_not_start();
match *item {
ItemContainer::Constant(..) => unreachable!(),
Expand Down
9 changes: 9 additions & 0 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ impl Builder {
self
}

#[allow(unused)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how it works but follow the "include"/"exclude" pattern above.

pub fn no_export_item<S: AsRef<str>>(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<S: AsRef<str>>(mut self, from: S, to: S) -> Builder {
self.config
Expand Down
2 changes: 2 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ pub struct ExportConfig {
pub include: Vec<String>,
/// A list of items to not include in the generated bindings
pub exclude: Vec<String>,
/// A list of items to not export in the generated bindings
pub no_export: Vec<String>,
/// Table of name conversions to apply to item names
pub rename: HashMap<String, String>,
/// Table of raw strings to prepend to the body of items.
Expand Down