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 a way to disable cfg to if defined() guards generation #995

Open
AlexanderSchuetz97 opened this issue Aug 17, 2024 · 2 comments
Open

Comments

@AlexanderSchuetz97
Copy link

I have a problem with a crate that provides a optional ffi api that cbindgen should generate headers for. The entire ffi api is optional in that crate as the provided functionality can be used directly as a pure rust dependency also.

This means that every export function (and struct) has this cfg feature above it.
#[cfg(feature = "ffi")]

Cbindgen is not really suited to handling this use case directly.
If I do nothing then it just does not generate the exports for any functions and the header is blank.
If I enable the feature in cbindgen.tomls parse.expand section
features = ["ffi"]
Then the generated header is correct but I get 100s of outputs
WARN: Missing [defines] entry for feature = "ffi" in cbindgen config.
This is concerning as it would drown out actual warnings that I do actually care about.

I would expect this warning to only occur once and there does not appear to be a way of telling cbindgen that this feature is always enabled in all situations where the header is used.

I have tried to example add
"feature = ffi" = ""
to the defines block.
This gets rid of the warning but the generated header is not quite correct.
It outputs stuff like this in the header:

#if defined()
bool export_function_name_here();
#endif

I would like a way to get rid of this warning and not emit any #if defined() block in the header.

@flaviojs
Copy link
Contributor

flaviojs commented Aug 18, 2024

In the current cbindgen (0.27.0) you can get around the issue by assigning a custom macro to the feature and always defining it in after_includes or header.

cbindgen.toml:

after_includes = """
#define WITH_FEATURE_FFI /* always expose #[cfg(feature = "ffi")] */
"""
[defines]
"feature = ffi" = "WITH_FEATURE_FFI"

@AlexanderSchuetz97
Copy link
Author

AlexanderSchuetz97 commented Aug 28, 2024

This generates a header that works but contains 3 more lines per definition than necessary. Not ideal.
Thank you for providing this info. I will use this as a work around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants