try_run.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. <SOURCES <srcfile...>] |
  11. SOURCE_FROM_ARG <name> <content>] |
  12. SOURCE_FROM_VAR <name> <var>] |
  13. SOURCE_FROM_FILE <name> <path> >...
  14. [CMAKE_FLAGS <flags>...]
  15. [COMPILE_DEFINITIONS <defs>...]
  16. [LINK_OPTIONS <options>...]
  17. [LINK_LIBRARIES <libs>...]
  18. [COMPILE_OUTPUT_VARIABLE <var>]
  19. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  20. [<LANG>_STANDARD <std>]
  21. [<LANG>_STANDARD_REQUIRED <bool>]
  22. [<LANG>_EXTENSIONS <bool>]
  23. [RUN_OUTPUT_VARIABLE <var>]
  24. [RUN_OUTPUT_STDOUT_VARIABLE <var>]
  25. [RUN_OUTPUT_STDERR_VARIABLE <var>]
  26. [OUTPUT_VARIABLE <var>]
  27. [WORKING_DIRECTORY <var>]
  28. [ARGS <args>...]
  29. )
  30. .. versionadded:: 3.25
  31. Try compiling a ``<srcfile>``. Returns ``TRUE`` or ``FALSE`` for success
  32. or failure in ``<compileResultVar>``. If the compile succeeded, runs the
  33. executable and returns its exit code in ``<runResultVar>``. If the
  34. executable was built, but failed to run, then ``<runResultVar>`` will be
  35. set to ``FAILED_TO_RUN``. See the :command:`try_compile` command for
  36. documentation of options common to both commands, and for information on how
  37. the test project is constructed to build the source file.
  38. One or more source files must be provided. Additionally, one of ``SOURCES``
  39. and/or ``SOURCE_FROM_*`` must precede other keywords.
  40. This command also supports an alternate signature
  41. which was present in older versions of CMake:
  42. .. code-block:: cmake
  43. try_run(<runResultVar> <compileResultVar>
  44. <bindir> <srcfile|SOURCES srcfile...>
  45. [CMAKE_FLAGS <flags>...]
  46. [COMPILE_DEFINITIONS <defs>...]
  47. [LINK_OPTIONS <options>...]
  48. [LINK_LIBRARIES <libs>...]
  49. [COMPILE_OUTPUT_VARIABLE <var>]
  50. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  51. [<LANG>_STANDARD <std>]
  52. [<LANG>_STANDARD_REQUIRED <bool>]
  53. [<LANG>_EXTENSIONS <bool>]
  54. [RUN_OUTPUT_VARIABLE <var>]
  55. [RUN_OUTPUT_STDOUT_VARIABLE <var>]
  56. [RUN_OUTPUT_STDERR_VARIABLE <var>]
  57. [OUTPUT_VARIABLE <var>]
  58. [WORKING_DIRECTORY <var>]
  59. [ARGS <args>...]
  60. )
  61. The options specific to ``try_run`` are:
  62. ``COMPILE_OUTPUT_VARIABLE <var>``
  63. Report the compile step build output in a given variable.
  64. ``OUTPUT_VARIABLE <var>``
  65. Report the compile build output and the output from running the executable
  66. in the given variable. This option exists for legacy reasons and is only
  67. supported by the old ``try_run`` signature.
  68. Prefer ``COMPILE_OUTPUT_VARIABLE`` and ``RUN_OUTPUT_VARIABLE`` instead.
  69. ``RUN_OUTPUT_VARIABLE <var>``
  70. Report the output from running the executable in a given variable.
  71. ``RUN_OUTPUT_STDOUT_VARIABLE <var>``
  72. .. versionadded:: 3.25
  73. Report the output of stdout from running the executable in a given variable.
  74. ``RUN_OUTPUT_STDERR_VARIABLE <var>``
  75. .. versionadded:: 3.25
  76. Report the output of stderr from running the executable in a given variable.
  77. ``WORKING_DIRECTORY <var>``
  78. .. versionadded:: 3.20
  79. Run the executable in the given directory. If no ``WORKING_DIRECTORY`` is
  80. specified, the executable will run in ``<bindir>`` or the current build
  81. directory.
  82. ``ARGS <args>...``
  83. Additional arguments to pass to the executable when running it.
  84. Other Behavior Settings
  85. ^^^^^^^^^^^^^^^^^^^^^^^
  86. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  87. a build configuration.
  88. Behavior when Cross Compiling
  89. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  90. .. versionadded:: 3.3
  91. Use ``CMAKE_CROSSCOMPILING_EMULATOR`` when running cross-compiled
  92. binaries.
  93. When cross compiling, the executable compiled in the first step
  94. usually cannot be run on the build host. The ``try_run`` command checks
  95. the :variable:`CMAKE_CROSSCOMPILING` variable to detect whether CMake is in
  96. cross-compiling mode. If that is the case, it will still try to compile
  97. the executable, but it will not try to run the executable unless the
  98. :variable:`CMAKE_CROSSCOMPILING_EMULATOR` variable is set. Instead it
  99. will create cache variables which must be filled by the user or by
  100. presetting them in some CMake script file to the values the executable
  101. would have produced if it had been run on its actual target platform.
  102. These cache entries are:
  103. ``<runResultVar>``
  104. Exit code if the executable were to be run on the target platform.
  105. ``<runResultVar>__TRYRUN_OUTPUT``
  106. Output from stdout and stderr if the executable were to be run on
  107. the target platform. This is created only if the
  108. ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` option was used.
  109. In order to make cross compiling your project easier, use ``try_run``
  110. only if really required. If you use ``try_run``, use the
  111. ``RUN_OUTPUT_STDOUT_VARIABLE``, ``RUN_OUTPUT_STDERR_VARIABLE``,
  112. ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` options only if really
  113. required. Using them will require that when cross-compiling, the cache
  114. variables will have to be set manually to the output of the executable.
  115. You can also "guard" the calls to ``try_run`` with an :command:`if`
  116. block checking the :variable:`CMAKE_CROSSCOMPILING` variable and
  117. provide an easy-to-preset alternative for this case.