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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::objects::{MajorMinor, Object, ObjectKind};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// The cmakeFiles object kind lists files used by `CMake` while configuring and generating the build system.
/// These include the CMakeLists.txt files as well as included .cmake files.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CMakeFiles {
    /// Kind of the CMakeFiles object.
    pub kind: ObjectKind,

    /// Version of the CMakeFiles object.
    pub version: MajorMinor,

    /// Paths of the CMakeFiles object.
    pub paths: Paths,

    /// Input file used by CMake when configuring and generating the build system.
    pub inputs: Vec<Input>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Paths {
    /// Absolute path to the top-level source directory, represented with forward slashes.
    pub build: PathBuf,

    /// Absolute path to the top-level build directory, represented with forward slashes.
    pub source: PathBuf,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Input {
    /// path to an input file to CMake, represented with forward slashes.
    /// If the file is inside the top-level source directory then the path is specified relative to that directory.
    /// Otherwise, the path is absolute.
    pub path: PathBuf,

    /// True if the path specifies a file that is under the top-level build directory and the build is out-of-source.
    #[serde(default)]
    pub is_generated: bool,

    /// True if the path specifies a file that is not under the top-level source or build directories.
    #[serde(default)]
    pub is_external: bool,

    /// True if the path specifies a file in the CMake installation.
    #[serde(default, rename = "isCMake")]
    pub is_cmake: bool,
}

impl Object for CMakeFiles {
    fn kind() -> ObjectKind {
        ObjectKind::CMakeFiles
    }

    fn major() -> u32 {
        1
    }
}

#[cfg(test)]
mod tests {
    use crate::objects::cmake_files_v1::*;
    use serde_json::json;

    #[test]
    fn test_configure_log() {
        let json = json!({
          "kind": "cmakeFiles",
          "version": { "major": 1, "minor": 0 },
          "paths": {
            "build": "/path/to/top-level-build-dir",
            "source": "/path/to/top-level-source-dir"
          },
          "inputs": [
            {
              "path": "CMakeLists.txt"
            },
            {
              "isGenerated": true,
              "path": "/path/to/top-level-build-dir/../CMakeSystem.cmake"
            },
            {
              "isExternal": true,
              "path": "/path/to/external/third-party/module.cmake"
            },
            {
              "isCMake": true,
              "isExternal": true,
              "path": "/path/to/cmake/Modules/CMakeGenericSystem.cmake"
            }
          ]
        });

        let cmake_files = serde_json::from_value::<CMakeFiles>(json).unwrap();
        assert_eq!(
            cmake_files,
            CMakeFiles {
                kind: ObjectKind::CMakeFiles,
                version: MajorMinor { major: 1, minor: 0 },
                paths: Paths {
                    build: "/path/to/top-level-build-dir".into(),
                    source: "/path/to/top-level-source-dir".into()
                },
                inputs: vec![
                    Input {
                        path: "CMakeLists.txt".into(),
                        ..Default::default()
                    },
                    Input {
                        is_generated: true,
                        path: "/path/to/top-level-build-dir/../CMakeSystem.cmake".into(),
                        ..Default::default()
                    },
                    Input {
                        is_external: true,
                        path: "/path/to/external/third-party/module.cmake".into(),
                        ..Default::default()
                    },
                    Input {
                        is_cmake: true,
                        is_external: true,
                        path: "/path/to/cmake/Modules/CMakeGenericSystem.cmake".into(),
                        ..Default::default()
                    }
                ]
            }
        );
    }
}