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 Mar 23, 2022
1 parent a69752a commit cbb2382
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Config {
if let Some(env_value) = self.env.target() {
return Some(Target::from(&env_value, target_list));
}
None
self.toml.as_ref().map_or(None, |t| t.target(target_list))
}

fn sum_of_env_toml_values(
Expand Down Expand Up @@ -391,6 +391,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 Down
9 changes: 7 additions & 2 deletions src/cross_toml.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![doc = include_str!("../docs/cross_toml.md")]

use crate::errors::*;
use crate::Target;
use crate::{Target, TargetList};
use serde::Deserialize;
use std::collections::HashMap;

Expand Down Expand Up @@ -91,6 +91,11 @@ impl CrossToml {
.map_or(Vec::new(), |t| t.volumes.clone())
}

/// Returns the default target to build,
pub fn target(&self, target_list: &TargetList) -> Option<Target> {
self.build.as_ref().and_then(|b| b.target.as_ref()).map(|t| Target::from(t, target_list))
}

/// Returns a reference to the [`CrossTargetConfig`] of a specific `target`
fn get_target(&self, target: &Target) -> Option<&CrossTargetConfig> {
self.targets.get(target)
Expand Down Expand Up @@ -136,7 +141,7 @@ mod tests {
let test_str = r#"
[build]
xargo = true
[build.env]
volumes = ["VOL1_ARG", "VOL2_ARG"]
passthrough = ["VAR1", "VAR2"]
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::errors::*;
use crate::extensions::CommandExt;
use crate::{Host, Target};

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

0 comments on commit cbb2382

Please sign in to comment.