add_custom_command.rst 7.3 KB

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