cmake_parser/doc/command/project/
link_libraries.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Link libraries to all targets added later.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/link_libraries.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate", positional)]
13pub struct LinkLibraries<'t> {
14    pub libs: Vec<LinkLibrary<'t>>,
15}
16
17impl<'t> ToCommandScope for LinkLibraries<'t> {
18    fn to_command_scope(&self) -> CommandScope {
19        CommandScope::Project
20    }
21}
22
23#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
24#[cmake(pkg = "crate", positional)]
25pub struct LinkLibrary<'t> {
26    build_configuraion: Option<BuildConfiguration>,
27    lib: Token<'t>,
28}
29
30#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
31#[cmake(pkg = "crate")]
32pub enum BuildConfiguration {
33    #[cmake(rename = "debug")]
34    Debug,
35    #[cmake(rename = "optimized")]
36    Optimized,
37    #[cmake(rename = "general")]
38    General,
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use crate::doc::cmake_parse::tests::token;
45    use crate::*;
46    use pretty_assertions::assert_eq;
47
48    #[test]
49    fn link_libraries() {
50        let src = include_bytes!("../../../../../fixture/commands/project/link_libraries");
51        let cmakelists = parse_cmakelists(src).unwrap();
52        let doc = Doc::from(cmakelists);
53        assert_eq!(
54            doc.commands(),
55            Ok(vec![Command::LinkLibraries(Box::new(LinkLibraries {
56                libs: vec![
57                    LinkLibrary {
58                        build_configuraion: None,
59                        lib: token(b"lib1"),
60                    },
61                    LinkLibrary {
62                        build_configuraion: Some(BuildConfiguration::Debug),
63                        lib: token(b"lib2"),
64                    },
65                    LinkLibrary {
66                        build_configuraion: None,
67                        lib: token(b"lib3"),
68                    },
69                ],
70            })),])
71        )
72    }
73}