cmake_parser/doc/command/project/
add_custom_target.rs

1use ::cmake_parser_derive::CMake;
2
3use crate::{command::common::CustomCommand, CommandScope, ToCommandScope, Token};
4
5/// Add a target with no output so it will always be built.
6///
7/// Adds a target with the given name that executes the given commands.
8/// The target has no output file and is always considered out of date even
9/// if the commands try to create a file with the name of the target. Use
10/// the `add_custom_command()` command to generate a file with dependencies.
11/// By default nothing depends on the custom target. Use the `add_dependencies()`
12/// command to add dependencies to or from other targets.
13///
14/// Reference: <https://cmake.org/cmake/help/v3.26/command/add_custom_target.html>
15#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16#[cmake(pkg = "crate", default = "commands")]
17pub struct AddCustomTarget<'t> {
18    #[cmake(positional)]
19    pub name: Token<'t>,
20    /// Indicate that this target should be added to the default build target so that it will be run every time (the command cannot be called ALL).
21    #[cmake(positional)]
22    pub all: bool,
23    /// Specify the command-line(s) to execute at build time. If more than one COMMAND is specified they will be executed in order, but not necessarily composed into a stateful shell or batch script. (To run a full script, use the configure_file() command or the file(GENERATE) command to create it, and then specify a COMMAND to launch it.)
24    #[cmake(rename = "COMMAND")]
25    pub commands: Option<Vec<CustomCommand<'t>>>,
26    /// Reference files and outputs of custom commands created with add_custom_command() command calls in the same directory (CMakeLists.txt file). They will be brought up to date when the target is built.
27    pub depends: Option<Vec<Token<'t>>>,
28    /// Specify the files the command is expected to produce but whose modification time may or may not be updated on subsequent builds. If a byproduct name is a relative path it will be interpreted relative to the build tree directory corresponding to the current source directory. Each byproduct file will be marked with the GENERATED source file property automatically.
29    pub byproducts: Option<Vec<Token<'t>>>,
30    /// Execute the command with the given current working directory. If it is a relative path it will be interpreted relative to the build tree directory corresponding to the current source directory.
31    pub working_directory: Option<Token<'t>>,
32    /// Display the given message before the commands are executed at build time.
33    pub comment: Option<Token<'t>>,
34    /// Specify a pool for the Ninja generator. Incompatible with USES_TERMINAL, which implies the console pool. Using a pool that is not defined by JOB_POOLS causes an error by ninja at build time.
35    pub job_pool: Option<Token<'t>>,
36    /// All arguments to the commands will be escaped properly for the build tool so that the invoked command receives each argument unchanged. Note that one level of escapes is still used by the CMake language processor before add_custom_target even sees the arguments. Use of VERBATIM is recommended as it enables correct behavior. When VERBATIM is not given the behavior is platform specific because there is no protection of tool-specific special characters.
37    pub verbatim: bool,
38    /// The command will be given direct access to the terminal if possible. With the Ninja generator, this places the command in the console pool.
39    pub uses_terminal: bool,
40    /// Lists in COMMAND arguments will be expanded, including those created with generator expressions, allowing COMMAND arguments such as ${CC} "-I$<JOIN:$<TARGET_PROPERTY:foo,INCLUDE_DIRECTORIES>,;-I>" foo.cc to be properly expanded.
41    pub command_expand_lists: bool,
42    /// Specify additional source files to be included in the custom target. Specified source files will be added to IDE project files for convenience in editing even if they have no build rules.
43    pub sources: Option<Vec<Token<'t>>>,
44}
45
46impl<'t> ToCommandScope for AddCustomTarget<'t> {
47    fn to_command_scope(&self) -> CommandScope {
48        CommandScope::Project
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::doc::cmake_parse::tests::{assert_parse, tokens};
56    use crate::*;
57
58    #[test]
59    fn custom_command() {
60        let input = tokens([b"command", b"arg1", b"arg2", b"arg3"]);
61        let (cmd, input): (CustomCommand, _) = CMakeParse::parse(&input).unwrap();
62        assert!(input.is_empty());
63        assert_eq!(
64            cmd,
65            CustomCommand {
66                name: Token::text_node(b"command", false),
67                args: Some(tokens([b"arg1", b"arg2", b"arg3"]).to_vec()),
68            }
69        );
70
71        let vec_custom_command: Vec<CustomCommand> = assert_parse(
72            [
73                b"QQQ",
74                b"command1",
75                b"arg1",
76                b"arg2",
77                b"arg3",
78                b"QQQ",
79                b"command2",
80                b"arg4",
81                b"arg5",
82                b"END",
83            ],
84            b"QQQ",
85        );
86        assert_eq!(
87            vec_custom_command,
88            vec![
89                CustomCommand {
90                    name: Token::text_node(b"command1", false),
91                    args: Some(tokens([b"arg1", b"arg2", b"arg3"]).to_vec()),
92                },
93                CustomCommand {
94                    name: Token::text_node(b"command2", false),
95                    args: Some(tokens([b"arg4", b"arg5"]).to_vec()),
96                }
97            ]
98        )
99    }
100
101    #[test]
102    fn add_custom_target() {
103        let src = include_bytes!("../../../../../fixture/commands/project/add_custom_target");
104        let cmakelists = parse_cmakelists(src).unwrap();
105        let doc = Doc::from(cmakelists);
106        let commands = doc.commands().unwrap();
107        dbg!(commands);
108    }
109}