try_run.rst 4.3 KB

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