try_run.rst 4.3 KB

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