@@ -19,7 +19,7 @@ pub struct TargetCfgConfig {
1919 pub other : BTreeMap < String , toml:: Value > ,
2020}
2121
22- /// Config definition of a `[target]` table.
22+ /// Config definition of a `[target]` table or `[host]` .
2323#[ derive( Debug , Clone ) ]
2424pub struct TargetConfig {
2525 /// 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
6464 Ok ( result)
6565}
6666
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+
67106/// Loads a single `[target]` table for the given triple.
68107pub ( 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 > {
69113 // This needs to get each field individually because it cannot fetch the
70114 // struct all at once due to `links_overrides`. Can't use `serde(flatten)`
71115 // because it causes serde to use `deserialize_map` which means the config
72116 // deserializer does not know which keys to deserialize, which means
73117 // 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 ) ) ?;
77121 // Links do not support environment variables.
78- let target_key = ConfigKey :: from_str ( & format ! ( "target.{}" , triple ) ) ;
122+ let target_key = ConfigKey :: from_str ( prefix ) ;
79123 let links_overrides = match config. get_table ( & target_key) ? {
80124 Some ( links) => parse_links_overrides ( & target_key, links. val , config) ?,
81125 None => BTreeMap :: new ( ) ,
0 commit comments