From 913f457989c424533f311b47a85b3ba2605949d1 Mon Sep 17 00:00:00 2001 From: Bjoern Hiller Date: Sat, 8 Jan 2022 14:07:09 +0100 Subject: [PATCH] Allow setting default target via environment --- src/config.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 1318ac517..197939bf7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -79,6 +79,10 @@ impl Environment { self.get_values_for("ENV_VOLUMES", target) } + fn target(&self) -> Option { + self.get_build_var("TARGET") + } + fn get_values_for( &self, var: &str, @@ -180,7 +184,10 @@ impl Config { Ok(collected) } - pub fn target(&self, _target_list: &TargetList) -> Result> { + pub fn target(&self, target_list: &TargetList) -> Result> { + if let Some(env_value) = self.env.target() { + return Ok(Some(Target::from(&env_value, target_list))); + } Ok(None) } @@ -379,6 +386,22 @@ mod tests { Ok(()) } + #[test] + pub fn env_and_toml_default_target_then_use_env() -> Result<()> { + let mut map = HashMap::new(); + map.insert("CROSS_BUILD_TARGET", "armv7-unknown-linux-musleabihf"); + let env = Environment::new(Some(map)); + let config = Config::new_with(Some(toml(TOML_DEFAULT_TARGET)?), env); + + let config_target = config.target(&target_list())?.unwrap(); + assert!(matches!( + config_target.triple(), + "armv7-unknown-linux-musleabihf" + )); + + Ok(()) + } + static TOML_BUILD_XARGO_FALSE: &str = r#" [build] xargo = false @@ -394,6 +417,11 @@ mod tests { volumes = ["VOLUME3", "VOLUME4"] [target.aarch64-unknown-linux-gnu] xargo = false + "#; + + static TOML_DEFAULT_TARGET: &str = r#" + [build] + default = "aarch64-unknown-linux-gnu" "#; } }