@@ -53,7 +53,7 @@ use rustc_middle::ty::TyCtxt;
5353use  rustc_parse:: { new_parser_from_file,  new_parser_from_source_str,  unwrap_or_emit_fatal} ; 
5454use  rustc_session:: config:: { 
5555    CG_OPTIONS ,  ErrorOutputType ,  Input ,  OptionDesc ,  OutFileName ,  OutputType ,  UnstableOptions , 
56-     Z_OPTIONS ,  nightly_options, 
56+     Z_OPTIONS ,  nightly_options,  parse_target_triple , 
5757} ; 
5858use  rustc_session:: getopts:: { self ,  Matches } ; 
5959use  rustc_session:: lint:: { Lint ,  LintId } ; 
@@ -916,13 +916,7 @@ pub fn version_at_macro_invocation(
916916        safe_println!( "host: {}" ,  config:: host_tuple( ) ) ; 
917917        safe_println!( "release: {release}" ) ; 
918918
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( ) ; 
926920    } 
927921} 
928922
@@ -1125,19 +1119,32 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) ->
11251119    } 
11261120
11271121    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( ) ; 
11351123        return  true ; 
11361124    } 
11371125
11381126    false 
11391127} 
11401128
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+ 
11411148fn  describe_debug_flags( )  { 
11421149    safe_println!( "\n Available options:\n " ) ; 
11431150    print_flag_list( "-Z" ,  config:: Z_OPTIONS ) ; 
0 commit comments