diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 38ee87601614b..4a5230e003cdd 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -625,7 +625,7 @@ fn list_metadata(sess: &Session, metadata_loader: &dyn MetadataLoader) { } } -fn print_crate_info( +pub fn print_crate_info( codegen_backend: &dyn CodegenBackend, sess: &Session, parse_attrs: bool, diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 995dd5b29474a..5c74399c8e9d2 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -33,7 +33,7 @@ use tracing::debug; pub use crate::config::cfg::{Cfg, CheckCfg, ExpectedValues}; use crate::config::native_libs::parse_native_libs; -pub use crate::config::print_request::{PrintKind, PrintRequest}; +pub use crate::config::print_request::{PrintKind, PrintRequest, collect_print_requests}; use crate::errors::FileWriteFail; pub use crate::options::*; use crate::search_paths::SearchPath; @@ -1364,16 +1364,15 @@ impl Sysroot { } } +/// Get the host triple out of the build environment. This ensures that our +/// idea of the host triple is the same as for the set of libraries we've +/// actually built. We can't just take LLVM's host triple because they +/// normalize all ix86 architectures to i386. pub fn host_tuple() -> &'static str { - // Get the host triple out of the build environment. This ensures that our - // idea of the host triple is the same as for the set of libraries we've - // actually built. We can't just take LLVM's host triple because they - // normalize all ix86 architectures to i386. - // // Instead of grabbing the host triple (for the current host), we grab (at // compile time) the target triple that this rustc is built with and // calling that (at runtime) the host triple. - (option_env!("CFG_COMPILER_HOST_TRIPLE")).expect("CFG_COMPILER_HOST_TRIPLE") + env!("CFG_COMPILER_HOST_TRIPLE") } fn file_path_mapping( diff --git a/compiler/rustc_session/src/config/print_request.rs b/compiler/rustc_session/src/config/print_request.rs index dc53fcc6955f7..7369ba8630731 100644 --- a/compiler/rustc_session/src/config/print_request.rs +++ b/compiler/rustc_session/src/config/print_request.rs @@ -21,30 +21,78 @@ pub struct PrintRequest { #[derive(AllVariants)] pub enum PrintKind { // tidy-alphabetical-start + /// All target specs. AllTargetSpecsJson, + + /// Does the backend supports Zstd compression? (perma-unstable) BackendHasZstd, + + /// List of supported calling conventions. CallingConventions, + + /// List of cfg values (1st flavour). Cfg, + + /// List of cfg values (2nd flavour). CheckCfg, + + /// List of available code models for the current backend. CodeModels, + + /// Name of the crate being compiled. CrateName, + CrateRootLintLevels, + + /// The current selected deployment target (Apple platforms). DeploymentTarget, + + /// THe names of the files created by the `link` `--emit` kind. FileNames, + + /// Target-tuple of the host compiler. HostTuple, + + /// Linker invocations. LinkArgs, + + /// When compiling a `staticlib` crate, print the linker flags used. NativeStaticLibs, + + /// List of available relocation models for the current backend. RelocationModels, + + /// List of available split debuginfos for the current target. SplitDebuginfo, + + /// List of available stack protector strategies for the current backend. StackProtectorStrategies, + + /// List of available crate types for the current target. SupportedCrateTypes, + + /// Path to the sysroot. Sysroot, + + /// List of available CPU values for the current target. TargetCPUs, + + /// List of available target features for the current target. TargetFeatures, + + /// Path to the target libdir. TargetLibdir, + + /// List of supported targets. TargetList, + + /// Current target spec. TargetSpecJson, + + /// Target spec schema. TargetSpecJsonSchema, + + /// List of available TLS models for the current backend. TlsModels, // tidy-alphabetical-end } @@ -142,7 +190,7 @@ pub(crate) static PRINT_HELP: LazyLock = LazyLock::new(|| { ) }); -pub(crate) fn collect_print_requests( +pub fn collect_print_requests( early_dcx: &EarlyDiagCtxt, cg: &mut CodegenOptions, unstable_opts: &UnstableOptions, diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 3d4b4e969157c..38bbeb1ae2002 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -9,8 +9,9 @@ use rustc_data_structures::fx::FxIndexMap; use rustc_errors::DiagCtxtHandle; use rustc_session::config::{ self, CodegenOptions, CrateType, ErrorOutputType, Externs, Input, JsonUnusedExterns, - OptionsTargetModifiers, OutFileName, Sysroot, UnstableOptions, get_cmd_lint_options, - nightly_options, parse_crate_types_from_list, parse_externs, parse_target_triple, + OptionsTargetModifiers, OutFileName, PrintKind, PrintRequest, Sysroot, UnstableOptions, + collect_print_requests, get_cmd_lint_options, nightly_options, parse_crate_types_from_list, + parse_externs, parse_target_triple, }; use rustc_session::lint::Level; use rustc_session::search_paths::SearchPath; @@ -119,6 +120,8 @@ pub(crate) struct Options { pub(crate) describe_lints: bool, /// What level to cap lints at. pub(crate) lint_cap: Option, + /// Print requests to hand to the compiler. + pub(crate) prints: Vec, // Options specific to running doctests /// Whether we should run doctests instead of generating docs. @@ -210,6 +213,7 @@ impl fmt::Debug for Options { .field("lint_opts", &self.lint_opts) .field("describe_lints", &self.describe_lints) .field("lint_cap", &self.lint_cap) + .field("prints", &self.prints) .field("should_test", &self.should_test) .field("test_args", &self.test_args) .field("test_run_directory", &self.test_run_directory) @@ -410,7 +414,7 @@ impl Options { let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default(); let mut target_modifiers = BTreeMap::::new(); - let codegen_options = CodegenOptions::build(early_dcx, matches, &mut target_modifiers); + let mut codegen_options = CodegenOptions::build(early_dcx, matches, &mut target_modifiers); let unstable_opts = UnstableOptions::build(early_dcx, matches, &mut target_modifiers); let remap_path_prefix = match parse_remap_path_prefix(matches) { @@ -564,6 +568,41 @@ impl Options { } }; + let mut prints = + collect_print_requests(early_dcx, &mut codegen_options, &unstable_opts, matches); + + { + use PrintKind::*; + prints.retain(|req| match req.kind { + // Compiler + TargetList + | AllTargetSpecsJson + | TargetSpecJson + | TargetSpecJsonSchema + | TargetCPUs + | TargetFeatures + | DeploymentTarget + | SupportedCrateTypes + + // Codegen + | CallingConventions + | CodeModels + | SplitDebuginfo + | StackProtectorStrategies + | RelocationModels + | TlsModels + | BackendHasZstd + + // Linker + | FileNames + | LinkArgs + | NativeStaticLibs + | TargetLibdir => false, + + Cfg | CheckCfg | CrateName | CrateRootLintLevels | HostTuple | Sysroot => true, + }); + } + let externs = parse_externs(early_dcx, matches, &unstable_opts); let extern_html_root_urls = match parse_extern_html_roots(matches) { Ok(ex) => ex, @@ -853,6 +892,7 @@ impl Options { lint_opts, describe_lints, lint_cap, + prints, should_test, test_args, show_coverage, diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 413163290ee30..7a65262947f6c 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -214,6 +214,7 @@ pub(crate) fn create_config( lint_opts, describe_lints, lint_cap, + prints, scrape_examples_options, remap_path_prefix, target_modifiers, @@ -269,6 +270,7 @@ pub(crate) fn create_config( diagnostic_width, edition, describe_lints, + prints, crate_name, test, remap_path_prefix, diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 137319ae0c069..2b5d70aacc940 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -542,6 +542,14 @@ fn opts() -> Vec { "Comma separated list of types of output for rustdoc to emit", "[toolchain-shared-resources,invocation-specific,dep-info]", ), + opt( + Unstable, + Multi, + "", + "print", + "Rustdoc information to print on stdout (or to a file)", + "[=]", + ), opt(Unstable, FlagMulti, "", "no-run", "Compile doctests without running them", ""), opt( Unstable, @@ -885,6 +893,12 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) { return; } + if rustc_driver::print_crate_info(&*compiler.codegen_backend, sess, true) + == rustc_driver::Compilation::Stop + { + return; + } + let krate = rustc_interface::passes::parse(sess); rustc_interface::create_and_enter_global_ctxt(compiler, krate, |tcx| { if sess.dcx().has_errors().is_some() { diff --git a/tests/run-make/rustdoc-default-output/output-default.stdout b/tests/run-make/rustdoc-default-output/output-default.stdout index 4e28be347cbb1..6495fef47688f 100644 --- a/tests/run-make/rustdoc-default-output/output-default.stdout +++ b/tests/run-make/rustdoc-default-output/output-default.stdout @@ -153,6 +153,8 @@ Options: --emit [toolchain-shared-resources,invocation-specific,dep-info] Comma separated list of types of output for rustdoc to emit + --print [=] + Rustdoc information to print on stdout (or to a file) --no-run Compile doctests without running them --merge-doctests yes|no|auto Force all doctests to be compiled as a single binary,