try_run.rst 4.6 KB

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