try_run.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. [LINKER_LANGUAGE <lang>]
  58. [COMPILE_OUTPUT_VARIABLE <var>]
  59. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  60. [<LANG>_STANDARD <std>]
  61. [<LANG>_STANDARD_REQUIRED <bool>]
  62. [<LANG>_EXTENSIONS <bool>]
  63. [RUN_OUTPUT_VARIABLE <var>]
  64. [OUTPUT_VARIABLE <var>]
  65. [WORKING_DIRECTORY <var>]
  66. [ARGS <args>...]
  67. )
  68. .. _`try_run Options`:
  69. Options
  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 variable :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` to choose a build
  97. configuration:
  98. * For multi-config generators, this selects which configuration to build.
  99. * For single-config generators, this sets :variable:`CMAKE_BUILD_TYPE` in
  100. the test project.
  101. Behavior when Cross Compiling
  102. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  103. .. versionadded:: 3.3
  104. Use ``CMAKE_CROSSCOMPILING_EMULATOR`` when running cross-compiled binaries.
  105. When cross compiling, the executable compiled in the first step
  106. usually cannot be run on the build host. The ``try_run`` command checks
  107. the :variable:`CMAKE_CROSSCOMPILING` variable to detect whether CMake is in
  108. cross-compiling mode. If that is the case, it will still try to compile
  109. the executable, but it will not try to run the executable unless the
  110. :variable:`CMAKE_CROSSCOMPILING_EMULATOR` variable is set. Instead it
  111. will create cache variables which must be filled by the user or by
  112. presetting them in some CMake script file to the values the executable
  113. would have produced if it had been run on its actual target platform.
  114. These cache entries are:
  115. ``<runResultVar>``
  116. Exit code if the executable were to be run on the target platform.
  117. ``<runResultVar>__TRYRUN_OUTPUT``
  118. Output from stdout and stderr if the executable were to be run on
  119. the target platform. This is created only if the
  120. ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` option was used.
  121. In order to make cross compiling your project easier, use ``try_run``
  122. only if really required. If you use ``try_run``, use the
  123. ``RUN_OUTPUT_STDOUT_VARIABLE``, ``RUN_OUTPUT_STDERR_VARIABLE``,
  124. ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` options only if really
  125. required. Using them will require that when cross-compiling, the cache
  126. variables will have to be set manually to the output of the executable.
  127. You can also "guard" the calls to ``try_run`` with an :command:`if`
  128. block checking the :variable:`CMAKE_CROSSCOMPILING` variable and
  129. provide an easy-to-preset alternative for this case.