Skip to content

Commit cffd850

Browse files
committed
Silence progress messages from MSVC link.exe
These cannot be silenced with a CLI flag, and are not useful to warn about. They can still be viewed for debugging purposes using `RUSTC_LOG=rustc_codegen_ssa::link::back`.
1 parent d29790d commit cffd850

File tree

1 file changed

+51
-29
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+51
-29
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+51-29
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,20 @@ fn link_natively(
992992

993993
match prog {
994994
Ok(prog) => {
995+
let is_msvc_link_exe = if let Some(code) = prog.status.code() {
996+
sess.target.is_like_msvc
997+
&& flavor == LinkerFlavor::Msvc(Lld::No)
998+
// Respect the command line override
999+
&& sess.opts.cg.linker.is_none()
1000+
// Match exactly "link.exe"
1001+
&& linker_path.to_str() == Some("link.exe")
1002+
// All Microsoft `link.exe` linking error codes are
1003+
// four digit numbers in the range 1000 to 9999 inclusive
1004+
&& (code < 1000 || code > 9999)
1005+
} else {
1006+
false
1007+
};
1008+
9951009
if !prog.status.success() {
9961010
let mut output = prog.stderr.clone();
9971011
output.extend_from_slice(&prog.stdout);
@@ -1007,44 +1021,52 @@ fn link_natively(
10071021
// If MSVC's `link.exe` was expected but the return code
10081022
// is not a Microsoft LNK error then suggest a way to fix or
10091023
// install the Visual Studio build tools.
1010-
if let Some(code) = prog.status.code() {
1011-
if sess.target.is_like_msvc
1012-
&& flavor == LinkerFlavor::Msvc(Lld::No)
1013-
// Respect the command line override
1014-
&& sess.opts.cg.linker.is_none()
1015-
// Match exactly "link.exe"
1016-
&& linker_path.to_str() == Some("link.exe")
1017-
// All Microsoft `link.exe` linking error codes are
1018-
// four digit numbers in the range 1000 to 9999 inclusive
1019-
&& (code < 1000 || code > 9999)
1020-
{
1021-
let is_vs_installed = windows_registry::find_vs_version().is_ok();
1022-
let has_linker =
1023-
windows_registry::find_tool(&sess.target.arch, "link.exe").is_some();
1024-
1025-
sess.dcx().emit_note(errors::LinkExeUnexpectedError);
1026-
if is_vs_installed && has_linker {
1027-
// the linker is broken
1028-
sess.dcx().emit_note(errors::RepairVSBuildTools);
1029-
sess.dcx().emit_note(errors::MissingCppBuildToolComponent);
1030-
} else if is_vs_installed {
1031-
// the linker is not installed
1032-
sess.dcx().emit_note(errors::SelectCppBuildToolWorkload);
1033-
} else {
1034-
// visual studio is not installed
1035-
sess.dcx().emit_note(errors::VisualStudioNotInstalled);
1036-
}
1024+
if is_msvc_link_exe {
1025+
let is_vs_installed = windows_registry::find_vs_version().is_ok();
1026+
let has_linker =
1027+
windows_registry::find_tool(&sess.target.arch, "link.exe").is_some();
1028+
1029+
sess.dcx().emit_note(errors::LinkExeUnexpectedError);
1030+
if is_vs_installed && has_linker {
1031+
// the linker is broken
1032+
sess.dcx().emit_note(errors::RepairVSBuildTools);
1033+
sess.dcx().emit_note(errors::MissingCppBuildToolComponent);
1034+
} else if is_vs_installed {
1035+
// the linker is not installed
1036+
sess.dcx().emit_note(errors::SelectCppBuildToolWorkload);
1037+
} else {
1038+
// visual studio is not installed
1039+
sess.dcx().emit_note(errors::VisualStudioNotInstalled);
10371040
}
10381041
}
10391042

10401043
sess.dcx().abort_if_errors();
10411044
}
10421045

10431046
let stderr = escape_string(&prog.stderr);
1044-
let stdout = escape_string(&prog.stdout);
1047+
let mut stdout = escape_string(&prog.stdout);
10451048
info!("linker stderr:\n{}", &stderr);
10461049
info!("linker stdout:\n{}", &stdout);
10471050

1051+
// Hide some progress messages from link.exe that we don't care about.
1052+
// See https://github.com/chromium/chromium/blob/bfa41e41145ffc85f041384280caf2949bb7bd72/build/toolchain/win/tool_wrapper.py#L144-L146
1053+
if is_msvc_link_exe {
1054+
if let Ok(str) = str::from_utf8(&prog.stdout) {
1055+
let mut output = String::with_capacity(str.len());
1056+
for line in stdout.lines() {
1057+
if line.starts_with(" Creating library")
1058+
|| line.starts_with("Generating code")
1059+
|| line.starts_with("Finished generating code")
1060+
{
1061+
continue;
1062+
}
1063+
output += line;
1064+
output += "\r\n"
1065+
}
1066+
stdout = escape_string(output.trim().as_bytes())
1067+
}
1068+
}
1069+
10481070
let (level, src) = codegen_results.crate_info.lint_levels.linker_messages;
10491071
let lint = |msg| {
10501072
lint_level(sess, LINKER_MESSAGES, level, src, None, |diag| {
@@ -1060,7 +1082,7 @@ fn link_natively(
10601082
.replace(": warning: ", ": ");
10611083
lint(format!("linker stderr: {stderr}"));
10621084
}
1063-
if !prog.stdout.is_empty() {
1085+
if !stdout.is_empty() {
10641086
lint(format!("linker stdout: {}", stdout))
10651087
}
10661088
}

0 commit comments

Comments
 (0)