Skip to content

Commit e0974e7

Browse files
committed
Split report_linker_output into its own function
1 parent 44f8f4b commit e0974e7

File tree

1 file changed

+47
-43
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+47
-43
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use super::rpath::{self, RPathConfig};
6060
use super::{apple, versioned_llvm_target};
6161
use crate::base::needs_allocator_shim_for_linking;
6262
use crate::{
63-
CodegenResults, CompiledModule, CrateInfo, NativeLib, errors, looks_like_rust_object_file,
63+
CodegenLintLevels, CodegenResults, CompiledModule, CrateInfo, NativeLib, errors, looks_like_rust_object_file
6464
};
6565

6666
pub fn ensure_removed(dcx: DiagCtxtHandle<'_>, path: &Path) {
@@ -678,6 +678,51 @@ fn is_msvc_link_exe(sess: &Session) -> bool {
678678
&& linker_path.to_str() == Some("link.exe")
679679
}
680680

681+
fn report_linker_output(sess: &Session, levels: CodegenLintLevels, stdout: &[u8], stderr: &[u8]) {
682+
let escaped_stderr = escape_string(&stderr);
683+
let mut escaped_stdout = escape_string(&stdout);
684+
info!("linker stderr:\n{}", &escaped_stderr);
685+
info!("linker stdout:\n{}", &escaped_stdout);
686+
687+
// Hide some progress messages from link.exe that we don't care about.
688+
// See https://github.com/chromium/chromium/blob/bfa41e41145ffc85f041384280caf2949bb7bd72/build/toolchain/win/tool_wrapper.py#L144-L146
689+
if is_msvc_link_exe(sess) {
690+
if let Ok(str) = str::from_utf8(&stdout) {
691+
let mut output = String::with_capacity(str.len());
692+
for line in str.lines() {
693+
if line.starts_with(" Creating library")
694+
|| line.starts_with("Generating code")
695+
|| line.starts_with("Finished generating code")
696+
{
697+
continue;
698+
} else {
699+
output += line;
700+
output += "\r\n"
701+
}
702+
}
703+
escaped_stdout = escape_string(output.trim().as_bytes())
704+
}
705+
}
706+
707+
let lint_msg = |msg| {
708+
lint_level(sess, LINKER_MESSAGES, levels.linker_messages, None, |diag| {
709+
LinkerOutput { inner: msg }.decorate_lint(diag)
710+
})
711+
};
712+
713+
if !escaped_stderr.is_empty() {
714+
// We already print `warning:` at the start of the diagnostic. Remove it from the linker output if present.
715+
let stderr = escaped_stderr
716+
.strip_prefix("warning: ")
717+
.unwrap_or(&escaped_stderr)
718+
.replace(": warning: ", ": ");
719+
lint_msg(format!("linker stderr: {stderr}"));
720+
}
721+
if !escaped_stdout.is_empty() {
722+
lint_msg(format!("linker stdout: {}", escaped_stdout))
723+
}
724+
}
725+
681726
/// Create a dynamic library or executable.
682727
///
683728
/// This will invoke the system linker/cc to create the resulting file. This links to all upstream
@@ -915,48 +960,7 @@ fn link_natively(
915960
sess.dcx().abort_if_errors();
916961
}
917962

918-
let stderr = escape_string(&prog.stderr);
919-
let mut stdout = escape_string(&prog.stdout);
920-
info!("linker stderr:\n{}", &stderr);
921-
info!("linker stdout:\n{}", &stdout);
922-
923-
// Hide some progress messages from link.exe that we don't care about.
924-
// See https://github.com/chromium/chromium/blob/bfa41e41145ffc85f041384280caf2949bb7bd72/build/toolchain/win/tool_wrapper.py#L144-L146
925-
if is_msvc_link_exe(sess) {
926-
if let Ok(str) = str::from_utf8(&prog.stdout) {
927-
let mut output = String::with_capacity(str.len());
928-
for line in stdout.lines() {
929-
if line.starts_with(" Creating library")
930-
|| line.starts_with("Generating code")
931-
|| line.starts_with("Finished generating code")
932-
{
933-
continue;
934-
}
935-
output += line;
936-
output += "\r\n"
937-
}
938-
stdout = escape_string(output.trim().as_bytes())
939-
}
940-
}
941-
942-
let level = codegen_results.crate_info.lint_levels.linker_messages;
943-
let lint = |msg| {
944-
lint_level(sess, LINKER_MESSAGES, level, None, |diag| {
945-
LinkerOutput { inner: msg }.decorate_lint(diag)
946-
})
947-
};
948-
949-
if !prog.stderr.is_empty() {
950-
// We already print `warning:` at the start of the diagnostic. Remove it from the linker output if present.
951-
let stderr = stderr
952-
.strip_prefix("warning: ")
953-
.unwrap_or(&stderr)
954-
.replace(": warning: ", ": ");
955-
lint(format!("linker stderr: {stderr}"));
956-
}
957-
if !stdout.is_empty() {
958-
lint(format!("linker stdout: {}", stdout))
959-
}
963+
report_linker_output(sess, codegen_results.crate_info.lint_levels, &prog.stdout, &prog.stderr);
960964
}
961965
Err(e) => {
962966
let linker_not_found = e.kind() == io::ErrorKind::NotFound;

0 commit comments

Comments
 (0)