@@ -79,11 +79,6 @@ impl TargetTriple {
79
79
} ) ) ;
80
80
}
81
81
82
- // Next, look at the CARGO_BUILD_TARGET env var.
83
- if let Some ( triple) = Self :: from_env ( ) ? {
84
- return Ok ( Some ( triple) ) ;
85
- }
86
-
87
82
// Finally, look at the cargo configs.
88
83
Self :: from_cargo_configs ( cargo_configs)
89
84
}
@@ -106,14 +101,25 @@ impl TargetTriple {
106
101
}
107
102
108
103
fn from_cargo_configs ( cargo_configs : & CargoConfigs ) -> Result < Option < Self > , TargetTripleError > {
109
- for ( source, config) in cargo_configs. discovered_configs ( ) ? {
110
- if let Some ( triple) = & config. build . target {
111
- return Ok ( Some ( TargetTriple {
112
- triple : triple. to_owned ( ) ,
113
- source : TargetTripleSource :: CargoConfig {
114
- source : source. clone ( ) ,
115
- } ,
116
- } ) ) ;
104
+ for discovered_config in cargo_configs. discovered_configs ( ) ? {
105
+ match discovered_config {
106
+ DiscoveredConfig :: CliOption { config, source }
107
+ | DiscoveredConfig :: File { config, source } => {
108
+ if let Some ( triple) = & config. build . target {
109
+ return Ok ( Some ( TargetTriple {
110
+ triple : triple. to_owned ( ) ,
111
+ source : TargetTripleSource :: CargoConfig {
112
+ source : source. clone ( ) ,
113
+ } ,
114
+ } ) ) ;
115
+ }
116
+ }
117
+ DiscoveredConfig :: Env => {
118
+ // Look at the CARGO_BUILD_TARGET env var.
119
+ if let Some ( triple) = Self :: from_env ( ) ? {
120
+ return Ok ( Some ( triple) ) ;
121
+ }
122
+ }
117
123
}
118
124
}
119
125
@@ -242,18 +248,48 @@ impl CargoConfigs {
242
248
pub ( crate ) fn discovered_configs (
243
249
& self ,
244
250
) -> Result <
245
- impl Iterator < Item = & ( CargoConfigSource , CargoConfig ) > + DoubleEndedIterator + ' _ ,
251
+ impl Iterator < Item = DiscoveredConfig < ' _ > > + DoubleEndedIterator + ' _ ,
246
252
CargoConfigError ,
247
253
> {
248
- let cli_iter = self . cli_configs . iter ( ) ;
249
- let file_iter = self
254
+ // TODO/NOTE: https://github.com/rust-lang/cargo/issues/10992 means that currently
255
+ // environment variables are privileged over files passed in over the CLI. Once this
256
+ // behavior is fixed in upstream cargo, it should also be fixed here.
257
+ let cli_option_iter = self . cli_configs . iter ( ) . filter_map ( |( source, config) | {
258
+ matches ! ( source, CargoConfigSource :: CliOption )
259
+ . then ( || DiscoveredConfig :: CliOption { config, source } )
260
+ } ) ;
261
+
262
+ let cli_file_iter = self . cli_configs . iter ( ) . filter_map ( |( source, config) | {
263
+ matches ! ( source, CargoConfigSource :: File ( _) )
264
+ . then ( || DiscoveredConfig :: File { config, source } )
265
+ } ) ;
266
+
267
+ let cargo_config_file_iter = self
250
268
. discovered
251
269
. get_or_try_init ( || discover_impl ( & self . cwd , self . terminate_search_at . as_deref ( ) ) ) ?
252
- . iter ( ) ;
253
- Ok ( cli_iter. chain ( file_iter) )
270
+ . iter ( )
271
+ . map ( |( source, config) | DiscoveredConfig :: File { config, source } ) ;
272
+
273
+ Ok ( cli_option_iter
274
+ . chain ( std:: iter:: once ( DiscoveredConfig :: Env ) )
275
+ . chain ( cli_file_iter)
276
+ . chain ( cargo_config_file_iter) )
254
277
}
255
278
}
256
279
280
+ pub ( crate ) enum DiscoveredConfig < ' a > {
281
+ CliOption {
282
+ config : & ' a CargoConfig ,
283
+ source : & ' a CargoConfigSource ,
284
+ } ,
285
+ // Sentinel value to indicate to users that they should look up their config in the environment.
286
+ Env ,
287
+ File {
288
+ config : & ' a CargoConfig ,
289
+ source : & ' a CargoConfigSource ,
290
+ } ,
291
+ }
292
+
257
293
fn parse_cli_configs (
258
294
cwd : & Utf8Path ,
259
295
cli_configs : impl Iterator < Item = impl AsRef < str > > ,
@@ -569,7 +605,7 @@ mod tests {
569
605
let dir_foo_bar_path = dir_foo_path. join ( "bar" ) ;
570
606
571
607
assert_eq ! (
572
- find_target_triple( & [ ] , & dir_foo_bar_path, & dir_path) ,
608
+ find_target_triple( & [ ] , None , & dir_foo_bar_path, & dir_path) ,
573
609
Some ( TargetTriple {
574
610
triple: "x86_64-unknown-linux-gnu" . into( ) ,
575
611
source: TargetTripleSource :: CargoConfig {
@@ -579,7 +615,7 @@ mod tests {
579
615
) ;
580
616
581
617
assert_eq ! (
582
- find_target_triple( & [ ] , & dir_foo_path, & dir_path) ,
618
+ find_target_triple( & [ ] , None , & dir_foo_path, & dir_path) ,
583
619
Some ( TargetTriple {
584
620
triple: "x86_64-pc-windows-msvc" . into( ) ,
585
621
source: TargetTripleSource :: CargoConfig {
@@ -591,6 +627,7 @@ mod tests {
591
627
assert_eq ! (
592
628
find_target_triple(
593
629
& [ "build.target=\" aarch64-unknown-linux-gnu\" " ] ,
630
+ None ,
594
631
& dir_foo_bar_path,
595
632
& dir_path
596
633
) ,
@@ -609,6 +646,7 @@ mod tests {
609
646
"build.target=\" aarch64-unknown-linux-gnu\" " ,
610
647
"build.target=\" x86_64-unknown-linux-musl\" "
611
648
] ,
649
+ None ,
612
650
& dir_foo_bar_path,
613
651
& dir_path
614
652
) ,
@@ -620,36 +658,73 @@ mod tests {
620
658
} )
621
659
) ;
622
660
623
- // --config <path> should be parsed correctly .
661
+ // --config is preferred over the environment .
624
662
assert_eq ! (
625
663
find_target_triple(
626
- & [
627
- "extra-config.toml" ,
628
- "build.target=\" x86_64-unknown-linux-musl\" " ,
629
- ] ,
630
- & dir_foo_path,
664
+ & [ "build.target=\" aarch64-unknown-linux-gnu\" " , ] ,
665
+ Some ( "aarch64-pc-windows-msvc" ) ,
666
+ & dir_foo_bar_path,
631
667
& dir_path
632
668
) ,
669
+ Some ( TargetTriple {
670
+ triple: "aarch64-unknown-linux-gnu" . into( ) ,
671
+ source: TargetTripleSource :: CargoConfig {
672
+ source: CargoConfigSource :: CliOption ,
673
+ } ,
674
+ } )
675
+ ) ;
676
+
677
+ // The environment is preferred over local paths.
678
+ assert_eq ! (
679
+ find_target_triple(
680
+ & [ ] ,
681
+ Some ( "aarch64-pc-windows-msvc" ) ,
682
+ & dir_foo_bar_path,
683
+ & dir_path
684
+ ) ,
685
+ Some ( TargetTriple {
686
+ triple: "aarch64-pc-windows-msvc" . into( ) ,
687
+ source: TargetTripleSource :: Env ,
688
+ } )
689
+ ) ;
690
+
691
+ // --config <path> should be parsed correctly. Config files currently come after
692
+ // keys and values passed in via --config, and after the environment.
693
+ assert_eq ! (
694
+ find_target_triple( & [ "extra-config.toml" ] , None , & dir_foo_path, & dir_path) ,
633
695
Some ( TargetTriple {
634
696
triple: "aarch64-unknown-linux-gnu" . into( ) ,
635
697
source: TargetTripleSource :: CargoConfig {
636
698
source: CargoConfigSource :: File ( dir_foo_path. join( "extra-config.toml" ) ) ,
637
699
} ,
638
700
} )
639
701
) ;
702
+ assert_eq ! (
703
+ find_target_triple(
704
+ & [ "extra-config.toml" ] ,
705
+ Some ( "aarch64-pc-windows-msvc" ) ,
706
+ & dir_foo_path,
707
+ & dir_path
708
+ ) ,
709
+ Some ( TargetTriple {
710
+ triple: "aarch64-pc-windows-msvc" . into( ) ,
711
+ source: TargetTripleSource :: Env ,
712
+ } )
713
+ ) ;
640
714
assert_eq ! (
641
715
find_target_triple(
642
716
& [
643
717
"../extra-config.toml" ,
644
718
"build.target=\" x86_64-unknown-linux-musl\" " ,
645
719
] ,
720
+ None ,
646
721
& dir_foo_bar_path,
647
722
& dir_path
648
723
) ,
649
724
Some ( TargetTriple {
650
- triple: "aarch64 -unknown-linux-gnu " . into( ) ,
725
+ triple: "x86_64 -unknown-linux-musl " . into( ) ,
651
726
source: TargetTripleSource :: CargoConfig {
652
- source: CargoConfigSource :: File ( dir_foo_bar_path . join ( "../extra-config.toml" ) ) ,
727
+ source: CargoConfigSource :: CliOption ,
653
728
} ,
654
729
} )
655
730
) ;
@@ -659,6 +734,7 @@ mod tests {
659
734
"build.target=\" x86_64-unknown-linux-musl\" " ,
660
735
"extra-config.toml" ,
661
736
] ,
737
+ None ,
662
738
& dir_foo_path,
663
739
& dir_path
664
740
) ,
@@ -670,7 +746,7 @@ mod tests {
670
746
} )
671
747
) ;
672
748
673
- assert_eq ! ( find_target_triple( & [ ] , & dir_path, & dir_path) , None ) ;
749
+ assert_eq ! ( find_target_triple( & [ ] , None , & dir_path, & dir_path) , None ) ;
674
750
}
675
751
676
752
fn setup_temp_dir ( ) -> Result < TempDir > {
@@ -704,13 +780,19 @@ mod tests {
704
780
705
781
fn find_target_triple (
706
782
cli_configs : & [ & str ] ,
783
+ env : Option < & str > ,
707
784
start_search_at : & Utf8Path ,
708
785
terminate_search_at : & Utf8Path ,
709
786
) -> Option < TargetTriple > {
710
787
let configs =
711
788
CargoConfigs :: new_with_isolation ( cli_configs, start_search_at, terminate_search_at)
712
789
. unwrap ( ) ;
713
- TargetTriple :: from_cargo_configs ( & configs) . unwrap ( )
790
+ if let Some ( env) = env {
791
+ std:: env:: set_var ( "CARGO_BUILD_TARGET" , env) ;
792
+ }
793
+ let ret = TargetTriple :: from_cargo_configs ( & configs) . unwrap ( ) ;
794
+ std:: env:: remove_var ( "CARGO_BUILD_TARGET" ) ;
795
+ ret
714
796
}
715
797
716
798
static FOO_CARGO_CONFIG_CONTENTS : & str = r#"
0 commit comments