cargo
cargo copied to clipboard
Allow crate-type=["staticlib","cdylib"] for [[bin]]
Cargo currently enforces only one library crate per package which makes total sense for Rust libraries. However it doesn't make much sense for cdylibs or staticlibs which are not going to be consumed by downstream Rust dependencies. Requiring that only the library crate can be a cdylib or staticlib, and not binary crates, sometimes forces people into mixing lib and cdylib or staticlib and Rust currently produces suboptimal results when doing so. It also means people cannot produce multiple cdylibs or staticlibs from a single package.
I'd like to propose extending [[bin]] to allowcdylib and staticlib for the crate-type.
I'd like to see this as well.
I don't know if it makes sense to do this by extending [[bin]], or by adding a new section [[clib]] or similar.
See #8628.
This is a very good proposal. And I have a use case where it would fit perfectly.
When it is necessary to use the same target to generate a desktop executable, but a cdylib for android and wasm, and a staticlib for ios.
It is related to: https://github.com/rust-lang/rfcs/pull/3180
It would not be possible to use the [lib] target to generate these libraries, as there are #[no_mangle] functions that cannot be present in an ordinary rlib, due to name collision. And that way it is also possible to have several native libraries using just one package.
This could also be potentially useful for building multiple wasm modules.
My current workaround is to use [[example]] section. This way I can create a cdylib binary, while keeping the core functionality in the [lib] that can be reused from other rust code. See sqlite-hashes and sqlite-compressions -- loadable SQLite extensions.