Skip to content

Commit 21cdebc

Browse files
Rollup merge of rust-lang#138641 - jieyouxu:print-supported-crate-types, r=Urgau
Add unstable `--print=supported-crate-types` option MCP: rust-lang/compiler-team#836 Tracking issue: rust-lang#138640 ### Test coverage Two tests: 1. `tests/ui/print-request/stability.rs` to check that `--print=supported-crate-types` is `-Zunstable-options`-gated 2. `tests/ui/print-request/supported-crate-types.rs` is added as a basic smoke test. Observe that the compiler stdout corresponds to the below *Example output* section (e.g. `proc-macro` is unsupported on `wasm32-unknown-unknown` currently). ### Example output <details> <summary>For `x86_64-unknown-linux-gnu`</summary> Notice the presence of `{c,}dylib` and `proc-macro`: ``` bin cdylib dylib lib proc-macro rlib staticlib ``` </details> <details> <summary>For `wasm32-unknown-unknown`</summary> Notice the absence of `dylib` and `proc-macro`: ``` bin cdylib lib rlib staticlib ``` </details> <details> <summary>For `x86_64-unknown-linux-musl`</summary> Notice the absence of `{c,}dylib` but presence of `proc-macro`: ``` bin lib proc-macro rlib staticlib ``` </details> ### Documentation I added an entry in the unstable book's print request section to document this `supported-crate-types` print request. ### Unresolved questions - [ ] (Name bikeshedding) is `supported-crate-types` a good name for the print request? I'm inclined to say it's good enough for an unstable print request, but may be worth revisiting at stabilization time. ### Stability This print request being added is *unstable* in this PR. A separate stabilization PR following the usual compiler flag stabilization procedure should be filed for stabilization after some baking time. ### Review remarks Best reviewed commit-by-commit. r? compiler
2 parents bb49f0d + f2cde8e commit 21cdebc

15 files changed

+98
-10
lines changed

compiler/rustc_driver_impl/src/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// tidy-alphabetical-end
2121

2222
use std::cmp::max;
23-
use std::collections::BTreeMap;
23+
use std::collections::{BTreeMap, BTreeSet};
2424
use std::ffi::OsString;
2525
use std::fmt::Write as _;
2626
use std::fs::{self, File};
@@ -61,7 +61,7 @@ use rustc_session::config::{
6161
};
6262
use rustc_session::getopts::{self, Matches};
6363
use rustc_session::lint::{Lint, LintId};
64-
use rustc_session::output::collect_crate_types;
64+
use rustc_session::output::{CRATE_TYPES, collect_crate_types, invalid_output_for_target};
6565
use rustc_session::{EarlyDiagCtxt, Session, config, filesearch};
6666
use rustc_span::FileName;
6767
use rustc_target::json::ToJson;
@@ -790,6 +790,16 @@ fn print_crate_info(
790790
sess.dcx().fatal("only Apple targets currently support deployment version info")
791791
}
792792
}
793+
SupportedCrateTypes => {
794+
let supported_crate_types = CRATE_TYPES
795+
.iter()
796+
.filter(|(_, crate_type)| !invalid_output_for_target(&sess, *crate_type))
797+
.map(|(crate_type_sym, _)| *crate_type_sym)
798+
.collect::<BTreeSet<_>>();
799+
for supported_crate_type in supported_crate_types {
800+
println_info!("{}", supported_crate_type.as_str());
801+
}
802+
}
793803
}
794804

795805
req.out.overwrite(&crate_info, sess);

compiler/rustc_session/src/config.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub const PRINT_KINDS: &[(&str, PrintKind)] = &[
5858
("relocation-models", PrintKind::RelocationModels),
5959
("split-debuginfo", PrintKind::SplitDebuginfo),
6060
("stack-protector-strategies", PrintKind::StackProtectorStrategies),
61+
("supported-crate-types", PrintKind::SupportedCrateTypes),
6162
("sysroot", PrintKind::Sysroot),
6263
("target-cpus", PrintKind::TargetCPUs),
6364
("target-features", PrintKind::TargetFeatures),
@@ -888,6 +889,7 @@ pub enum PrintKind {
888889
RelocationModels,
889890
SplitDebuginfo,
890891
StackProtectorStrategies,
892+
SupportedCrateTypes,
891893
Sysroot,
892894
TargetCPUs,
893895
TargetFeatures,
@@ -2063,7 +2065,10 @@ fn check_print_request_stability(
20632065
(print_name, print_kind): (&str, PrintKind),
20642066
) {
20652067
match print_kind {
2066-
PrintKind::AllTargetSpecsJson | PrintKind::CheckCfg | PrintKind::TargetSpecJson
2068+
PrintKind::AllTargetSpecsJson
2069+
| PrintKind::CheckCfg
2070+
| PrintKind::SupportedCrateTypes
2071+
| PrintKind::TargetSpecJson
20672072
if !unstable_opts.unstable_options =>
20682073
{
20692074
early_dcx.early_fatal(format!(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# `print=supported-crate-types`
2+
3+
The tracking issue for this feature is: [#138640](https://github.com/rust-lang/rust/issues/138640).
4+
5+
------------------------
6+
7+
This option of the `--print` flag produces a list of crate types (delimited by newlines) supported for the given target.
8+
9+
The crate type strings correspond to the values accepted by the `--crate-type` flag.
10+
11+
Intended to be used like this:
12+
13+
```bash
14+
rustc --print=supported-crate-types -Zunstable-options --target=x86_64-unknown-linux-gnu
15+
```
16+
17+
Example output for `x86_64-unknown-linux-gnu`:
18+
19+
```text
20+
bin
21+
cdylib
22+
dylib
23+
lib
24+
proc-macro
25+
rlib
26+
staticlib
27+
```

tests/run-make/rustc-help/help-v.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Options:
2929
--emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]
3030
Comma separated list of types of output for the
3131
compiler to emit
32-
--print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models]
32+
--print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models]
3333
Compiler information to print on stdout
3434
-g Equivalent to -C debuginfo=2
3535
-O Equivalent to -C opt-level=3

tests/run-make/rustc-help/help.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Options:
2929
--emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]
3030
Comma separated list of types of output for the
3131
compiler to emit
32-
--print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models]
32+
--print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models]
3333
Compiler information to print on stdout
3434
-g Equivalent to -C debuginfo=2
3535
-O Equivalent to -C opt-level=3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Argument to option 'print' missing
22
Usage:
3-
--print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models]
3+
--print [all-target-specs-json|calling-conventions|cfg|check-cfg|code-models|crate-name|deployment-target|file-names|host-tuple|link-args|native-static-libs|relocation-models|split-debuginfo|stack-protector-strategies|supported-crate-types|sysroot|target-cpus|target-features|target-libdir|target-list|target-spec-json|tls-models]
44
Compiler information to print on stdout
55

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unknown print request: `yyyy`
22
|
3-
= help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models`
3+
= help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models`
44
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
55

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Check that we point to `-Whelp` to guide the user to find the list of lints if the user requests
2+
//! `--print=lints` (which is not a valid print request).
3+
4+
//@ compile-flags: --print lints
5+
//@ error-pattern: error: unknown print request: `lints`
6+
//@ error-pattern: help: use `-Whelp` to print a list of lints
7+
//@ error-pattern: help: for more information, see the rustc book
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: unknown print request: `lints`
22
|
3-
= help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models`
3+
= help: valid print requests are: `all-target-specs-json`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `tls-models`
44
= help: use `-Whelp` to print a list of lints
55
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
66

tests/ui/print-request/stability.rs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
//@[check_cfg] compile-flags: --print=check-cfg
2323
//@[check_cfg] error-pattern: the `-Z unstable-options` flag must also be passed
2424

25+
//@ revisions: supported_crate_types
26+
//@[supported_crate_types] compile-flags: --print=supported-crate-types
27+
//@[supported_crate_types] error-pattern: the `-Z unstable-options` flag must also be passed
28+
2529
//@ revisions: target_spec_json
2630
//@[target_spec_json] compile-flags: --print=target-spec-json
2731
//@[target_spec_json] error-pattern: the `-Z unstable-options` flag must also be passed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bin
2+
cdylib
3+
dylib
4+
lib
5+
proc-macro
6+
rlib
7+
staticlib
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin
2+
lib
3+
proc-macro
4+
rlib
5+
staticlib
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Basic smoke test for `--print=supported-crate-types`, which should print a newline delimited
2+
//! list of crate types supported by the given target. This test cherry-picks a few well-known
3+
//! targets as examples.
4+
//!
5+
//! Tracking issue: <https://github.com/rust-lang/rust/issues/138640>
6+
7+
// ignore-tidy-linelength
8+
9+
//@ check-pass
10+
11+
//@ revisions: wasm musl linux
12+
13+
//@[wasm] compile-flags: --target=wasm32-unknown-unknown --print=supported-crate-types -Zunstable-options
14+
//@[wasm] needs-llvm-components: webassembly
15+
16+
//@[musl] compile-flags: --target=x86_64-unknown-linux-musl --print=supported-crate-types -Zunstable-options
17+
//@[musl] needs-llvm-components: x86
18+
19+
//@[linux] compile-flags: --target=x86_64-unknown-linux-gnu --print=supported-crate-types -Zunstable-options
20+
//@[linux] needs-llvm-components: x86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin
2+
cdylib
3+
lib
4+
rlib
5+
staticlib

tests/ui/rustc-print-info-issue-138612.rs

-2
This file was deleted.

0 commit comments

Comments
 (0)