try_run.rst 3.8 KB

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