Skip to content

Commit cfa5d53

Browse files
authoredNov 14, 2021
Merge pull request #3180 from seanmonstar/cargo-crate-type-cli
Cargo `--crate-type` CLI Argument
2 parents 6214e77 + ac3ac32 commit cfa5d53

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
 

‎text/3180-cargo-cli-crate-type.md

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# RFC: Cargo `--crate-type` CLI Argument
2+
3+
- Feature Name: `cargo_cli_crate_type`
4+
- Start Date: 2021-10-07
5+
- RFC PR: [rust-lang/rfcs#3180](https://github.com/rust-lang/rfcs/pull/3180)
6+
- Tracking Issue: [rust-lang/cargo#10083](https://github.com/rust-lang/cargo/issues/10083)
7+
8+
# Summary
9+
[summary]: #summary
10+
11+
Add the ability to provide `--crate-type <crate-type>` as an argument to `cargo rustc`. This would have the same affect of adding `crate-type` in the `Cargo.toml`, while taking higher priority than any value specified there.
12+
13+
[Previous implementation PR](https://github.com/rust-lang/cargo/pull/8789)
14+
15+
# Motivation
16+
[motivation]: #motivation
17+
18+
A crate can declare in its `Cargo.toml` manifest what sort of sort of compilation artifact to produce. However, there are times when the *user* of such a crate, as opposed to the author, would want to alter what artifacts are produced.
19+
20+
Some crates may provide both a Rust API and an optional C API. A current example is the [hyper](https://github.com/hyperium/hyper) crate. Most users of the Rust API only need an `rlib`, so forcing the compilation of a `cdylib` as well is a waste. It can also cause problems for people including such a crate as a dependency when cross-compiling, or when combining with `-C prefer-dynamic` ([example](https://github.com/rust-lang/rust/issues/82151)).
21+
22+
Another usecase is sharing a library across different platforms (e.g. iOS, Android, WASM). iOS requires static linking (`staticlib`) [[1]](https://github.com/rust-lang/cargo/issues/4881#issuecomment-732751642), [[2]](https://github.com/rust-lang/rust/pull/77716), Android and WASM require dynamic linking (`cdylib`) and in order to use it as a dependency in Rust requires `rlib`.
23+
24+
Lastly, being able to pick a specific crate type also decreases build times when you already know which platform you're targeting.
25+
26+
# Guide-level explanation
27+
[guide-level-explanation]: #guide-level-explanation
28+
29+
When a user builds a library, using `cargo rustc`, they can provide a `--crate-type` argument to adjust the crate type that is compiled. The argument can be any that can also be listed in the `Cargo.toml`.
30+
31+
Some examples:
32+
33+
```shell
34+
cargo rustc --crate-type staticlib
35+
36+
cargo rustc --crate-type cdylib --features ffi
37+
```
38+
39+
# Reference-level explanation
40+
[reference-level-explanation]: #reference-level-explanation
41+
42+
A new command-line argument, `--crate-type`, will be added to Cargo. It must be provided a comma-separated list of 1 or more crate types, of which the allowed values are the same as can be [provided in the manifest](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field).
43+
44+
The argument will be added for `cargo rustc`.
45+
46+
As with the existing `crate-type` manifest property, this will only work when building a `lib` or `example`.
47+
48+
If the manifest contains a list, and this new command-line argument is provided by the user, the command-line argument value will override what is in the manifest. For example:
49+
50+
```toml
51+
[lib]
52+
crate-type = ["lib", "staticlib", "cdylib"]
53+
```
54+
55+
```shell
56+
cargo rustc --crate-type staticlib
57+
```
58+
59+
This will produce output only as a `staticlib`, ignoring the other values in the manifest.
60+
61+
# Drawbacks
62+
[drawbacks]: #drawbacks
63+
64+
The usual reasons to not do this apply here:
65+
66+
- An additional feature means more surface area to maintain, and more possibility of bugs.
67+
- The Cargo team is already stretched too thin to ask for another feature. However, in this case, an implementation is already written.
68+
69+
# Rationale and alternatives
70+
[rationale-and-alternatives]: #rationale-and-alternatives
71+
72+
This gives direct control of the compilation to the end user, instead of making them depend on whatever the crate author put in the `Cargo.toml`.
73+
74+
An alternative detail to this proposal is to make it so specifying `--crate-type` adds to the list in the `Cargo.toml`, instead of overriding it. However, there would still be a need for end users to override, so there would need to be an additional argument, such as `--no-default-crate-type`. Overriding feels like the less complex solution for a user to comprehend.
75+
76+
The story around compiling Rust for different targets, and especially in ways that are compatible with C, needs to grow stronger. Choosing not to do this would mean this pain point would continue to exist, which hurts the adoption of writing libraries in Rust instead of C.
77+
78+
# Prior art
79+
[prior-art]: #prior-art
80+
81+
There are a couple similar-looking features already in cargo:
82+
83+
- `--target`: When building a crate, a user can specify the specific target architecture of the compilation output. When not specified, it defaults to the host architecture.
84+
- `--features`: A user can specify a list of features to enable when building a crate directly with `cargo build`. The `Cargo.toml` can provide a default set of features to compile. This differs from the other art, since specifying `--features` will *add* to the default, instead of *overriding* it.
85+
86+
# Unresolved questions
87+
[unresolved-questions]: #unresolved-questions
88+
89+
**Should a user be able to configure the crate-type of _all_ crates in the dependency graph?**
90+
91+
When this feature was first proposed, [it was suggested](https://github.com/rust-lang/cargo/pull/8789#issuecomment-713161246) that a user may wish to configure many dependencies at once, not just the top level crate. This RFC doesn't propose how to solve that, but claims that it can be safely considered out of scope.
92+
93+
First, such a feature is much larger, and there isn't prior art *in Cargo* to have command-line arguments configuring other dependencies. Designing that would take much more effort. There doesn't seem to be resources available to explore that.
94+
95+
Additionally, the small focus of the feature proposed in this RFC doesn't prevent that larger design from being explored and added at a later point. Figuring out a way to specify configuration arguments for other dependencies would likely need to work for the existing `--features` argument. Therefore, this isn't a 1-way-door decision, and thus we don't need to stop fixing this particular pain before that is figured out.
96+
97+
# Future possibilities
98+
[future-possibilities]: #future-possibilities
99+
100+
The command-line argument may be useful for other `cargo` commands in the future. This RFC starts with a conservative list.
101+
102+
It may also be of interest to allow a crate "feature" enable different crate-types, such as `--features ffi` enabling `--crate-type cdylib`.

0 commit comments

Comments
 (0)
Please sign in to comment.