@@ -453,7 +453,7 @@ impl TargetCfgs {
453453 let mut all_families = HashSet :: new ( ) ;
454454 let mut all_pointer_widths = HashSet :: new ( ) ;
455455
456- for ( target, cfg) in targets. into_iter ( ) {
456+ for ( target, cfg) in targets. iter ( ) {
457457 all_archs. insert ( cfg. arch . clone ( ) ) ;
458458 all_oses. insert ( cfg. os . clone ( ) ) ;
459459 all_oses_and_envs. insert ( cfg. os_and_env ( ) ) ;
@@ -464,11 +464,11 @@ impl TargetCfgs {
464464 }
465465 all_pointer_widths. insert ( format ! ( "{}bit" , cfg. pointer_width) ) ;
466466
467- all_targets. insert ( target. into ( ) ) ;
467+ all_targets. insert ( target. clone ( ) ) ;
468468 }
469469
470470 Self {
471- current : Self :: get_current_target_config ( config) ,
471+ current : Self :: get_current_target_config ( config, & targets ) ,
472472 all_targets,
473473 all_archs,
474474 all_oses,
@@ -480,16 +480,20 @@ impl TargetCfgs {
480480 }
481481 }
482482
483- fn get_current_target_config ( config : & Config ) -> TargetCfg {
484- let mut arch = None ;
485- let mut os = None ;
486- let mut env = None ;
487- let mut abi = None ;
488- let mut families = Vec :: new ( ) ;
489- let mut pointer_width = None ;
490- let mut endian = None ;
491- let mut panic = None ;
492-
483+ fn get_current_target_config (
484+ config : & Config ,
485+ targets : & HashMap < String , TargetCfg > ,
486+ ) -> TargetCfg {
487+ let mut cfg = targets[ & config. target ] . clone ( ) ;
488+
489+ // To get the target information for the current target, we take the target spec obtained
490+ // from `--print=all-target-specs-json`, and then we enrich it with the information
491+ // gathered from `--print=cfg --target=$target`.
492+ //
493+ // This is done because some parts of the target spec can be overridden with `-C` flags,
494+ // which are respected for `--print=cfg` but not for `--print=all-target-specs-json`. The
495+ // code below extracts them from `--print=cfg`: make sure to only override fields that can
496+ // actually be changed with `-C` flags.
493497 for config in
494498 rustc_output ( config, & [ "--print=cfg" , "--target" , & config. target ] ) . trim ( ) . lines ( )
495499 {
@@ -507,60 +511,16 @@ impl TargetCfgs {
507511 } )
508512 . unwrap_or_else ( || ( config, None ) ) ;
509513
510- match name {
511- "target_arch" => {
512- arch = Some ( value. expect ( "target_arch should be a key-value pair" ) . to_string ( ) ) ;
513- }
514- "target_os" => {
515- os = Some ( value. expect ( "target_os sould be a key-value pair" ) . to_string ( ) ) ;
516- }
517- "target_env" => {
518- env = Some ( value. expect ( "target_env should be a key-value pair" ) . to_string ( ) ) ;
519- }
520- "target_abi" => {
521- abi = Some ( value. expect ( "target_abi should be a key-value pair" ) . to_string ( ) ) ;
522- }
523- "target_family" => {
524- families
525- . push ( value. expect ( "target_family should be a key-value pair" ) . to_string ( ) ) ;
526- }
527- "target_pointer_width" => {
528- pointer_width = Some (
529- value
530- . expect ( "target_pointer_width should be a key-value pair" )
531- . parse :: < u32 > ( )
532- . expect ( "target_pointer_width should be a valid u32" ) ,
533- ) ;
534- }
535- "target_endian" => {
536- endian = Some ( match value. expect ( "target_endian should be a key-value pair" ) {
537- "big" => Endian :: Big ,
538- "little" => Endian :: Little ,
539- _ => panic ! ( "target_endian should be either 'big' or 'little'" ) ,
540- } ) ;
541- }
542- "panic" => {
543- panic = Some ( match value. expect ( "panic should be a key-value pair" ) {
544- "abort" => PanicStrategy :: Abort ,
545- "unwind" => PanicStrategy :: Unwind ,
546- _ => panic ! ( "panic should be either 'abort' or 'unwind'" ) ,
547- } ) ;
548- }
549- _ => ( ) ,
514+ match ( name, value) {
515+ // Can be overridden with `-C panic=$strategy`.
516+ ( "panic" , Some ( "abort" ) ) => cfg. panic = PanicStrategy :: Abort ,
517+ ( "panic" , Some ( "unwind" ) ) => cfg. panic = PanicStrategy :: Unwind ,
518+ ( "panic" , other) => panic ! ( "unexpected value for panic cfg: {other:?}" ) ,
519+ _ => { }
550520 }
551521 }
552522
553- TargetCfg {
554- arch : arch. expect ( "target configuration should specify target_arch" ) ,
555- os : os. expect ( "target configuration should specify target_os" ) ,
556- env : env. expect ( "target configuration should specify target_env" ) ,
557- abi : abi. expect ( "target configuration should specify target_abi" ) ,
558- families,
559- pointer_width : pointer_width
560- . expect ( "target configuration should specify target_pointer_width" ) ,
561- endian : endian. expect ( "target configuration should specify target_endian" ) ,
562- panic : panic. expect ( "target configuration should specify panic" ) ,
563- }
523+ cfg
564524 }
565525}
566526
0 commit comments