Skip to content

Commit 236ed07

Browse files
committed
don't show the full linker args unless --verbose is passed
the linker arguments can be *very* long, especially for crates with many dependencies. often they are not useful. omit them unless the user specifically requests them.
1 parent 5bbbc09 commit 236ed07

File tree

8 files changed

+89
-14
lines changed

8 files changed

+89
-14
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
1818
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, FatalError};
1919
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
2020
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
21+
use rustc_macros::Diagnostic;
2122
use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file};
2223
use rustc_metadata::{find_native_static_library, walk_native_lib_search_dirs};
2324
use rustc_middle::bug;
@@ -998,12 +999,12 @@ fn link_natively(
998999
let mut output = prog.stderr.clone();
9991000
output.extend_from_slice(&prog.stdout);
10001001
let escaped_output = escape_linker_output(&output, flavor);
1001-
// FIXME: Add UI tests for this error.
10021002
let err = errors::LinkingFailed {
10031003
linker_path: &linker_path,
10041004
exit_status: prog.status,
10051005
command: &cmd,
10061006
escaped_output,
1007+
verbose: sess.opts.verbose,
10071008
};
10081009
sess.dcx().emit_err(err);
10091010
// If MSVC's `link.exe` was expected but the return code

compiler/rustc_codegen_ssa/src/errors.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ pub(crate) struct LinkingFailed<'a> {
349349
pub exit_status: ExitStatus,
350350
pub command: &'a Command,
351351
pub escaped_output: String,
352+
pub verbose: bool,
352353
}
353354

354355
impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
@@ -359,7 +360,13 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
359360

360361
let contains_undefined_ref = self.escaped_output.contains("undefined reference to");
361362

362-
diag.note(format!("{:?}", self.command)).note(self.escaped_output);
363+
if self.verbose {
364+
diag.note(format!("{:?}", self.command));
365+
} else {
366+
diag.note("use `--verbose` to show all linker arguments");
367+
}
368+
369+
diag.note(self.escaped_output);
363370

364371
// Trying to match an error from OS linkers
365372
// which by now we have no way to translate.

src/tools/run-make-support/src/external_deps/rustc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ impl Rustc {
319319
self
320320
}
321321

322+
/// Pass the `--verbose` flag.
323+
pub fn verbose(&mut self) -> &mut Self {
324+
self.cmd.arg("--verbose");
325+
self
326+
}
327+
322328
/// `EXTRARSCXXFLAGS`
323329
pub fn extra_rs_cxx_flags(&mut self) -> &mut Self {
324330
// Adapted from tools.mk (trimmed):

tests/run-make/link-args-order/rmake.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ fn main() {
1515
.link_args("b c")
1616
.link_args("d e")
1717
.link_arg("f")
18+
.arg("--print=link-args")
1819
.run_fail()
19-
.assert_stderr_contains(r#""a" "b" "c" "d" "e" "f""#);
20+
.assert_stdout_contains(r#""a" "b" "c" "d" "e" "f""#);
2021
rustc()
2122
.input("empty.rs")
2223
.linker_flavor(linker)
2324
.arg("-Zpre-link-arg=a")
2425
.arg("-Zpre-link-args=b c")
2526
.arg("-Zpre-link-args=d e")
2627
.arg("-Zpre-link-arg=f")
28+
.arg("--print=link-args")
2729
.run_fail()
28-
.assert_stderr_contains(r#""a" "b" "c" "d" "e" "f""#);
30+
.assert_stdout_contains(r#""a" "b" "c" "d" "e" "f""#);
2931
}

tests/run-make/link-dedup/rmake.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ fn main() {
1414
rustc().input("depb.rs").run();
1515
rustc().input("depc.rs").run();
1616

17-
let output = rustc().input("empty.rs").cfg("bar").run_fail();
18-
output.assert_stderr_contains(needle_from_libs(&["testa", "testb", "testa"]));
17+
let output = rustc().input("empty.rs").cfg("bar").arg("--print=link-args").run_fail();
18+
output.assert_stdout_contains(needle_from_libs(&["testa", "testb", "testa"]));
1919

20-
let output = rustc().input("empty.rs").run_fail();
21-
output.assert_stderr_contains(needle_from_libs(&["testa"]));
22-
output.assert_stderr_not_contains(needle_from_libs(&["testb"]));
23-
output.assert_stderr_not_contains(needle_from_libs(&["testa", "testa", "testa"]));
20+
let output = rustc().input("empty.rs").arg("--print=link-args").run_fail();
21+
output.assert_stdout_contains(needle_from_libs(&["testa"]));
22+
output.assert_stdout_not_contains(needle_from_libs(&["testb"]));
23+
output.assert_stdout_not_contains(needle_from_libs(&["testa", "testa", "testa"]));
2424
// Adjacent identical native libraries are no longer deduplicated if
2525
// they come from different crates (https://github.com/rust-lang/rust/pull/103311)
2626
// so the following will fail:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
for arg in std::env::args() {
3+
match &*arg {
4+
"run_make_info" => println!("foo"),
5+
"run_make_warn" => eprintln!("warning: bar"),
6+
"run_make_error" => {
7+
eprintln!("error: baz");
8+
std::process::exit(1);
9+
}
10+
_ => (),
11+
}
12+
}
13+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::path::Path;
2+
3+
use run_make_support::rfs::remove_file;
4+
use run_make_support::{Rustc, rustc};
5+
6+
fn run_rustc() -> Rustc {
7+
let mut rustc = rustc();
8+
rustc.arg("main.rs").output("main").linker("./fake-linker");
9+
rustc
10+
}
11+
12+
fn main() {
13+
// first, compile our linker
14+
rustc().arg("fake-linker.rs").output("fake-linker").run();
15+
16+
// Run rustc with our fake linker, and make sure it shows warnings
17+
let warnings = run_rustc().link_arg("run_make_warn").run();
18+
warnings.assert_stderr_contains("warning: linker stderr: bar");
19+
20+
// Make sure it shows stdout, but only when --verbose is passed
21+
run_rustc()
22+
.link_arg("run_make_info")
23+
.verbose()
24+
.run()
25+
.assert_stderr_contains("warning: linker stdout: foo");
26+
run_rustc()
27+
.link_arg("run_make_info")
28+
.run()
29+
.assert_stderr_not_contains("warning: linker stdout: foo");
30+
31+
// Make sure we short-circuit this new path if the linker exits with an error
32+
// (so the diagnostic is less verbose)
33+
run_rustc().link_arg("run_make_error").run_fail().assert_stderr_contains("note: error: baz");
34+
35+
// Make sure we don't show the linker args unless `--verbose` is passed
36+
run_rustc()
37+
.link_arg("run_make_error")
38+
.verbose()
39+
.run_fail()
40+
.assert_stderr_contains_regex("fake-linker.*run_make_error");
41+
run_rustc()
42+
.link_arg("run_make_error")
43+
.run_fail()
44+
.assert_stderr_not_contains_regex("fake-linker.*run_make_error");
45+
}

tests/run-make/rust-lld/rmake.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ fn main() {
1414
// Opt-in to lld and the self-contained linker, to link with rust-lld. We'll check that by
1515
// asking the linker to display its version number with a link-arg.
1616
let output = rustc()
17-
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
1817
.arg("-Zlinker-features=+lld")
1918
.arg("-Clink-self-contained=+linker")
2019
.arg("-Zunstable-options")
20+
.arg("--verbose")
2121
.link_arg(linker_version_flag)
2222
.input("main.rs")
2323
.run();
@@ -29,8 +29,8 @@ fn main() {
2929

3030
// It should not be used when we explicitly opt-out of lld.
3131
let output = rustc()
32-
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
3332
.link_arg(linker_version_flag)
33+
.arg("--verbose")
3434
.arg("-Zlinker-features=-lld")
3535
.input("main.rs")
3636
.run();
@@ -43,8 +43,8 @@ fn main() {
4343
// While we're here, also check that the last linker feature flag "wins" when passed multiple
4444
// times to rustc.
4545
let output = rustc()
46-
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
4746
.link_arg(linker_version_flag)
47+
.arg("--verbose")
4848
.arg("-Clink-self-contained=+linker")
4949
.arg("-Zunstable-options")
5050
.arg("-Zlinker-features=-lld")
@@ -60,6 +60,7 @@ fn main() {
6060
}
6161

6262
fn find_lld_version_in_logs(stderr: String) -> bool {
63-
let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
63+
let lld_version_re =
64+
Regex::new(r"^warning: linker stdout: LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
6465
stderr.lines().any(|line| lld_version_re.is_match(line.trim()))
6566
}

0 commit comments

Comments
 (0)