cmake_parser/doc/command/scripting/
find_library.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    command::common::{FindPath, FindRoot, Names, WindowsRegistryView},
5    doc::command_scope::{CommandScope, ToCommandScope},
6    Token,
7};
8
9/// This command is used to find a library.
10///
11/// Reference: <https://cmake.org/cmake/help/v3.26/command/find_library.html>
12#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[cmake(pkg = "crate", untagged)]
14pub enum FindLibrary<'t> {
15    General(FindLibraryGeneral<'t>),
16    Short(FindLibraryShort<'t>),
17}
18
19impl<'t> ToCommandScope for FindLibrary<'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 FindLibraryGeneral<'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<FindPath<'t>>>,
34    pub paths: Option<Vec<FindPath<'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 FindLibraryShort<'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_library() {
68        let src = include_bytes!("../../../../../fixture/commands/scripting/find_library");
69        let cmakelists = parse_cmakelists(src).unwrap();
70        let doc = Doc::from(cmakelists);
71        assert_eq!(
72            doc.commands(),
73            Ok(vec![
74                Command::FindLibrary(Box::new(FindLibrary::General(FindLibraryGeneral {
75                    variable: token(b"variable1"),
76                    names: Names::Single(token(b"name1")),
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::FindLibrary(Box::new(FindLibrary::Short(FindLibraryShort {
96                    variable: token(b"variable1"),
97                    name: token(b"name1"),
98                    paths: tokens_vec([b"path1"]),
99                }))),
100                Command::FindLibrary(Box::new(FindLibrary::General(FindLibraryGeneral {
101                    variable: token(b"variable1"),
102                    names: Names::Multi(tokens_vec([b"name1", b"name2"])),
103                    names_per_dir: false,
104                    hints: None,
105                    paths: None,
106                    registry_view: None,
107                    path_suffixes: None,
108                    validator: None,
109                    doc: None,
110                    no_cache: false,
111                    required: false,
112                    no_default_path: false,
113                    no_package_root_path: false,
114                    no_cmake_path: false,
115                    no_cmake_environment_path: false,
116                    no_system_environment_path: false,
117                    no_cmake_system_path: false,
118                    no_cmake_install_prefix: false,
119                    find_root: None,
120                }))),
121                Command::FindLibrary(Box::new(FindLibrary::General(FindLibraryGeneral {
122                    variable: token(b"variable1"),
123                    names: Names::Multi(tokens_vec([b"name1", b"name2"])),
124                    names_per_dir: true,
125                    hints: Some(vec![
126                        FindPath::Path(token(b"path1")),
127                        FindPath::Path(token(b"path2")),
128                        FindPath::Env(token(b"env1")),
129                    ]),
130                    paths: Some(vec![
131                        FindPath::Env(token(b"env1")),
132                        FindPath::Env(token(b"env2")),
133                        FindPath::Path(token(b"path1")),
134                    ]),
135                    registry_view: Some(WindowsRegistryView::Target),
136                    path_suffixes: Some(tokens_vec([b"suffix1", b"suffix2"])),
137                    validator: Some(token(b"validator1")),
138                    doc: Some(token(b"doc1")),
139                    no_cache: true,
140                    required: true,
141                    no_default_path: true,
142                    no_package_root_path: true,
143                    no_cmake_path: true,
144                    no_cmake_environment_path: true,
145                    no_system_environment_path: true,
146                    no_cmake_system_path: true,
147                    no_cmake_install_prefix: true,
148                    find_root: Some(FindRoot::CMakeFindRootPathBoth),
149                }))),
150            ])
151        )
152    }
153}