try_run.rst 5.8 KB

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