Skip to content

Commit

Permalink
Allow setting default target via Cross.toml
Browse files Browse the repository at this point in the history
N.B. `Debug` is needed for `TargetList` as linting rules demand it.
  • Loading branch information
zappolowski committed Jan 8, 2022
1 parent 913f457 commit 22ab4d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl Config {
if let Some(env_value) = self.env.target() {
return Ok(Some(Target::from(&env_value, target_list)));
}
Ok(None)
self.toml.as_ref().map_or(Ok(None), |t| t.target(target_list))
}

fn sum_of_env_toml_values<'a>(
Expand Down Expand Up @@ -402,6 +402,20 @@ mod tests {
Ok(())
}

#[test]
pub fn no_env_but_toml_default_target_then_use_toml() -> Result<()> {
let env = Environment::new(None);
let config = Config::new_with(Some(toml(TOML_DEFAULT_TARGET)?), env);

let config_target = config.target(&target_list())?.unwrap();
assert!(matches!(
config_target.triple(),
"aarch64-unknown-linux-gnu"
));

Ok(())
}

static TOML_BUILD_XARGO_FALSE: &str = r#"
[build]
xargo = false
Expand All @@ -421,7 +435,7 @@ mod tests {

static TOML_DEFAULT_TARGET: &str = r#"
[build]
default = "aarch64-unknown-linux-gnu"
target = "aarch64-unknown-linux-gnu"
"#;
}
}
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,18 @@ impl Toml {
self.target_env(target, "volumes")
}

/// Returns the default target to build,
pub fn target(&self, target_list: &TargetList) -> Result<Option<Target>> {
if let Some(value) = self.table.get("build").and_then(|t| t.get("target")) {
let value = value
.as_str()
.ok_or_else(|| "build.target must be a string".to_string())?;
Ok(Some(Target::from(&value, target_list)))
} else {
Ok(None)
}
}

fn target_env(&self, target: &Target, key: &str) -> Result<Vec<&str>> {
let triple = target.triple();

Expand Down
1 change: 1 addition & 0 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{Host, Target};
use crate::errors::*;
use crate::extensions::CommandExt;

#[derive(Debug)]
pub struct TargetList {
pub triples: Vec<String>,
}
Expand Down

0 comments on commit 22ab4d1

Please sign in to comment.