Skip to content

Commit

Permalink
Refactor host and target config table loads to deduplicate logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshilliard committed Apr 26, 2021
1 parent 083cc9e commit e51d151
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/cargo/util/config/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,45 +66,31 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, Targ

/// Loads a single `[host]` table for the given triple.
pub(super) fn load_host_triple(config: &Config, triple: &str) -> CargoResult<TargetConfig> {
// This needs to get each field individually because it cannot fetch the
// struct all at once due to `links_overrides`. Can't use `serde(flatten)`
// because it causes serde to use `deserialize_map` which means the config
// deserializer does not know which keys to deserialize, which means
// environment variables would not work.
let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple));
let host_prefix = match config.get_cv(&host_triple_key)? {
Some(_) => format!("host.{}", triple),
None => "host".to_string(),
};
let runner: OptValue<PathAndArgs> = config.get(&format!("{}.runner", host_prefix))?;
let rustflags: OptValue<StringList> = config.get(&format!("{}.rustflags", host_prefix))?;
let linker: OptValue<ConfigRelativePath> = config.get(&format!("{}.linker", host_prefix))?;
// Links do not support environment variables.
let target_key = ConfigKey::from_str(&host_prefix);
let links_overrides = match config.get_table(&target_key)? {
Some(links) => parse_links_overrides(&target_key, links.val, config)?,
None => BTreeMap::new(),
};
Ok(TargetConfig {
runner,
rustflags,
linker,
links_overrides,
})
load_config_table(config, &host_prefix)
}

/// Loads a single `[target]` table for the given triple.
pub(super) fn load_target_triple(config: &Config, triple: &str) -> CargoResult<TargetConfig> {
load_config_table(config, &format!("target.{}", triple))
}

/// Loads a single table for the given prefix.
fn load_config_table(config: &Config, prefix: &str) -> CargoResult<TargetConfig> {
// This needs to get each field individually because it cannot fetch the
// struct all at once due to `links_overrides`. Can't use `serde(flatten)`
// because it causes serde to use `deserialize_map` which means the config
// deserializer does not know which keys to deserialize, which means
// environment variables would not work.
let runner: OptValue<PathAndArgs> = config.get(&format!("target.{}.runner", triple))?;
let rustflags: OptValue<StringList> = config.get(&format!("target.{}.rustflags", triple))?;
let linker: OptValue<ConfigRelativePath> = config.get(&format!("target.{}.linker", triple))?;
let runner: OptValue<PathAndArgs> = config.get(&format!("{}.runner", prefix))?;
let rustflags: OptValue<StringList> = config.get(&format!("{}.rustflags", prefix))?;
let linker: OptValue<ConfigRelativePath> = config.get(&format!("{}.linker", prefix))?;
// Links do not support environment variables.
let target_key = ConfigKey::from_str(&format!("target.{}", triple));
let target_key = ConfigKey::from_str(prefix);
let links_overrides = match config.get_table(&target_key)? {
Some(links) => parse_links_overrides(&target_key, links.val, config)?,
None => BTreeMap::new(),
Expand Down

0 comments on commit e51d151

Please sign in to comment.