execute_process.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. execute_process
  2. ---------------
  3. Execute one or more child processes.
  4. .. code-block:: cmake
  5. execute_process(COMMAND <cmd1> [<arguments>]
  6. [COMMAND <cmd2> [<arguments>]]...
  7. [WORKING_DIRECTORY <directory>]
  8. [TIMEOUT <seconds>]
  9. [RESULT_VARIABLE <variable>]
  10. [RESULTS_VARIABLE <variable>]
  11. [OUTPUT_VARIABLE <variable>]
  12. [ERROR_VARIABLE <variable>]
  13. [INPUT_FILE <file>]
  14. [OUTPUT_FILE <file>]
  15. [ERROR_FILE <file>]
  16. [OUTPUT_QUIET]
  17. [ERROR_QUIET]
  18. [COMMAND_ECHO <where>]
  19. [OUTPUT_STRIP_TRAILING_WHITESPACE]
  20. [ERROR_STRIP_TRAILING_WHITESPACE]
  21. [ENCODING <name>]
  22. [ECHO_OUTPUT_VARIABLE]
  23. [ECHO_ERROR_VARIABLE]
  24. [COMMAND_ERROR_IS_FATAL <ANY|LAST>])
  25. Runs the given sequence of one or more commands.
  26. Commands are executed concurrently as a pipeline, with the standard
  27. output of each process piped to the standard input of the next.
  28. A single standard error pipe is used for all processes.
  29. ``execute_process`` runs commands while CMake is configuring the project,
  30. prior to build system generation. Use the :command:`add_custom_target` and
  31. :command:`add_custom_command` commands to create custom commands that run
  32. at build time.
  33. Options:
  34. ``COMMAND``
  35. A child process command line.
  36. CMake executes the child process using operating system APIs directly:
  37. * On POSIX platforms, the command line is passed to the
  38. child process in an ``argv[]`` style array.
  39. * On Windows platforms, the command line is encoded as a string such
  40. that child processes using ``CommandLineToArgvW`` will decode the
  41. original arguments.
  42. No intermediate shell is used, so shell operators such as ``>``
  43. are treated as normal arguments.
  44. (Use the ``INPUT_*``, ``OUTPUT_*``, and ``ERROR_*`` options to
  45. redirect stdin, stdout, and stderr.)
  46. For **sequential execution** of multiple commands use multiple
  47. ``execute_process`` calls each with a single ``COMMAND`` argument.
  48. ``WORKING_DIRECTORY``
  49. The named directory will be set as the current working directory of
  50. the child processes.
  51. ``TIMEOUT``
  52. After the specified number of seconds (fractions allowed), all unfinished
  53. child processes will be terminated, and the ``RESULT_VARIABLE`` will be
  54. set to a string mentioning the "timeout".
  55. ``RESULT_VARIABLE``
  56. The variable will be set to contain the result of last child process.
  57. This will be an integer return code from the last child or a string
  58. describing an error condition.
  59. ``RESULTS_VARIABLE <variable>``
  60. .. versionadded:: 3.10
  61. The variable will be set to contain the result of all processes as a
  62. :ref:`semicolon-separated list <CMake Language Lists>`, in order of the
  63. given ``COMMAND`` arguments. Each entry will be an integer return code
  64. from the corresponding child or a string describing an error condition.
  65. ``INPUT_FILE <file>``
  66. ``<file>`` is attached to the standard input pipe of the *first* ``COMMAND``
  67. process.
  68. ``OUTPUT_FILE <file>``
  69. ``<file>`` is attached to the standard output pipe of the *last* ``COMMAND``
  70. process.
  71. ``ERROR_FILE <file>``
  72. ``<file>`` is attached to the standard error pipe of *all* ``COMMAND``
  73. processes.
  74. .. versionadded:: 3.3
  75. If the same ``<file>`` is named for both ``OUTPUT_FILE`` and ``ERROR_FILE``
  76. then it will be used for both standard output and standard error pipes.
  77. ``OUTPUT_QUIET``, ``ERROR_QUIET``
  78. The standard output on ``OUTPUT_VARIABLE`` or standard error on
  79. ``ERROR_VARIABLE`` are not connected (no variable content).
  80. The ``*_FILE`` and ``ECHO_*_VARIABLE`` options are not affected.
  81. ``OUTPUT_VARIABLE``, ``ERROR_VARIABLE``
  82. The variable named will be set with the contents of the standard output
  83. and standard error pipes, respectively. If the same variable is named
  84. for both pipes their output will be merged in the order produced.
  85. ``ECHO_OUTPUT_VARIABLE``, ``ECHO_ERROR_VARIABLE``
  86. .. versionadded:: 3.18
  87. The standard output or standard error will not be exclusively redirected to
  88. the specified variables.
  89. The output will be duplicated into the specified variables and also onto
  90. standard output or standard error analogous to the ``tee`` Unix command.
  91. .. note::
  92. If more than one ``OUTPUT_*`` or ``ERROR_*`` option is given for the
  93. same pipe the precedence is *not specified*.
  94. If no ``OUTPUT_*`` or ``ERROR_*`` options are given the output will
  95. be shared with the corresponding pipes of the CMake process itself.
  96. ``COMMAND_ECHO <where>``
  97. .. versionadded:: 3.15
  98. The command being run will be echo'ed to ``<where>`` with ``<where>``
  99. being set to one of ``STDERR``, ``STDOUT`` or ``NONE``.
  100. See the :variable:`CMAKE_EXECUTE_PROCESS_COMMAND_ECHO` variable for a way
  101. to control the default behavior when this option is not present.
  102. ``ENCODING <name>``
  103. .. versionadded:: 3.8
  104. On Windows, the encoding that is used to decode output from the process.
  105. Ignored on other platforms.
  106. Valid encoding names are:
  107. ``NONE``
  108. Perform no decoding. This assumes that the process output is encoded
  109. in the same way as CMake's internal encoding (UTF-8).
  110. This was the default in CMake 3.14 and older.
  111. ``AUTO``
  112. Use the current active console's codepage or if that isn't
  113. available then use ANSI.
  114. This was the default in CMake 3.15 through 3.30.
  115. ``ANSI``
  116. Use the ANSI codepage.
  117. ``OEM``
  118. Use the original equipment manufacturer (OEM) code page.
  119. ``UTF-8``
  120. .. versionadded:: 3.11
  121. Use the UTF-8 codepage.
  122. This is the default. See policy :policy:`CMP0176`.
  123. ``UTF8``
  124. Use the UTF-8 codepage. Use of this name is discouraged in favor
  125. of ``UTF-8`` to match the `UTF-8 RFC <https://www.ietf.org/rfc/rfc3629>`_
  126. naming convention.
  127. ``COMMAND_ERROR_IS_FATAL <ANY|LAST>``
  128. .. versionadded:: 3.19
  129. The option following ``COMMAND_ERROR_IS_FATAL`` determines the behavior when
  130. an error is encountered:
  131. ``ANY``
  132. If any of the commands in the list of commands fail, the
  133. ``execute_process()`` command halts with an error.
  134. ``LAST``
  135. If the last command in the list of commands fails, the
  136. ``execute_process()`` command halts with an error. Commands earlier in the
  137. list will not cause a fatal error.