@@ -572,7 +572,51 @@ fn local_crates(metadata: &Metadata) -> String {
572
572
local_crates
573
573
}
574
574
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 > ) {
576
620
// Check for version and help flags even when invoked as `cargo-miri`.
577
621
if has_arg_flag ( "--help" ) || has_arg_flag ( "-h" ) {
578
622
show_help ( ) ;
@@ -694,18 +738,12 @@ fn phase_cargo_miri(mut args: env::Args) {
694
738
cmd. env ( "RUSTDOC" , & cargo_miri_path) ;
695
739
696
740
cmd. env ( "MIRI_LOCAL_CRATES" , local_crates ( & metadata) ) ;
697
-
698
- // Run cargo.
699
741
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) ;
707
742
cmd. env ( "MIRI_VERBOSE" , verbose. to_string ( ) ) ; // This makes the other phases verbose.
708
743
}
744
+
745
+ // Run cargo.
746
+ debug_cmd ( "[cargo-miri miri]" , verbose, & cmd) ;
709
747
exec ( cmd)
710
748
}
711
749
@@ -913,14 +951,8 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
913
951
eprintln ! (
914
952
"[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} print={print}"
915
953
) ;
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) ;
923
954
}
955
+ debug_cmd ( "[cargo-miri rustc]" , verbose, & cmd) ;
924
956
exec ( cmd) ;
925
957
926
958
// Create a stub .rlib file if "link" was requested by cargo.
@@ -938,23 +970,6 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
938
970
}
939
971
}
940
972
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
-
958
973
#[ derive( Debug , Copy , Clone , PartialEq ) ]
959
974
enum RunnerPhase {
960
975
/// `cargo` is running a binary
@@ -963,8 +978,9 @@ enum RunnerPhase {
963
978
Rustdoc ,
964
979
}
965
980
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" ) ) ;
968
984
969
985
let file = File :: open ( & binary)
970
986
. 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) {
991
1007
// Set missing env vars. We prefer build-time env vars over run-time ones; see
992
1008
// <https://github.com/rust-lang/miri/issues/1661> for the kind of issue that fixes.
993
1009
for ( name, val) in info. env {
994
- if verbose {
1010
+ if verbose > 0 {
995
1011
if let Some ( old_val) = env:: var_os ( & name) {
996
1012
if old_val != val {
997
1013
eprintln ! (
@@ -1048,18 +1064,16 @@ fn phase_runner(binary: &Path, binary_args: env::Args, phase: RunnerPhase) {
1048
1064
cmd. env ( "MIRI_CWD" , env:: current_dir ( ) . unwrap ( ) ) ;
1049
1065
1050
1066
// Run it.
1051
- if verbose {
1052
- eprintln ! ( "[cargo-miri runner] {:?}" , cmd) ;
1053
- }
1054
-
1067
+ debug_cmd ( "[cargo-miri runner]" , verbose, & cmd) ;
1055
1068
match phase {
1056
1069
RunnerPhase :: Rustdoc => exec_with_pipe ( cmd, & info. stdin ) ,
1057
1070
RunnerPhase :: Cargo => exec ( cmd) ,
1058
1071
}
1059
1072
}
1060
1073
1061
1074
fn 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" ) ) ;
1063
1077
1064
1078
// phase_cargo_miri sets the RUSTDOC env var to ourselves, so we can't use that here;
1065
1079
// just default to a straight-forward invocation for now:
@@ -1126,10 +1140,7 @@ fn phase_rustdoc(fst_arg: &str, mut args: env::Args) {
1126
1140
cmd. arg ( "--test-builder" ) . arg ( & cargo_miri_path) ; // invoked by forwarding most arguments
1127
1141
cmd. arg ( "--runtool" ) . arg ( & cargo_miri_path) ; // invoked with just a single path argument
1128
1142
1129
- if verbose {
1130
- eprintln ! ( "[cargo-miri rustdoc] {:?}" , cmd) ;
1131
- }
1132
-
1143
+ debug_cmd ( "[cargo-miri rustdoc]" , verbose, & cmd) ;
1133
1144
exec ( cmd)
1134
1145
}
1135
1146
0 commit comments