add_custom_target.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. Specify the files the command is expected to produce but whose
  29. modification time may or may not be updated on subsequent builds.
  30. If a byproduct name is a relative path it will be interpreted
  31. relative to the build tree directory corresponding to the
  32. current source directory.
  33. Each byproduct file will be marked with the :prop_sf:`GENERATED`
  34. source file property automatically.
  35. Explicit specification of byproducts is supported by the
  36. :generator:`Ninja` generator to tell the ``ninja`` build tool
  37. how to regenerate byproducts when they are missing. It is
  38. also useful when other build rules (e.g. custom commands)
  39. depend on the byproducts. Ninja requires a build rule for any
  40. generated file on which another rule depends even if there are
  41. order-only dependencies to ensure the byproducts will be
  42. available before their dependents build.
  43. The ``BYPRODUCTS`` option is ignored on non-Ninja generators
  44. except to mark byproducts ``GENERATED``.
  45. ``COMMAND``
  46. Specify the command-line(s) to execute at build time.
  47. If more than one ``COMMAND`` is specified they will be executed in order,
  48. but *not* necessarily composed into a stateful shell or batch script.
  49. (To run a full script, use the :command:`configure_file` command or the
  50. :command:`file(GENERATE)` command to create it, and then specify
  51. a ``COMMAND`` to launch it.)
  52. If ``COMMAND`` specifies an executable target name (created by the
  53. :command:`add_executable` command) it will automatically be replaced
  54. by the location of the executable created at build time. If set, the
  55. :prop_tgt:`CROSSCOMPILING_EMULATOR` executable target property will
  56. also be prepended to the command to allow the executable to run on
  57. the host.
  58. Additionally a target-level dependency will be added so that the
  59. executable target will be built before this custom target.
  60. Arguments to ``COMMAND`` may use
  61. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  62. References to target names in generator expressions imply target-level
  63. dependencies.
  64. The command and arguments are optional and if not specified an empty
  65. target will be created.
  66. ``COMMENT``
  67. Display the given message before the commands are executed at
  68. build time.
  69. ``DEPENDS``
  70. Reference files and outputs of custom commands created with
  71. :command:`add_custom_command` command calls in the same directory
  72. (``CMakeLists.txt`` file). They will be brought up to date when
  73. the target is built.
  74. Use the :command:`add_dependencies` command to add dependencies
  75. on other targets.
  76. ``COMMAND_EXPAND_LISTS``
  77. Lists in ``COMMAND`` arguments will be expanded, including those
  78. created with
  79. :manual:`generator expressions <cmake-generator-expressions(7)>`,
  80. allowing ``COMMAND`` arguments such as
  81. ``${CC} "-I$<JOIN:$<TARGET_PROPERTY:foo,INCLUDE_DIRECTORIES>,;-I>" foo.cc``
  82. to be properly expanded.
  83. ``JOB_POOL``
  84. Specify a :prop_gbl:`pool <JOB_POOLS>` for the :generator:`Ninja`
  85. generator. Incompatible with ``USES_TERMINAL``, which implies
  86. the ``console`` pool.
  87. Using a pool that is not defined by :prop_gbl:`JOB_POOLS` causes
  88. an error by ninja at build time.
  89. ``SOURCES``
  90. Specify additional source files to be included in the custom target.
  91. Specified source files will be added to IDE project files for
  92. convenience in editing even if they have no build rules.
  93. ``VERBATIM``
  94. All arguments to the commands will be escaped properly for the
  95. build tool so that the invoked command receives each argument
  96. unchanged. Note that one level of escapes is still used by the
  97. CMake language processor before ``add_custom_target`` even sees
  98. the arguments. Use of ``VERBATIM`` is recommended as it enables
  99. correct behavior. When ``VERBATIM`` is not given the behavior
  100. is platform specific because there is no protection of
  101. tool-specific special characters.
  102. ``USES_TERMINAL``
  103. The command will be given direct access to the terminal if possible.
  104. With the :generator:`Ninja` generator, this places the command in
  105. the ``console`` :prop_gbl:`pool <JOB_POOLS>`.
  106. ``WORKING_DIRECTORY``
  107. Execute the command with the given current working directory.
  108. If it is a relative path it will be interpreted relative to the
  109. build tree directory corresponding to the current source directory.
  110. Arguments to ``WORKING_DIRECTORY`` may use
  111. :manual:`generator expressions <cmake-generator-expressions(7)>`.