@@ -479,6 +479,7 @@ impl TargetCfgs {
479479 let mut targets: HashMap < String , TargetCfg > = serde_json:: from_str ( & rustc_output (
480480 config,
481481 & [ "--print=all-target-specs-json" , "-Zunstable-options" ] ,
482+ Default :: default ( ) ,
482483 ) )
483484 . unwrap ( ) ;
484485
@@ -491,16 +492,33 @@ impl TargetCfgs {
491492 let mut all_families = HashSet :: new ( ) ;
492493 let mut all_pointer_widths = HashSet :: new ( ) ;
493494
494- // Handle custom target specs, which are not included in `--print=all-target-specs-json`.
495- if config. target . ends_with ( ".json" ) {
496- targets. insert (
497- config. target . clone ( ) ,
498- serde_json:: from_str ( & rustc_output (
499- config,
500- & [ "--print=target-spec-json" , "-Zunstable-options" , "--target" , & config. target ] ,
501- ) )
502- . unwrap ( ) ,
503- ) ;
495+ // If current target is not included in the `--print=all-target-specs-json` output,
496+ // we check whether it is a custom target from the user or a synthetic target from bootstrap.
497+ if !targets. contains_key ( & config. target ) {
498+ let mut envs: HashMap < String , String > = HashMap :: new ( ) ;
499+
500+ if let Ok ( t) = std:: env:: var ( "RUST_TARGET_PATH" ) {
501+ envs. insert ( "RUST_TARGET_PATH" . into ( ) , t) ;
502+ }
503+
504+ // This returns false only when the target is neither a synthetic target
505+ // nor a custom target from the user, indicating it is most likely invalid.
506+ if config. target . ends_with ( ".json" ) || !envs. is_empty ( ) {
507+ targets. insert (
508+ config. target . clone ( ) ,
509+ serde_json:: from_str ( & rustc_output (
510+ config,
511+ & [
512+ "--print=target-spec-json" ,
513+ "-Zunstable-options" ,
514+ "--target" ,
515+ & config. target ,
516+ ] ,
517+ envs,
518+ ) )
519+ . unwrap ( ) ,
520+ ) ;
521+ }
504522 }
505523
506524 for ( target, cfg) in targets. iter ( ) {
@@ -545,7 +563,9 @@ impl TargetCfgs {
545563 // code below extracts them from `--print=cfg`: make sure to only override fields that can
546564 // actually be changed with `-C` flags.
547565 for config in
548- rustc_output ( config, & [ "--print=cfg" , "--target" , & config. target ] ) . trim ( ) . lines ( )
566+ rustc_output ( config, & [ "--print=cfg" , "--target" , & config. target ] , Default :: default ( ) )
567+ . trim ( )
568+ . lines ( )
549569 {
550570 let ( name, value) = config
551571 . split_once ( "=\" " )
@@ -624,11 +644,12 @@ pub enum Endian {
624644 Big ,
625645}
626646
627- fn rustc_output ( config : & Config , args : & [ & str ] ) -> String {
647+ fn rustc_output ( config : & Config , args : & [ & str ] , envs : HashMap < String , String > ) -> String {
628648 let mut command = Command :: new ( & config. rustc_path ) ;
629649 add_dylib_path ( & mut command, iter:: once ( & config. compile_lib_path ) ) ;
630650 command. args ( & config. target_rustcflags ) . args ( args) ;
631651 command. env ( "RUSTC_BOOTSTRAP" , "1" ) ;
652+ command. envs ( envs) ;
632653
633654 let output = match command. output ( ) {
634655 Ok ( output) => output,
0 commit comments