cmake_parser/doc/command/project/
add_library.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Add a library to the project using the specified source files.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/add_library.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate", positional)]
13pub struct AddLibrary<'t> {
14    pub name: Token<'t>,
15    pub library: Library<'t>,
16}
17
18impl<'t> ToCommandScope for AddLibrary<'t> {
19    fn to_command_scope(&self) -> CommandScope {
20        CommandScope::Project
21    }
22}
23
24#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
25#[cmake(pkg = "crate", untagged)]
26pub enum Library<'t> {
27    #[cmake(transparent)]
28    Object(ObjectLibrary<'t>),
29    #[cmake(transparent)]
30    Interface(InterfaceLibrary<'t>),
31    Imported(ImportedLibrary),
32    #[cmake(transparent)]
33    Alias(AliasLibrary<'t>),
34    Normal(NormalLibrary<'t>),
35}
36
37#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
38#[cmake(pkg = "crate", positional)]
39pub struct ObjectLibrary<'t> {
40    pub sources: Option<Vec<Token<'t>>>,
41}
42
43#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
44#[cmake(pkg = "crate", positional)]
45pub struct InterfaceLibrary<'t> {
46    pub sources: Option<Vec<Token<'t>>>,
47    pub exclude_from_all: bool,
48}
49
50#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
51#[cmake(pkg = "crate", default = "sources", positional)]
52pub struct NormalLibrary<'t> {
53    pub library_type: Option<NormalLibraryType>,
54    pub exclude_from_all: bool,
55    #[cmake(rename = "")]
56    pub sources: Option<Vec<Token<'t>>>,
57}
58
59#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
60#[cmake(pkg = "crate")]
61pub enum NormalLibraryType {
62    Static,
63    Shared,
64    Module,
65}
66
67#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
68#[cmake(pkg = "crate", positional)]
69pub struct ImportedLibrary {
70    #[cmake(keyword_after = "IMPORTED")]
71    pub library_type: ImportedLibraryType,
72    pub global: bool,
73}
74
75#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
76#[cmake(pkg = "crate")]
77pub enum ImportedLibraryType {
78    Static,
79    Shared,
80    Module,
81    Unknown,
82}
83
84#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
85#[cmake(pkg = "crate", positional)]
86pub struct AliasLibrary<'t> {
87    pub target: Token<'t>,
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93    use crate::*;
94    use pretty_assertions::assert_eq;
95
96    #[test]
97    fn add_library() {
98        let src = include_bytes!("../../../../../fixture/commands/project/add_library");
99        let cmakelists = parse_cmakelists(src).unwrap();
100        let doc = Doc::from(cmakelists);
101        assert_eq!(
102            doc.commands(),
103            Ok(vec![
104                Command::AddLibrary(Box::new(AddLibrary {
105                    name: b"MyProgram".into(),
106                    library: Library::Normal(NormalLibrary {
107                        library_type: Some(NormalLibraryType::Static),
108                        exclude_from_all: true,
109                        sources: Some(vec![b"my_program.cpp".into()])
110                    })
111                })),
112                Command::AddLibrary(Box::new(AddLibrary {
113                    name: b"MyStaticProgram".into(),
114                    library: Library::Normal(NormalLibrary {
115                        library_type: Some(NormalLibraryType::Static),
116                        exclude_from_all: false,
117                        sources: Some(vec![b"my_static_program.cpp".into()])
118                    })
119                })),
120                Command::AddLibrary(Box::new(AddLibrary {
121                    name: b"ClangFormat".into(),
122                    library: Library::Imported(ImportedLibrary {
123                        library_type: ImportedLibraryType::Unknown,
124                        global: true
125                    })
126                })),
127                Command::AddLibrary(Box::new(AddLibrary {
128                    name: b"MyAliasedProgram".into(),
129                    library: Library::Alias(AliasLibrary {
130                        target: b"MyProgram".into()
131                    })
132                })),
133                Command::AddLibrary(Box::new(AddLibrary {
134                    name: b"MyInterfaceLib".into(),
135                    library: Library::Interface(InterfaceLibrary {
136                        sources: None,
137                        exclude_from_all: false,
138                    }),
139                })),
140                Command::AddLibrary(Box::new(AddLibrary {
141                    name: b"ObjLib".into(),
142                    library: Library::Object(ObjectLibrary {
143                        sources: Some(vec![b"src1.c".into(), b"src2.c".into()])
144                    })
145                })),
146                Command::AddLibrary(Box::new(AddLibrary {
147                    name: b"kernels".into(),
148                    library: Library::Normal(NormalLibrary {
149                        library_type: None,
150                        exclude_from_all: false,
151                        sources: Some(vec![b"test.cu".into(), b"test.cuh".into()])
152                    })
153                })),
154            ])
155        );
156    }
157}