try_run.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. <bindir> <srcfile> [CMAKE_FLAGS <flags>...]
  11. [COMPILE_DEFINITIONS <defs>...]
  12. [LINK_OPTIONS <options>...]
  13. [LINK_LIBRARIES <libs>...]
  14. [COMPILE_OUTPUT_VARIABLE <var>]
  15. [RUN_OUTPUT_VARIABLE <var>]
  16. [RUN_OUTPUT_STDOUT_VARIABLE <var>]
  17. [RUN_OUTPUT_STDERR_VARIABLE <var>]
  18. [OUTPUT_VARIABLE <var>]
  19. [WORKING_DIRECTORY <var>]
  20. [ARGS <args>...])
  21. Try compiling a ``<srcfile>``. Returns ``TRUE`` or ``FALSE`` for success
  22. or failure in ``<compileResultVar>``. If the compile succeeded, runs the
  23. executable and returns its exit code in ``<runResultVar>``. If the
  24. executable was built, but failed to run, then ``<runResultVar>`` will be
  25. set to ``FAILED_TO_RUN``. See the :command:`try_compile` command for
  26. information on how the test project is constructed to build the source file.
  27. The options are:
  28. ``CMAKE_FLAGS <flags>...``
  29. Specify flags of the form ``-DVAR:TYPE=VALUE`` to be passed to
  30. the ``cmake`` command-line used to drive the test build.
  31. The example in :command:`try_compile` shows how values for variables
  32. ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
  33. are used.
  34. ``COMPILE_DEFINITIONS <defs>...``
  35. Specify ``-Ddefinition`` arguments to pass to :command:`add_definitions`
  36. in the generated test project.
  37. ``COMPILE_OUTPUT_VARIABLE <var>``
  38. Report the compile step build output in a given variable.
  39. ``LINK_LIBRARIES <libs>...``
  40. .. versionadded:: 3.2
  41. Specify libraries to be linked in the generated project.
  42. The list of libraries may refer to system libraries and to
  43. :ref:`Imported Targets <Imported Targets>` from the calling project.
  44. If this option is specified, any ``-DLINK_LIBRARIES=...`` value
  45. given to the ``CMAKE_FLAGS`` option will be ignored.
  46. ``LINK_OPTIONS <options>...``
  47. .. versionadded:: 3.14
  48. Specify link step options to pass to :command:`target_link_options` in the
  49. generated project.
  50. ``OUTPUT_VARIABLE <var>``
  51. Report the compile build output and the output from running the executable
  52. in the given variable. This option exists for legacy reasons. Prefer
  53. ``COMPILE_OUTPUT_VARIABLE`` and ``RUN_OUTPUT_VARIABLE`` instead.
  54. ``RUN_OUTPUT_VARIABLE <var>``
  55. Report the output from running the executable in a given variable.
  56. ``RUN_OUTPUT_STDOUT_VARIABLE <var>``
  57. .. versionadded:: 3.25
  58. Report the output of stdout from running the executable in a given variable.
  59. ``RUN_OUTPUT_STDERR_VARIABLE <var>``
  60. .. versionadded:: 3.25
  61. Report the output of stderr from running the executable in a given variable.
  62. ``WORKING_DIRECTORY <var>``
  63. .. versionadded:: 3.20
  64. Run the executable in the given directory. If no ``WORKING_DIRECTORY`` is
  65. specified, the executable will run in ``<bindir>``.
  66. Other Behavior Settings
  67. ^^^^^^^^^^^^^^^^^^^^^^^
  68. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  69. a build configuration.
  70. Behavior when Cross Compiling
  71. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  72. .. versionadded:: 3.3
  73. Use ``CMAKE_CROSSCOMPILING_EMULATOR`` when running cross-compiled
  74. binaries.
  75. When cross compiling, the executable compiled in the first step
  76. usually cannot be run on the build host. The ``try_run`` command checks
  77. the :variable:`CMAKE_CROSSCOMPILING` variable to detect whether CMake is in
  78. cross-compiling mode. If that is the case, it will still try to compile
  79. the executable, but it will not try to run the executable unless the
  80. :variable:`CMAKE_CROSSCOMPILING_EMULATOR` variable is set. Instead it
  81. will create cache variables which must be filled by the user or by
  82. presetting them in some CMake script file to the values the executable
  83. would have produced if it had been run on its actual target platform.
  84. These cache entries are:
  85. ``<runResultVar>``
  86. Exit code if the executable were to be run on the target platform.
  87. ``<runResultVar>__TRYRUN_OUTPUT``
  88. Output from stdout and stderr if the executable were to be run on
  89. the target platform. This is created only if the
  90. ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` option was used.
  91. In order to make cross compiling your project easier, use ``try_run``
  92. only if really required. If you use ``try_run``, use the
  93. ``RUN_OUTPUT_STDOUT_VARIABLE``, ``RUN_OUTPUT_STDERR_VARIABLE``,
  94. ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` options only if really
  95. required. Using them will require that when cross-compiling, the cache
  96. variables will have to be set manually to the output of the executable.
  97. You can also "guard" the calls to ``try_run`` with an :command:`if`
  98. block checking the :variable:`CMAKE_CROSSCOMPILING` variable and
  99. provide an easy-to-preset alternative for this case.