about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJieyou Xu <jieyouxu@outlook.com>2025-03-18 13:16:01 +0800
committerJieyou Xu <jieyouxu@outlook.com>2025-03-23 19:08:54 +0800
commit0e7dbab1fc4a2cfc63bac4f5c4d283d2504f1547 (patch)
treebf9f39c1c4343a949781da0d12e4f085e91fb5b3
parent60a3084f64607e86dd7715d72f11764cd500c364 (diff)
downloadrust-0e7dbab1fc4a2cfc63bac4f5c4d283d2504f1547.tar.gz
rust-0e7dbab1fc4a2cfc63bac4f5c4d283d2504f1547.zip
Implement `supported-crate-types` print request
As an unstable print request.
-rw-r--r--compiler/rustc_driver_impl/src/lib.rs14
-rw-r--r--compiler/rustc_session/src/config.rs7
-rw-r--r--tests/ui/print-request/stability.rs4
-rw-r--r--tests/ui/print-request/supported-crate-types.linux.stdout7
-rw-r--r--tests/ui/print-request/supported-crate-types.musl.stdout5
-rw-r--r--tests/ui/print-request/supported-crate-types.rs20
-rw-r--r--tests/ui/print-request/supported-crate-types.wasm.stdout5
7 files changed, 59 insertions, 3 deletions
diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs
index 4ba076c64e1..37755e7d61d 100644
--- a/compiler/rustc_driver_impl/src/lib.rs
+++ b/compiler/rustc_driver_impl/src/lib.rs
@@ -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};
@@ -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;
@@ -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);
diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs
index 43b78423c72..ed336cc5596 100644
--- a/compiler/rustc_session/src/config.rs
+++ b/compiler/rustc_session/src/config.rs
@@ -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),
@@ -888,6 +889,7 @@ pub enum PrintKind {
     RelocationModels,
     SplitDebuginfo,
     StackProtectorStrategies,
+    SupportedCrateTypes,
     Sysroot,
     TargetCPUs,
     TargetFeatures,
@@ -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!(
diff --git a/tests/ui/print-request/stability.rs b/tests/ui/print-request/stability.rs
index b205b055569..c3421224d72 100644
--- a/tests/ui/print-request/stability.rs
+++ b/tests/ui/print-request/stability.rs
@@ -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
diff --git a/tests/ui/print-request/supported-crate-types.linux.stdout b/tests/ui/print-request/supported-crate-types.linux.stdout
new file mode 100644
index 00000000000..721adb432e7
--- /dev/null
+++ b/tests/ui/print-request/supported-crate-types.linux.stdout
@@ -0,0 +1,7 @@
+bin
+cdylib
+dylib
+lib
+proc-macro
+rlib
+staticlib
diff --git a/tests/ui/print-request/supported-crate-types.musl.stdout b/tests/ui/print-request/supported-crate-types.musl.stdout
new file mode 100644
index 00000000000..1f4b991e49f
--- /dev/null
+++ b/tests/ui/print-request/supported-crate-types.musl.stdout
@@ -0,0 +1,5 @@
+bin
+lib
+proc-macro
+rlib
+staticlib
diff --git a/tests/ui/print-request/supported-crate-types.rs b/tests/ui/print-request/supported-crate-types.rs
new file mode 100644
index 00000000000..c8b4c0c1a41
--- /dev/null
+++ b/tests/ui/print-request/supported-crate-types.rs
@@ -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
diff --git a/tests/ui/print-request/supported-crate-types.wasm.stdout b/tests/ui/print-request/supported-crate-types.wasm.stdout
new file mode 100644
index 00000000000..ca1de519598
--- /dev/null
+++ b/tests/ui/print-request/supported-crate-types.wasm.stdout
@@ -0,0 +1,5 @@
+bin
+cdylib
+lib
+rlib
+staticlib