@@ -19,7 +19,7 @@ pub struct TargetCfgConfig {
19
19
pub other : BTreeMap < String , toml:: Value > ,
20
20
}
21
21
22
- /// Config definition of a `[target]` table.
22
+ /// Config definition of a `[target]` table or `[host]` .
23
23
#[ derive( Debug , Clone ) ]
24
24
pub struct TargetConfig {
25
25
/// Process to run as a wrapper for `cargo run`, `test`, and `bench` commands.
@@ -64,18 +64,62 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, Targ
64
64
Ok ( result)
65
65
}
66
66
67
+ /// Returns true if the `[target]` table should be applied to host targets.
68
+ pub ( super ) fn get_target_applies_to_host ( config : & Config ) -> CargoResult < bool > {
69
+ if config. cli_unstable ( ) . target_applies_to_host {
70
+ if let Ok ( target_applies_to_host) = config. get :: < bool > ( "target-applies-to-host" ) {
71
+ Ok ( target_applies_to_host)
72
+ } else {
73
+ Ok ( !config. cli_unstable ( ) . host_config )
74
+ }
75
+ } else {
76
+ if config. cli_unstable ( ) . host_config {
77
+ anyhow:: bail!(
78
+ "the -Zhost-config flag requires the -Ztarget-applies-to-host flag to be set"
79
+ ) ;
80
+ } else {
81
+ Ok ( true )
82
+ }
83
+ }
84
+ }
85
+
86
+ /// Loads a single `[host]` table for the given triple.
87
+ pub ( super ) fn load_host_triple ( config : & Config , triple : & str ) -> CargoResult < TargetConfig > {
88
+ if config. cli_unstable ( ) . host_config {
89
+ let host_triple_prefix = format ! ( "host.{}" , triple) ;
90
+ let host_triple_key = ConfigKey :: from_str ( & host_triple_prefix) ;
91
+ let host_prefix = match config. get_cv ( & host_triple_key) ? {
92
+ Some ( _) => host_triple_prefix,
93
+ None => "host" . to_string ( ) ,
94
+ } ;
95
+ load_config_table ( config, & host_prefix)
96
+ } else {
97
+ Ok ( TargetConfig {
98
+ runner : None ,
99
+ rustflags : None ,
100
+ linker : None ,
101
+ links_overrides : BTreeMap :: new ( ) ,
102
+ } )
103
+ }
104
+ }
105
+
67
106
/// Loads a single `[target]` table for the given triple.
68
107
pub ( super ) fn load_target_triple ( config : & Config , triple : & str ) -> CargoResult < TargetConfig > {
108
+ load_config_table ( config, & format ! ( "target.{}" , triple) )
109
+ }
110
+
111
+ /// Loads a single table for the given prefix.
112
+ fn load_config_table ( config : & Config , prefix : & str ) -> CargoResult < TargetConfig > {
69
113
// This needs to get each field individually because it cannot fetch the
70
114
// struct all at once due to `links_overrides`. Can't use `serde(flatten)`
71
115
// because it causes serde to use `deserialize_map` which means the config
72
116
// deserializer does not know which keys to deserialize, which means
73
117
// environment variables would not work.
74
- let runner: OptValue < PathAndArgs > = config. get ( & format ! ( "target. {}.runner" , triple ) ) ?;
75
- let rustflags: OptValue < StringList > = config. get ( & format ! ( "target. {}.rustflags" , triple ) ) ?;
76
- let linker: OptValue < ConfigRelativePath > = config. get ( & format ! ( "target. {}.linker" , triple ) ) ?;
118
+ let runner: OptValue < PathAndArgs > = config. get ( & format ! ( "{}.runner" , prefix ) ) ?;
119
+ let rustflags: OptValue < StringList > = config. get ( & format ! ( "{}.rustflags" , prefix ) ) ?;
120
+ let linker: OptValue < ConfigRelativePath > = config. get ( & format ! ( "{}.linker" , prefix ) ) ?;
77
121
// Links do not support environment variables.
78
- let target_key = ConfigKey :: from_str ( & format ! ( "target.{}" , triple ) ) ;
122
+ let target_key = ConfigKey :: from_str ( prefix ) ;
79
123
let links_overrides = match config. get_table ( & target_key) ? {
80
124
Some ( links) => parse_links_overrides ( & target_key, links. val , config) ?,
81
125
None => BTreeMap :: new ( ) ,
0 commit comments