try_run.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. try_run
  2. -------
  3. .. only:: html
  4. .. contents::
  5. Try compiling and then running some code.
  6. Try Compiling and Running Source Files
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. .. code-block:: cmake
  9. try_run(<runResultVar> <compileResultVar>
  10. <SOURCES <srcfile...> |
  11. SOURCE_FROM_CONTENT <name> <content> |
  12. SOURCE_FROM_VAR <name> <var> |
  13. SOURCE_FROM_FILE <name> <path> >...
  14. [LOG_DESCRIPTION <text>]
  15. [NO_CACHE]
  16. [NO_LOG]
  17. [CMAKE_FLAGS <flags>...]
  18. [COMPILE_DEFINITIONS <defs>...]
  19. [LINK_OPTIONS <options>...]
  20. [LINK_LIBRARIES <libs>...]
  21. [COMPILE_OUTPUT_VARIABLE <var>]
  22. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  23. [<LANG>_STANDARD <std>]
  24. [<LANG>_STANDARD_REQUIRED <bool>]
  25. [<LANG>_EXTENSIONS <bool>]
  26. [RUN_OUTPUT_VARIABLE <var>]
  27. [RUN_OUTPUT_STDOUT_VARIABLE <var>]
  28. [RUN_OUTPUT_STDERR_VARIABLE <var>]
  29. [OUTPUT_VARIABLE <var>]
  30. [WORKING_DIRECTORY <var>]
  31. [ARGS <args>...]
  32. )
  33. .. versionadded:: 3.25
  34. Try compiling a ``<srcfile>``. Returns ``TRUE`` or ``FALSE`` for success
  35. or failure in ``<compileResultVar>``. If the compile succeeded, runs the
  36. executable and returns its exit code in ``<runResultVar>``. If the
  37. executable was built, but failed to run, then ``<runResultVar>`` will be
  38. set to ``FAILED_TO_RUN``. See the :command:`try_compile` command for
  39. documentation of options common to both commands, and for information on how
  40. the test project is constructed to build the source file.
  41. One or more source files must be provided. Additionally, one of ``SOURCES``
  42. and/or ``SOURCE_FROM_*`` must precede other keywords.
  43. .. versionadded:: 3.26
  44. This command records a
  45. :ref:`configure-log try_run event <try_run configure-log event>`
  46. if the ``NO_LOG`` option is not specified.
  47. This command also supports an alternate signature
  48. which was present in older versions of CMake:
  49. .. code-block:: cmake
  50. try_run(<runResultVar> <compileResultVar>
  51. <bindir> <srcfile|SOURCES srcfile...>
  52. [LOG_DESCRIPTION <text>]
  53. [NO_CACHE]
  54. [NO_LOG]
  55. [CMAKE_FLAGS <flags>...]
  56. [COMPILE_DEFINITIONS <defs>...]
  57. [LINK_OPTIONS <options>...]
  58. [LINK_LIBRARIES <libs>...]
  59. [COMPILE_OUTPUT_VARIABLE <var>]
  60. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  61. [<LANG>_STANDARD <std>]
  62. [<LANG>_STANDARD_REQUIRED <bool>]
  63. [<LANG>_EXTENSIONS <bool>]
  64. [RUN_OUTPUT_VARIABLE <var>]
  65. [RUN_OUTPUT_STDOUT_VARIABLE <var>]
  66. [RUN_OUTPUT_STDERR_VARIABLE <var>]
  67. [OUTPUT_VARIABLE <var>]
  68. [WORKING_DIRECTORY <var>]
  69. [ARGS <args>...]
  70. )
  71. The options specific to ``try_run`` are:
  72. ``COMPILE_OUTPUT_VARIABLE <var>``
  73. Report the compile step build output in a given variable.
  74. ``OUTPUT_VARIABLE <var>``
  75. Report the compile build output and the output from running the executable
  76. in the given variable. This option exists for legacy reasons and is only
  77. supported by the old ``try_run`` signature.
  78. Prefer ``COMPILE_OUTPUT_VARIABLE`` and ``RUN_OUTPUT_VARIABLE`` instead.
  79. ``RUN_OUTPUT_VARIABLE <var>``
  80. Report the output from running the executable in a given variable.
  81. ``RUN_OUTPUT_STDOUT_VARIABLE <var>``
  82. .. versionadded:: 3.25
  83. Report the output of stdout from running the executable in a given variable.
  84. ``RUN_OUTPUT_STDERR_VARIABLE <var>``
  85. .. versionadded:: 3.25
  86. Report the output of stderr from running the executable in a given variable.
  87. ``WORKING_DIRECTORY <var>``
  88. .. versionadded:: 3.20
  89. Run the executable in the given directory. If no ``WORKING_DIRECTORY`` is
  90. specified, the executable will run in ``<bindir>`` or the current build
  91. directory.
  92. ``ARGS <args>...``
  93. Additional arguments to pass to the executable when running it.
  94. Other Behavior Settings
  95. ^^^^^^^^^^^^^^^^^^^^^^^
  96. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  97. a build configuration.
  98. Behavior when Cross Compiling
  99. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  100. .. versionadded:: 3.3
  101. Use ``CMAKE_CROSSCOMPILING_EMULATOR`` when running cross-compiled
  102. binaries.
  103. When cross compiling, the executable compiled in the first step
  104. usually cannot be run on the build host. The ``try_run`` command checks
  105. the :variable:`CMAKE_CROSSCOMPILING` variable to detect whether CMake is in
  106. cross-compiling mode. If that is the case, it will still try to compile
  107. the executable, but it will not try to run the executable unless the
  108. :variable:`CMAKE_CROSSCOMPILING_EMULATOR` variable is set. Instead it
  109. will create cache variables which must be filled by the user or by
  110. presetting them in some CMake script file to the values the executable
  111. would have produced if it had been run on its actual target platform.
  112. These cache entries are:
  113. ``<runResultVar>``
  114. Exit code if the executable were to be run on the target platform.
  115. ``<runResultVar>__TRYRUN_OUTPUT``
  116. Output from stdout and stderr if the executable were to be run on
  117. the target platform. This is created only if the
  118. ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` option was used.
  119. In order to make cross compiling your project easier, use ``try_run``
  120. only if really required. If you use ``try_run``, use the
  121. ``RUN_OUTPUT_STDOUT_VARIABLE``, ``RUN_OUTPUT_STDERR_VARIABLE``,
  122. ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` options only if really
  123. required. Using them will require that when cross-compiling, the cache
  124. variables will have to be set manually to the output of the executable.
  125. You can also "guard" the calls to ``try_run`` with an :command:`if`
  126. block checking the :variable:`CMAKE_CROSSCOMPILING` variable and
  127. provide an easy-to-preset alternative for this case.