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

Stabilize --crate-type flag for cargo rustc #10838

Merged
merged 3 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn cli() -> App {
.arg(multi_opt(
CRATE_TYPE_ARG_NAME,
"CRATE-TYPE",
"Comma separated list of types of crates for the compiler to emit (unstable)",
"Comma separated list of types of crates for the compiler to emit",
))
.arg_target_dir()
.arg_manifest_path()
Expand Down Expand Up @@ -88,9 +88,6 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
compile_opts.target_rustc_crate_types = if crate_types.is_empty() {
None
} else {
config
.cli_unstable()
.fail_if_stable_opt(CRATE_TYPE_ARG_NAME, 10083)?;
Some(crate_types)
};
ops::compile(&ws, &compile_opts)?;
Expand Down
17 changes: 17 additions & 0 deletions src/doc/man/cargo-rustc.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ See the [the reference](../reference/profiles.html) for more details on profiles

{{> options-timings }}

{{#option "`--crate-type` _crate-type_"}}
Build for the given crate type. This flag accepts a comma-separated list of
1 or more crate types, of which the allowed values are the same as `crate-type`
field in the manifest for configurating a Cargo target. See
[`crate-type` field](../reference/cargo-targets.html#the-crate-type-field)
for possible values.

If the manifest contains a list, and `--crate-type` is provided,
the command-line argument value will override what is in the manifest.

This flag only works when building a `lib` or `example` library target.
{{/option}}

{{/options}}

### Output Options
Expand Down Expand Up @@ -124,5 +137,9 @@ See the [the reference](../reference/profiles.html) for more details on profiles

cargo rustc --lib -- -Z print-type-sizes

3. Override `crate-type` field in Cargo.toml with command-line option:

cargo rustc --lib --crate-type lib,cdylib

## SEE ALSO
{{man "cargo" 1}}, {{man "cargo-build" 1}}, {{man "rustc" 1}}
17 changes: 17 additions & 0 deletions src/doc/man/generated_txt/cargo-rustc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ OPTIONS
o json (unstable, requires -Zunstable-options): Emit
machine-readable JSON information about timing information.

--crate-type crate-type
Build for the given crate type. This flag accepts a comma-separated
list of 1 or more crate types, of which the allowed values are the
same as crate-type field in the manifest for configurating a Cargo
target. See crate-type field
<https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field>
for possible values.

If the manifest contains a list, and --crate-type is provided, the
command-line argument value will override what is in the manifest.

This flag only works when building a lib or example library target.

Output Options
--target-dir directory
Directory for all generated artifacts and intermediate files. May
Expand Down Expand Up @@ -340,6 +353,10 @@ EXAMPLES

cargo rustc --lib -- -Z print-type-sizes

3. Override crate-type field in Cargo.toml with command-line option:

cargo rustc --lib --crate-type lib,cdylib

SEE ALSO
cargo(1), cargo-build(1), rustc(1)

15 changes: 15 additions & 0 deletions src/doc/src/commands/cargo-rustc.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ information about timing information.</li>



<dt class="option-term" id="option-cargo-rustc---crate-type"><a class="option-anchor" href="#option-cargo-rustc---crate-type"></a><code>--crate-type</code> <em>crate-type</em></dt>
<dd class="option-desc">Build for the given crate type. This flag accepts a comma-separated list of
1 or more crate types, of which the allowed values are the same as <code>crate-type</code>
field in the manifest for configurating a Cargo target. See
<a href="../reference/cargo-targets.html#the-crate-type-field"><code>crate-type</code> field</a>
for possible values.</p>
<p>If the manifest contains a list, and <code>--crate-type</code> is provided,
the command-line argument value will override what is in the manifest.</p>
<p>This flag only works when building a <code>lib</code> or <code>example</code> library target.</dd>


</dl>

### Output Options
Expand Down Expand Up @@ -411,5 +422,9 @@ details on environment variables that Cargo reads.

cargo rustc --lib -- -Z print-type-sizes

3. Override `crate-type` field in Cargo.toml with command-line option:

cargo rustc --lib --crate-type lib,cdylib

## SEE ALSO
[cargo(1)](cargo.html), [cargo-build(1)](cargo-build.html), [rustc(1)](https://doc.rust-lang.org/rustc/index.html)
23 changes: 6 additions & 17 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,23 +409,6 @@ like to stabilize it somehow!

[rust-lang/rust#64158]: https://github.com/rust-lang/rust/pull/64158

### crate-type
* Tracking Issue: [#10083](https://github.com/rust-lang/cargo/issues/10083)
* RFC: [#3180](https://github.com/rust-lang/rfcs/pull/3180)
* Original Pull Request: [#10093](https://github.com/rust-lang/cargo/pull/10093)

`cargo rustc --crate-type=lib,cdylib` forwards the `--crate-type` flag to `rustc`.
This runs `rustc` with the corresponding
[`--crate-type`](https://doc.rust-lang.org/rustc/command-line-arguments.html#--crate-type-a-list-of-types-of-crates-for-the-compiler-to-emit)
flag, and compiling.

When using it, it requires the `-Z unstable-options`
command-line option:

```console
cargo rustc --crate-type lib,cdylib -Z unstable-options
```

### keep-going
* Tracking Issue: [#10496](https://github.com/rust-lang/cargo/issues/10496)

Expand Down Expand Up @@ -1547,3 +1530,9 @@ information.
The `-Z multitarget` option has been stabilized in the 1.64 release.
See [`build.target`](config.md#buildtarget) for more information about
setting the default target platform triples.

### crate-type

The `--crate-type` flag for `cargo rustc` has been stabilized in the 1.64
release. See the [`cargo rustc` documentation](../commands/cargo-rustc.md)
for more information.
24 changes: 24 additions & 0 deletions src/etc/man/cargo-rustc.1
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ and does not provide machine\-readable timing data.
information about timing information.
.RE
.RE
.sp
\fB\-\-crate\-type\fR \fIcrate\-type\fR
.RS 4
Build for the given crate type. This flag accepts a comma\-separated list of
1 or more crate types, of which the allowed values are the same as \fBcrate\-type\fR
field in the manifest for configurating a Cargo target. See
\fI\f(BIcrate\-type\fI field\fR <https://doc.rust\-lang.org/cargo/reference/cargo\-targets.html#the\-crate\-type\-field>
for possible values.
.sp
If the manifest contains a list, and \fB\-\-crate\-type\fR is provided,
the command\-line argument value will override what is in the manifest.
.sp
This flag only works when building a \fBlib\fR or \fBexample\fR library target.
.RE
.SS "Output Options"
.sp
\fB\-\-target\-dir\fR \fIdirectory\fR
Expand Down Expand Up @@ -432,5 +446,15 @@ cargo rustc \-\-lib \-\- \-Z print\-type\-sizes
.fi
.RE
.RE
.sp
.RS 4
\h'-04' 3.\h'+01'Override \fBcrate\-type\fR field in Cargo.toml with command\-line option:
.sp
.RS 4
.nf
cargo rustc \-\-lib \-\-crate\-type lib,cdylib
.fi
.RE
.RE
.SH "SEE ALSO"
\fBcargo\fR(1), \fBcargo\-build\fR(1), \fBrustc\fR(1)
41 changes: 9 additions & 32 deletions tests/testsuite/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,6 @@ fn fails_with_args_to_all_binaries() {
.run();
}

#[cargo_test]
fn fails_with_crate_type_and_without_unstable_options() {
let p = project().file("src/lib.rs", r#" "#).build();

p.cargo("rustc --crate-type lib")
.masquerade_as_nightly_cargo(&["crate-type"])
.with_status(101)
.with_stderr(
"[ERROR] the `crate-type` flag is unstable, pass `-Z unstable-options` to enable it
See https://github.com/rust-lang/cargo/issues/10083 for more information about the `crate-type` flag.",
)
.run();
}

#[cargo_test]
fn fails_with_crate_type_to_multi_binaries() {
let p = project()
Expand All @@ -157,8 +143,7 @@ fn fails_with_crate_type_to_multi_binaries() {
.file("src/lib.rs", r#" "#)
.build();

p.cargo("rustc --crate-type lib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc --crate-type lib")
.with_status(101)
.with_stderr(
"[ERROR] crate types to rustc can only be passed to one target, consider filtering
Expand Down Expand Up @@ -191,8 +176,7 @@ fn fails_with_crate_type_to_multi_examples() {
.file("examples/ex2.rs", "")
.build();

p.cargo("rustc -v --example ex1 --example ex2 --crate-type lib,cdylib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc -v --example ex1 --example ex2 --crate-type lib,cdylib")
.with_status(101)
.with_stderr(
"[ERROR] crate types to rustc can only be passed to one target, consider filtering
Expand All @@ -205,8 +189,7 @@ the package by passing, e.g., `--lib` or `--example` to specify a single target"
fn fails_with_crate_type_to_binary() {
let p = project().file("src/bin/foo.rs", "fn main() {}").build();

p.cargo("rustc --crate-type lib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc --crate-type lib")
.with_status(101)
.with_stderr(
"[ERROR] crate types can only be specified for libraries and example libraries.
Expand All @@ -219,8 +202,7 @@ Binaries, tests, and benchmarks are always the `bin` crate type",
fn build_with_crate_type_for_foo() {
let p = project().file("src/lib.rs", "").build();

p.cargo("rustc -v --crate-type cdylib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc -v --crate-type cdylib")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([CWD])
Expand Down Expand Up @@ -257,8 +239,7 @@ fn build_with_crate_type_for_foo_with_deps() {
.file("a/src/lib.rs", "pub fn hello() {}")
.build();

p.cargo("rustc -v --crate-type cdylib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc -v --crate-type cdylib")
.with_stderr(
"\
[COMPILING] a v0.1.0 ([CWD]/a)
Expand All @@ -275,8 +256,7 @@ fn build_with_crate_type_for_foo_with_deps() {
fn build_with_crate_types_for_foo() {
let p = project().file("src/lib.rs", "").build();

p.cargo("rustc -v --crate-type lib,cdylib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc -v --crate-type lib,cdylib")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([CWD])
Expand Down Expand Up @@ -307,8 +287,7 @@ fn build_with_crate_type_to_example() {
.file("examples/ex.rs", "")
.build();

p.cargo("rustc -v --example ex --crate-type cdylib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc -v --example ex --crate-type cdylib")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([CWD])
Expand Down Expand Up @@ -340,8 +319,7 @@ fn build_with_crate_types_to_example() {
.file("examples/ex.rs", "")
.build();

p.cargo("rustc -v --example ex --crate-type lib,cdylib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc -v --example ex --crate-type lib,cdylib")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([CWD])
Expand Down Expand Up @@ -377,8 +355,7 @@ fn build_with_crate_types_to_one_of_multi_examples() {
.file("examples/ex2.rs", "")
.build();

p.cargo("rustc -v --example ex1 --crate-type lib,cdylib -Zunstable-options")
.masquerade_as_nightly_cargo(&["crate-type"])
p.cargo("rustc -v --example ex1 --crate-type lib,cdylib")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([CWD])
Expand Down