add_custom_target.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. add_custom_target
  2. -----------------
  3. Add a target with no output so it will always be built.
  4. .. code-block:: cmake
  5. add_custom_target(Name [ALL] [command1 [args1...]]
  6. [COMMAND command2 [args2...] ...]
  7. [DEPENDS depend depend depend ...]
  8. [BYPRODUCTS [files...]]
  9. [WORKING_DIRECTORY dir]
  10. [COMMENT comment]
  11. [JOB_POOL job_pool]
  12. [JOB_SERVER_AWARE <bool>]
  13. [VERBATIM] [USES_TERMINAL]
  14. [COMMAND_EXPAND_LISTS]
  15. [SOURCES src1 [src2...]])
  16. Adds a target with the given name that executes the given commands.
  17. The target has no output file and is *always considered out of date*
  18. even if the commands try to create a file with the name of the target.
  19. Use the :command:`add_custom_command` command to generate a file with
  20. dependencies. By default nothing depends on the custom target. Use
  21. the :command:`add_dependencies` command to add dependencies to or
  22. from other targets.
  23. The options are:
  24. ``ALL``
  25. Indicate that this target should be added to the default build
  26. target so that it will be run every time (the command cannot be
  27. called ``ALL``).
  28. ``BYPRODUCTS``
  29. .. versionadded:: 3.2
  30. Specify the files the command is expected to produce but whose
  31. modification time may or may not be updated on subsequent builds.
  32. If a byproduct name is a relative path it will be interpreted
  33. relative to the build tree directory corresponding to the
  34. current source directory.
  35. Each byproduct file will be marked with the :prop_sf:`GENERATED`
  36. source file property automatically.
  37. *See policy* :policy:`CMP0058` *for the motivation behind this feature.*
  38. Explicit specification of byproducts is supported by the
  39. :generator:`Ninja` generator to tell the ``ninja`` build tool
  40. how to regenerate byproducts when they are missing. It is
  41. also useful when other build rules (e.g. custom commands)
  42. depend on the byproducts. Ninja requires a build rule for any
  43. generated file on which another rule depends even if there are
  44. order-only dependencies to ensure the byproducts will be
  45. available before their dependents build.
  46. The :ref:`Makefile Generators` will remove ``BYPRODUCTS`` and other
  47. :prop_sf:`GENERATED` files during ``make clean``.
  48. .. versionadded:: 3.20
  49. Arguments to ``BYPRODUCTS`` may use a restricted set of
  50. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  51. :ref:`Target-dependent expressions <Target-Dependent Expressions>`
  52. are not permitted.
  53. .. versionchanged:: 3.28
  54. In custom targets using :ref:`file sets`, byproducts are now
  55. considered private unless they are listed in a non-private file set.
  56. See policy :policy:`CMP0154`.
  57. ``COMMAND``
  58. Specify the command-line(s) to execute at build time.
  59. If more than one ``COMMAND`` is specified they will be executed in order,
  60. but *not* necessarily composed into a stateful shell or batch script.
  61. (To run a full script, use the :command:`configure_file` command or the
  62. :command:`file(GENERATE)` command to create it, and then specify
  63. a ``COMMAND`` to launch it.)
  64. If ``COMMAND`` specifies an executable target name (created by the
  65. :command:`add_executable` command), it will automatically be replaced
  66. by the location of the executable created at build time if either of
  67. the following is true:
  68. * The target is not being cross-compiled (i.e. the
  69. :variable:`CMAKE_CROSSCOMPILING` variable is not set to true).
  70. * .. versionadded:: 3.6
  71. The target is being cross-compiled and an emulator is provided (i.e.
  72. its :prop_tgt:`CROSSCOMPILING_EMULATOR` target property is set).
  73. In this case, the contents of :prop_tgt:`CROSSCOMPILING_EMULATOR` will be
  74. prepended to the command before the location of the target executable.
  75. If neither of the above conditions are met, it is assumed that the
  76. command name is a program to be found on the ``PATH`` at build time.
  77. Arguments to ``COMMAND`` may use
  78. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  79. Use the :genex:`TARGET_FILE` generator expression to refer to the location
  80. of a target later in the command line (i.e. as a command argument rather
  81. than as the command to execute).
  82. Whenever one of the following target based generator expressions are used as
  83. a command to execute or is mentioned in a command argument, a target-level
  84. dependency will be added automatically so that the mentioned target will be
  85. built before this custom target (see policy :policy:`CMP0112`).
  86. * ``TARGET_FILE``
  87. * ``TARGET_LINKER_FILE``
  88. * ``TARGET_SONAME_FILE``
  89. * ``TARGET_PDB_FILE``
  90. The command and arguments are optional and if not specified an empty
  91. target will be created.
  92. ``COMMENT``
  93. Display the given message before the commands are executed at
  94. build time.
  95. .. versionadded:: 3.26
  96. Arguments to ``COMMENT`` may use
  97. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  98. ``DEPENDS``
  99. Reference files and outputs of custom commands created with
  100. :command:`add_custom_command` command calls in the same directory
  101. (``CMakeLists.txt`` file). They will be brought up to date when
  102. the target is built.
  103. .. versionchanged:: 3.16
  104. A target-level dependency is added if any dependency is a byproduct
  105. of a target or any of its build events in the same directory to ensure
  106. the byproducts will be available before this target is built.
  107. Use the :command:`add_dependencies` command to add dependencies
  108. on other targets.
  109. ``COMMAND_EXPAND_LISTS``
  110. .. versionadded:: 3.8
  111. Lists in ``COMMAND`` arguments will be expanded, including those
  112. created with
  113. :manual:`generator expressions <cmake-generator-expressions(7)>`,
  114. allowing ``COMMAND`` arguments such as
  115. ``${CC} "-I$<JOIN:$<TARGET_PROPERTY:foo,INCLUDE_DIRECTORIES>,;-I>" foo.cc``
  116. to be properly expanded.
  117. ``JOB_POOL``
  118. .. versionadded:: 3.15
  119. Specify a :prop_gbl:`pool <JOB_POOLS>` for the :generator:`Ninja`
  120. generator. Incompatible with ``USES_TERMINAL``, which implies
  121. the ``console`` pool.
  122. Using a pool that is not defined by :prop_gbl:`JOB_POOLS` causes
  123. an error by ninja at build time.
  124. ``JOB_SERVER_AWARE``
  125. .. versionadded:: 3.28
  126. Specify that the command is GNU Make job server aware.
  127. For the :generator:`Unix Makefiles`, :generator:`MSYS Makefiles`, and
  128. :generator:`MinGW Makefiles` generators this will add the ``+`` prefix to the
  129. recipe line. See the `GNU Make Documentation`_ for more information.
  130. This option is silently ignored by other generators.
  131. .. _`GNU Make Documentation`: https://www.gnu.org/software/make/manual/html_node/MAKE-Variable.html
  132. ``SOURCES``
  133. Specify additional source files to be included in the custom target.
  134. Specified source files will be added to IDE project files for
  135. convenience in editing even if they have no build rules.
  136. ``VERBATIM``
  137. All arguments to the commands will be escaped properly for the
  138. build tool so that the invoked command receives each argument
  139. unchanged. Note that one level of escapes is still used by the
  140. CMake language processor before ``add_custom_target`` even sees
  141. the arguments. Use of ``VERBATIM`` is recommended as it enables
  142. correct behavior. When ``VERBATIM`` is not given the behavior
  143. is platform specific because there is no protection of
  144. tool-specific special characters.
  145. ``USES_TERMINAL``
  146. .. versionadded:: 3.2
  147. The command will be given direct access to the terminal if possible.
  148. With the :generator:`Ninja` generator, this places the command in
  149. the ``console`` :prop_gbl:`pool <JOB_POOLS>`.
  150. ``WORKING_DIRECTORY``
  151. Execute the command with the given current working directory.
  152. If it is a relative path it will be interpreted relative to the
  153. build tree directory corresponding to the current source directory.
  154. .. versionadded:: 3.13
  155. Arguments to ``WORKING_DIRECTORY`` may use
  156. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  157. Ninja Multi-Config
  158. ^^^^^^^^^^^^^^^^^^
  159. .. versionadded:: 3.20
  160. ``add_custom_target`` supports the :generator:`Ninja Multi-Config`
  161. generator's cross-config capabilities. See the generator documentation
  162. for more information.
  163. See Also
  164. ^^^^^^^^
  165. * :command:`add_custom_command`