1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// This file is part of Release Manager

// Release Manager is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Release Manager is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Release Manager  If not, see <http://www.gnu.org/licenses/>.

use std::collections::HashMap;

use super::{Arch, OS, Target};

#[derive(Serialize, Deserialize)]
pub struct Config {
    pub release_path: String,
    pub license: String,
    pub readme: String,
    pub config: HashMap<String, HashMap<String, TargetConfig>>,
}

impl Config {
    pub fn targets(&self) -> Vec<Target> {
        let mut targets = Vec::new();

        for (os, value) in self.config.iter() {
            match os.as_ref() {
                "Linux" => {
                    build_os(&mut targets, OS::Linux, value);
                }
                "Windows" => {
                    build_os(&mut targets, OS::Windows, value);
                }
                "Mac" => {
                    build_os(&mut targets, OS::Mac, value);
                }
                _ => {
                    debug!("{} not a valid Operating System", os);
                }
            }
        }

        targets
    }
}

#[derive(Serialize, Deserialize)]
pub struct TargetConfig {
    libs: Vec<String>,
    env: HashMap<String, String>,
}

fn add_target(targets: &mut Vec<Target>, os: OS, arch: Arch, tc: &TargetConfig) {
    if let Ok(mut target) = Target::new(os.clone(), arch) {
        target.add_libs(&tc.libs);
        target.add_env(&tc.env);
        targets.push(target);
    }
}

fn build_os(targets: &mut Vec<Target>, os: OS, value: &HashMap<String, TargetConfig>) {
    for (arch, tc) in value.iter() {
        match arch.as_ref() {
            "aarch64" => {
                add_target(targets, os.clone(), Arch::Aarch64, tc);
            }
            "armv7h" => {
                add_target(targets, os.clone(), Arch::Armv7h, tc);
            }
            "armv7hmusl" => {
                add_target(targets, os.clone(), Arch::Armv7hMusl, tc);
            }
            "armh" => {
                add_target(targets, os.clone(), Arch::Armh, tc);
            }
            "armhmusl" => {
                add_target(targets, os.clone(), Arch::ArmhMusl, tc);
            }
            "amd64" => {
                add_target(targets, os.clone(), Arch::Amd64, tc);
            }
            "amd64musl" => {
                add_target(targets, os.clone(), Arch::Amd64Musl, tc);
            }
            "i686" => {
                add_target(targets, os.clone(), Arch::I686, tc);
            }
            _ => {
                debug!("{} not a valid architecture", arch);
            }
        }
    }
}