From ad583fb0ca5e2cf52fe9ddffa121f97fb3afd525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Wed, 6 Apr 2022 09:28:44 +0200 Subject: [PATCH] fix wrong impl of env.passthrough and env.volumes on targets --- docs/cross_toml.md | 12 +++++++-- src/cross_toml.rs | 66 ++++++++++++++++++++++------------------------ 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/docs/cross_toml.md b/docs/cross_toml.md index 32e0a39dc..662c541af 100644 --- a/docs/cross_toml.md +++ b/docs/cross_toml.md @@ -25,9 +25,17 @@ The `target` key allows you to specify parameters for specific compilation targe ```toml [target.aarch64-unknown-linux-gnu] -volumes = ["VOL1_ARG", "VOL2_ARG"] -passthrough = ["VAR1", "VAR2"] xargo = false image = "test-image" runner = "custom-runner" ``` + +# `target.TARGET.env` +The `target` key allows you to specify environment variables that should be used for a specific compilation target. +This is similar to `build.env`, but allows you to be more specific per target. + +```toml +[target.x86_64-unknown-linux-gnu.env] +volumes = ["VOL1_ARG", "VOL2_ARG"] +passthrough = ["IMPORTANT_ENV_VARIABLES"] +``` \ No newline at end of file diff --git a/src/cross_toml.rs b/src/cross_toml.rs index 2a571f175..3968d1963 100644 --- a/src/cross_toml.rs +++ b/src/cross_toml.rs @@ -5,9 +5,9 @@ use crate::{Target, TargetList}; use serde::Deserialize; use std::collections::HashMap; -/// Build environment configuration -#[derive(Debug, Deserialize, PartialEq)] -pub struct CrossBuildEnvConfig { +/// Environment configuration +#[derive(Debug, Deserialize, PartialEq, Default)] +pub struct CrossEnvConfig { #[serde(default)] volumes: Vec, #[serde(default)] @@ -18,7 +18,8 @@ pub struct CrossBuildEnvConfig { #[derive(Debug, Deserialize, PartialEq, Default)] #[serde(rename_all = "kebab-case")] pub struct CrossBuildConfig { - env: Option, + #[serde(default)] + env: CrossEnvConfig, xargo: Option, default_target: Option, } @@ -26,13 +27,11 @@ pub struct CrossBuildConfig { /// Target configuration #[derive(Debug, Deserialize, PartialEq)] pub struct CrossTargetConfig { - #[serde(default)] - passthrough: Vec, - #[serde(default)] - volumes: Vec, xargo: Option, image: Option, runner: Option, + #[serde(default)] + env: CrossEnvConfig, } /// Cross configuration @@ -40,7 +39,8 @@ pub struct CrossTargetConfig { pub struct CrossToml { #[serde(default, rename = "target")] targets: HashMap, - build: Option, + #[serde(default)] + build: CrossBuildConfig, } impl CrossToml { @@ -62,7 +62,7 @@ impl CrossToml { /// Returns the `build.xargo` or the `target.{}.xargo` part of `Cross.toml` pub fn xargo(&self, target: &Target) -> (Option, Option) { - let build_xargo = self.build.as_ref().and_then(|b| b.xargo); + let build_xargo = self.build.xargo; let target_xargo = self.get_target(target).and_then(|t| t.xargo); (build_xargo, target_xargo) @@ -70,33 +70,31 @@ impl CrossToml { /// Returns the list of environment variables to pass through for `build`, pub fn env_passthrough_build(&self) -> Vec { - self.get_build_env() - .map_or(Vec::new(), |e| e.passthrough.clone()) + self.build.env.passthrough.clone() } /// Returns the list of environment variables to pass through for `target`, pub fn env_passthrough_target(&self, target: &Target) -> Vec { self.get_target(target) - .map_or(Vec::new(), |t| t.passthrough.clone()) + .map_or(Vec::new(), |t| t.env.passthrough.clone()) } /// Returns the list of environment variables to pass through for `build`, pub fn env_volumes_build(&self) -> Vec { - self.get_build_env() - .map_or(Vec::new(), |e| e.volumes.clone()) + self.build.env.volumes.clone() } /// Returns the list of environment variables to pass through for `target`, pub fn env_volumes_target(&self, target: &Target) -> Vec { self.get_target(target) - .map_or(Vec::new(), |t| t.volumes.clone()) + .map_or(Vec::new(), |t| t.env.volumes.clone()) } /// Returns the default target to build, pub fn default_target(&self, target_list: &TargetList) -> Option { self.build + .default_target .as_ref() - .and_then(|b| b.default_target.as_ref()) .map(|t| Target::from(t, target_list)) } @@ -104,11 +102,6 @@ impl CrossToml { fn get_target(&self, target: &Target) -> Option<&CrossTargetConfig> { self.targets.get(target) } - - /// Returns a reference to the [`CrossBuildEnvConfig`] - fn get_build_env(&self) -> Option<&CrossBuildEnvConfig> { - self.build.as_ref().and_then(|b| b.env.as_ref()) - } } #[cfg(test)] @@ -119,7 +112,7 @@ mod tests { pub fn parse_empty_toml() -> Result<()> { let cfg = CrossToml { targets: HashMap::new(), - build: None, + build: CrossBuildConfig::default(), }; let parsed_cfg = CrossToml::from_str("")?; @@ -132,14 +125,14 @@ mod tests { pub fn parse_build_toml() -> Result<()> { let cfg = CrossToml { targets: HashMap::new(), - build: Some(CrossBuildConfig { - env: Some(CrossBuildEnvConfig { + build: CrossBuildConfig { + env: CrossEnvConfig { volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()], passthrough: vec!["VAR1".to_string(), "VAR2".to_string()], - }), + }, xargo: Some(true), default_target: None, - }), + }, }; let test_str = r#" @@ -165,8 +158,10 @@ mod tests { triple: "aarch64-unknown-linux-gnu".to_string(), }, CrossTargetConfig { - passthrough: vec!["VAR1".to_string(), "VAR2".to_string()], - volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()], + env: CrossEnvConfig { + passthrough: vec!["VAR1".to_string(), "VAR2".to_string()], + volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()], + }, xargo: Some(false), image: Some("test-image".to_string()), runner: None, @@ -175,15 +170,16 @@ mod tests { let cfg = CrossToml { targets: target_map, - build: None, + build: CrossBuildConfig::default(), }; let test_str = r#" - [target.aarch64-unknown-linux-gnu] - volumes = ["VOL1_ARG", "VOL2_ARG"] - passthrough = ["VAR1", "VAR2"] - xargo = false - image = "test-image" + [target.aarch64-unknown-linux-gnu.env] + volumes = ["VOL1_ARG", "VOL2_ARG"] + passthrough = ["VAR1", "VAR2"] + [target.aarch64-unknown-linux-gnu] + xargo = false + image = "test-image" "#; let parsed_cfg = CrossToml::from_str(test_str)?;