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

Types not exported when using build script but CLI works #931

Open
nbigaouette opened this issue Mar 7, 2024 · 2 comments
Open

Types not exported when using build script but CLI works #931

nbigaouette opened this issue Mar 7, 2024 · 2 comments
Labels

Comments

@nbigaouette
Copy link

I am using cbindgen in a build script (build.rs) to generate a C header. I am building a static lib (using crate-type = ["staticlib"]) to be linked in a C project.

I had a problem where some types, defined in one of my dependency and which appear as function arguments are not exported in the header. Trying to debug the issue I compared the generated header with what the cbindgen CLI produces.

It seems that depending on how the header is generated (build script vs cli) the required types are exported or not.

I found this issue #127 where someone suggested to use cbindgen::Builder::with_parse_deps() and cbindgen::Builder::with_parse_include() but it did not solve my issue. It also links to this RFC issue rust-lang/rfcs#2771 saying that the symbols are not properly exported by Rust. But I don't understand how this could affect differently the CLI or the build script.

I made a small example to reproduce this. See https://github.com/nbigaouette/issue_with_cbindgen. In there I have a workspace with two crates, one is a rust dependency of the other. The CLI produces a header that is valid while the build script misses a type used in the API. Both are using the same configuration.

I've enabled logging (RUST_LOG=trace) for both runs. The build script reports the following:

2024-03-07T22:18:44Z INFO  cbindgen::bindgen::parser] Take ffi::free_standing_function.
[2024-03-07T22:18:44Z WARN  cbindgen::bindgen::ir::ty] Can't find TypeA. This usually means that this type was incompatible or not found.

Clearly it couldn't find TypeA...

@nbigaouette
Copy link
Author

I was able to debug this further.

The CLI will call Builder::with_cargo(): https://github.com/mozilla/cbindgen/blob/v0.26.0/src/main.rs#L132 while I was using Builder::with_src() (https://github.com/nbigaouette/issue_with_cbindgen/blob/b98849570b82dc2bd10cb55498b5ce22/b/build.rs#L36)

Unfortunately with_cargo() is not public (https://github.com/mozilla/cbindgen/blob/v0.26.0/src/bindgen/builder.rs#L332). The closest method seems to be with_crate().

Even with with_crate(), I still need to use ParseConfig::include with the crate where the struct resides for it to appear in the final header.

@emilio
Copy link
Collaborator

emilio commented Apr 14, 2024

This seems like it was removed intentionally from the public API in 0d8c53e. @eqrion maybe you happen to have more context?

@emilio emilio added the bug label Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants