@@ -18,6 +18,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
1818use rustc_errors:: { DiagCtxtHandle , FatalError } ;
1919use rustc_fs_util:: { fix_windows_verbatim_for_gcc, try_canonicalize} ;
2020use rustc_hir:: def_id:: { CrateNum , LOCAL_CRATE } ;
21+ use rustc_macros:: Diagnostic ;
2122use rustc_metadata:: fs:: { METADATA_FILENAME , copy_to_stdout, emit_wrapper_file} ;
2223use rustc_metadata:: { find_native_static_library, walk_native_lib_search_dirs} ;
2324use rustc_middle:: bug;
@@ -750,6 +751,14 @@ fn link_dwarf_object(sess: &Session, cg_results: &CodegenResults, executable_out
750751 }
751752}
752753
754+ #[ derive( Diagnostic ) ]
755+ #[ diag( codegen_ssa_linker_output) ]
756+ /// Translating this is kind of useless. We don't pass translation flags to the linker, so we'd just
757+ /// end up with inconsistent languages within the same diagnostic.
758+ struct LinkerOutput {
759+ inner : String ,
760+ }
761+
753762/// Create a dynamic library or executable.
754763///
755764/// This will invoke the system linker/cc to create the resulting file. This links to all upstream
@@ -1033,8 +1042,22 @@ fn link_natively(
10331042
10341043 sess. dcx ( ) . abort_if_errors ( ) ;
10351044 }
1036- info ! ( "linker stderr:\n {}" , escape_string( & prog. stderr) ) ;
1037- info ! ( "linker stdout:\n {}" , escape_string( & prog. stdout) ) ;
1045+
1046+ if !prog. stderr . is_empty ( ) {
1047+ // We already print `warning:` at the start of the diagnostic. Remove it from the linker output if present.
1048+ let stderr = escape_string ( & prog. stderr ) ;
1049+ debug ! ( "original stderr: {stderr}" ) ;
1050+ let stderr = stderr
1051+ . strip_prefix ( "warning: " )
1052+ . unwrap_or ( & stderr)
1053+ . replace ( ": warning: " , ": " ) ;
1054+ sess. dcx ( ) . emit_warn ( LinkerOutput { inner : format ! ( "linker stderr: {stderr}" ) } ) ;
1055+ }
1056+ if !prog. stdout . is_empty ( ) && sess. opts . verbose {
1057+ sess. dcx ( ) . emit_warn ( LinkerOutput {
1058+ inner : format ! ( "linker stdout: {}" , escape_string( & prog. stdout) ) ,
1059+ } ) ;
1060+ }
10381061 }
10391062 Err ( e) => {
10401063 let linker_not_found = e. kind ( ) == io:: ErrorKind :: NotFound ;
0 commit comments