add_custom_target.rst 7.1 KB

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