cmake_file_api/objects/
toolchains_v1.rs

1use crate::objects::{MajorMinor, Object, ObjectKind};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5/// The toolchains object kind lists properties of the toolchains used during the build
6#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
7#[serde(rename_all = "camelCase")]
8#[non_exhaustive]
9pub struct Toolchains {
10    /// Kind of the toolchains object.
11    pub kind: ObjectKind,
12
13    /// Version of the toolchains object.
14    pub version: MajorMinor,
15
16    /// Toolchains.
17    pub toolchains: Vec<Toolchain>,
18}
19
20#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22#[non_exhaustive]
23pub struct Toolchain {
24    /// Toolchain language, like C or CXX.
25    pub language: String,
26
27    /// Compiler information.
28    pub compiler: Compiler,
29
30    /// Optional member that is present when the `CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS` variable is defined for the current language.
31    /// Each string holds a file extension (without the leading dot) for the language
32    #[serde(default)]
33    pub source_file_extensions: Vec<String>,
34}
35
36#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
37#[serde(rename_all = "camelCase")]
38#[non_exhaustive]
39pub struct Compiler {
40    /// Optional member that is present when the `CMAKE_<LANG>_COMPILER` variable is defined for the current language.
41    /// Holding the absolute path to the compiler.
42    pub path: Option<PathBuf>,
43
44    /// Optional member that is present when the `CMAKE_<LANG>_COMPILER_ID` variable is defined for the current language.
45    /// Holding the ID (GNU, MSVC, etc.) of the compiler.
46    pub id: Option<String>,
47
48    /// Optional member that is present when the `CMAKE_<LANG>_COMPILER_VERSION` variable is defined for the current language.
49    /// Holding the version of the compiler.
50    pub version: Option<String>,
51
52    /// Optional member that is present when the `CMAKE_<LANG>_COMPILER_TARGET` variable is defined for the current language.
53    /// Holding the cross-compiling target of the compiler.
54    pub target: Option<String>,
55
56    /// Implicit compiler info for `CMAKE_<LANG>_IMPLICIT_*` variables.
57    pub implicit: Implicit,
58}
59
60#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
61#[serde(rename_all = "camelCase")]
62#[non_exhaustive]
63pub struct Implicit {
64    /// Optional member that is present when the `CMAKE_<LANG>_IMPLICIT_INCLUDE_DIRECTORIES` variable is defined for the current language.
65    /// Each path points to an implicit include directory for the compiler.
66    #[serde(default)]
67    pub include_directories: Vec<PathBuf>,
68
69    /// Optional member that is present when the `CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES` variable is defined for the current language.
70    /// Each path points to an implicit link directory for the compiler.
71    #[serde(default)]
72    pub link_directories: Vec<PathBuf>,
73
74    /// Optional member that is present when the `CMAKE_<LANG>_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES` variable is defined for the current language.
75    /// Each path points to an implicit link framework directory for the compiler.
76    #[serde(default)]
77    pub link_framework_directories: Vec<PathBuf>,
78
79    /// Optional member that is present when the `CMAKE_<LANG>_IMPLICIT_LINK_LIBRARIES` variable is defined for the current language.
80    /// Each path points to an implicit link library for the compiler.
81    #[serde(default)]
82    pub link_libraries: Vec<PathBuf>,
83}
84
85impl Object for Toolchains {
86    fn kind() -> ObjectKind {
87        ObjectKind::Toolchains
88    }
89
90    fn major() -> u32 {
91        1
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use crate::objects::toolchains_v1::*;
98    use serde_json::json;
99
100    #[test]
101    fn test_toolchains() {
102        let json = json!({
103          "kind": "toolchains",
104          "version": { "major": 1, "minor": 0 },
105          "toolchains": [
106            {
107              "language": "C",
108              "compiler": {
109                "path": "/usr/bin/cc",
110                "id": "GNU",
111                "version": "9.3.0",
112                "implicit": {
113                  "includeDirectories": [
114                    "/usr/lib/gcc/x86_64-linux-gnu/9/include",
115                    "/usr/local/include",
116                    "/usr/include/x86_64-linux-gnu",
117                    "/usr/include"
118                  ],
119                  "linkDirectories": [
120                    "/usr/lib/gcc/x86_64-linux-gnu/9",
121                    "/usr/lib/x86_64-linux-gnu",
122                    "/usr/lib",
123                    "/lib/x86_64-linux-gnu",
124                    "/lib"
125                  ],
126                  "linkFrameworkDirectories": [],
127                  "linkLibraries": [ "gcc", "gcc_s", "c", "gcc", "gcc_s" ]
128                }
129              },
130              "sourceFileExtensions": [ "c", "m" ]
131            },
132            {
133              "language": "CXX",
134              "compiler": {
135                "path": "/usr/bin/c++",
136                "id": "GNU",
137                "version": "9.3.0",
138                "implicit": {
139                  "includeDirectories": [
140                    "/usr/include/c++/9",
141                    "/usr/include/x86_64-linux-gnu/c++/9",
142                    "/usr/include/c++/9/backward",
143                    "/usr/lib/gcc/x86_64-linux-gnu/9/include",
144                    "/usr/local/include",
145                    "/usr/include/x86_64-linux-gnu",
146                    "/usr/include"
147                  ],
148                  "linkDirectories": [
149                    "/usr/lib/gcc/x86_64-linux-gnu/9",
150                    "/usr/lib/x86_64-linux-gnu",
151                    "/usr/lib",
152                    "/lib/x86_64-linux-gnu",
153                    "/lib"
154                  ],
155                  "linkFrameworkDirectories": [],
156                  "linkLibraries": [
157                    "stdc++", "m", "gcc_s", "gcc", "c", "gcc_s", "gcc"
158                  ]
159                }
160              },
161              "sourceFileExtensions": [
162                "C", "M", "c++", "cc", "cpp", "cxx", "mm", "CPP"
163              ]
164            }
165          ]
166        });
167
168        let toolchains = serde_json::from_value::<Toolchains>(json).unwrap();
169        assert_eq!(toolchains.kind, ObjectKind::Toolchains);
170        assert_eq!(toolchains.version, MajorMinor { major: 1, minor: 0 });
171        assert_eq!(toolchains.toolchains.len(), 2);
172        assert_eq!(toolchains.toolchains[0].language, "C");
173        assert_eq!(toolchains.toolchains[1].language, "CXX");
174
175        assert_eq!(
176            toolchains.toolchains[0].compiler.id.as_ref().unwrap(),
177            "GNU"
178        );
179        assert_eq!(
180            toolchains.toolchains[1].compiler.id.as_ref().unwrap(),
181            "GNU"
182        );
183    }
184}