add_custom_command.rst 23 KB

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