@@ -49,6 +49,7 @@ use std::cmp::max;
4949use std:: collections:: BTreeMap ;
5050use std:: env;
5151use std:: ffi:: OsString ;
52+ use std:: fmt:: Write as _;
5253use std:: fs;
5354use std:: io:: { self , IsTerminal , Read , Write } ;
5455use std:: panic:: { self , catch_unwind} ;
@@ -65,6 +66,11 @@ macro do_not_use_print($($t:tt)*) {
6566 )
6667}
6768
69+ #[ allow( unused_macros) ]
70+ macro do_not_use_safe_print( $( $t: tt) * ) {
71+ std:: compile_error!( "Don't use `safe_print` or `safe_println` here, use `println_info` instead" )
72+ }
73+
6874// This import blocks the use of panicking `print` and `println` in all the code
6975// below. Please use `safe_print` and `safe_println` to avoid ICE when
7076// encountering an I/O error during print.
@@ -713,6 +719,13 @@ fn print_crate_info(
713719 parse_attrs: bool ,
714720) -> Compilation {
715721 use rustc_session:: config:: PrintKind :: * ;
722+
723+ // This import prevents the following code from using the printing macros
724+ // used by the rest of the module. Within this function, we only write to
725+ // the output specified by `sess.io.output_file`.
726+ #[ allow( unused_imports) ]
727+ use { do_not_use_safe_print as safe_print, do_not_use_safe_print as safe_println} ;
728+
716729 // NativeStaticLibs and LinkArgs are special - printed during linking
717730 // (empty iterator returns true)
718731 if sess. opts. prints. iter( ) . all( |p| p. kind == NativeStaticLibs || p. kind == LinkArgs ) {
@@ -731,17 +744,23 @@ fn print_crate_info(
731744 } else {
732745 None
733746 } ;
747+
734748 for req in & sess. opts. prints {
749+ let mut crate_info = String :: new( ) ;
750+ macro println_info( $( $arg: tt) * ) {
751+ crate_info. write_fmt( format_args!( "{}\n " , format_args!( $( $arg) * ) ) ) . unwrap( )
752+ }
753+
735754 match req. kind {
736755 TargetList => {
737756 let mut targets = rustc_target:: spec:: TARGETS . to_vec( ) ;
738757 targets. sort_unstable( ) ;
739- safe_println ! ( "{}" , targets. join( "\n " ) ) ;
758+ println_info !( "{}" , targets. join( "\n " ) ) ;
740759 }
741- Sysroot => safe_println ! ( "{}" , sess. sysroot. display( ) ) ,
742- TargetLibdir => safe_println ! ( "{}" , sess. target_tlib_path. dir. display( ) ) ,
760+ Sysroot => println_info !( "{}" , sess. sysroot. display( ) ) ,
761+ TargetLibdir => println_info !( "{}" , sess. target_tlib_path. dir. display( ) ) ,
743762 TargetSpec => {
744- safe_println ! ( "{}" , serde_json:: to_string_pretty( & sess. target. to_json( ) ) . unwrap( ) ) ;
763+ println_info !( "{}" , serde_json:: to_string_pretty( & sess. target. to_json( ) ) . unwrap( ) ) ;
745764 }
746765 AllTargetSpecs => {
747766 let mut targets = BTreeMap :: new( ) ;
@@ -750,7 +769,7 @@ fn print_crate_info(
750769 let target = Target :: expect_builtin( & triple) ;
751770 targets. insert( name, target. to_json( ) ) ;
752771 }
753- safe_println ! ( "{}" , serde_json:: to_string_pretty( & targets) . unwrap( ) ) ;
772+ println_info !( "{}" , serde_json:: to_string_pretty( & targets) . unwrap( ) ) ;
754773 }
755774 FileNames | CrateName => {
756775 let Some ( attrs) = attrs. as_ref( ) else {
@@ -760,14 +779,14 @@ fn print_crate_info(
760779 let t_outputs = rustc_interface:: util:: build_output_filenames( attrs, sess) ;
761780 let id = rustc_session:: output:: find_crate_name( sess, attrs) ;
762781 if req. kind == CrateName {
763- safe_println ! ( "{id}" ) ;
764- continue ;
765- }
766- let crate_types = collect_crate_types ( sess , attrs ) ;
767- for & style in & crate_types {
768- let fname =
769- rustc_session :: output :: filename_for_input ( sess , style , id , & t_outputs ) ;
770- safe_println ! ( "{}" , fname . as_path ( ) . file_name ( ) . unwrap ( ) . to_string_lossy ( ) ) ;
782+ println_info !( "{id}" ) ;
783+ } else {
784+ let crate_types = collect_crate_types ( sess , attrs ) ;
785+ for & style in & crate_types {
786+ let fname =
787+ rustc_session :: output :: filename_for_input ( sess , style , id , & t_outputs ) ;
788+ println_info! ( "{}" , fname . as_path ( ) . file_name ( ) . unwrap ( ) . to_string_lossy ( ) ) ;
789+ }
771790 }
772791 }
773792 Cfg => {
@@ -801,13 +820,13 @@ fn print_crate_info(
801820
802821 cfgs. sort( ) ;
803822 for cfg in cfgs {
804- safe_println ! ( "{cfg}" ) ;
823+ println_info !( "{cfg}" ) ;
805824 }
806825 }
807826 CallingConventions => {
808827 let mut calling_conventions = rustc_target:: spec:: abi:: all_names( ) ;
809828 calling_conventions. sort_unstable( ) ;
810- safe_println ! ( "{}" , calling_conventions. join( "\n " ) ) ;
829+ println_info !( "{}" , calling_conventions. join( "\n " ) ) ;
811830 }
812831 RelocationModels
813832 | CodeModels
@@ -825,15 +844,15 @@ fn print_crate_info(
825844
826845 for split in & [ Off , Packed , Unpacked ] {
827846 if sess. target. options. supported_split_debuginfo. contains( split) {
828- safe_println ! ( "{split}" ) ;
847+ println_info !( "{split}" ) ;
829848 }
830849 }
831850 }
832851 DeploymentTarget => {
833852 use rustc_target:: spec:: current_apple_deployment_target;
834853
835854 if sess. target. is_like_osx {
836- safe_println ! (
855+ println_info !(
837856 "deployment_target={}" ,
838857 current_apple_deployment_target( & sess. target)
839858 . expect( "unknown Apple target OS" )
@@ -844,6 +863,8 @@ fn print_crate_info(
844863 }
845864 }
846865 }
866+
867+ req. out. overwrite( & crate_info, sess) ;
847868 }
848869 Compilation :: Stop
849870}
0 commit comments