try_run.rst 5.5 KB

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