add_custom_command.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. The ``COMMENT``, ``MAIN_DEPENDENCY``, and ``WORKING_DIRECTORY``
  40. options are currently ignored when APPEND is given, but may be
  41. used in the future.
  42. ``BYPRODUCTS``
  43. .. versionadded:: 3.2
  44. Specify the files the command is expected to produce but whose
  45. modification time may or may not be newer than the dependencies.
  46. If a byproduct name is a relative path it will be interpreted
  47. relative to the build tree directory corresponding to the
  48. current source directory.
  49. Each byproduct file will be marked with the :prop_sf:`GENERATED`
  50. source file property automatically.
  51. Explicit specification of byproducts is supported by the
  52. :generator:`Ninja` generator to tell the ``ninja`` build tool
  53. how to regenerate byproducts when they are missing. It is
  54. also useful when other build rules (e.g. custom commands)
  55. depend on the byproducts. Ninja requires a build rule for any
  56. generated file on which another rule depends even if there are
  57. order-only dependencies to ensure the byproducts will be
  58. available before their dependents build.
  59. The :ref:`Makefile Generators` will remove ``BYPRODUCTS`` and other
  60. :prop_sf:`GENERATED` files during ``make clean``.
  61. ``COMMAND``
  62. Specify the command-line(s) to execute at build time.
  63. If more than one ``COMMAND`` is specified they will be executed in order,
  64. but *not* necessarily composed into a stateful shell or batch script.
  65. (To run a full script, use the :command:`configure_file` command or the
  66. :command:`file(GENERATE)` command to create it, and then specify
  67. a ``COMMAND`` to launch it.)
  68. The optional ``ARGS`` argument is for backward compatibility and
  69. will be ignored.
  70. If ``COMMAND`` specifies an executable target name (created by the
  71. :command:`add_executable` command), it will automatically be replaced
  72. by the location of the executable created at build time if either of
  73. the following is true:
  74. * The target is not being cross-compiled (i.e. the
  75. :variable:`CMAKE_CROSSCOMPILING` variable is not set to true).
  76. * .. versionadded:: 3.6
  77. The target is being cross-compiled and an emulator is provided (i.e.
  78. its :prop_tgt:`CROSSCOMPILING_EMULATOR` target property is set).
  79. In this case, the contents of :prop_tgt:`CROSSCOMPILING_EMULATOR` will be
  80. prepended to the command before the location of the target executable.
  81. If neither of the above conditions are met, it is assumed that the
  82. command name is a program to be found on the ``PATH`` at build time.
  83. Arguments to ``COMMAND`` may use
  84. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  85. Use the ``TARGET_FILE`` generator expression to refer to the location of
  86. a target later in the command line (i.e. as a command argument rather
  87. than as the command to execute).
  88. Whenever one of the following target based generator expressions are used as
  89. a command to execute or is mentioned in a command argument, a target-level
  90. dependency will be added automatically so that the mentioned target will be
  91. built before any target using this custom command
  92. (see policy :policy:`CMP0112`).
  93. * ``TARGET_FILE``
  94. * ``TARGET_LINKER_FILE``
  95. * ``TARGET_SONAME_FILE``
  96. * ``TARGET_PDB_FILE``
  97. This target-level dependency does NOT add a file-level dependency that would
  98. cause the custom command to re-run whenever the executable is recompiled.
  99. List target names with the ``DEPENDS`` option to add such file-level
  100. dependencies.
  101. ``COMMENT``
  102. Display the given message before the commands are executed at
  103. build time.
  104. ``DEPENDS``
  105. Specify files on which the command depends. Each argument is converted
  106. to a dependency as follows:
  107. 1. If the argument is the name of a target (created by the
  108. :command:`add_custom_target`, :command:`add_executable`, or
  109. :command:`add_library` command) a target-level dependency is
  110. created to make sure the target is built before any target
  111. using this custom command. Additionally, if the target is an
  112. executable or library, a file-level dependency is created to
  113. cause the custom command to re-run whenever the target is
  114. recompiled.
  115. 2. If the argument is an absolute path, a file-level dependency
  116. is created on that path.
  117. 3. If the argument is the name of a source file that has been
  118. added to a target or on which a source file property has been set,
  119. a file-level dependency is created on that source file.
  120. 4. If the argument is a relative path and it exists in the current
  121. source directory, a file-level dependency is created on that
  122. file in the current source directory.
  123. 5. Otherwise, a file-level dependency is created on that path relative
  124. to the current binary directory.
  125. If any dependency is an ``OUTPUT`` of another custom command in the same
  126. directory (``CMakeLists.txt`` file), CMake automatically brings the other
  127. custom command into the target in which this command is built.
  128. .. versionadded:: 3.16
  129. A target-level dependency is added if any dependency is listed as
  130. ``BYPRODUCTS`` of a target or any of its build events in the same
  131. directory to ensure the byproducts will be available.
  132. If ``DEPENDS`` is not specified, the command will run whenever
  133. the ``OUTPUT`` is missing; if the command does not actually
  134. create the ``OUTPUT``, the rule will always run.
  135. .. versionadded:: 3.1
  136. Arguments to ``DEPENDS`` may use
  137. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  138. ``COMMAND_EXPAND_LISTS``
  139. .. versionadded:: 3.8
  140. Lists in ``COMMAND`` arguments will be expanded, including those
  141. created with
  142. :manual:`generator expressions <cmake-generator-expressions(7)>`,
  143. allowing ``COMMAND`` arguments such as
  144. ``${CC} "-I$<JOIN:$<TARGET_PROPERTY:foo,INCLUDE_DIRECTORIES>,;-I>" foo.cc``
  145. to be properly expanded.
  146. ``IMPLICIT_DEPENDS``
  147. Request scanning of implicit dependencies of an input file.
  148. The language given specifies the programming language whose
  149. corresponding dependency scanner should be used.
  150. Currently only ``C`` and ``CXX`` language scanners are supported.
  151. The language has to be specified for every file in the
  152. ``IMPLICIT_DEPENDS`` list. Dependencies discovered from the
  153. scanning are added to those of the custom command at build time.
  154. Note that the ``IMPLICIT_DEPENDS`` option is currently supported
  155. only for Makefile generators and will be ignored by other generators.
  156. ``JOB_POOL``
  157. .. versionadded:: 3.15
  158. Specify a :prop_gbl:`pool <JOB_POOLS>` for the :generator:`Ninja`
  159. generator. Incompatible with ``USES_TERMINAL``, which implies
  160. the ``console`` pool.
  161. Using a pool that is not defined by :prop_gbl:`JOB_POOLS` causes
  162. an error by ninja at build time.
  163. ``MAIN_DEPENDENCY``
  164. Specify the primary input source file to the command. This is
  165. treated just like any value given to the ``DEPENDS`` option
  166. but also suggests to Visual Studio generators where to hang
  167. the custom command. Each source file may have at most one command
  168. specifying it as its main dependency. A compile command (i.e. for a
  169. library or an executable) counts as an implicit main dependency which
  170. gets silently overwritten by a custom command specification.
  171. ``OUTPUT``
  172. Specify the output files the command is expected to produce.
  173. If an output name is a relative path it will be interpreted
  174. relative to the build tree directory corresponding to the
  175. current source directory.
  176. Each output file will be marked with the :prop_sf:`GENERATED`
  177. source file property automatically.
  178. If the output of the custom command is not actually created
  179. as a file on disk it should be marked with the :prop_sf:`SYMBOLIC`
  180. source file property.
  181. ``USES_TERMINAL``
  182. .. versionadded:: 3.2
  183. The command will be given direct access to the terminal if possible.
  184. With the :generator:`Ninja` generator, this places the command in
  185. the ``console`` :prop_gbl:`pool <JOB_POOLS>`.
  186. ``VERBATIM``
  187. All arguments to the commands will be escaped properly for the
  188. build tool so that the invoked command receives each argument
  189. unchanged. Note that one level of escapes is still used by the
  190. CMake language processor before add_custom_command even sees the
  191. arguments. Use of ``VERBATIM`` is recommended as it enables
  192. correct behavior. When ``VERBATIM`` is not given the behavior
  193. is platform specific because there is no protection of
  194. tool-specific special characters.
  195. ``WORKING_DIRECTORY``
  196. Execute the command with the given current working directory.
  197. If it is a relative path it will be interpreted relative to the
  198. build tree directory corresponding to the current source directory.
  199. .. versionadded:: 3.13
  200. Arguments to ``WORKING_DIRECTORY`` may use
  201. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  202. ``DEPFILE``
  203. .. versionadded:: 3.7
  204. Specify a ``.d`` depfile for the :generator:`Ninja` generator.
  205. A ``.d`` file holds dependencies usually emitted by the custom
  206. command itself.
  207. Using ``DEPFILE`` with other generators than Ninja is an error.
  208. If the ``DEPFILE`` argument is relative, it should be relative to
  209. :variable:`CMAKE_CURRENT_BINARY_DIR`, and any relative paths inside the
  210. ``DEPFILE`` should also be relative to :variable:`CMAKE_CURRENT_BINARY_DIR`
  211. (see policy :policy:`CMP0116`.)
  212. Examples: Generating Files
  213. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  214. Custom commands may be used to generate source files.
  215. For example, the code:
  216. .. code-block:: cmake
  217. add_custom_command(
  218. OUTPUT out.c
  219. COMMAND someTool -i ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  220. -o out.c
  221. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  222. VERBATIM)
  223. add_library(myLib out.c)
  224. adds a custom command to run ``someTool`` to generate ``out.c`` and then
  225. compile the generated source as part of a library. The generation rule
  226. will re-run whenever ``in.txt`` changes.
  227. Build Events
  228. ^^^^^^^^^^^^
  229. The second signature adds a custom command to a target such as a
  230. library or executable. This is useful for performing an operation
  231. before or after building the target. The command becomes part of the
  232. target and will only execute when the target itself is built. If the
  233. target is already built, the command will not execute.
  234. .. code-block:: cmake
  235. add_custom_command(TARGET <target>
  236. PRE_BUILD | PRE_LINK | POST_BUILD
  237. COMMAND command1 [ARGS] [args1...]
  238. [COMMAND command2 [ARGS] [args2...] ...]
  239. [BYPRODUCTS [files...]]
  240. [WORKING_DIRECTORY dir]
  241. [COMMENT comment]
  242. [VERBATIM] [USES_TERMINAL]
  243. [COMMAND_EXPAND_LISTS])
  244. This defines a new command that will be associated with building the
  245. specified ``<target>``. The ``<target>`` must be defined in the current
  246. directory; targets defined in other directories may not be specified.
  247. When the command will happen is determined by which
  248. of the following is specified:
  249. ``PRE_BUILD``
  250. On :ref:`Visual Studio Generators`, run before any other rules are
  251. executed within the target.
  252. On other generators, run just before ``PRE_LINK`` commands.
  253. ``PRE_LINK``
  254. Run after sources have been compiled but before linking the binary
  255. or running the librarian or archiver tool of a static library.
  256. This is not defined for targets created by the
  257. :command:`add_custom_target` command.
  258. ``POST_BUILD``
  259. Run after all other rules within the target have been executed.
  260. .. note::
  261. Because generator expressions can be used in custom commands,
  262. it is possible to define ``COMMAND`` lines or whole custom commands
  263. which evaluate to empty strings for certain configurations.
  264. For **Visual Studio 2010 (and newer)** generators these command
  265. lines or custom commands will be omitted for the specific
  266. configuration and no "empty-string-command" will be added.
  267. This allows to add individual build events for every configuration.
  268. Examples: Build Events
  269. ^^^^^^^^^^^^^^^^^^^^^^
  270. A ``POST_BUILD`` event may be used to post-process a binary after linking.
  271. For example, the code:
  272. .. code-block:: cmake
  273. add_executable(myExe myExe.c)
  274. add_custom_command(
  275. TARGET myExe POST_BUILD
  276. COMMAND someHasher -i "$<TARGET_FILE:myExe>"
  277. -o "$<TARGET_FILE:myExe>.hash"
  278. VERBATIM)
  279. will run ``someHasher`` to produce a ``.hash`` file next to the executable
  280. after linking.