execute_process.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. Options:
  30. ``COMMAND``
  31. A child process command line.
  32. CMake executes the child process using operating system APIs directly:
  33. * On POSIX platforms, the command line is passed to the
  34. child process in an ``argv[]`` style array.
  35. * On Windows platforms, the command line is encoded as a string such
  36. that child processes using ``CommandLineToArgvW`` will decode the
  37. original arguments.
  38. No intermediate shell is used, so shell operators such as ``>``
  39. are treated as normal arguments.
  40. (Use the ``INPUT_*``, ``OUTPUT_*``, and ``ERROR_*`` options to
  41. redirect stdin, stdout, and stderr.)
  42. If a sequential execution of multiple commands is required, use multiple
  43. :command:`execute_process` calls with a single ``COMMAND`` argument.
  44. ``WORKING_DIRECTORY``
  45. The named directory will be set as the current working directory of
  46. the child processes.
  47. ``TIMEOUT``
  48. After the specified number of seconds (fractions allowed), all unfinished
  49. child processes will be terminated, and the ``RESULT_VARIABLE`` will be
  50. set to a string mentioning the "timeout".
  51. ``RESULT_VARIABLE``
  52. The variable will be set to contain the result of last child process.
  53. This will be an integer return code from the last child or a string
  54. describing an error condition.
  55. ``RESULTS_VARIABLE <variable>``
  56. .. versionadded:: 3.10
  57. The variable will be set to contain the result of all processes as a
  58. :ref:`semicolon-separated list <CMake Language Lists>`, in order of the
  59. given ``COMMAND`` arguments. Each entry will be an integer return code
  60. from the corresponding child or a string describing an error condition.
  61. ``OUTPUT_VARIABLE``, ``ERROR_VARIABLE``
  62. The variable named will be set with the contents of the standard output
  63. and standard error pipes, respectively. If the same variable is named
  64. for both pipes their output will be merged in the order produced.
  65. ``INPUT_FILE, OUTPUT_FILE``, ``ERROR_FILE``
  66. The file named will be attached to the standard input of the first
  67. process, standard output of the last process, or standard error of
  68. all processes, respectively.
  69. .. versionadded:: 3.3
  70. If the same file is named for both output and error then it will be used
  71. for both.
  72. ``OUTPUT_QUIET``, ``ERROR_QUIET``
  73. The standard output or standard error results will be quietly ignored.
  74. ``COMMAND_ECHO <where>``
  75. .. versionadded:: 3.15
  76. The command being run will be echo'ed to ``<where>`` with ``<where>``
  77. being set to one of ``STDERR``, ``STDOUT`` or ``NONE``.
  78. See the :variable:`CMAKE_EXECUTE_PROCESS_COMMAND_ECHO` variable for a way
  79. to control the default behavior when this option is not present.
  80. ``ENCODING <name>``
  81. .. versionadded:: 3.8
  82. On Windows, the encoding that is used to decode output from the process.
  83. Ignored on other platforms.
  84. Valid encoding names are:
  85. ``NONE``
  86. Perform no decoding. This assumes that the process output is encoded
  87. in the same way as CMake's internal encoding (UTF-8).
  88. This is the default.
  89. ``AUTO``
  90. Use the current active console's codepage or if that isn't
  91. available then use ANSI.
  92. ``ANSI``
  93. Use the ANSI codepage.
  94. ``OEM``
  95. Use the original equipment manufacturer (OEM) code page.
  96. ``UTF8`` or ``UTF-8``
  97. Use the UTF-8 codepage.
  98. .. versionadded:: 3.11
  99. Accept ``UTF-8`` spelling for consistency with the
  100. `UTF-8 RFC <https://www.ietf.org/rfc/rfc3629>`_ naming convention.
  101. ``ECHO_OUTPUT_VARIABLE``, ``ECHO_ERROR_VARIABLE``
  102. .. versionadded:: 3.18
  103. The standard output or standard error will not be exclusively redirected to
  104. the configured variables.
  105. The output will be duplicated, it will be sent into the configured variables
  106. and also on standard output or standard error.
  107. This is analogous to the ``tee`` Unix command.
  108. ``COMMAND_ERROR_IS_FATAL <ANY|LAST>``
  109. .. versionadded:: 3.19
  110. The option following ``COMMAND_ERROR_IS_FATAL`` determines the behavior when
  111. an error is encountered:
  112. ``ANY``
  113. If any of the commands in the list of commands fail, the
  114. ``execute_process()`` command halts with an error.
  115. ``LAST``
  116. If the last command in the list of commands fails, the
  117. ``execute_process()`` command halts with an error. Commands earlier in the
  118. list will not cause a fatal error.
  119. If more than one ``OUTPUT_*`` or ``ERROR_*`` option is given for the
  120. same pipe the precedence is not specified.
  121. If no ``OUTPUT_*`` or ``ERROR_*`` options are given the output will
  122. be shared with the corresponding pipes of the CMake process itself.
  123. The :command:`execute_process` command is a newer more powerful version of
  124. :command:`exec_program`, but the old command has been kept for compatibility.
  125. Both commands run while CMake is processing the project prior to build
  126. system generation. Use :command:`add_custom_target` and
  127. :command:`add_custom_command` to create custom commands that run at
  128. build time.