Skip to content

Commit

Permalink
fix wrong impl of env.passthrough and env.volumes on targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilgardis committed Apr 6, 2022
1 parent 0a57e56 commit 3bfd789
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
12 changes: 10 additions & 2 deletions docs/cross_toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ 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"
dockerfile = { file = "./Dockerfile", context = ".", build-args = { ARG1 = "foo" } } # also supports `dockerfile = "./Dockerfile"`
pre-build = ["echo 'Hello world!'"]
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"]
```
66 changes: 31 additions & 35 deletions src/cross_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use serde::Deserialize;
use std::collections::HashMap;
use std::str::FromStr;

/// Build environment configuration
#[derive(Debug, Deserialize, PartialEq)]
pub struct CrossBuildEnvConfig {
/// Environment configuration
#[derive(Debug, Deserialize, PartialEq, Default)]
pub struct CrossEnvConfig {
#[serde(default)]
volumes: Vec<String>,
#[serde(default)]
Expand All @@ -19,7 +19,8 @@ pub struct CrossBuildEnvConfig {
#[derive(Debug, Deserialize, PartialEq, Default)]
#[serde(rename_all = "kebab-case")]
pub struct CrossBuildConfig {
env: Option<CrossBuildEnvConfig>,
#[serde(default)]
env: CrossEnvConfig,
xargo: Option<bool>,
default_target: Option<String>,
}
Expand All @@ -28,16 +29,14 @@ pub struct CrossBuildConfig {
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct CrossTargetConfig {
#[serde(default)]
passthrough: Vec<String>,
#[serde(default)]
volumes: Vec<String>,
xargo: Option<bool>,
image: Option<String>,
#[serde(default, deserialize_with = "opt_string_or_struct")]
dockerfile: Option<CrossTargetConfigDockerfile>,
pre_build: Option<Vec<String>>,
runner: Option<String>,
#[serde(default)]
env: CrossEnvConfig,
}

/// Dockerfile configuration
Expand Down Expand Up @@ -66,7 +65,8 @@ impl FromStr for CrossTargetConfigDockerfile {
pub struct CrossToml {
#[serde(default, rename = "target")]
targets: HashMap<Target, CrossTargetConfig>,
build: Option<CrossBuildConfig>,
#[serde(default)]
build: CrossBuildConfig,
}

impl CrossToml {
Expand Down Expand Up @@ -114,53 +114,46 @@ impl CrossToml {

/// Returns the `build.xargo` or the `target.{}.xargo` part of `Cross.toml`
pub fn xargo(&self, target: &Target) -> (Option<bool>, Option<bool>) {
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)
}

/// Returns the list of environment variables to pass through for `build`,
pub fn env_passthrough_build(&self) -> Vec<String> {
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<String> {
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<String> {
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<String> {
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<Target> {
self.build
.default_target
.as_ref()
.and_then(|b| b.default_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)
}

/// Returns a reference to the [`CrossBuildEnvConfig`]
fn get_build_env(&self) -> Option<&CrossBuildEnvConfig> {
self.build.as_ref().and_then(|b| b.env.as_ref())
}
}

fn opt_string_or_struct<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
Expand Down Expand Up @@ -212,7 +205,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("")?;

Expand All @@ -225,14 +218,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#"
Expand All @@ -258,8 +251,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,
Expand All @@ -270,15 +265,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)?;

Expand Down

0 comments on commit 3bfd789

Please sign in to comment.