Skip to content

Commit e51d151

Browse files
committed
Refactor host and target config table loads to deduplicate logic.
1 parent 083cc9e commit e51d151

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

src/cargo/util/config/target.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,45 +66,31 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, Targ
6666

6767
/// Loads a single `[host]` table for the given triple.
6868
pub(super) fn load_host_triple(config: &Config, triple: &str) -> CargoResult<TargetConfig> {
69-
// This needs to get each field individually because it cannot fetch the
70-
// struct all at once due to `links_overrides`. Can't use `serde(flatten)`
71-
// because it causes serde to use `deserialize_map` which means the config
72-
// deserializer does not know which keys to deserialize, which means
73-
// environment variables would not work.
7469
let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple));
7570
let host_prefix = match config.get_cv(&host_triple_key)? {
7671
Some(_) => format!("host.{}", triple),
7772
None => "host".to_string(),
7873
};
79-
let runner: OptValue<PathAndArgs> = config.get(&format!("{}.runner", host_prefix))?;
80-
let rustflags: OptValue<StringList> = config.get(&format!("{}.rustflags", host_prefix))?;
81-
let linker: OptValue<ConfigRelativePath> = config.get(&format!("{}.linker", host_prefix))?;
82-
// Links do not support environment variables.
83-
let target_key = ConfigKey::from_str(&host_prefix);
84-
let links_overrides = match config.get_table(&target_key)? {
85-
Some(links) => parse_links_overrides(&target_key, links.val, config)?,
86-
None => BTreeMap::new(),
87-
};
88-
Ok(TargetConfig {
89-
runner,
90-
rustflags,
91-
linker,
92-
links_overrides,
93-
})
74+
load_config_table(config, &host_prefix)
9475
}
9576

9677
/// Loads a single `[target]` table for the given triple.
9778
pub(super) fn load_target_triple(config: &Config, triple: &str) -> CargoResult<TargetConfig> {
79+
load_config_table(config, &format!("target.{}", triple))
80+
}
81+
82+
/// Loads a single table for the given prefix.
83+
fn load_config_table(config: &Config, prefix: &str) -> CargoResult<TargetConfig> {
9884
// This needs to get each field individually because it cannot fetch the
9985
// struct all at once due to `links_overrides`. Can't use `serde(flatten)`
10086
// because it causes serde to use `deserialize_map` which means the config
10187
// deserializer does not know which keys to deserialize, which means
10288
// environment variables would not work.
103-
let runner: OptValue<PathAndArgs> = config.get(&format!("target.{}.runner", triple))?;
104-
let rustflags: OptValue<StringList> = config.get(&format!("target.{}.rustflags", triple))?;
105-
let linker: OptValue<ConfigRelativePath> = config.get(&format!("target.{}.linker", triple))?;
89+
let runner: OptValue<PathAndArgs> = config.get(&format!("{}.runner", prefix))?;
90+
let rustflags: OptValue<StringList> = config.get(&format!("{}.rustflags", prefix))?;
91+
let linker: OptValue<ConfigRelativePath> = config.get(&format!("{}.linker", prefix))?;
10692
// Links do not support environment variables.
107-
let target_key = ConfigKey::from_str(&format!("target.{}", triple));
93+
let target_key = ConfigKey::from_str(prefix);
10894
let links_overrides = match config.get_table(&target_key)? {
10995
Some(links) => parse_links_overrides(&target_key, links.val, config)?,
11096
None => BTreeMap::new(),

0 commit comments

Comments
 (0)