add_custom_command.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. add_custom_command
  2. ------------------
  3. Add a custom build rule to the generated build system.
  4. There are two main signatures for ``add_custom_command``.
  5. Generating Files
  6. ^^^^^^^^^^^^^^^^
  7. The first signature is for adding a custom command to produce an output:
  8. .. code-block:: cmake
  9. add_custom_command(OUTPUT output1 [output2 ...]
  10. COMMAND command1 [ARGS] [args1...]
  11. [COMMAND command2 [ARGS] [args2...] ...]
  12. [MAIN_DEPENDENCY depend]
  13. [DEPENDS [depends...]]
  14. [BYPRODUCTS [files...]]
  15. [IMPLICIT_DEPENDS <lang1> depend1
  16. [<lang2> depend2] ...]
  17. [WORKING_DIRECTORY dir]
  18. [COMMENT comment]
  19. [DEPFILE depfile]
  20. [JOB_POOL job_pool]
  21. [JOB_SERVER_AWARE <bool>]
  22. [VERBATIM] [APPEND] [USES_TERMINAL]
  23. [CODEGEN]
  24. [COMMAND_EXPAND_LISTS]
  25. [DEPENDS_EXPLICIT_ONLY])
  26. This defines a command to generate specified ``OUTPUT`` file(s).
  27. A target created in the same directory (``CMakeLists.txt`` file)
  28. that specifies any output of the custom command as a source file
  29. is given a rule to generate the file using the command at build time.
  30. Do not list the output in more than one independent target that
  31. may build in parallel or the instances of the rule may conflict.
  32. Instead, use the :command:`add_custom_target` command to drive the
  33. command and make the other targets depend on that one. See the
  34. `Example: Generating Files for Multiple Targets`_ below.
  35. The options are:
  36. ``APPEND``
  37. Append the ``COMMAND`` and ``DEPENDS`` option values to the custom
  38. command for the first output specified. There must have already
  39. been a previous call to this command with the same output.
  40. If the previous call specified the output via a generator expression,
  41. the output specified by the current call must match in at least one
  42. configuration after evaluating generator expressions. In this case,
  43. the appended commands and dependencies apply to all configurations.
  44. The ``COMMENT``, ``MAIN_DEPENDENCY``, and ``WORKING_DIRECTORY``
  45. options are currently ignored when APPEND is given, but may be
  46. used in the future.
  47. ``BYPRODUCTS``
  48. .. versionadded:: 3.2
  49. Specify the files the command is expected to produce but whose
  50. modification time may or may not be newer than the dependencies.
  51. If a byproduct name is a relative path it will be interpreted
  52. relative to the build tree directory corresponding to the
  53. current source directory.
  54. Each byproduct file will be marked with the :prop_sf:`GENERATED`
  55. source file property automatically.
  56. *See policy* :policy:`CMP0058` *for the motivation behind this feature.*
  57. Explicit specification of byproducts is supported by the
  58. :generator:`Ninja` generator to tell the ``ninja`` build tool
  59. how to regenerate byproducts when they are missing. It is
  60. also useful when other build rules (e.g. custom commands)
  61. depend on the byproducts. Ninja requires a build rule for any
  62. generated file on which another rule depends even if there are
  63. order-only dependencies to ensure the byproducts will be
  64. available before their dependents build.
  65. The :ref:`Makefile Generators` will remove ``BYPRODUCTS`` and other
  66. :prop_sf:`GENERATED` files during ``make clean``.
  67. .. versionadded:: 3.20
  68. Arguments to ``BYPRODUCTS`` may use a restricted set of
  69. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  70. :ref:`Target-dependent expressions <Target-Dependent Expressions>`
  71. are not permitted.
  72. .. versionchanged:: 3.28
  73. In targets using :ref:`file sets`, custom command byproducts are now
  74. considered private unless they are listed in a non-private file set.
  75. See policy :policy:`CMP0154`.
  76. ``COMMAND``
  77. Specify the command-line(s) to execute at build time.
  78. If more than one ``COMMAND`` is specified they will be executed in order,
  79. but *not* necessarily composed into a stateful shell or batch script.
  80. (To run a full script, use the :command:`configure_file` command or the
  81. :command:`file(GENERATE)` command to create it, and then specify
  82. a ``COMMAND`` to launch it.)
  83. The optional ``ARGS`` argument is for backward compatibility and
  84. will be ignored.
  85. If ``COMMAND`` specifies an executable target name (created by the
  86. :command:`add_executable` command), it will automatically be replaced
  87. by the location of the executable created at build time if either of
  88. the following is true:
  89. * The target is not being cross-compiled (i.e. the
  90. :variable:`CMAKE_CROSSCOMPILING` variable is not set to true).
  91. * .. versionadded:: 3.6
  92. The target is being cross-compiled and an emulator is provided (i.e.
  93. its :prop_tgt:`CROSSCOMPILING_EMULATOR` target property is set).
  94. In this case, the contents of :prop_tgt:`CROSSCOMPILING_EMULATOR` will be
  95. prepended to the command before the location of the target executable.
  96. If neither of the above conditions are met, it is assumed that the
  97. command name is a program to be found on the ``PATH`` at build time.
  98. Arguments to ``COMMAND`` may use
  99. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  100. Use the :genex:`TARGET_FILE` generator expression to refer to the location
  101. of a target later in the command line (i.e. as a command argument rather
  102. than as the command to execute).
  103. Whenever one of the following target based generator expressions are used as
  104. a command to execute or is mentioned in a command argument, a target-level
  105. dependency will be added automatically so that the mentioned target will be
  106. built before any target using this custom command
  107. (see policy :policy:`CMP0112`).
  108. * ``TARGET_FILE``
  109. * ``TARGET_LINKER_FILE``
  110. * ``TARGET_SONAME_FILE``
  111. * ``TARGET_PDB_FILE``
  112. This target-level dependency does NOT add a file-level dependency that would
  113. cause the custom command to re-run whenever the executable is recompiled.
  114. List target names with the ``DEPENDS`` option to add such file-level
  115. dependencies.
  116. ``COMMENT``
  117. Display the given message before the commands are executed at
  118. build time.
  119. .. versionadded:: 3.26
  120. Arguments to ``COMMENT`` may use
  121. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  122. ``DEPENDS``
  123. Specify files on which the command depends. Each argument is converted
  124. to a dependency as follows:
  125. 1. If the argument is the name of a target (created by the
  126. :command:`add_custom_target`, :command:`add_executable`, or
  127. :command:`add_library` command) a target-level dependency is
  128. created to make sure the target is built before any target
  129. using this custom command. Additionally, if the target is an
  130. executable or library, a file-level dependency is created to
  131. cause the custom command to re-run whenever the target is
  132. recompiled.
  133. 2. If the argument is an absolute path, a file-level dependency
  134. is created on that path.
  135. 3. If the argument is the name of a source file that has been
  136. added to a target or on which a source file property has been set,
  137. a file-level dependency is created on that source file.
  138. 4. If the argument is a relative path and it exists in the current
  139. source directory, a file-level dependency is created on that
  140. file in the current source directory.
  141. 5. Otherwise, a file-level dependency is created on that path relative
  142. to the current binary directory.
  143. If any dependency is an ``OUTPUT`` of another custom command in the same
  144. directory (``CMakeLists.txt`` file), CMake automatically brings the other
  145. custom command into the target in which this command is built.
  146. .. versionadded:: 3.16
  147. A target-level dependency is added if any dependency is listed as
  148. ``BYPRODUCTS`` of a target or any of its build events in the same
  149. directory to ensure the byproducts will be available.
  150. If ``DEPENDS`` is not specified, the command will run whenever
  151. the ``OUTPUT`` is missing; if the command does not actually
  152. create the ``OUTPUT``, the rule will always run.
  153. .. versionadded:: 3.1
  154. Arguments to ``DEPENDS`` may use
  155. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  156. ``COMMAND_EXPAND_LISTS``
  157. .. versionadded:: 3.8
  158. Lists in ``COMMAND`` arguments will be expanded, including those
  159. created with
  160. :manual:`generator expressions <cmake-generator-expressions(7)>`,
  161. allowing ``COMMAND`` arguments such as
  162. ``${CC} "-I$<JOIN:$<TARGET_PROPERTY:foo,INCLUDE_DIRECTORIES>,;-I>" foo.cc``
  163. to be properly expanded.
  164. ``CODEGEN``
  165. .. versionadded:: 3.31
  166. Adds the custom command to a global ``codegen`` target that can be
  167. used to execute the custom command while avoiding the majority of the
  168. build graph.
  169. This option is supported only by :ref:`Ninja Generators` and
  170. :ref:`Makefile Generators`, and is ignored by other generators.
  171. Furthermore, this option is allowed only if policy :policy:`CMP0171`
  172. is set to ``NEW``.
  173. ``IMPLICIT_DEPENDS``
  174. Request scanning of implicit dependencies of an input file.
  175. The language given specifies the programming language whose
  176. corresponding dependency scanner should be used.
  177. Currently only ``C`` and ``CXX`` language scanners are supported.
  178. The language has to be specified for every file in the
  179. ``IMPLICIT_DEPENDS`` list. Dependencies discovered from the
  180. scanning are added to those of the custom command at build time.
  181. Note that the ``IMPLICIT_DEPENDS`` option is currently supported
  182. only for Makefile generators and will be ignored by other generators.
  183. .. note::
  184. This option cannot be specified at the same time as ``DEPFILE`` option.
  185. ``JOB_POOL``
  186. .. versionadded:: 3.15
  187. Specify a :prop_gbl:`pool <JOB_POOLS>` for the :generator:`Ninja`
  188. generator. Incompatible with ``USES_TERMINAL``, which implies
  189. the ``console`` pool.
  190. Using a pool that is not defined by :prop_gbl:`JOB_POOLS` causes
  191. an error by ninja at build time.
  192. ``JOB_SERVER_AWARE``
  193. .. versionadded:: 3.28
  194. Specify that the command is GNU Make job server aware.
  195. For the :generator:`Unix Makefiles`, :generator:`MSYS Makefiles`, and
  196. :generator:`MinGW Makefiles` generators this will add the ``+`` prefix to the
  197. recipe line. See the `GNU Make Documentation`_ for more information.
  198. This option is silently ignored by other generators.
  199. .. _`GNU Make Documentation`: https://www.gnu.org/software/make/manual/html_node/MAKE-Variable.html
  200. ``MAIN_DEPENDENCY``
  201. Specify the primary input source file to the command. This is
  202. treated just like any value given to the ``DEPENDS`` option
  203. but also suggests to Visual Studio generators where to hang
  204. the custom command. Each source file may have at most one command
  205. specifying it as its main dependency. A compile command (i.e. for a
  206. library or an executable) counts as an implicit main dependency which
  207. gets silently overwritten by a custom command specification.
  208. ``OUTPUT``
  209. Specify the output files the command is expected to produce.
  210. Each output file will be marked with the :prop_sf:`GENERATED`
  211. source file property automatically.
  212. If the output of the custom command is not actually created
  213. as a file on disk it should be marked with the :prop_sf:`SYMBOLIC`
  214. source file property.
  215. If an output file name is a relative path, its absolute path is
  216. determined by interpreting it relative to:
  217. 1. the build directory corresponding to the current source directory
  218. (:variable:`CMAKE_CURRENT_BINARY_DIR`), or
  219. 2. the current source directory (:variable:`CMAKE_CURRENT_SOURCE_DIR`).
  220. The path in the build directory is preferred unless the path in the
  221. source tree is mentioned as an absolute source file path elsewhere
  222. in the current directory.
  223. The output file path may not contain ``<`` or ``>`` characters.
  224. .. versionadded:: 3.20
  225. Arguments to ``OUTPUT`` may use a restricted set of
  226. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  227. :ref:`Target-dependent expressions <Target-Dependent Expressions>`
  228. are not permitted.
  229. .. versionchanged:: 3.28
  230. In targets using :ref:`file sets`, custom command outputs are now
  231. considered private unless they are listed in a non-private file set.
  232. See policy :policy:`CMP0154`.
  233. .. versionchanged:: 3.30
  234. The output file path may now use ``#`` characters, except
  235. when using the :generator:`Borland Makefiles` generator.
  236. ``USES_TERMINAL``
  237. .. versionadded:: 3.2
  238. The command will be given direct access to the terminal if possible.
  239. With the :generator:`Ninja` generator, this places the command in
  240. the ``console`` :prop_gbl:`pool <JOB_POOLS>`.
  241. ``VERBATIM``
  242. All arguments to the commands will be escaped properly for the
  243. build tool so that the invoked command receives each argument
  244. unchanged. Note that one level of escapes is still used by the
  245. CMake language processor before add_custom_command even sees the
  246. arguments. Use of ``VERBATIM`` is recommended as it enables
  247. correct behavior. When ``VERBATIM`` is not given the behavior
  248. is platform specific because there is no protection of
  249. tool-specific special characters.
  250. ``WORKING_DIRECTORY``
  251. Execute the command with the given current working directory.
  252. If it is a relative path it will be interpreted relative to the
  253. build tree directory corresponding to the current source directory.
  254. .. versionadded:: 3.13
  255. Arguments to ``WORKING_DIRECTORY`` may use
  256. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  257. ``DEPFILE``
  258. .. versionadded:: 3.7
  259. Specify a depfile which holds dependencies for the custom command. It is
  260. usually emitted by the custom command itself. This keyword may only be used
  261. if the generator supports it, as detailed below.
  262. The expected format, compatible with what is generated by ``gcc`` with the
  263. option ``-M``, is independent of the generator or platform.
  264. The formal syntax, as specified using
  265. `BNF <https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form>`_ notation with
  266. the regular extensions, is the following:
  267. .. raw:: latex
  268. \begin{small}
  269. .. productionlist:: depfile
  270. depfile: `rule`*
  271. rule: `targets` (':' (`separator` `dependencies`?)?)? `eol`
  272. targets: `target` (`separator` `target`)* `separator`*
  273. target: `pathname`
  274. dependencies: `dependency` (`separator` `dependency`)* `separator`*
  275. dependency: `pathname`
  276. separator: (`space` | `line_continue`)+
  277. line_continue: '\' `eol`
  278. space: ' ' | '\t'
  279. pathname: `character`+
  280. character: `std_character` | `dollar` | `hash` | `whitespace`
  281. std_character: <any character except '$', '#' or ' '>
  282. dollar: '$$'
  283. hash: '\#'
  284. whitespace: '\ '
  285. eol: '\r'? '\n'
  286. .. raw:: latex
  287. \end{small}
  288. .. note::
  289. As part of ``pathname``, any slash and backslash is interpreted as
  290. a directory separator.
  291. .. versionadded:: 3.7
  292. The :generator:`Ninja` generator supports ``DEPFILE`` since the keyword
  293. was first added.
  294. .. versionadded:: 3.17
  295. Added the :generator:`Ninja Multi-Config` generator, which included
  296. support for the ``DEPFILE`` keyword.
  297. .. versionadded:: 3.20
  298. Added support for :ref:`Makefile Generators`.
  299. .. note::
  300. ``DEPFILE`` cannot be specified at the same time as the
  301. ``IMPLICIT_DEPENDS`` option for :ref:`Makefile Generators`.
  302. .. versionadded:: 3.21
  303. Added support for :ref:`Visual Studio Generators` with VS 2012 and above,
  304. and for the :generator:`Xcode` generator. Support for
  305. :manual:`generator expressions <cmake-generator-expressions(7)>` was also
  306. added.
  307. .. versionadded:: 3.29
  308. The :ref:`Ninja Generators` will now incorporate the dependencies into its
  309. "deps log" database if the file is not listed in ``OUTPUTS`` or
  310. ``BYPRODUCTS``.
  311. Using ``DEPFILE`` with generators other than those listed above is an error.
  312. If the ``DEPFILE`` argument is relative, it should be relative to
  313. :variable:`CMAKE_CURRENT_BINARY_DIR`, and any relative paths inside the
  314. ``DEPFILE`` should also be relative to :variable:`CMAKE_CURRENT_BINARY_DIR`.
  315. See policy :policy:`CMP0116`, which is always ``NEW`` for
  316. :ref:`Makefile Generators`, :ref:`Visual Studio Generators`,
  317. and the :generator:`Xcode` generator.
  318. ``DEPENDS_EXPLICIT_ONLY``
  319. .. versionadded:: 3.27
  320. Indicates that the command's ``DEPENDS`` argument represents all files
  321. required by the command and implicit dependencies are not required.
  322. Without this option, if any target uses the output of the custom command,
  323. CMake will consider that target's dependencies as implicit dependencies for
  324. the custom command in case this custom command requires files implicitly
  325. created by those targets.
  326. This option can be enabled on all custom commands by setting
  327. :variable:`CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY` to ``ON``.
  328. Only the :ref:`Ninja Generators` actually use this information to remove
  329. unnecessary implicit dependencies.
  330. See also the :prop_tgt:`OPTIMIZE_DEPENDENCIES` target property, which may
  331. provide another way for reducing the impact of target dependencies in some
  332. scenarios.
  333. Examples: Generating Files
  334. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  335. Custom commands may be used to generate source files.
  336. For example, the code:
  337. .. code-block:: cmake
  338. add_custom_command(
  339. OUTPUT out.c
  340. COMMAND someTool -i ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  341. -o out.c
  342. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  343. VERBATIM)
  344. add_library(myLib out.c)
  345. adds a custom command to run ``someTool`` to generate ``out.c`` and then
  346. compile the generated source as part of a library. The generation rule
  347. will re-run whenever ``in.txt`` changes.
  348. .. versionadded:: 3.20
  349. One may use generator expressions to specify per-configuration outputs.
  350. For example, the code:
  351. .. code-block:: cmake
  352. add_custom_command(
  353. OUTPUT "out-$<CONFIG>.c"
  354. COMMAND someTool -i ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  355. -o "out-$<CONFIG>.c"
  356. -c "$<CONFIG>"
  357. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  358. VERBATIM)
  359. add_library(myLib "out-$<CONFIG>.c")
  360. adds a custom command to run ``someTool`` to generate ``out-<config>.c``,
  361. where ``<config>`` is the build configuration, and then compile the generated
  362. source as part of a library.
  363. .. versionadded:: 3.31
  364. Use the ``CODEGEN`` option to add a custom command's outputs to the builtin
  365. ``codegen`` target. This is useful to make generated code available for
  366. static analysis without building the entire project. For example:
  367. .. code-block:: cmake
  368. add_executable(someTool someTool.c)
  369. add_custom_command(
  370. OUTPUT out.c
  371. COMMAND someTool -o out.c
  372. CODEGEN)
  373. add_library(myLib out.c)
  374. A user may build the ``codegen`` target to generate ``out.c``.
  375. ``someTool`` is built as dependency, but ``myLib`` is not built at all.
  376. Example: Generating Files for Multiple Targets
  377. """"""""""""""""""""""""""""""""""""""""""""""
  378. If multiple independent targets need the same custom command output,
  379. it must be attached to a single custom target on which they all depend.
  380. Consider the following example:
  381. .. code-block:: cmake
  382. add_custom_command(
  383. OUTPUT table.csv
  384. COMMAND makeTable -i ${CMAKE_CURRENT_SOURCE_DIR}/input.dat
  385. -o table.csv
  386. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/input.dat
  387. VERBATIM)
  388. add_custom_target(generate_table_csv DEPENDS table.csv)
  389. add_custom_command(
  390. OUTPUT foo.cxx
  391. COMMAND genFromTable -i table.csv -case foo -o foo.cxx
  392. DEPENDS table.csv # file-level dependency
  393. generate_table_csv # target-level dependency
  394. VERBATIM)
  395. add_library(foo foo.cxx)
  396. add_custom_command(
  397. OUTPUT bar.cxx
  398. COMMAND genFromTable -i table.csv -case bar -o bar.cxx
  399. DEPENDS table.csv # file-level dependency
  400. generate_table_csv # target-level dependency
  401. VERBATIM)
  402. add_library(bar bar.cxx)
  403. Output ``foo.cxx`` is needed only by target ``foo`` and output ``bar.cxx``
  404. is needed only by target ``bar``, but *both* targets need ``table.csv``,
  405. transitively. Since ``foo`` and ``bar`` are independent targets that may
  406. build concurrently, we prevent them from racing to generate ``table.csv``
  407. by placing its custom command in a separate target, ``generate_table_csv``.
  408. The custom commands generating ``foo.cxx`` and ``bar.cxx`` each specify a
  409. target-level dependency on ``generate_table_csv``, so the targets using them,
  410. ``foo`` and ``bar``, will not build until after target ``generate_table_csv``
  411. is built.
  412. .. _`add_custom_command(TARGET)`:
  413. Build Events
  414. ^^^^^^^^^^^^
  415. The second signature adds a custom command to a target such as a
  416. library or executable. This is useful for performing an operation
  417. before or after building the target. The command becomes part of the
  418. target and will only execute when the target itself is built. If the
  419. target is already built, the command will not execute.
  420. .. code-block:: cmake
  421. add_custom_command(TARGET <target>
  422. PRE_BUILD | PRE_LINK | POST_BUILD
  423. COMMAND command1 [ARGS] [args1...]
  424. [COMMAND command2 [ARGS] [args2...] ...]
  425. [BYPRODUCTS [files...]]
  426. [WORKING_DIRECTORY dir]
  427. [COMMENT comment]
  428. [VERBATIM]
  429. [COMMAND_EXPAND_LISTS])
  430. This defines a new command that will be associated with building the
  431. specified ``<target>``. The ``<target>`` must be defined in the current
  432. directory; targets defined in other directories may not be specified.
  433. When the command will happen is determined by which
  434. of the following is specified:
  435. ``PRE_BUILD``
  436. This option has unique behavior for the :ref:`Visual Studio Generators`.
  437. When using one of the Visual Studio generators, the command will run before
  438. any other rules are executed within the target. With all other generators,
  439. this option behaves the same as ``PRE_LINK`` instead. Because of this,
  440. it is recommended to avoid using ``PRE_BUILD`` except when it is known that
  441. a Visual Studio generator is being used.
  442. ``PRE_LINK``
  443. Run after sources have been compiled but before linking the binary
  444. or running the librarian or archiver tool of a static library.
  445. This is not defined for targets created by the
  446. :command:`add_custom_target` command.
  447. ``POST_BUILD``
  448. Run after all other rules within the target have been executed.
  449. Projects should always specify one of the above three keywords when using
  450. the ``TARGET`` form. For backward compatibility reasons, ``POST_BUILD`` is
  451. assumed if no such keyword is given, but projects should explicitly provide
  452. one of the keywords to make clear the behavior they expect.
  453. .. note::
  454. Because generator expressions can be used in custom commands,
  455. it is possible to define ``COMMAND`` lines or whole custom commands
  456. which evaluate to empty strings for certain configurations.
  457. For **Visual Studio 12 2013 (and newer)** generators these command
  458. lines or custom commands will be omitted for the specific
  459. configuration and no "empty-string-command" will be added.
  460. This allows adding individual build events for every configuration.
  461. .. versionadded:: 3.21
  462. Support for target-dependent generator expressions.
  463. .. versionadded:: 3.29
  464. The ``<target>`` may be an :ref:`ALIAS target <Alias Targets>`.
  465. Examples: Build Events
  466. ^^^^^^^^^^^^^^^^^^^^^^
  467. A ``POST_BUILD`` event may be used to post-process a binary after linking.
  468. For example, the code:
  469. .. code-block:: cmake
  470. add_executable(myExe myExe.c)
  471. add_custom_command(
  472. TARGET myExe POST_BUILD
  473. COMMAND someHasher -i "$<TARGET_FILE:myExe>"
  474. -o "$<TARGET_FILE:myExe>.hash"
  475. VERBATIM)
  476. will run ``someHasher`` to produce a ``.hash`` file next to the executable
  477. after linking.
  478. .. versionadded:: 3.20
  479. One may use generator expressions to specify per-configuration byproducts.
  480. For example, the code:
  481. .. code-block:: cmake
  482. add_library(myPlugin MODULE myPlugin.c)
  483. add_custom_command(
  484. TARGET myPlugin POST_BUILD
  485. COMMAND someHasher -i "$<TARGET_FILE:myPlugin>"
  486. --as-code "myPlugin-hash-$<CONFIG>.c"
  487. BYPRODUCTS "myPlugin-hash-$<CONFIG>.c"
  488. VERBATIM)
  489. add_executable(myExe myExe.c "myPlugin-hash-$<CONFIG>.c")
  490. will run ``someHasher`` after linking ``myPlugin``, e.g. to produce a ``.c``
  491. file containing code to check the hash of ``myPlugin`` that the ``myExe``
  492. executable can use to verify it before loading.
  493. Ninja Multi-Config
  494. ^^^^^^^^^^^^^^^^^^
  495. .. versionadded:: 3.20
  496. ``add_custom_command`` supports the :generator:`Ninja Multi-Config`
  497. generator's cross-config capabilities. See the generator documentation
  498. for more information.
  499. See Also
  500. ^^^^^^^^
  501. * :command:`add_custom_target`