@@ -53,7 +53,7 @@ use rustc_middle::ty::TyCtxt;
53
53
use rustc_parse:: { new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal} ;
54
54
use rustc_session:: config:: {
55
55
CG_OPTIONS , ErrorOutputType , Input , OptionDesc , OutFileName , OutputType , UnstableOptions ,
56
- Z_OPTIONS , nightly_options,
56
+ Z_OPTIONS , nightly_options, parse_target_triple ,
57
57
} ;
58
58
use rustc_session:: getopts:: { self , Matches } ;
59
59
use rustc_session:: lint:: { Lint , LintId } ;
@@ -916,13 +916,7 @@ pub fn version_at_macro_invocation(
916
916
safe_println!( "host: {}" , config:: host_tuple( ) ) ;
917
917
safe_println!( "release: {release}" ) ;
918
918
919
- let debug_flags = matches. opt_strs( "Z" ) ;
920
- let backend_name = debug_flags. iter( ) . find_map( |x| x. strip_prefix( "codegen-backend=" ) ) ;
921
- let opts = config:: Options :: default ( ) ;
922
- let sysroot = filesearch:: materialize_sysroot( opts. maybe_sysroot. clone( ) ) ;
923
- let target = config:: build_target_config( early_dcx, & opts, & sysroot) ;
924
-
925
- get_codegen_backend( early_dcx, & sysroot, backend_name, & target) . print_version( ) ;
919
+ get_backend_from_raw_matches( early_dcx, matches) . print_version( ) ;
926
920
}
927
921
}
928
922
@@ -1125,19 +1119,32 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) ->
1125
1119
}
1126
1120
1127
1121
if cg_flags. iter( ) . any( |x| * x == "passes=list" ) {
1128
- let backend_name = debug_flags. iter( ) . find_map( |x| x. strip_prefix( "codegen-backend=" ) ) ;
1129
-
1130
- let opts = config:: Options :: default ( ) ;
1131
- let sysroot = filesearch:: materialize_sysroot( opts. maybe_sysroot. clone( ) ) ;
1132
- let target = config:: build_target_config( early_dcx, & opts, & sysroot) ;
1133
-
1134
- get_codegen_backend( early_dcx, & sysroot, backend_name, & target) . print_passes( ) ;
1122
+ get_backend_from_raw_matches( early_dcx, matches) . print_passes( ) ;
1135
1123
return true ;
1136
1124
}
1137
1125
1138
1126
false
1139
1127
}
1140
1128
1129
+ /// Get the codegen backend based on the raw [`Matches`].
1130
+ ///
1131
+ /// `rustc -vV` and `rustc -Cpasses=list` need to get the codegen backend before we have parsed all
1132
+ /// arguments and created a [`Session`]. This function reads `-Zcodegen-backend`, `--target` and
1133
+ /// `--sysroot` without validating any other arguments and loads the codegen backend based on these
1134
+ /// arguments.
1135
+ fn get_backend_from_raw_matches(
1136
+ early_dcx: & EarlyDiagCtxt ,
1137
+ matches: & Matches ,
1138
+ ) -> Box <dyn CodegenBackend > {
1139
+ let debug_flags = matches. opt_strs( "Z" ) ;
1140
+ let backend_name = debug_flags. iter( ) . find_map( |x| x. strip_prefix( "codegen-backend=" ) ) ;
1141
+ let target = parse_target_triple( early_dcx, matches) ;
1142
+ let sysroot = filesearch:: materialize_sysroot( matches. opt_str( "sysroot" ) . map( PathBuf :: from) ) ;
1143
+ let target = config:: build_target_config( early_dcx, & target, & sysroot) ;
1144
+
1145
+ get_codegen_backend( early_dcx, & sysroot, backend_name, & target)
1146
+ }
1147
+
1141
1148
fn describe_debug_flags( ) {
1142
1149
safe_println!( "\n Available options:\n " ) ;
1143
1150
print_flag_list( "-Z" , config:: Z_OPTIONS ) ;
0 commit comments