try_run.rst 5.4 KB

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