add_custom_target.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. add_custom_target
  2. -----------------
  3. Add a target with no output so it will always be built.
  4. ::
  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. [VERBATIM] [USES_TERMINAL]
  12. [SOURCES src1 [src2...]])
  13. Adds a target with the given name that executes the given commands.
  14. The target has no output file and is *always considered out of date*
  15. even if the commands try to create a file with the name of the target.
  16. Use the :command:`add_custom_command` command to generate a file with
  17. dependencies. By default nothing depends on the custom target. Use
  18. the :command:`add_dependencies` command to add dependencies to or
  19. from other targets.
  20. The options are:
  21. ``ALL``
  22. Indicate that this target should be added to the default build
  23. target so that it will be run every time (the command cannot be
  24. called ``ALL``).
  25. ``BYPRODUCTS``
  26. Specify the files the command is expected to produce but whose
  27. modification time may or may not be updated on subsequent builds.
  28. If a byproduct name is a relative path it will be interpreted
  29. relative to the build tree directory corresponding to the
  30. current source directory.
  31. Each byproduct file will be marked with the :prop_sf:`GENERATED`
  32. source file property automatically.
  33. Explicit specification of byproducts is supported by the
  34. :generator:`Ninja` generator to tell the ``ninja`` build tool
  35. how to regenerate byproducts when they are missing. It is
  36. also useful when other build rules (e.g. custom commands)
  37. depend on the byproducts. Ninja requires a build rule for any
  38. generated file on which another rule depends even if there are
  39. order-only dependencies to ensure the byproducts will be
  40. available before their dependents build.
  41. The ``BYPRODUCTS`` option is ignored on non-Ninja generators
  42. except to mark byproducts ``GENERATED``.
  43. ``COMMAND``
  44. Specify the command-line(s) to execute at build time.
  45. If more than one ``COMMAND`` is specified they will be executed in order,
  46. but *not* necessarily composed into a stateful shell or batch script.
  47. (To run a full script, use the :command:`configure_file` command or the
  48. :command:`file(GENERATE)` command to create it, and then specify
  49. a ``COMMAND`` to launch it.)
  50. If ``COMMAND`` specifies an executable target (created by the
  51. :command:`add_executable` command) it will automatically be replaced
  52. by the location of the executable created at build time.
  53. Additionally a target-level dependency will be added so that the
  54. executable target will be built before this custom target.
  55. Arguments to ``COMMAND`` may use
  56. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  57. References to target names in generator expressions imply target-level
  58. dependencies.
  59. The command and arguments are optional and if not specified an empty
  60. target will be created.
  61. ``COMMENT``
  62. Display the given message before the commands are executed at
  63. build time.
  64. ``DEPENDS``
  65. Reference files and outputs of custom commands created with
  66. :command:`add_custom_command` command calls in the same directory
  67. (``CMakeLists.txt`` file). They will be brought up to date when
  68. the target is built.
  69. Use the :command:`add_dependencies` command to add dependencies
  70. on other targets.
  71. ``SOURCES``
  72. Specify additional source files to be included in the custom target.
  73. Specified source files will be added to IDE project files for
  74. convenience in editing even if they have no build rules.
  75. ``VERBATIM``
  76. All arguments to the commands will be escaped properly for the
  77. build tool so that the invoked command receives each argument
  78. unchanged. Note that one level of escapes is still used by the
  79. CMake language processor before ``add_custom_target`` even sees
  80. the arguments. Use of ``VERBATIM`` is recommended as it enables
  81. correct behavior. When ``VERBATIM`` is not given the behavior
  82. is platform specific because there is no protection of
  83. tool-specific special characters.
  84. ``USES_TERMINAL``
  85. The command will be given direct access to the terminal if possible.
  86. With the :generator:`Ninja` generator, this places the command in
  87. the ``console`` :prop_gbl:`pool <JOB_POOLS>`.
  88. ``WORKING_DIRECTORY``
  89. Execute the command with the given current working directory.
  90. If it is a relative path it will be interpreted relative to the
  91. build tree directory corresponding to the current source directory.