crossbuild_core/
config.rs1use std::collections::BTreeMap;
4use std::env;
5use std::path::{Path, PathBuf};
6
7use anyhow::Result;
8use crate::error::CrossBuildError;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct CrossBuildConfig {
13 pub cargo_program: PathBuf,
14 pub target_dir: PathBuf,
15 pub extra_env: BTreeMap<String, String>,
16}
17
18impl Default for CrossBuildConfig {
19 fn default() -> Self {
20 Self {
21 cargo_program: PathBuf::from("cargo"),
22 target_dir: PathBuf::from("target").join("crossbuild"),
23 extra_env: BTreeMap::new(),
24 }
25 }
26}
27
28impl CrossBuildConfig {
29 pub fn from_environment() -> Result<Self, CrossBuildError> {
30 let mut config = Self::default();
31
32 if let Ok(value) = std::env::var("CARGO") {
33 if !value.trim().is_empty() {
34 config.cargo_program = PathBuf::from(value);
35 }
36 }
37
38 if let Ok(value) = env::var("CROSSBUILD_TARGET_DIR") {
39 if !value.trim().is_empty() {
40 config.target_dir = PathBuf::from(value);
41 }
42 }
43
44 if let Ok(value) = env::var("CROSSBUILD_COLOR") {
45 if !value.trim().is_empty() {
46 config
47 .extra_env
48 .insert("CARGO_TERM_COLOR".to_string(), value);
49 }
50 }
51
52 for (key, value) in env::vars() {
54 if key.starts_with("CARGO_") && !value.trim().is_empty() {
55 config.extra_env.insert(key, value);
56 }
57 }
58
59 Ok(config)
60 }
61
62 pub fn cargo_program_str(&self) -> String {
63 self.cargo_program.to_string_lossy().into_owned()
64 }
65
66 pub fn target_dir_for(&self, workspace_root: &Path) -> PathBuf {
67 if self.target_dir.is_absolute() {
68 self.target_dir.clone()
69 } else {
70 workspace_root.join(&self.target_dir)
71 }
72 }
73}