cmake_parser/doc/command/project/add_custom_command.rs
1use cmake_parser_derive::CMake;
2
3use crate::{
4 doc::command_scope::{CommandScope, ToCommandScope},
5 Token,
6};
7
8/// Add a custom build rule to the generated build system.
9///
10/// There are two main signatures for add_custom_command.
11///
12/// Reference: <https://cmake.org/cmake/help/v3.26/command/add_custom_command.html>
13#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[cmake(pkg = "crate")]
15pub enum AddCustomCommand<'t> {
16 Output(AddCustomCommandOutput<'t>),
17 Target(AddCustomCommandTarget<'t>),
18}
19
20impl<'t> ToCommandScope for AddCustomCommand<'t> {
21 fn to_command_scope(&self) -> CommandScope {
22 CommandScope::Project
23 }
24}
25
26#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
27#[cmake(pkg = "crate")]
28pub struct AddCustomCommandOutput<'t> {
29 /// Specify the output files the command is expected to produce. Each output file will be marked with the GENERATED source file property automatically. If the output of the custom command is not actually created as a file on disk it should be marked with the SYMBOLIC source file property.
30 ///
31 /// If an output file name is a relative path, its absolute path is determined by interpreting it relative to:
32 ///
33 /// 1. the build directory corresponding to the current source directory (CMAKE_CURRENT_BINARY_DIR), or
34 /// 1. the current source directory (CMAKE_CURRENT_SOURCE_DIR).
35 ///
36 /// The path in the build directory is preferred unless the path in the source tree is mentioned as an absolute source file path elsewhere in the current directory.
37 pub output: Vec<Token<'t>>,
38 /// 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.) The optional ARGS argument is for backward compatibility and will be ignored.
39 ///
40 /// 1. If COMMAND specifies an executable target name (created by the add_executable() command), it will automatically be replaced by the location of the executable created at build time if either of the following is true:
41 /// - The target is not being cross-compiled (i.e. the CMAKE_CROSSCOMPILING variable is not set to true).
42 /// - New in version 3.6: The target is being cross-compiled and an emulator is provided (i.e. its CROSSCOMPILING_EMULATOR target property is set). In this case, the contents of CROSSCOMPILING_EMULATOR will be prepended to the command before the location of the target executable.
43 /// 1. If neither of the above conditions are met, it is assumed that the command name is a program to be found on the PATH at build time.
44 ///
45 /// Arguments to COMMAND may use generator expressions. Use the TARGET_FILE generator expression to refer to the location of a target later in the command line (i.e. as a command argument rather than as the command to execute).
46 ///
47 /// Whenever one of the following target based generator expressions are used as a command to execute or is mentioned in a command argument, a target-level dependency will be added automatically so that the mentioned target will be built before any target using this custom command (see policy CMP0112).
48 ///
49 /// - TARGET_FILE
50 /// - TARGET_LINKER_FILE
51 /// - TARGET_SONAME_FILE
52 /// - TARGET_PDB_FILE
53 ///
54 /// This target-level dependency does NOT add a file-level dependency that would cause the custom command to re-run whenever the executable is recompiled. List target names with the DEPENDS option to add such file-level dependencies.
55 #[cmake(rename = "COMMAND")]
56 pub commands: Vec<Vec<Token<'t>>>,
57 /// Specify the primary input source file to the command. This is treated just like any value given to the DEPENDS option but also suggests to Visual Studio generators where to hang the custom command. Each source file may have at most one command specifying it as its main dependency. A compile command (i.e. for a library or an executable) counts as an implicit main dependency which gets silently overwritten by a custom command specification.
58 pub main_dependency: Option<Token<'t>>,
59 /// Specify files on which the command depends. Each argument is converted to a dependency as follows:
60 ///
61 /// 1. If the argument is the name of a target (created by the add_custom_target(), add_executable(), or add_library() command) a target-level dependency is created to make sure the target is built before any target using this custom command. Additionally, if the target is an executable or library, a file-level dependency is created to cause the custom command to re-run whenever the target is recompiled.
62 /// 1. If the argument is an absolute path, a file-level dependency is created on that path.
63 /// 1. If the argument is the name of a source file that has been added to a target or on which a source file property has been set, a file-level dependency is created on that source file.
64 /// 1. If the argument is a relative path and it exists in the current source directory, a file-level dependency is created on that file in the current source directory.
65 /// 1. Otherwise, a file-level dependency is created on that path relative to the current binary directory.
66 ///
67 /// If any dependency is an OUTPUT of another custom command in the same directory (CMakeLists.txt file), CMake automatically brings the other custom command into the target in which this command is built.
68 ///
69 /// New in version 3.16: A target-level dependency is added if any dependency is listed as BYPRODUCTS of a target or any of its build events in the same directory to ensure the byproducts will be available.
70 ///
71 /// If DEPENDS is not specified, the command will run whenever the OUTPUT is missing; if the command does not actually create the OUTPUT, the rule will always run.
72 ///
73 /// New in version 3.1: Arguments to DEPENDS may use generator expressions.
74 pub depends: Option<Vec<Token<'t>>>,
75 /// Specify the files the command is expected to produce but whose modification time may or may not be newer than the dependencies. 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.
76 ///
77 /// See policy CMP0058 for the motivation behind this feature.
78 ///
79 /// Explicit specification of byproducts is supported by the Ninja generator to tell the ninja build tool how to regenerate byproducts when they are missing. It is also useful when other build rules (e.g. custom commands) depend on the byproducts. Ninja requires a build rule for any generated file on which another rule depends even if there are order-only dependencies to ensure the byproducts will be available before their dependents build.
80 ///
81 /// The Makefile Generators will remove BYPRODUCTS and other GENERATED files during make clean.
82 ///
83 /// New in version 3.20: Arguments to BYPRODUCTS may use a restricted set of generator expressions. Target-dependent expressions are not permitted.
84 pub byproducts: Option<Vec<Token<'t>>>,
85 /// Request scanning of implicit dependencies of an input file. The language given specifies the programming language whose corresponding dependency scanner should be used. Currently only C and CXX language scanners are supported. The language has to be specified for every file in the IMPLICIT_DEPENDS list. Dependencies discovered from the scanning are added to those of the custom command at build time. Note that the IMPLICIT_DEPENDS option is currently supported only for Makefile generators and will be ignored by other generators.
86 ///
87 /// Note: This option cannot be specified at the same time as DEPFILE option.
88 pub implicit_depends: Option<Vec<(Token<'t>, Token<'t>)>>,
89 /// 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.
90 pub working_directory: Option<Token<'t>>,
91 /// Display the given message before the commands are executed at build time.
92 ///
93 /// New in version 3.26: Arguments to COMMENT may use generator expressions.
94 pub comment: Option<Token<'t>>,
95 /// Specify a depfile which holds dependencies for the custom command. It is usually emitted by the custom command itself. This keyword may only be used if the generator supports it, as detailed below.
96 ///
97 /// The expected format, compatible with what is generated by gcc with the option -M, is independent of the generator or platform.
98 ///
99 /// Note: DEPFILE cannot be specified at the same time as the IMPLICIT_DEPENDS option for Makefile Generators.
100 pub depfile: Option<Token<'t>>,
101 /// 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.
102 pub job_pool: Option<Token<'t>>,
103 /// 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_command 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.
104 pub verbatim: bool,
105 /// Append the COMMAND and DEPENDS option values to the custom command for the first output specified. There must have already been a previous call to this command with the same output.
106 ///
107 /// If the previous call specified the output via a generator expression, the output specified by the current call must match in at least one configuration after evaluating generator expressions. In this case, the appended commands and dependencies apply to all configurations.
108 ///
109 /// The COMMENT, MAIN_DEPENDENCY, and WORKING_DIRECTORY options are currently ignored when APPEND is given, but may be used in the future.
110 pub append: bool,
111 /// The command will be given direct access to the terminal if possible. With the Ninja generator, this places the command in the console pool.
112 pub uses_terminal: bool,
113 /// 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.
114 pub command_expands_list: bool,
115}
116
117/// This defines a new command that will be associated with building the specified <target>. The <target> must be defined in the current directory; targets defined in other directories may not be specified.
118#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
119#[cmake(pkg = "crate")]
120pub struct AddCustomCommandTarget<'t> {
121 pub target: Token<'t>,
122 /// When the command will happen
123 ///
124 /// Projects should always specify one of the above three keywords when using the TARGET form. For backward compatibility reasons, POST_BUILD is assumed if no such keyword is given, but projects should explicitly provide one of the keywords to make clear the behavior they expect.
125 pub when: AddCustomCommandTargetWhen,
126 /// 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.) The optional ARGS argument is for backward compatibility and will be ignored.
127 ///
128 /// 1. If COMMAND specifies an executable target name (created by the add_executable() command), it will automatically be replaced by the location of the executable created at build time if either of the following is true:
129 /// - The target is not being cross-compiled (i.e. the CMAKE_CROSSCOMPILING variable is not set to true).
130 /// - New in version 3.6: The target is being cross-compiled and an emulator is provided (i.e. its CROSSCOMPILING_EMULATOR target property is set). In this case, the contents of CROSSCOMPILING_EMULATOR will be prepended to the command before the location of the target executable.
131 /// 1. If neither of the above conditions are met, it is assumed that the command name is a program to be found on the PATH at build time.
132 ///
133 /// Arguments to COMMAND may use generator expressions. Use the TARGET_FILE generator expression to refer to the location of a target later in the command line (i.e. as a command argument rather than as the command to execute).
134 ///
135 /// Whenever one of the following target based generator expressions are used as a command to execute or is mentioned in a command argument, a target-level dependency will be added automatically so that the mentioned target will be built before any target using this custom command (see policy CMP0112).
136 ///
137 /// - TARGET_FILE
138 /// - TARGET_LINKER_FILE
139 /// - TARGET_SONAME_FILE
140 /// - TARGET_PDB_FILE
141 ///
142 /// This target-level dependency does NOT add a file-level dependency that would cause the custom command to re-run whenever the executable is recompiled. List target names with the DEPENDS option to add such file-level dependencies.
143 #[cmake(rename = "COMMAND")]
144 pub commands: Vec<Vec<Token<'t>>>,
145 /// Specify the primary input source file to the command. This is treated just like any value given to the DEPENDS option but also suggests to Visual Studio generators where to hang the custom command. Each source file may have at most one command specifying it as its main dependency. A compile command (i.e. for a library or an executable) counts as an implicit main dependency which gets silently overwritten by a custom command specification.
146 pub main_dependency: Option<Token<'t>>,
147 /// Specify files on which the command depends. Each argument is converted to a dependency as follows:
148 ///
149 /// 1. If the argument is the name of a target (created by the add_custom_target(), add_executable(), or add_library() command) a target-level dependency is created to make sure the target is built before any target using this custom command. Additionally, if the target is an executable or library, a file-level dependency is created to cause the custom command to re-run whenever the target is recompiled.
150 /// 1. If the argument is an absolute path, a file-level dependency is created on that path.
151 /// 1. If the argument is the name of a source file that has been added to a target or on which a source file property has been set, a file-level dependency is created on that source file.
152 /// 1. If the argument is a relative path and it exists in the current source directory, a file-level dependency is created on that file in the current source directory.
153 /// 1. Otherwise, a file-level dependency is created on that path relative to the current binary directory.
154 ///
155 /// If any dependency is an OUTPUT of another custom command in the same directory (CMakeLists.txt file), CMake automatically brings the other custom command into the target in which this command is built.
156 ///
157 /// New in version 3.16: A target-level dependency is added if any dependency is listed as BYPRODUCTS of a target or any of its build events in the same directory to ensure the byproducts will be available.
158 ///
159 /// If DEPENDS is not specified, the command will run whenever the OUTPUT is missing; if the command does not actually create the OUTPUT, the rule will always run.
160 ///
161 /// New in version 3.1: Arguments to DEPENDS may use generator expressions.
162 pub depends: Option<Vec<Token<'t>>>,
163 /// Specify the files the command is expected to produce but whose modification time may or may not be newer than the dependencies. 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.
164 ///
165 /// See policy CMP0058 for the motivation behind this feature.
166 ///
167 /// Explicit specification of byproducts is supported by the Ninja generator to tell the ninja build tool how to regenerate byproducts when they are missing. It is also useful when other build rules (e.g. custom commands) depend on the byproducts. Ninja requires a build rule for any generated file on which another rule depends even if there are order-only dependencies to ensure the byproducts will be available before their dependents build.
168 ///
169 /// The Makefile Generators will remove BYPRODUCTS and other GENERATED files during make clean.
170 ///
171 /// New in version 3.20: Arguments to BYPRODUCTS may use a restricted set of generator expressions. Target-dependent expressions are not permitted.
172 pub byproducts: Option<Vec<Token<'t>>>,
173 /// 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.
174 pub working_directory: Option<Token<'t>>,
175 /// Display the given message before the commands are executed at build time.
176 ///
177 /// New in version 3.26: Arguments to COMMENT may use generator expressions.
178 pub comment: Option<Token<'t>>,
179 /// 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_command 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.
180 pub verbatim: bool,
181 /// The command will be given direct access to the terminal if possible. With the Ninja generator, this places the command in the console pool.
182 pub uses_terminal: bool,
183 /// 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.
184 pub command_expands_list: bool,
185}
186
187#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
188#[cmake(pkg = "crate")]
189pub enum AddCustomCommandTargetWhen {
190 /// On [Visual Studio Generators](https://cmake.org/cmake/help/v3.26/manual/cmake-generators.7.html#visual-studio-generators), run before any other rules are executed within the target. On other generators, run just before PRE_LINK commands.
191 PreBuild,
192 /// Run after sources have been compiled but before linking the binary or running the librarian or archiver tool of a static library. This is not defined for targets created by the [add_custom_target()](https://cmake.org/cmake/help/v3.26/command/add_custom_target.html#command:add_custom_target) command.
193 PreLink,
194 /// Run after all other rules within the target have been executed.
195 PostBuild,
196}
197
198#[cfg(test)]
199mod tests {
200 use crate::*;
201
202 #[test]
203 fn add_custom_command() {
204 let src = include_bytes!("../../../../../fixture/commands/project/add_custom_command");
205 let cmakelists = parse_cmakelists(src).unwrap();
206 let doc = Doc::from(cmakelists);
207 let commands = doc.commands().unwrap();
208 dbg!(commands);
209 }
210}