execute_process.rst 5.7 KB

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