getditto / safer_ffi

Write safer FFI code in Rust without polluting it with unsafe code
http://getditto.github.io/safer_ffi
MIT License
925 stars 40 forks source link

Exports from some public mods aren't included in headers #161

Closed luketpeterson closed 1 year ago

luketpeterson commented 1 year ago

Check the attachments for a complete reproduction Cargo project.

I have a lib.rs file that re-exports some crate modules. Like this:

use ::safer_ffi::prelude::*;

pub mod my_mod;

#[ffi_export]
pub fn init() {  
}

Within the exported mod (my_mod.rs), I have an item called tokenizer, which does not end up in the generated header, in spite of it being #[ffi_export]. This is my primary complaint.

However, the plot thickens because, if I then uncomment useless_enum from the same file, both items are now included in the generated header.

Here is a complete repro case, just as described. It follows the same structure as the QuickStart example, so you can generate the header by running:

cargo build
cargo run --features headers --bin generate-headers

safer_ffi_bug_regression.zip

Thank you

danielhenrymantilla commented 1 year ago

This seems to be https://github.com/dtolnay/inventory/issues/52

From that issue, and https://github.com/dtolnay/inventory/pull/57/files, it is mentioned that using codegen-units = 1, at least when on macOS, may work around the issue. I've tested just that, and it seems to work:

# On macOS
CARGO_PROFILE_DEV_CODEGEN_UNITS=1 cargo r --features=headers
danielhenrymantilla commented 1 year ago

Alas, I don't know of a reliable way to handle this; in order for the header generation of #[ffi_export] to work, a way to globally collect header generation snippets is necessary, and at the moment, there are only two crates that offer this: ::inventory and ::linkme, and both rely on linker shenanigans, which can be brittle to certain linkers agressively removing stuff. It's thus kind of beyond my capacity to properly fix this 😔

So I'll have to close this issue, but feel free to post any further questions you may have, or ideas related to this 🙂