Skip to content

Commit 0e7dbab

Browse files
committed
Implement supported-crate-types print request
As an unstable print request.
1 parent 60a3084 commit 0e7dbab

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
lines changed

Diff for: 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);

Diff for: 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!(

Diff for: 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

Diff for: tests/ui/print-request/supported-crate-types.rs

+20
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

0 commit comments

Comments
 (0)