add_custom_command.rst 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. add_custom_command(OUTPUT output1 [output2 ...]
  9. COMMAND command1 [ARGS] [args1...]
  10. [COMMAND command2 [ARGS] [args2...] ...]
  11. [MAIN_DEPENDENCY depend]
  12. [DEPENDS [depends...]]
  13. [BYPRODUCTS [files...]]
  14. [IMPLICIT_DEPENDS <lang1> depend1
  15. [<lang2> depend2] ...]
  16. [WORKING_DIRECTORY dir]
  17. [COMMENT comment]
  18. [VERBATIM] [APPEND] [USES_TERMINAL])
  19. This defines a command to generate specified ``OUTPUT`` file(s).
  20. A target created in the same directory (``CMakeLists.txt`` file)
  21. that specifies any output of the custom command as a source file
  22. is given a rule to generate the file using the command at build time.
  23. Do not list the output in more than one independent target that
  24. may build in parallel or the two instances of the rule may conflict
  25. (instead use the :command:`add_custom_target` command to drive the
  26. command and make the other targets depend on that one).
  27. In makefile terms this creates a new target in the following form::
  28. OUTPUT: MAIN_DEPENDENCY DEPENDS
  29. COMMAND
  30. The options are:
  31. ``APPEND``
  32. Append the ``COMMAND`` and ``DEPENDS`` option values to the custom
  33. command for the first output specified. There must have already
  34. been a previous call to this command with the same output.
  35. The ``COMMENT``, ``MAIN_DEPENDENCY``, and ``WORKING_DIRECTORY``
  36. options are currently ignored when APPEND is given, but may be
  37. used in the future.
  38. ``BYPRODUCTS``
  39. Specify the files the command is expected to produce but whose
  40. modification time may or may not be newer than the dependencies.
  41. If a byproduct name is a relative path it will be interpreted
  42. relative to the build tree directory corresponding to the
  43. current source directory.
  44. Each byproduct file will be marked with the :prop_sf:`GENERATED`
  45. source file property automatically.
  46. Explicit specification of byproducts is supported by the
  47. :generator:`Ninja` generator to tell the ``ninja`` build tool
  48. how to regenerate byproducts when they are missing. It is
  49. also useful when other build rules (e.g. custom commands)
  50. depend on the byproducts. Ninja requires a build rule for any
  51. generated file on which another rule depends even if there are
  52. order-only dependencies to ensure the byproducts will be
  53. available before their dependents build.
  54. The ``BYPRODUCTS`` option is ignored on non-Ninja generators
  55. except to mark byproducts ``GENERATED``.
  56. ``COMMAND``
  57. Specify the command-line(s) to execute at build time.
  58. If more than one ``COMMAND`` is specified they will be executed in order,
  59. but *not* necessarily composed into a stateful shell or batch script.
  60. (To run a full script, use the :command:`configure_file` command or the
  61. :command:`file(GENERATE)` command to create it, and then specify
  62. a ``COMMAND`` to launch it.)
  63. The optional ``ARGS`` argument is for backward compatibility and
  64. will be ignored.
  65. If ``COMMAND`` specifies an executable target (created by the
  66. :command:`add_executable` command) it will automatically be replaced
  67. by the location of the executable created at build time.
  68. (Use the ``TARGET_FILE``
  69. :manual:`generator expression <cmake-generator-expressions(7)>` to
  70. reference an executable later in the command line.)
  71. Additionally a target-level dependency will be added so that the
  72. executable target will be built before any target using this custom
  73. command. However this does NOT add a file-level dependency that
  74. would cause the custom command to re-run whenever the executable is
  75. recompiled.
  76. Arguments to ``COMMAND`` may use
  77. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  78. References to target names in generator expressions imply target-level
  79. dependencies, but NOT file-level dependencies. List target names with
  80. the ``DEPENDS`` option to add file-level dependencies.
  81. ``COMMENT``
  82. Display the given message before the commands are executed at
  83. build time.
  84. ``DEPENDS``
  85. Specify files on which the command depends. If any dependency is
  86. an ``OUTPUT`` of another custom command in the same directory
  87. (``CMakeLists.txt`` file) CMake automatically brings the other
  88. custom command into the target in which this command is built.
  89. If ``DEPENDS`` is not specified the command will run whenever
  90. the ``OUTPUT`` is missing; if the command does not actually
  91. create the ``OUTPUT`` then the rule will always run.
  92. If ``DEPENDS`` specifies any target (created by the
  93. :command:`add_custom_target`, :command:`add_executable`, or
  94. :command:`add_library` command) a target-level dependency is
  95. created to make sure the target is built before any target
  96. using this custom command. Additionally, if the target is an
  97. executable or library a file-level dependency is created to
  98. cause the custom command to re-run whenever the target is
  99. recompiled.
  100. Arguments to ``DEPENDS`` may use
  101. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  102. ``IMPLICIT_DEPENDS``
  103. Request scanning of implicit dependencies of an input file.
  104. The language given specifies the programming language whose
  105. corresponding dependency scanner should be used.
  106. Currently only ``C`` and ``CXX`` language scanners are supported.
  107. The language has to be specified for every file in the
  108. ``IMPLICIT_DEPENDS`` list. Dependencies discovered from the
  109. scanning are added to those of the custom command at build time.
  110. Note that the ``IMPLICIT_DEPENDS`` option is currently supported
  111. only for Makefile generators and will be ignored by other generators.
  112. ``MAIN_DEPENDENCY``
  113. Specify the primary input source file to the command. This is
  114. treated just like any value given to the ``DEPENDS`` option
  115. but also suggests to Visual Studio generators where to hang
  116. the custom command. At most one custom command may specify a
  117. given source file as its main dependency.
  118. ``OUTPUT``
  119. Specify the output files the command is expected to produce.
  120. If an output name is a relative path it will be interpreted
  121. relative to the build tree directory corresponding to the
  122. current source directory.
  123. Each output file will be marked with the :prop_sf:`GENERATED`
  124. source file property automatically.
  125. If the output of the custom command is not actually created
  126. as a file on disk it should be marked with the :prop_sf:`SYMBOLIC`
  127. source file property.
  128. ``USES_TERMINAL``
  129. The command will be given direct access to the terminal if possible.
  130. With the :generator:`Ninja` generator, this places the command in
  131. the ``console`` :prop_gbl:`pool <JOB_POOLS>`.
  132. ``VERBATIM``
  133. All arguments to the commands will be escaped properly for the
  134. build tool so that the invoked command receives each argument
  135. unchanged. Note that one level of escapes is still used by the
  136. CMake language processor before add_custom_command even sees the
  137. arguments. Use of ``VERBATIM`` is recommended as it enables
  138. correct behavior. When ``VERBATIM`` is not given the behavior
  139. is platform specific because there is no protection of
  140. tool-specific special characters.
  141. ``WORKING_DIRECTORY``
  142. Execute the command with the given current working directory.
  143. If it is a relative path it will be interpreted relative to the
  144. build tree directory corresponding to the current source directory.
  145. Build Events
  146. ^^^^^^^^^^^^
  147. The second signature adds a custom command to a target such as a
  148. library or executable. This is useful for performing an operation
  149. before or after building the target. The command becomes part of the
  150. target and will only execute when the target itself is built. If the
  151. target is already built, the command will not execute.
  152. ::
  153. add_custom_command(TARGET <target>
  154. PRE_BUILD | PRE_LINK | POST_BUILD
  155. COMMAND command1 [ARGS] [args1...]
  156. [COMMAND command2 [ARGS] [args2...] ...]
  157. [BYPRODUCTS [files...]]
  158. [WORKING_DIRECTORY dir]
  159. [COMMENT comment]
  160. [VERBATIM] [USES_TERMINAL])
  161. This defines a new command that will be associated with building the
  162. specified ``<target>``. The ``<target>`` must be defined in the current
  163. directory; targets defined in other directories may not be specified.
  164. When the command will happen is determined by which
  165. of the following is specified:
  166. ``PRE_BUILD``
  167. Run before any other rules are executed within the target.
  168. This is supported only on Visual Studio 7 or later.
  169. For all other generators ``PRE_BUILD`` will be treated as
  170. ``PRE_LINK``.
  171. ``PRE_LINK``
  172. Run after sources have been compiled but before linking the binary
  173. or running the librarian or archiver tool of a static library.
  174. This is not defined for targets created by the
  175. :command:`add_custom_target` command.
  176. ``POST_BUILD``
  177. Run after all other rules within the target have been executed.