try_run.rst 5.1 KB

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