exec_program.rst 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. exec_program
  2. ------------
  3. .. versionchanged:: 3.28
  4. This command is available only if policy :policy:`CMP0153` is not set to ``NEW``.
  5. Port projects to the :command:`execute_process` command.
  6. .. deprecated:: 3.0
  7. Use the :command:`execute_process` command instead.
  8. Runs an executable program during the processing of a CMake file or script:
  9. .. code-block:: cmake
  10. exec_program(
  11. <executable>
  12. [<working-dir>]
  13. [ARGS <arguments-to-executable>...]
  14. [OUTPUT_VARIABLE <var>]
  15. [RETURN_VALUE <var>]
  16. )
  17. The ``<executable>`` is run in the optionally specified directory
  18. ``<working-dir>``. The
  19. executable can include arguments if it is double quoted, but it is
  20. better to use the optional ``ARGS`` argument to specify arguments to the
  21. executable program. This is because CMake will then be able to escape spaces in
  22. the executable path. An optional argument ``OUTPUT_VARIABLE`` specifies a
  23. variable in which to store the output. To capture the return value of
  24. the execution, provide a ``RETURN_VALUE``. If ``OUTPUT_VARIABLE`` is
  25. specified, then no output will go to the stdout/stderr of the console
  26. running CMake.
  27. Examples
  28. ^^^^^^^^
  29. Example of the legacy ``exec_program()`` command used in earlier versions of
  30. CMake:
  31. .. code-block:: cmake
  32. exec_program(
  33. some_command
  34. ${dir}
  35. ARGS arg_1 arg_2 args "\"<quoted-arg>\""
  36. OUTPUT_VARIABLE output
  37. RETURN_VALUE result
  38. )
  39. A direct equivalent replacement of the previous example using the
  40. :command:`execute_process` command in new code:
  41. .. code-block:: cmake
  42. execute_process(
  43. COMMAND some_command arg_1 arg_2 args "<quoted-arg>"
  44. WORKING_DIRECTORY ${dir}
  45. RESULT_VARIABLE result
  46. OUTPUT_VARIABLE output
  47. ERROR_VARIABLE output
  48. OUTPUT_STRIP_TRAILING_WHITESPACE
  49. )