cmake_parser/doc/command/project/
target_compile_definitions.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Add compile definitions to a target.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/target_compile_definitions.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate")]
13pub struct TargetCompileDefinitions<'t> {
14    #[cmake(positional)]
15    pub target: Token<'t>,
16    pub definitions: Vec<Definition<'t>>,
17}
18
19impl<'t> ToCommandScope for TargetCompileDefinitions<'t> {
20    fn to_command_scope(&self) -> CommandScope {
21        CommandScope::Project
22    }
23}
24
25#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[cmake(pkg = "crate", transparent)]
27pub enum Definition<'t> {
28    Interface(Vec<Token<'t>>),
29    Public(Vec<Token<'t>>),
30    Private(Vec<Token<'t>>),
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    use crate::doc::cmake_parse::tests::{token, tokens_vec};
37    use crate::*;
38    use pretty_assertions::assert_eq;
39
40    #[test]
41    fn target_compile_definitions() {
42        let src =
43            include_bytes!("../../../../../fixture/commands/project/target_compile_definitions");
44        let cmakelists = parse_cmakelists(src).unwrap();
45        let doc = Doc::from(cmakelists);
46        assert_eq!(
47            doc.commands(),
48            Ok(vec![
49                Command::TargetCompileDefinitions(Box::new(TargetCompileDefinitions {
50                    target: token(b"LibXml2"),
51                    definitions: vec![Definition::Private(tokens_vec([
52                        b"SYSCONFDIR=\"${CMAKE_INSTALL_FULL_SYSCONFDIR}\""
53                    ]))]
54                })),
55                Command::TargetCompileDefinitions(Box::new(TargetCompileDefinitions {
56                    target: token(b"LibXml2"),
57                    definitions: vec![
58                        Definition::Interface(tokens_vec([b"LIBXML_STATIC"])),
59                        Definition::Private(tokens_vec([b"qqq", b"bbb"]))
60                    ]
61                })),
62            ])
63        )
64    }
65}