Allow --crate-type
for binaries
#14225
Labels
C-feature-request
Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
S-triage
Status: This issue is waiting on initial triage.
Problem
Currently when trying to write cross-platform applications in Rust targeting Web (through Wasm), Android and iOS, users end up having to create a separate library target. Desktop requires
bin
, Web and Android (APKs) requirecdylib
and iOS requiresstaticlib
.There are multiple ways to achieve this:
[lib]
target, e.g.staticlib
but gets both nevertheless when usingcargo build --lib
. This would be addressed by Support [target.'cfg(...)'.lib] sections #12260.Also officially supporting this kind of setup would require removing this warning.
[lib]
target by creating alib.rs
file. Which has the same downsides as 2. and would also be addressed by Support [target.'cfg(...)'.lib] sections #12260.In reality of course all these targets require some form of post processing:
wasm-bindgen
) and generating an appropriate JS file. This is popularly handled bywasm-pack
.cargo apk
.None of these tools though can circumvent the fact that users have to do some manual work to get the basic build process working. This not only causes every cross-platform project targeting Web and mobile to a have an additional
lib.rs
, but also examples for popular libraries to require a dedicated one or either not work.Proposed Solution
Cargo already supports
cargo rustc --crate-type type
and there are already some tools out there using it, but they all still require a library target.I don't know why Cargo currently disallows using
--crate-type
on binary targets, but allowing it would be simple and essentially solve the problem outlined here.Notes
I stumbled on this limitation in #12260 (comment).
#6351 might be related.
Compiling for Web (through Wasm) actually works with a binary target and doesn't require compiling a
cdylib
, but it is generally not recommended because Rustc inserts a ton of unnecessary bloat in the resulting Wasm module.I was told before, by more knowledgeable people then myself, that this is considered a bug in Rustc.
The text was updated successfully, but these errors were encountered: