add_custom_command.rst 20 KB

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