cmake_file_api/objects/
cmake_files_v1.rs

1use crate::objects::{MajorMinor, Object, ObjectKind};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5/// The cmakeFiles object kind lists files used by `CMake` while configuring and generating the build system.
6/// These include the CMakeLists.txt files as well as included .cmake files.
7#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9#[non_exhaustive]
10pub struct CMakeFiles {
11    /// Kind of the CMakeFiles object.
12    pub kind: ObjectKind,
13
14    /// Version of the CMakeFiles object.
15    pub version: MajorMinor,
16
17    /// Paths of the CMakeFiles object.
18    pub paths: Paths,
19
20    /// Input file used by CMake when configuring and generating the build system.
21    pub inputs: Vec<Input>,
22}
23
24#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26#[non_exhaustive]
27pub struct Paths {
28    /// Absolute path to the top-level source directory, represented with forward slashes.
29    pub build: PathBuf,
30
31    /// Absolute path to the top-level build directory, represented with forward slashes.
32    pub source: PathBuf,
33}
34
35#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
36#[serde(rename_all = "camelCase")]
37#[non_exhaustive]
38pub struct Input {
39    /// path to an input file to CMake, represented with forward slashes.
40    /// If the file is inside the top-level source directory then the path is specified relative to that directory.
41    /// Otherwise, the path is absolute.
42    pub path: PathBuf,
43
44    /// True if the path specifies a file that is under the top-level build directory and the build is out-of-source.
45    #[serde(default)]
46    pub is_generated: bool,
47
48    /// True if the path specifies a file that is not under the top-level source or build directories.
49    #[serde(default)]
50    pub is_external: bool,
51
52    /// True if the path specifies a file in the CMake installation.
53    #[serde(default, rename = "isCMake")]
54    pub is_cmake: bool,
55}
56
57impl Object for CMakeFiles {
58    fn kind() -> ObjectKind {
59        ObjectKind::CMakeFiles
60    }
61
62    fn major() -> u32 {
63        1
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use crate::objects::cmake_files_v1::*;
70    use serde_json::json;
71
72    #[test]
73    fn test_configure_log() {
74        let json = json!({
75          "kind": "cmakeFiles",
76          "version": { "major": 1, "minor": 0 },
77          "paths": {
78            "build": "/path/to/top-level-build-dir",
79            "source": "/path/to/top-level-source-dir"
80          },
81          "inputs": [
82            {
83              "path": "CMakeLists.txt"
84            },
85            {
86              "isGenerated": true,
87              "path": "/path/to/top-level-build-dir/../CMakeSystem.cmake"
88            },
89            {
90              "isExternal": true,
91              "path": "/path/to/external/third-party/module.cmake"
92            },
93            {
94              "isCMake": true,
95              "isExternal": true,
96              "path": "/path/to/cmake/Modules/CMakeGenericSystem.cmake"
97            }
98          ]
99        });
100
101        let cmake_files = serde_json::from_value::<CMakeFiles>(json).unwrap();
102        assert_eq!(
103            cmake_files,
104            CMakeFiles {
105                kind: ObjectKind::CMakeFiles,
106                version: MajorMinor { major: 1, minor: 0 },
107                paths: Paths {
108                    build: "/path/to/top-level-build-dir".into(),
109                    source: "/path/to/top-level-source-dir".into()
110                },
111                inputs: vec![
112                    Input {
113                        path: "CMakeLists.txt".into(),
114                        ..Default::default()
115                    },
116                    Input {
117                        is_generated: true,
118                        path: "/path/to/top-level-build-dir/../CMakeSystem.cmake".into(),
119                        ..Default::default()
120                    },
121                    Input {
122                        is_external: true,
123                        path: "/path/to/external/third-party/module.cmake".into(),
124                        ..Default::default()
125                    },
126                    Input {
127                        is_cmake: true,
128                        is_external: true,
129                        path: "/path/to/cmake/Modules/CMakeGenericSystem.cmake".into(),
130                        ..Default::default()
131                    }
132                ]
133            }
134        );
135    }
136}