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

Allow --crate-type for binaries #14225

Closed
daxpedda opened this issue Jul 9, 2024 · 1 comment
Closed

Allow --crate-type for binaries #14225

daxpedda opened this issue Jul 9, 2024 · 1 comment
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.

Comments

@daxpedda
Copy link
Contributor

daxpedda commented Jul 9, 2024

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) require cdylib and iOS requires staticlib.

There are multiple ways to achieve this:

  1. Create a separate library package.
  2. Explicitly create a [lib] target, e.g.
    [lib]
    crate-type = ["cdylib", "staticlib"]
    path = "src/main.rs
    This has the downside of superfluously compiling more targets then necessary for specific platforms. E.g. Android doesn't need staticlib but gets both nevertheless when using cargo 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.
  3. Creating an automatic [lib] target by creating a lib.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:

  • Web requires post-processing the Wasm file (in the case of wasm-bindgen) and generating an appropriate JS file. This is popularly handled by wasm-pack.
  • Android requires generating an APK package, which is popularly done by cargo apk.
  • iOS requires going through XCode (I know of cargo dinghy and cargo mobile2 but never tried them).

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.

@daxpedda daxpedda added 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. labels Jul 9, 2024
@epage
Copy link
Contributor

epage commented Jul 10, 2024

The root problem sounds like its the same as #12260, this just discusses an alternative solution. I'm not seeing the value of forking the conversation for that problem to two separate issues for us to coordinate between and lean towards closing this in favor of #12260 where we can talk about several solutions and not just the one proposed with the problem Let us know if there is a reason this should be kept open, rather than discussing it there.

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

2 participants