add_custom_command.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. 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. ``DEPENDS``
  114. Specify files on which the command depends. Each argument is converted
  115. to a dependency as follows:
  116. 1. If the argument is the name of a target (created by the
  117. :command:`add_custom_target`, :command:`add_executable`, or
  118. :command:`add_library` command) a target-level dependency is
  119. created to make sure the target is built before any target
  120. using this custom command. Additionally, if the target is an
  121. executable or library, a file-level dependency is created to
  122. cause the custom command to re-run whenever the target is
  123. recompiled.
  124. 2. If the argument is an absolute path, a file-level dependency
  125. is created on that path.
  126. 3. If the argument is the name of a source file that has been
  127. added to a target or on which a source file property has been set,
  128. a file-level dependency is created on that source file.
  129. 4. If the argument is a relative path and it exists in the current
  130. source directory, a file-level dependency is created on that
  131. file in the current source directory.
  132. 5. Otherwise, a file-level dependency is created on that path relative
  133. to the current binary directory.
  134. If any dependency is an ``OUTPUT`` of another custom command in the same
  135. directory (``CMakeLists.txt`` file), CMake automatically brings the other
  136. custom command into the target in which this command is built.
  137. .. versionadded:: 3.16
  138. A target-level dependency is added if any dependency is listed as
  139. ``BYPRODUCTS`` of a target or any of its build events in the same
  140. directory to ensure the byproducts will be available.
  141. If ``DEPENDS`` is not specified, the command will run whenever
  142. the ``OUTPUT`` is missing; if the command does not actually
  143. create the ``OUTPUT``, the rule will always run.
  144. .. versionadded:: 3.1
  145. Arguments to ``DEPENDS`` may use
  146. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  147. ``COMMAND_EXPAND_LISTS``
  148. .. versionadded:: 3.8
  149. Lists in ``COMMAND`` arguments will be expanded, including those
  150. created with
  151. :manual:`generator expressions <cmake-generator-expressions(7)>`,
  152. allowing ``COMMAND`` arguments such as
  153. ``${CC} "-I$<JOIN:$<TARGET_PROPERTY:foo,INCLUDE_DIRECTORIES>,;-I>" foo.cc``
  154. to be properly expanded.
  155. ``IMPLICIT_DEPENDS``
  156. Request scanning of implicit dependencies of an input file.
  157. The language given specifies the programming language whose
  158. corresponding dependency scanner should be used.
  159. Currently only ``C`` and ``CXX`` language scanners are supported.
  160. The language has to be specified for every file in the
  161. ``IMPLICIT_DEPENDS`` list. Dependencies discovered from the
  162. scanning are added to those of the custom command at build time.
  163. Note that the ``IMPLICIT_DEPENDS`` option is currently supported
  164. only for Makefile generators and will be ignored by other generators.
  165. .. note::
  166. This option cannot be specified at the same time as ``DEPFILE`` option.
  167. ``JOB_POOL``
  168. .. versionadded:: 3.15
  169. Specify a :prop_gbl:`pool <JOB_POOLS>` for the :generator:`Ninja`
  170. generator. Incompatible with ``USES_TERMINAL``, which implies
  171. the ``console`` pool.
  172. Using a pool that is not defined by :prop_gbl:`JOB_POOLS` causes
  173. an error by ninja at build time.
  174. ``MAIN_DEPENDENCY``
  175. Specify the primary input source file to the command. This is
  176. treated just like any value given to the ``DEPENDS`` option
  177. but also suggests to Visual Studio generators where to hang
  178. the custom command. Each source file may have at most one command
  179. specifying it as its main dependency. A compile command (i.e. for a
  180. library or an executable) counts as an implicit main dependency which
  181. gets silently overwritten by a custom command specification.
  182. ``OUTPUT``
  183. Specify the output files the command is expected to produce.
  184. If an output name is a relative path it will be interpreted
  185. relative to the build tree directory corresponding to the
  186. current source directory.
  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. .. versionadded:: 3.20
  193. Arguments to ``OUTPUT`` may use a restricted set of
  194. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  195. :ref:`Target-dependent expressions <Target-Dependent Queries>` are not
  196. permitted.
  197. ``USES_TERMINAL``
  198. .. versionadded:: 3.2
  199. The command will be given direct access to the terminal if possible.
  200. With the :generator:`Ninja` generator, this places the command in
  201. the ``console`` :prop_gbl:`pool <JOB_POOLS>`.
  202. ``VERBATIM``
  203. All arguments to the commands will be escaped properly for the
  204. build tool so that the invoked command receives each argument
  205. unchanged. Note that one level of escapes is still used by the
  206. CMake language processor before add_custom_command even sees the
  207. arguments. Use of ``VERBATIM`` is recommended as it enables
  208. correct behavior. When ``VERBATIM`` is not given the behavior
  209. is platform specific because there is no protection of
  210. tool-specific special characters.
  211. ``WORKING_DIRECTORY``
  212. Execute the command with the given current working directory.
  213. If it is a relative path it will be interpreted relative to the
  214. build tree directory corresponding to the current source directory.
  215. .. versionadded:: 3.13
  216. Arguments to ``WORKING_DIRECTORY`` may use
  217. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  218. ``DEPFILE``
  219. .. versionadded:: 3.7
  220. Specify a ``.d`` depfile for the :generator:`Ninja`, :generator:`Xcode` and
  221. :ref:`Makefile <Makefile Generators>` generators. The depfile may use
  222. "generator expressions" with the syntax ``$<...>``. See the
  223. :manual:`generator-expressions(7) <cmake-generator-expressions(7)>` manual
  224. for available expressions. A ``.d`` file holds dependencies usually emitted
  225. by the custom command itself.
  226. Using ``DEPFILE`` with other generators than :generator:`Ninja`,
  227. :generator:`Xcode` or :ref:`Makefile <Makefile Generators>` is an error.
  228. .. versionadded:: 3.20
  229. Added the support of :ref:`Makefile Generators`.
  230. .. versionadded:: 3.21
  231. Added the support of :generator:`Xcode` generator and
  232. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  233. If the ``DEPFILE`` argument is relative, it should be relative to
  234. :variable:`CMAKE_CURRENT_BINARY_DIR`, and any relative paths inside the
  235. ``DEPFILE`` should also be relative to :variable:`CMAKE_CURRENT_BINARY_DIR`
  236. (see policy :policy:`CMP0116`. This policy is always ``NEW`` for
  237. :ref:`Makefile <Makefile Generators>` and :generator:`Xcode` generators).
  238. .. note::
  239. For :ref:`Makefile Generators`, this option cannot be specified at the
  240. same time as ``IMPLICIT_DEPENDS`` option.
  241. Examples: Generating Files
  242. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  243. Custom commands may be used to generate source files.
  244. For example, the code:
  245. .. code-block:: cmake
  246. add_custom_command(
  247. OUTPUT out.c
  248. COMMAND someTool -i ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  249. -o out.c
  250. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  251. VERBATIM)
  252. add_library(myLib out.c)
  253. adds a custom command to run ``someTool`` to generate ``out.c`` and then
  254. compile the generated source as part of a library. The generation rule
  255. will re-run whenever ``in.txt`` changes.
  256. .. versionadded:: 3.20
  257. One may use generator expressions to specify per-configuration outputs.
  258. For example, the code:
  259. .. code-block:: cmake
  260. add_custom_command(
  261. OUTPUT "out-$<CONFIG>.c"
  262. COMMAND someTool -i ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  263. -o "out-$<CONFIG>.c"
  264. -c "$<CONFIG>"
  265. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  266. VERBATIM)
  267. add_library(myLib "out-$<CONFIG>.c")
  268. adds a custom command to run ``someTool`` to generate ``out-<config>.c``,
  269. where ``<config>`` is the build configuration, and then compile the generated
  270. source as part of a library.
  271. .. _`add_custom_command(TARGET)`:
  272. Build Events
  273. ^^^^^^^^^^^^
  274. The second signature adds a custom command to a target such as a
  275. library or executable. This is useful for performing an operation
  276. before or after building the target. The command becomes part of the
  277. target and will only execute when the target itself is built. If the
  278. target is already built, the command will not execute.
  279. .. code-block:: cmake
  280. add_custom_command(TARGET <target>
  281. PRE_BUILD | PRE_LINK | POST_BUILD
  282. COMMAND command1 [ARGS] [args1...]
  283. [COMMAND command2 [ARGS] [args2...] ...]
  284. [BYPRODUCTS [files...]]
  285. [WORKING_DIRECTORY dir]
  286. [COMMENT comment]
  287. [VERBATIM] [USES_TERMINAL]
  288. [COMMAND_EXPAND_LISTS])
  289. This defines a new command that will be associated with building the
  290. specified ``<target>``. The ``<target>`` must be defined in the current
  291. directory; targets defined in other directories may not be specified.
  292. When the command will happen is determined by which
  293. of the following is specified:
  294. ``PRE_BUILD``
  295. On :ref:`Visual Studio Generators`, run before any other rules are
  296. executed within the target.
  297. On other generators, run just before ``PRE_LINK`` commands.
  298. ``PRE_LINK``
  299. Run after sources have been compiled but before linking the binary
  300. or running the librarian or archiver tool of a static library.
  301. This is not defined for targets created by the
  302. :command:`add_custom_target` command.
  303. ``POST_BUILD``
  304. Run after all other rules within the target have been executed.
  305. .. note::
  306. Because generator expressions can be used in custom commands,
  307. it is possible to define ``COMMAND`` lines or whole custom commands
  308. which evaluate to empty strings for certain configurations.
  309. For **Visual Studio 2010 (and newer)** generators these command
  310. lines or custom commands will be omitted for the specific
  311. configuration and no "empty-string-command" will be added.
  312. This allows to add individual build events for every configuration.
  313. .. versionadded:: 3.21
  314. Support for target-dependent generator expressions.
  315. Examples: Build Events
  316. ^^^^^^^^^^^^^^^^^^^^^^
  317. A ``POST_BUILD`` event may be used to post-process a binary after linking.
  318. For example, the code:
  319. .. code-block:: cmake
  320. add_executable(myExe myExe.c)
  321. add_custom_command(
  322. TARGET myExe POST_BUILD
  323. COMMAND someHasher -i "$<TARGET_FILE:myExe>"
  324. -o "$<TARGET_FILE:myExe>.hash"
  325. VERBATIM)
  326. will run ``someHasher`` to produce a ``.hash`` file next to the executable
  327. after linking.
  328. .. versionadded:: 3.20
  329. One may use generator expressions to specify per-configuration byproducts.
  330. For example, the code:
  331. .. code-block:: cmake
  332. add_library(myPlugin MODULE myPlugin.c)
  333. add_custom_command(
  334. TARGET myPlugin POST_BUILD
  335. COMMAND someHasher -i "$<TARGET_FILE:myPlugin>"
  336. --as-code "myPlugin-hash-$<CONFIG>.c"
  337. BYPRODUCTS "myPlugin-hash-$<CONFIG>.c"
  338. VERBATIM)
  339. add_executable(myExe myExe.c "myPlugin-hash-$<CONFIG>.c")
  340. will run ``someHasher`` after linking ``myPlugin``, e.g. to produce a ``.c``
  341. file containing code to check the hash of ``myPlugin`` that the ``myExe``
  342. executable can use to verify it before loading.
  343. Ninja Multi-Config
  344. ^^^^^^^^^^^^^^^^^^
  345. .. versionadded:: 3.20
  346. ``add_custom_command`` supports the :generator:`Ninja Multi-Config`
  347. generator's cross-config capabilities. See the generator documentation
  348. for more information.