Implement printing to file in print_crate_info

This commit is contained in:
David Tolnay 2023-07-16 22:29:05 -07:00
parent f2e3d3fc63
commit 5a60660ff8
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -49,6 +49,7 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::env; use std::env;
use std::ffi::OsString; use std::ffi::OsString;
use std::fmt::Write as _;
use std::fs; use std::fs;
use std::io::{self, IsTerminal, Read, Write}; use std::io::{self, IsTerminal, Read, Write};
use std::panic::{self, catch_unwind}; use std::panic::{self, catch_unwind};
@ -65,6 +66,11 @@
) )
} }
#[allow(unused_macros)]
macro do_not_use_safe_print($($t:tt)*) {
std::compile_error!("Don't use `safe_print` or `safe_println` here, use `println_info` instead")
}
// This import blocks the use of panicking `print` and `println` in all the code // This import blocks the use of panicking `print` and `println` in all the code
// below. Please use `safe_print` and `safe_println` to avoid ICE when // below. Please use `safe_print` and `safe_println` to avoid ICE when
// encountering an I/O error during print. // encountering an I/O error during print.
@ -713,6 +719,13 @@ fn print_crate_info(
parse_attrs: bool, parse_attrs: bool,
) -> Compilation { ) -> Compilation {
use rustc_session::config::PrintKind::*; use rustc_session::config::PrintKind::*;
// This import prevents the following code from using the printing macros
// used by the rest of the module. Within this function, we only write to
// the output specified by `sess.io.output_file`.
#[allow(unused_imports)]
use {do_not_use_safe_print as safe_print, do_not_use_safe_print as safe_println};
// NativeStaticLibs and LinkArgs are special - printed during linking // NativeStaticLibs and LinkArgs are special - printed during linking
// (empty iterator returns true) // (empty iterator returns true)
if sess.opts.prints.iter().all(|p| p.kind == NativeStaticLibs || p.kind == LinkArgs) { if sess.opts.prints.iter().all(|p| p.kind == NativeStaticLibs || p.kind == LinkArgs) {
@ -731,17 +744,23 @@ fn print_crate_info(
} else { } else {
None None
}; };
for req in &sess.opts.prints { for req in &sess.opts.prints {
let mut crate_info = String::new();
macro println_info($($arg:tt)*) {
crate_info.write_fmt(format_args!("{}\n", format_args!($($arg)*))).unwrap()
}
match req.kind { match req.kind {
TargetList => { TargetList => {
let mut targets = rustc_target::spec::TARGETS.to_vec(); let mut targets = rustc_target::spec::TARGETS.to_vec();
targets.sort_unstable(); targets.sort_unstable();
safe_println!("{}", targets.join("\n")); println_info!("{}", targets.join("\n"));
} }
Sysroot => safe_println!("{}", sess.sysroot.display()), Sysroot => println_info!("{}", sess.sysroot.display()),
TargetLibdir => safe_println!("{}", sess.target_tlib_path.dir.display()), TargetLibdir => println_info!("{}", sess.target_tlib_path.dir.display()),
TargetSpec => { TargetSpec => {
safe_println!("{}", serde_json::to_string_pretty(&sess.target.to_json()).unwrap()); println_info!("{}", serde_json::to_string_pretty(&sess.target.to_json()).unwrap());
} }
AllTargetSpecs => { AllTargetSpecs => {
let mut targets = BTreeMap::new(); let mut targets = BTreeMap::new();
@ -750,7 +769,7 @@ fn print_crate_info(
let target = Target::expect_builtin(&triple); let target = Target::expect_builtin(&triple);
targets.insert(name, target.to_json()); targets.insert(name, target.to_json());
} }
safe_println!("{}", serde_json::to_string_pretty(&targets).unwrap()); println_info!("{}", serde_json::to_string_pretty(&targets).unwrap());
} }
FileNames | CrateName => { FileNames | CrateName => {
let Some(attrs) = attrs.as_ref() else { let Some(attrs) = attrs.as_ref() else {
@ -760,14 +779,14 @@ fn print_crate_info(
let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess); let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess);
let id = rustc_session::output::find_crate_name(sess, attrs); let id = rustc_session::output::find_crate_name(sess, attrs);
if req.kind == CrateName { if req.kind == CrateName {
safe_println!("{id}"); println_info!("{id}");
continue; } else {
} let crate_types = collect_crate_types(sess, attrs);
let crate_types = collect_crate_types(sess, attrs); for &style in &crate_types {
for &style in &crate_types { let fname =
let fname = rustc_session::output::filename_for_input(sess, style, id, &t_outputs);
rustc_session::output::filename_for_input(sess, style, id, &t_outputs); println_info!("{}", fname.as_path().file_name().unwrap().to_string_lossy());
safe_println!("{}", fname.as_path().file_name().unwrap().to_string_lossy()); }
} }
} }
Cfg => { Cfg => {
@ -801,13 +820,13 @@ fn print_crate_info(
cfgs.sort(); cfgs.sort();
for cfg in cfgs { for cfg in cfgs {
safe_println!("{cfg}"); println_info!("{cfg}");
} }
} }
CallingConventions => { CallingConventions => {
let mut calling_conventions = rustc_target::spec::abi::all_names(); let mut calling_conventions = rustc_target::spec::abi::all_names();
calling_conventions.sort_unstable(); calling_conventions.sort_unstable();
safe_println!("{}", calling_conventions.join("\n")); println_info!("{}", calling_conventions.join("\n"));
} }
RelocationModels RelocationModels
| CodeModels | CodeModels
@ -825,7 +844,7 @@ fn print_crate_info(
for split in &[Off, Packed, Unpacked] { for split in &[Off, Packed, Unpacked] {
if sess.target.options.supported_split_debuginfo.contains(split) { if sess.target.options.supported_split_debuginfo.contains(split) {
safe_println!("{split}"); println_info!("{split}");
} }
} }
} }
@ -833,7 +852,7 @@ fn print_crate_info(
use rustc_target::spec::current_apple_deployment_target; use rustc_target::spec::current_apple_deployment_target;
if sess.target.is_like_osx { if sess.target.is_like_osx {
safe_println!( println_info!(
"deployment_target={}", "deployment_target={}",
current_apple_deployment_target(&sess.target) current_apple_deployment_target(&sess.target)
.expect("unknown Apple target OS") .expect("unknown Apple target OS")
@ -844,6 +863,8 @@ fn print_crate_info(
} }
} }
} }
req.out.overwrite(&crate_info, sess);
} }
Compilation::Stop Compilation::Stop
} }