@@ -572,7 +572,51 @@ fn local_crates(metadata: &Metadata) -> String {
572572 local_crates
573573}
574574
575- fn phase_cargo_miri ( mut args : env:: Args ) {
575+ fn env_vars_from_cmd ( cmd : & Command ) -> Vec < ( String , String ) > {
576+ let mut envs = HashMap :: new ( ) ;
577+ for ( key, value) in std:: env:: vars ( ) {
578+ envs. insert ( key, value) ;
579+ }
580+ for ( key, value) in cmd. get_envs ( ) {
581+ if let Some ( value) = value {
582+ envs. insert ( key. to_string_lossy ( ) . to_string ( ) , value. to_string_lossy ( ) . to_string ( ) ) ;
583+ } else {
584+ envs. remove ( & key. to_string_lossy ( ) . to_string ( ) ) ;
585+ }
586+ }
587+ let mut envs: Vec < _ > = envs. into_iter ( ) . collect ( ) ;
588+ envs. sort ( ) ;
589+ envs
590+ }
591+
592+ /// Debug-print a command that is going to be run.
593+ fn debug_cmd ( prefix : & str , verbose : usize , cmd : & Command ) {
594+ if verbose == 0 {
595+ return ;
596+ }
597+ // We only do a single `eprintln!` call to minimize concurrency interactions.
598+ let mut out = prefix. to_string ( ) ;
599+ writeln ! ( out, " running command: env \\ " ) . unwrap ( ) ;
600+ if verbose > 1 {
601+ // Print the full environment this will be called in.
602+ for ( key, value) in env_vars_from_cmd ( cmd) {
603+ writeln ! ( out, "{key}={value:?} \\ " ) . unwrap ( ) ;
604+ }
605+ } else {
606+ // Print only what has been changed for this `cmd`.
607+ for ( var, val) in cmd. get_envs ( ) {
608+ if let Some ( val) = val {
609+ writeln ! ( out, "{}={:?} \\ " , var. to_string_lossy( ) , val) . unwrap ( ) ;
610+ } else {
611+ writeln ! ( out, "--unset={}" , var. to_string_lossy( ) ) . unwrap ( ) ;
612+ }
613+ }
614+ }
615+ write ! ( out, "{cmd:?}" ) . unwrap ( ) ;
616+ eprintln ! ( "{}" , out) ;
617+ }
618+
619+ fn phase_cargo_miri ( mut args : impl Iterator < Item = String > ) {
576620 // Check for version and help flags even when invoked as `cargo-miri`.
577621 if has_arg_flag ( "--help" ) || has_arg_flag ( "-h" ) {
578622 show_help ( ) ;
@@ -694,18 +738,12 @@ fn phase_cargo_miri(mut args: env::Args) {
694738 cmd. env ( "RUSTDOC" , & cargo_miri_path) ;
695739
696740 cmd. env ( "MIRI_LOCAL_CRATES" , local_crates ( & metadata) ) ;
697-
698- // Run cargo.
699741 if verbose > 0 {
700- eprintln ! ( "[cargo-miri miri] RUSTC_WRAPPER={:?}" , cargo_miri_path) ;
701- eprintln ! ( "[cargo-miri miri] {}={:?}" , target_runner_env_name, cargo_miri_path) ;
702- if * target != host {
703- eprintln ! ( "[cargo-miri miri] {}={:?}" , host_runner_env_name, cargo_miri_path) ;
704- }
705- eprintln ! ( "[cargo-miri miri] RUSTDOC={:?}" , cargo_miri_path) ;
706- eprintln ! ( "[cargo-miri miri] {:?}" , cmd) ;
707742 cmd. env ( "MIRI_VERBOSE" , verbose. to_string ( ) ) ; // This makes the other phases verbose.
708743 }
744+
745+ // Run cargo.
746+ debug_cmd ( "[cargo-miri miri]" , verbose, & cmd) ;
709747 exec ( cmd)
710748}
711749
@@ -913,14 +951,8 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
913951 eprintln ! (
914952 "[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} print={print}"
915953 ) ;
916- eprintln ! ( "[cargo-miri rustc] going to run:" ) ;
917- if verbose > 1 {
918- for ( key, value) in env_vars_from_cmd ( & cmd) {
919- eprintln ! ( "{key}={value:?} \\ " ) ;
920- }
921- }
922- eprintln ! ( "{:?}" , cmd) ;
923954 }
955+ debug_cmd ( "[cargo-miri rustc]" , verbose, & cmd) ;
924956 exec ( cmd) ;
925957
926958 // Create a stub .rlib file if "link" was requested by cargo.
@@ -938,23 +970,6 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
938970 }
939971}
940972
941- fn env_vars_from_cmd ( cmd : & Command ) -> Vec < ( String , String ) > {
942- let mut envs = HashMap :: new ( ) ;
943- for ( key, value) in std:: env:: vars ( ) {
944- envs. insert ( key, value) ;
945- }
946- for ( key, value) in cmd. get_envs ( ) {
947- if let Some ( value) = value {
948- envs. insert ( key. to_str ( ) . unwrap ( ) . into ( ) , value. to_str ( ) . unwrap ( ) . to_owned ( ) ) ;
949- } else {
950- envs. remove ( key. to_str ( ) . unwrap ( ) ) ;
951- }
952- }
953- let mut envs: Vec < _ > = envs. into_iter ( ) . collect ( ) ;
954- envs. sort ( ) ;
955- envs
956- }
957-
958973#[ derive( Debug , Copy , Clone , PartialEq ) ]
959974enum RunnerPhase {
960975 /// `cargo` is running a binary
@@ -963,8 +978,9 @@ enum RunnerPhase {
963978 Rustdoc ,
964979}
965980
966- fn phase_runner ( binary : & Path , binary_args : env:: Args , phase : RunnerPhase ) {
967- let verbose = std:: env:: var_os ( "MIRI_VERBOSE" ) . is_some ( ) ;
981+ fn phase_runner ( binary : & Path , binary_args : impl Iterator < Item = String > , phase : RunnerPhase ) {
982+ let verbose = std:: env:: var ( "MIRI_VERBOSE" )
983+ . map_or ( 0 , |verbose| verbose. parse ( ) . expect ( "verbosity flag must be an integer" ) ) ;
968984
969985 let file = File :: open ( & binary)
970986 . unwrap_or_else ( |_| show_error ( format ! ( "file {:?} not found or `cargo-miri` invoked incorrectly; please only invoke this binary through `cargo miri`" , binary) ) ) ;
@@ -991,7 +1007,7 @@ fn phase_runner(binary: &Path, binary_args: env::Args, phase: RunnerPhase) {
9911007 // Set missing env vars. We prefer build-time env vars over run-time ones; see
9921008 // <https://github.com/rust-lang/miri/issues/1661> for the kind of issue that fixes.
9931009 for ( name, val) in info. env {
994- if verbose {
1010+ if verbose > 0 {
9951011 if let Some ( old_val) = env:: var_os ( & name) {
9961012 if old_val != val {
9971013 eprintln ! (
@@ -1048,18 +1064,16 @@ fn phase_runner(binary: &Path, binary_args: env::Args, phase: RunnerPhase) {
10481064 cmd. env ( "MIRI_CWD" , env:: current_dir ( ) . unwrap ( ) ) ;
10491065
10501066 // Run it.
1051- if verbose {
1052- eprintln ! ( "[cargo-miri runner] {:?}" , cmd) ;
1053- }
1054-
1067+ debug_cmd ( "[cargo-miri runner]" , verbose, & cmd) ;
10551068 match phase {
10561069 RunnerPhase :: Rustdoc => exec_with_pipe ( cmd, & info. stdin ) ,
10571070 RunnerPhase :: Cargo => exec ( cmd) ,
10581071 }
10591072}
10601073
10611074fn phase_rustdoc ( fst_arg : & str , mut args : env:: Args ) {
1062- let verbose = std:: env:: var_os ( "MIRI_VERBOSE" ) . is_some ( ) ;
1075+ let verbose = std:: env:: var ( "MIRI_VERBOSE" )
1076+ . map_or ( 0 , |verbose| verbose. parse ( ) . expect ( "verbosity flag must be an integer" ) ) ;
10631077
10641078 // phase_cargo_miri sets the RUSTDOC env var to ourselves, so we can't use that here;
10651079 // just default to a straight-forward invocation for now:
@@ -1126,10 +1140,7 @@ fn phase_rustdoc(fst_arg: &str, mut args: env::Args) {
11261140 cmd. arg ( "--test-builder" ) . arg ( & cargo_miri_path) ; // invoked by forwarding most arguments
11271141 cmd. arg ( "--runtool" ) . arg ( & cargo_miri_path) ; // invoked with just a single path argument
11281142
1129- if verbose {
1130- eprintln ! ( "[cargo-miri rustdoc] {:?}" , cmd) ;
1131- }
1132-
1143+ debug_cmd ( "[cargo-miri rustdoc]" , verbose, & cmd) ;
11331144 exec ( cmd)
11341145}
11351146
0 commit comments