cmake_parser/doc/command/scripting/
find_program.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    command::common::{FindPath as CommonFindPath, FindRoot, Names, WindowsRegistryView},
5    doc::command_scope::{CommandScope, ToCommandScope},
6    Token,
7};
8
9/// This command is used to find a program.
10///
11/// Reference: <https://cmake.org/cmake/help/v3.26/command/find_program.html>
12#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[cmake(pkg = "crate", untagged)]
14pub enum FindProgram<'t> {
15    General(FindProgramGeneral<'t>),
16    Short(FindProgramShort<'t>),
17}
18
19impl<'t> ToCommandScope for FindProgram<'t> {
20    fn to_command_scope(&self) -> CommandScope {
21        CommandScope::Scripting
22    }
23}
24
25#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[cmake(pkg = "crate", default = "names")]
27pub struct FindProgramGeneral<'t> {
28    #[cmake(positional)]
29    pub variable: Token<'t>,
30    #[cmake(rename = "")]
31    pub names: Names<'t>,
32    pub names_per_dir: bool,
33    pub hints: Option<Vec<CommonFindPath<'t>>>,
34    pub paths: Option<Vec<CommonFindPath<'t>>>,
35    pub registry_view: Option<WindowsRegistryView>,
36    pub path_suffixes: Option<Vec<Token<'t>>>,
37    pub validator: Option<Token<'t>>,
38    pub doc: Option<Token<'t>>,
39    pub no_cache: bool,
40    pub required: bool,
41    pub no_default_path: bool,
42    pub no_package_root_path: bool,
43    pub no_cmake_path: bool,
44    pub no_cmake_environment_path: bool,
45    pub no_system_environment_path: bool,
46    pub no_cmake_system_path: bool,
47    pub no_cmake_install_prefix: bool,
48    pub find_root: Option<FindRoot>,
49}
50
51#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
52#[cmake(pkg = "crate", positional)]
53pub struct FindProgramShort<'t> {
54    pub variable: Token<'t>,
55    pub name: Token<'t>,
56    pub paths: Vec<Token<'t>>,
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62    use crate::doc::cmake_parse::tests::{token, tokens_vec};
63    use crate::*;
64    use pretty_assertions::assert_eq;
65
66    #[test]
67    fn find_program() {
68        let src = include_bytes!("../../../../../fixture/commands/scripting/find_program");
69        let cmakelists = parse_cmakelists(src).unwrap();
70        let doc = Doc::from(cmakelists);
71        assert_eq!(
72            doc.commands(),
73            Ok(vec![
74                Command::FindProgram(Box::new(FindProgram::General(FindProgramGeneral {
75                    variable: token(b"variable1"),
76                    names: Names::Multi(tokens_vec([b"name1", b"name2"])),
77                    names_per_dir: false,
78                    hints: None,
79                    paths: None,
80                    registry_view: None,
81                    path_suffixes: None,
82                    validator: None,
83                    doc: None,
84                    no_cache: false,
85                    required: false,
86                    no_default_path: false,
87                    no_package_root_path: false,
88                    no_cmake_path: false,
89                    no_cmake_environment_path: false,
90                    no_system_environment_path: false,
91                    no_cmake_system_path: false,
92                    no_cmake_install_prefix: false,
93                    find_root: None,
94                }))),
95                Command::FindProgram(Box::new(FindProgram::Short(FindProgramShort {
96                    variable: token(b"variable1"),
97                    name: token(b"name1"),
98                    paths: tokens_vec([b"path1"]),
99                }))),
100                Command::FindProgram(Box::new(FindProgram::General(FindProgramGeneral {
101                    variable: token(b"variable1"),
102                    names: Names::Multi(tokens_vec([b"name1", b"name2"])),
103                    names_per_dir: true,
104                    hints: Some(vec![
105                        CommonFindPath::Path(token(b"path1")),
106                        CommonFindPath::Path(token(b"path2")),
107                        CommonFindPath::Env(token(b"env1")),
108                    ]),
109                    paths: Some(vec![
110                        CommonFindPath::Env(token(b"env1")),
111                        CommonFindPath::Env(token(b"env2")),
112                        CommonFindPath::Path(token(b"path1")),
113                    ]),
114                    registry_view: Some(WindowsRegistryView::Target),
115                    path_suffixes: Some(tokens_vec([b"suffix1", b"suffix2"])),
116                    validator: Some(token(b"validator1")),
117                    doc: Some(token(b"doc1")),
118                    no_cache: true,
119                    required: true,
120                    no_default_path: true,
121                    no_package_root_path: true,
122                    no_cmake_path: true,
123                    no_cmake_environment_path: true,
124                    no_system_environment_path: true,
125                    no_cmake_system_path: true,
126                    no_cmake_install_prefix: true,
127                    find_root: Some(FindRoot::CMakeFindRootPathBoth),
128                }))),
129            ])
130        )
131    }
132}