try_run.rst 6.4 KB

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