10
10
use crate :: core:: compiler:: apply_env_config;
11
11
use crate :: core:: compiler:: { BuildRunner , CompileKind , CompileMode , CompileTarget , CrateType } ;
12
12
use crate :: core:: { Dependency , Package , Target , TargetKind , Workspace } ;
13
- use crate :: util:: context:: { GlobalContext , StringList , TargetConfig } ;
13
+ use crate :: util:: context:: { GlobalContext , TargetConfig } ;
14
14
use crate :: util:: interning:: InternedString ;
15
15
use crate :: util:: { CargoResult , Rustc } ;
16
16
use anyhow:: Context as _;
@@ -161,14 +161,16 @@ impl TargetInfo {
161
161
requested_kinds : & [ CompileKind ] ,
162
162
rustc : & Rustc ,
163
163
kind : CompileKind ,
164
- // This config is used for `links_overrides` and `linker`.
165
- //
166
- // In the case of target_applies_to_host=true (default) it may contain
167
- // incorrect rustflags.
168
164
target_config : & TargetConfig ,
169
165
) -> CargoResult < TargetInfo > {
170
- let mut rustflags =
171
- extra_args ( gctx, requested_kinds, & rustc. host , None , kind, Flags :: Rust ) ?;
166
+ let mut rustflags = extra_args (
167
+ gctx,
168
+ requested_kinds,
169
+ None ,
170
+ target_config,
171
+ kind,
172
+ Flags :: Rust ,
173
+ ) ?;
172
174
let mut turn = 0 ;
173
175
loop {
174
176
let extra_fingerprint = kind. fingerprint_hash ( ) ;
@@ -290,8 +292,8 @@ impl TargetInfo {
290
292
let new_flags = extra_args (
291
293
gctx,
292
294
requested_kinds,
293
- & rustc. host ,
294
295
Some ( & cfg) ,
296
+ target_config,
295
297
kind,
296
298
Flags :: Rust ,
297
299
) ?;
@@ -324,8 +326,8 @@ impl TargetInfo {
324
326
rustdocflags : extra_args (
325
327
gctx,
326
328
requested_kinds,
327
- & rustc. host ,
328
329
Some ( & cfg) ,
330
+ target_config,
329
331
kind,
330
332
Flags :: Rustdoc ,
331
333
) ?
@@ -678,13 +680,6 @@ enum Flags {
678
680
}
679
681
680
682
impl Flags {
681
- fn as_key ( self ) -> & ' static str {
682
- match self {
683
- Flags :: Rust => "rustflags" ,
684
- Flags :: Rustdoc => "rustdocflags" ,
685
- }
686
- }
687
-
688
683
fn as_env ( self ) -> & ' static str {
689
684
match self {
690
685
Flags :: Rust => "RUSTFLAGS" ,
@@ -721,8 +716,8 @@ impl Flags {
721
716
fn extra_args (
722
717
gctx : & GlobalContext ,
723
718
requested_kinds : & [ CompileKind ] ,
724
- host_triple : & str ,
725
719
target_cfg : Option < & [ Cfg ] > ,
720
+ target_config : & TargetConfig ,
726
721
kind : CompileKind ,
727
722
flags : Flags ,
728
723
) -> CargoResult < Vec < String > > {
@@ -742,7 +737,7 @@ fn extra_args(
742
737
// --target. Or, phrased differently, no `--target` behaves the same as `--target
743
738
// <host>`, and host artifacts are always "special" (they don't pick up `RUSTFLAGS` for
744
739
// example).
745
- return Ok ( rustflags_from_host ( gctx , flags, host_triple ) ?. unwrap_or_else ( Vec :: new) ) ;
740
+ return Ok ( rustflags_from_host ( target_config , flags) ?. unwrap_or_else ( Vec :: new) ) ;
746
741
}
747
742
}
748
743
@@ -752,9 +747,7 @@ fn extra_args(
752
747
753
748
if let Some ( rustflags) = rustflags_from_env ( gctx, flags) {
754
749
Ok ( rustflags)
755
- } else if let Some ( rustflags) =
756
- rustflags_from_target ( gctx, host_triple, target_cfg, kind, flags) ?
757
- {
750
+ } else if let Some ( rustflags) = rustflags_from_target ( gctx, target_cfg, target_config, flags) ? {
758
751
Ok ( rustflags)
759
752
} else if let Some ( rustflags) = rustflags_from_build ( gctx, flags) ? {
760
753
Ok ( rustflags)
@@ -793,21 +786,18 @@ fn rustflags_from_env(gctx: &GlobalContext, flags: Flags) -> Option<Vec<String>>
793
786
/// See [`extra_args`] for more.
794
787
fn rustflags_from_target (
795
788
gctx : & GlobalContext ,
796
- host_triple : & str ,
797
789
target_cfg : Option < & [ Cfg ] > ,
798
- kind : CompileKind ,
790
+ target_config : & TargetConfig ,
799
791
flag : Flags ,
800
792
) -> CargoResult < Option < Vec < String > > > {
801
793
let mut rustflags = Vec :: new ( ) ;
802
794
803
- // Then the target.*.rustflags value...
804
- let target = match & kind {
805
- CompileKind :: Host => host_triple,
806
- CompileKind :: Target ( target) => target. short_name ( ) ,
795
+ let config_flags = match flag {
796
+ Flags :: Rust => & target_config. rustflags ,
797
+ Flags :: Rustdoc => & target_config. rustdocflags ,
807
798
} ;
808
- let key = format ! ( "target.{}.{}" , target, flag. as_key( ) ) ;
809
- if let Some ( args) = gctx. get :: < Option < StringList > > ( & key) ? {
810
- rustflags. extend ( args. as_slice ( ) . iter ( ) . cloned ( ) ) ;
799
+ if let Some ( args) = config_flags {
800
+ rustflags. extend ( args. val . as_slice ( ) . iter ( ) . cloned ( ) ) ;
811
801
}
812
802
// ...including target.'cfg(...)'.rustflags
813
803
if let Some ( target_cfg) = target_cfg {
@@ -875,13 +865,11 @@ fn target_linker(
875
865
/// Gets compiler flags from `[host]` section in the config.
876
866
/// See [`extra_args`] for more.
877
867
fn rustflags_from_host (
878
- gctx : & GlobalContext ,
868
+ target_config : & TargetConfig ,
879
869
flag : Flags ,
880
- host_triple : & str ,
881
870
) -> CargoResult < Option < Vec < String > > > {
882
- let target_cfg = gctx. host_cfg_triple ( host_triple) ?;
883
871
let list = match flag {
884
- Flags :: Rust => & target_cfg . rustflags ,
872
+ Flags :: Rust => & target_config . rustflags ,
885
873
Flags :: Rustdoc => {
886
874
// host.rustdocflags is not a thing, since it does not make sense
887
875
return Ok ( None ) ;
@@ -940,9 +928,14 @@ impl<'gctx> RustcTargetData<'gctx> {
940
928
let target_applies_to_host = gctx. target_applies_to_host ( ) ?;
941
929
let host_target = CompileTarget :: new ( & rustc. host ) ?;
942
930
943
- // This config is used for link overrides and choosing a linker.
944
931
let host_config = if target_applies_to_host {
945
- gctx. target_cfg_triple ( & rustc. host ) ?
932
+ let mut config = gctx. target_cfg_triple ( & rustc. host ) ?;
933
+ if requested_kinds != [ CompileKind :: Host ] {
934
+ // When an explicit target flag is passed rustflags are ignored for host artifacts.
935
+ config. rustflags = None ;
936
+ config. rustdocflags = None ;
937
+ }
938
+ config
946
939
} else {
947
940
gctx. host_cfg_triple ( & rustc. host ) ?
948
941
} ;
0 commit comments