try_run.rst 4.0 KB

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