@@ -479,6 +479,7 @@ impl TargetCfgs {
479
479
let mut targets: HashMap < String , TargetCfg > = serde_json:: from_str ( & rustc_output (
480
480
config,
481
481
& [ "--print=all-target-specs-json" , "-Zunstable-options" ] ,
482
+ Default :: default ( ) ,
482
483
) )
483
484
. unwrap ( ) ;
484
485
@@ -491,16 +492,33 @@ impl TargetCfgs {
491
492
let mut all_families = HashSet :: new ( ) ;
492
493
let mut all_pointer_widths = HashSet :: new ( ) ;
493
494
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
+ }
504
522
}
505
523
506
524
for ( target, cfg) in targets. iter ( ) {
@@ -545,7 +563,9 @@ impl TargetCfgs {
545
563
// code below extracts them from `--print=cfg`: make sure to only override fields that can
546
564
// actually be changed with `-C` flags.
547
565
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 ( )
549
569
{
550
570
let ( name, value) = config
551
571
. split_once ( "=\" " )
@@ -624,11 +644,12 @@ pub enum Endian {
624
644
Big ,
625
645
}
626
646
627
- fn rustc_output ( config : & Config , args : & [ & str ] ) -> String {
647
+ fn rustc_output ( config : & Config , args : & [ & str ] , envs : HashMap < String , String > ) -> String {
628
648
let mut command = Command :: new ( & config. rustc_path ) ;
629
649
add_dylib_path ( & mut command, iter:: once ( & config. compile_lib_path ) ) ;
630
650
command. args ( & config. target_rustcflags ) . args ( args) ;
631
651
command. env ( "RUSTC_BOOTSTRAP" , "1" ) ;
652
+ command. envs ( envs) ;
632
653
633
654
let output = match command. output ( ) {
634
655
Ok ( output) => output,
0 commit comments