add_custom_command.rst 12 KB

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