From 2a6a7bef01c6b8fe16cd2ada1c7e7133b7dc596c Mon Sep 17 00:00:00 2001 From: jyn Date: Sun, 24 Dec 2023 19:49:23 -0500 Subject: [PATCH] don't show the full linker args unless `--verbose` is passed the linker arguments can be *very* long, especially for crates with many dependencies. some parts of them are not very useful. unless specifically requested: - omit object files specific to the current invocation - fold rlib files into a single braced argument (in shell expansion format) this shortens the output significantly without removing too much information. --- compiler/rustc_codegen_ssa/src/back/link.rs | 4 +- compiler/rustc_codegen_ssa/src/errors.rs | 70 +++++++++++++++++-- .../src/external_deps/rustc.rs | 6 ++ tests/run-make/link-args-order/rmake.rs | 6 +- tests/run-make/link-dedup/rmake.rs | 12 ++-- tests/run-make/linker-warning/fake-linker.rs | 13 ++++ tests/run-make/linker-warning/main.rs | 1 + tests/run-make/linker-warning/rmake.rs | 28 ++++++++ 8 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 tests/run-make/linker-warning/fake-linker.rs create mode 100644 tests/run-make/linker-warning/main.rs create mode 100644 tests/run-make/linker-warning/rmake.rs diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 35d18d0206dbb..b030ea3f6df83 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -992,12 +992,12 @@ fn link_natively( let mut output = prog.stderr.clone(); output.extend_from_slice(&prog.stdout); let escaped_output = escape_linker_output(&output, flavor); - // FIXME: Add UI tests for this error. let err = errors::LinkingFailed { linker_path: &linker_path, exit_status: prog.status, - command: &cmd, + command: cmd, escaped_output, + verbose: sess.opts.verbose, }; sess.dcx().emit_err(err); // If MSVC's `link.exe` was expected but the return code diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 00f8654e670b1..c7213bbc801f9 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -1,6 +1,7 @@ //! Errors emitted by codegen_ssa use std::borrow::Cow; +use std::ffi::OsString; use std::io::Error; use std::num::ParseIntError; use std::path::{Path, PathBuf}; @@ -345,21 +346,82 @@ impl Diagnostic<'_, G> for ThorinErrorWrapper { } pub(crate) struct LinkingFailed<'a> { - pub linker_path: &'a PathBuf, + pub linker_path: &'a Path, pub exit_status: ExitStatus, - pub command: &'a Command, + pub command: Command, pub escaped_output: String, + pub verbose: bool, } impl Diagnostic<'_, G> for LinkingFailed<'_> { - fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> { + fn into_diag(mut self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> { let mut diag = Diag::new(dcx, level, fluent::codegen_ssa_linking_failed); diag.arg("linker_path", format!("{}", self.linker_path.display())); diag.arg("exit_status", format!("{}", self.exit_status)); let contains_undefined_ref = self.escaped_output.contains("undefined reference to"); - diag.note(format!("{:?}", self.command)).note(self.escaped_output); + if self.verbose { + diag.note(format!("{:?}", self.command)); + } else { + enum ArgGroup { + Regular(OsString), + Objects(usize), + Rlibs(PathBuf, Vec), + } + + // Omit rust object files and fold rlibs in the error by default to make linker errors a + // bit less verbose. + let orig_args = self.command.take_args(); + let mut args: Vec = vec![]; + for arg in orig_args { + if arg.as_encoded_bytes().ends_with(b".rcgu.o") { + if let Some(ArgGroup::Objects(n)) = args.last_mut() { + *n += 1; + } else { + args.push(ArgGroup::Objects(1)); + } + } else if arg.as_encoded_bytes().ends_with(b".rlib") { + let rlib_path = Path::new(&arg); + let dir = rlib_path.parent().unwrap(); + let filename = rlib_path.file_name().unwrap().to_owned(); + if let Some(ArgGroup::Rlibs(parent, rlibs)) = args.last_mut() { + if parent == dir { + rlibs.push(filename); + } else { + args.push(ArgGroup::Rlibs(dir.to_owned(), vec![filename])); + } + } else { + args.push(ArgGroup::Rlibs(dir.to_owned(), vec![filename])); + } + } else { + args.push(ArgGroup::Regular(arg)); + } + } + self.command.args(args.into_iter().map(|arg_group| match arg_group { + ArgGroup::Regular(arg) => arg, + ArgGroup::Objects(n) => OsString::from(format!("<{n} object files omitted>")), + ArgGroup::Rlibs(dir, rlibs) => { + let mut arg = dir.into_os_string(); + arg.push("/{"); + let mut first = true; + for rlib in rlibs { + if !first { + arg.push(","); + } + first = false; + arg.push(rlib); + } + arg.push("}"); + arg + } + })); + + diag.note(format!("{:?}", self.command)); + diag.note("some arguments are omitted. use `--verbose` to show all linker arguments"); + } + + diag.note(self.escaped_output); // Trying to match an error from OS linkers // which by now we have no way to translate. diff --git a/src/tools/run-make-support/src/external_deps/rustc.rs b/src/tools/run-make-support/src/external_deps/rustc.rs index ffe10092cc28a..8894ea7fb209b 100644 --- a/src/tools/run-make-support/src/external_deps/rustc.rs +++ b/src/tools/run-make-support/src/external_deps/rustc.rs @@ -325,6 +325,12 @@ impl Rustc { self } + /// Pass the `--verbose` flag. + pub fn verbose(&mut self) -> &mut Self { + self.cmd.arg("--verbose"); + self + } + /// `EXTRARSCXXFLAGS` pub fn extra_rs_cxx_flags(&mut self) -> &mut Self { // Adapted from tools.mk (trimmed): diff --git a/tests/run-make/link-args-order/rmake.rs b/tests/run-make/link-args-order/rmake.rs index b7ef8333267f2..fe0d02926eff4 100644 --- a/tests/run-make/link-args-order/rmake.rs +++ b/tests/run-make/link-args-order/rmake.rs @@ -15,8 +15,9 @@ fn main() { .link_args("b c") .link_args("d e") .link_arg("f") + .arg("--print=link-args") .run_fail() - .assert_stderr_contains(r#""a" "b" "c" "d" "e" "f""#); + .assert_stdout_contains(r#""a" "b" "c" "d" "e" "f""#); rustc() .input("empty.rs") .linker_flavor(linker) @@ -24,6 +25,7 @@ fn main() { .arg("-Zpre-link-args=b c") .arg("-Zpre-link-args=d e") .arg("-Zpre-link-arg=f") + .arg("--print=link-args") .run_fail() - .assert_stderr_contains(r#""a" "b" "c" "d" "e" "f""#); + .assert_stdout_contains(r#""a" "b" "c" "d" "e" "f""#); } diff --git a/tests/run-make/link-dedup/rmake.rs b/tests/run-make/link-dedup/rmake.rs index 6075f31095424..f38603dee8cbb 100644 --- a/tests/run-make/link-dedup/rmake.rs +++ b/tests/run-make/link-dedup/rmake.rs @@ -14,13 +14,13 @@ fn main() { rustc().input("depb.rs").run(); rustc().input("depc.rs").run(); - let output = rustc().input("empty.rs").cfg("bar").run_fail(); - output.assert_stderr_contains(needle_from_libs(&["testa", "testb", "testa"])); + let output = rustc().input("empty.rs").cfg("bar").arg("--print=link-args").run_fail(); + output.assert_stdout_contains(needle_from_libs(&["testa", "testb", "testa"])); - let output = rustc().input("empty.rs").run_fail(); - output.assert_stderr_contains(needle_from_libs(&["testa"])); - output.assert_stderr_not_contains(needle_from_libs(&["testb"])); - output.assert_stderr_not_contains(needle_from_libs(&["testa", "testa", "testa"])); + let output = rustc().input("empty.rs").arg("--print=link-args").run_fail(); + output.assert_stdout_contains(needle_from_libs(&["testa"])); + output.assert_stdout_not_contains(needle_from_libs(&["testb"])); + output.assert_stdout_not_contains(needle_from_libs(&["testa", "testa", "testa"])); // Adjacent identical native libraries are no longer deduplicated if // they come from different crates (https://github.com/rust-lang/rust/pull/103311) // so the following will fail: diff --git a/tests/run-make/linker-warning/fake-linker.rs b/tests/run-make/linker-warning/fake-linker.rs new file mode 100644 index 0000000000000..30497eea2ccd9 --- /dev/null +++ b/tests/run-make/linker-warning/fake-linker.rs @@ -0,0 +1,13 @@ +fn main() { + for arg in std::env::args() { + match &*arg { + "run_make_info" => println!("foo"), + "run_make_warn" => eprintln!("warning: bar"), + "run_make_error" => { + eprintln!("error: baz"); + std::process::exit(1); + } + _ => (), + } + } +} diff --git a/tests/run-make/linker-warning/main.rs b/tests/run-make/linker-warning/main.rs new file mode 100644 index 0000000000000..f328e4d9d04c3 --- /dev/null +++ b/tests/run-make/linker-warning/main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/tests/run-make/linker-warning/rmake.rs b/tests/run-make/linker-warning/rmake.rs new file mode 100644 index 0000000000000..4a916b534ef4e --- /dev/null +++ b/tests/run-make/linker-warning/rmake.rs @@ -0,0 +1,28 @@ +use run_make_support::{Rustc, rustc}; + +fn run_rustc() -> Rustc { + let mut rustc = rustc(); + rustc.arg("main.rs").output("main").linker("./fake-linker"); + rustc +} + +fn main() { + // first, compile our linker + rustc().arg("fake-linker.rs").output("fake-linker").run(); + + // Make sure we don't show the linker args unless `--verbose` is passed + run_rustc() + .link_arg("run_make_error") + .verbose() + .run_fail() + .assert_stderr_contains_regex("fake-linker.*run_make_error") + .assert_stderr_not_contains("object files omitted") + .assert_stderr_contains_regex(r"lib[/\\]libstd"); + run_rustc() + .link_arg("run_make_error") + .run_fail() + .assert_stderr_contains("fake-linker") + .assert_stderr_contains("object files omitted") + .assert_stderr_contains_regex(r"\{") + .assert_stderr_not_contains_regex(r"lib[/\\]libstd"); +}