add_custom_command.rst 22 KB

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