Skip to content

Add unstable --print=supported-crate-types option #138641

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

Merged
merged 4 commits into from
Mar 24, 2025
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
14 changes: 12 additions & 2 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// tidy-alphabetical-end

use std::cmp::max;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsString;
use std::fmt::Write as _;
use std::fs::{self, File};
Expand Down Expand Up @@ -61,7 +61,7 @@ use rustc_session::config::{
};
use rustc_session::getopts::{self, Matches};
use rustc_session::lint::{Lint, LintId};
use rustc_session::output::collect_crate_types;
use rustc_session::output::{CRATE_TYPES, collect_crate_types, invalid_output_for_target};
use rustc_session::{EarlyDiagCtxt, Session, config, filesearch};
use rustc_span::FileName;
use rustc_target::json::ToJson;
Expand Down Expand Up @@ -790,6 +790,16 @@ fn print_crate_info(
sess.dcx().fatal("only Apple targets currently support deployment version info")
}
}
SupportedCrateTypes => {
let supported_crate_types = CRATE_TYPES
.iter()
.filter(|(_, crate_type)| !invalid_output_for_target(&sess, *crate_type))
.map(|(crate_type_sym, _)| *crate_type_sym)
.collect::<BTreeSet<_>>();
for supported_crate_type in supported_crate_types {
println_info!("{}", supported_crate_type.as_str());
}
}
}

req.out.overwrite(&crate_info, sess);
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub const PRINT_KINDS: &[(&str, PrintKind)] = &[
("relocation-models", PrintKind::RelocationModels),
("split-debuginfo", PrintKind::SplitDebuginfo),
("stack-protector-strategies", PrintKind::StackProtectorStrategies),
("supported-crate-types", PrintKind::SupportedCrateTypes),
("sysroot", PrintKind::Sysroot),
("target-cpus", PrintKind::TargetCPUs),
("target-features", PrintKind::TargetFeatures),
Expand Down Expand Up @@ -888,6 +889,7 @@ pub enum PrintKind {
RelocationModels,
SplitDebuginfo,
StackProtectorStrategies,
SupportedCrateTypes,
Sysroot,
TargetCPUs,
TargetFeatures,
Expand Down Expand Up @@ -2063,7 +2065,10 @@ fn check_print_request_stability(
(print_name, print_kind): (&str, PrintKind),
) {
match print_kind {
PrintKind::AllTargetSpecsJson | PrintKind::CheckCfg | PrintKind::TargetSpecJson
PrintKind::AllTargetSpecsJson
| PrintKind::CheckCfg
| PrintKind::SupportedCrateTypes
| PrintKind::TargetSpecJson
if !unstable_opts.unstable_options =>
{
early_dcx.early_fatal(format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# `print=supported-crate-types`

The tracking issue for this feature is: [#138640](https://github.com/rust-lang/rust/issues/138640).

------------------------

This option of the `--print` flag produces a list of crate types (delimited by newlines) supported for the given target.

The crate type strings correspond to the values accepted by the `--crate-type` flag.

Intended to be used like this:

```bash
rustc --print=supported-crate-types -Zunstable-options --target=x86_64-unknown-linux-gnu
```

Example output for `x86_64-unknown-linux-gnu`:

```text
bin
cdylib
dylib
lib
proc-macro
rlib
staticlib
```
2 changes: 1 addition & 1 deletion tests/run-make/rustc-help/help-v.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Options:
--emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]
Comma separated list of types of output for the
compiler to emit
--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]
--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]
Compiler information to print on stdout
-g Equivalent to -C debuginfo=2
-O Equivalent to -C opt-level=3
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/rustc-help/help.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Options:
--emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]
Comma separated list of types of output for the
compiler to emit
--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]
--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]
Compiler information to print on stdout
-g Equivalent to -C debuginfo=2
-O Equivalent to -C opt-level=3
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid-compile-flags/print-without-arg.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: Argument to option 'print' missing
Usage:
--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]
--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]
Compiler information to print on stdout

2 changes: 1 addition & 1 deletion tests/ui/invalid-compile-flags/print.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unknown print request: `yyyy`
|
= 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`
= 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`
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information

7 changes: 7 additions & 0 deletions tests/ui/print-request/print-lints-help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Check that we point to `-Whelp` to guide the user to find the list of lints if the user requests
//! `--print=lints` (which is not a valid print request).

//@ compile-flags: --print lints
//@ error-pattern: error: unknown print request: `lints`
//@ error-pattern: help: use `-Whelp` to print a list of lints
//@ error-pattern: help: for more information, see the rustc book
Comment on lines +5 to +7
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the test intention can't be accidentally blessed away as easily.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: unknown print request: `lints`
|
= 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`
= 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`
= help: use `-Whelp` to print a list of lints
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information

4 changes: 4 additions & 0 deletions tests/ui/print-request/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
//@[check_cfg] compile-flags: --print=check-cfg
//@[check_cfg] error-pattern: the `-Z unstable-options` flag must also be passed

//@ revisions: supported_crate_types
//@[supported_crate_types] compile-flags: --print=supported-crate-types
//@[supported_crate_types] error-pattern: the `-Z unstable-options` flag must also be passed

//@ revisions: target_spec_json
//@[target_spec_json] compile-flags: --print=target-spec-json
//@[target_spec_json] error-pattern: the `-Z unstable-options` flag must also be passed
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/print-request/supported-crate-types.linux.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bin
cdylib
dylib
lib
proc-macro
rlib
staticlib
5 changes: 5 additions & 0 deletions tests/ui/print-request/supported-crate-types.musl.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin
lib
proc-macro
rlib
staticlib
20 changes: 20 additions & 0 deletions tests/ui/print-request/supported-crate-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Basic smoke test for `--print=supported-crate-types`, which should print a newline delimited
//! list of crate types supported by the given target. This test cherry-picks a few well-known
//! targets as examples.
//!
//! Tracking issue: <https://github.com/rust-lang/rust/issues/138640>

// ignore-tidy-linelength

//@ check-pass

//@ revisions: wasm musl linux

//@[wasm] compile-flags: --target=wasm32-unknown-unknown --print=supported-crate-types -Zunstable-options
//@[wasm] needs-llvm-components: webassembly

//@[musl] compile-flags: --target=x86_64-unknown-linux-musl --print=supported-crate-types -Zunstable-options
//@[musl] needs-llvm-components: x86

//@[linux] compile-flags: --target=x86_64-unknown-linux-gnu --print=supported-crate-types -Zunstable-options
//@[linux] needs-llvm-components: x86
5 changes: 5 additions & 0 deletions tests/ui/print-request/supported-crate-types.wasm.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin
cdylib
lib
rlib
staticlib
2 changes: 0 additions & 2 deletions tests/ui/rustc-print-info-issue-138612.rs

This file was deleted.

Loading