add_custom_command.rst 19 KB

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