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
137
138
139
140
141
142
143
144
145
146
147
148
149
use cmake_parser_derive::CMake;

use crate::{
    doc::command_scope::{CommandScope, ToCommandScope},
    Token,
};

/// Add a library to the project using the specified source files.
///
/// Reference: <https://cmake.org/cmake/help/v3.26/command/add_library.html>
#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct AddLibrary<'t> {
    pub name: Token<'t>,
    pub library: Library<'t>,
}

impl<'t> ToCommandScope for AddLibrary<'t> {
    fn to_command_scope(&self) -> CommandScope {
        CommandScope::Project
    }
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", untagged)]
pub enum Library<'t> {
    #[cmake(transparent)]
    Object(ObjectLibrary<'t>),
    #[cmake(transparent)]
    Interface(InterfaceLibrary<'t>),
    Imported(ImportedLibrary),
    #[cmake(transparent)]
    Alias(AliasLibrary<'t>),
    Normal(NormalLibrary<'t>),
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct ObjectLibrary<'t> {
    pub sources: Option<Vec<Token<'t>>>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct InterfaceLibrary<'t> {
    pub sources: Option<Vec<Token<'t>>>,
    pub exclude_from_all: bool,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", default = "sources")]
pub struct NormalLibrary<'t> {
    pub library_type: Option<NormalLibraryType>,
    pub exclude_from_all: bool,
    #[cmake(rename = "")]
    pub sources: Option<Vec<Token<'t>>>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub enum NormalLibraryType {
    Static,
    Shared,
    Module,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct ImportedLibrary {
    #[cmake(keyword_after = "IMPORTED")]
    pub library_type: ImportedLibraryType,
    pub global: bool,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub enum ImportedLibraryType {
    Static,
    Shared,
    Module,
    Unknown,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct AliasLibrary<'t> {
    pub target: Token<'t>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn add_library() {
        let src = include_bytes!("../../../../../fixture/commands/project/add_library");
        let cmakelists = parse_cmakelists(src).unwrap();
        let doc = Doc::from(cmakelists);
        assert_eq!(
            doc.commands(),
            Ok(vec![
                Command::AddLibrary(Box::new(AddLibrary {
                    name: b"MyProgram".into(),
                    library: Library::Normal(NormalLibrary {
                        library_type: Some(NormalLibraryType::Static),
                        exclude_from_all: true,
                        sources: Some(vec![b"my_program.cpp".into()])
                    })
                })),
                Command::AddLibrary(Box::new(AddLibrary {
                    name: b"ClangFormat".into(),
                    library: Library::Imported(ImportedLibrary {
                        library_type: ImportedLibraryType::Unknown,
                        global: true
                    })
                })),
                Command::AddLibrary(Box::new(AddLibrary {
                    name: b"MyAliasedProgram".into(),
                    library: Library::Alias(AliasLibrary {
                        target: b"MyProgram".into()
                    })
                })),
                Command::AddLibrary(Box::new(AddLibrary {
                    name: b"MyInterfaceLib".into(),
                    library: Library::Interface(InterfaceLibrary {
                        sources: None,
                        exclude_from_all: false,
                    }),
                })),
                Command::AddLibrary(Box::new(AddLibrary {
                    name: b"ObjLib".into(),
                    library: Library::Object(ObjectLibrary {
                        sources: Some(vec![b"src1.c".into(), b"src2.c".into()])
                    })
                })),
                Command::AddLibrary(Box::new(AddLibrary {
                    name: b"kernels".into(),
                    library: Library::Normal(NormalLibrary {
                        library_type: None,
                        exclude_from_all: false,
                        sources: Some(vec![b"test.cu".into(), b"test.cuh".into()])
                    })
                })),
            ])
        );
    }
}