cmake_parser/doc/command/project/
export.rs

1use ::cmake_parser_derive::CMake;
2
3use crate::{CommandScope, ToCommandScope, Token};
4
5/// Export targets or packages for outside projects to use them directly from the current project's build tree, without installation.
6///
7/// Reference: <https://cmake.org/cmake/help/v3.26/command/export.html>
8#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cmake(pkg = "crate")]
10pub enum Export<'t> {
11    Targets(TargetsExport<'t>),
12    Export(ExportExport<'t>),
13    Package(PackageExport<'t>),
14}
15
16impl<'t> ToCommandScope for Export<'t> {
17    fn to_command_scope(&self) -> CommandScope {
18        CommandScope::Project
19    }
20}
21
22#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23#[cmake(pkg = "crate", untagged)]
24pub enum TargetsExport<'t> {
25    File(FileTargetsExport<'t>),
26    AndroidMk(AndroidMkTargetsExport<'t>),
27}
28
29#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
30#[cmake(pkg = "crate")]
31pub struct FileTargetsExport<'t> {
32    pub targets: Vec<Token<'t>>,
33    pub namespace: Option<Token<'t>>,
34    pub append: bool,
35    pub file: Token<'t>,
36    pub export_link_interface_libraries: bool,
37    pub cxx_modules_directory: Option<Token<'t>>,
38}
39
40#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
41#[cmake(pkg = "crate")]
42pub struct AndroidMkTargetsExport<'t> {
43    pub targets: Vec<Token<'t>>,
44    pub android_mk: Token<'t>,
45}
46
47#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
48#[cmake(pkg = "crate")]
49pub struct ExportExport<'t> {
50    #[cmake(rename = "EXPORT")]
51    pub exports: Vec<Token<'t>>,
52    pub namespace: Option<Token<'t>>,
53    pub file: Option<Token<'t>>,
54    pub cxx_modules_directory: Option<Token<'t>>,
55}
56
57#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
58#[cmake(pkg = "crate")]
59pub struct PackageExport<'t> {
60    pub package: Token<'t>,
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use crate::doc::cmake_parse::tests::{quoted_token, token, tokens_vec};
67    use crate::*;
68    use pretty_assertions::assert_eq;
69
70    #[test]
71    fn export() {
72        let src = include_bytes!("../../../../../fixture/commands/project/export");
73        let cmakelists = parse_cmakelists(src).unwrap();
74        let doc = Doc::from(cmakelists);
75
76        assert_eq!(
77            doc.commands(),
78            Ok(vec![
79                Command::Export(Box::new(Export::Targets(TargetsExport::File(
80                    FileTargetsExport {
81                        targets: tokens_vec([b"MathFunctionsTargets"]),
82                        namespace: None,
83                        append: false,
84                        file: quoted_token(
85                            b"${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake",
86                        ),
87                        export_link_interface_libraries: false,
88                        cxx_modules_directory: None,
89                    }
90                )))),
91                Command::Export(Box::new(Export::Export(ExportExport {
92                    exports: tokens_vec([b"MathFunctionsTargets"]),
93                    namespace: None,
94                    file: Some(quoted_token(
95                        b"${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake",
96                    )),
97                    cxx_modules_directory: None,
98                }))),
99                Command::Export(Box::new(Export::Targets(TargetsExport::AndroidMk(
100                    AndroidMkTargetsExport {
101                        targets: tokens_vec([b"android1", b"android2"]),
102                        android_mk: token(b"../NDK1"),
103                    }
104                )))),
105                Command::Export(Box::new(Export::Package(PackageExport {
106                    package: token(b"hello"),
107                }))),
108            ])
109        )
110    }
111}