cmake_parser/doc/command/project/
add_executable.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Add an executable to the project using the specified source files.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/add_executable.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate", positional)]
13pub struct AddExecutable<'t> {
14    pub name: Token<'t>,
15    pub executable: Executable<'t>,
16}
17
18impl<'t> ToCommandScope for AddExecutable<'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 Executable<'t> {
27    #[cmake(transparent)]
28    Alias(AliasExecutable<'t>),
29    #[cmake(transparent)]
30    Imported(ImportedExecutable),
31    Normal(NormalExecutable<'t>),
32}
33
34#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
35#[cmake(pkg = "crate", positional)]
36pub struct NormalExecutable<'t> {
37    pub win32: bool,
38    pub macosx_bundle: bool,
39    pub exclude_from_all: bool,
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 ImportedExecutable {
46    pub global: bool,
47}
48
49#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
50#[cmake(pkg = "crate", positional)]
51pub struct AliasExecutable<'t> {
52    pub target: Token<'t>,
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use crate::*;
59
60    #[test]
61    fn add_executable() {
62        let src = include_bytes!("../../../../../fixture/commands/project/add_executable");
63        let cmakelists = parse_cmakelists(src).unwrap();
64        let doc = Doc::from(cmakelists);
65        assert_eq!(
66            doc.commands(),
67            Ok(vec![
68                Command::AddExecutable(Box::new(AddExecutable {
69                    name: b"MyProgram".into(),
70                    executable: Executable::Normal(NormalExecutable {
71                        win32: false,
72                        macosx_bundle: false,
73                        exclude_from_all: true,
74                        sources: Some(vec![b"my_program.cpp".into(),],),
75                    },),
76                }),),
77                Command::AddExecutable(Box::new(AddExecutable {
78                    name: b"ClangFormat".into(),
79                    executable: Executable::Imported(ImportedExecutable { global: false },),
80                }),),
81                Command::AddExecutable(Box::new(AddExecutable {
82                    name: b"MyAliasedProgram".into(),
83                    executable: Executable::Alias(AliasExecutable {
84                        target: b"MyProgram".into(),
85                    },),
86                }),),
87            ],)
88        );
89    }
90}