cmake_language.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. cmake_language
  2. --------------
  3. .. versionadded:: 3.18
  4. Call meta-operations on CMake commands.
  5. Synopsis
  6. ^^^^^^^^
  7. .. parsed-literal::
  8. cmake_language(`CALL`_ <command> [<arg>...])
  9. cmake_language(`EVAL`_ CODE <code>...)
  10. cmake_language(`DEFER`_ <options>... CALL <command> [<arg>...])
  11. Introduction
  12. ^^^^^^^^^^^^
  13. This command will call meta-operations on built-in CMake commands or
  14. those created via the :command:`macro` or :command:`function` commands.
  15. ``cmake_language`` does not introduce a new variable or policy scope.
  16. Calling Commands
  17. ^^^^^^^^^^^^^^^^
  18. .. _CALL:
  19. .. code-block:: cmake
  20. cmake_language(CALL <command> [<arg>...])
  21. Calls the named ``<command>`` with the given arguments (if any).
  22. For example, the code:
  23. .. code-block:: cmake
  24. set(message_command "message")
  25. cmake_language(CALL ${message_command} STATUS "Hello World!")
  26. is equivalent to
  27. .. code-block:: cmake
  28. message(STATUS "Hello World!")
  29. .. note::
  30. To ensure consistency of the code, the following commands are not allowed:
  31. * ``if`` / ``elseif`` / ``else`` / ``endif``
  32. * ``while`` / ``endwhile``
  33. * ``foreach`` / ``endforeach``
  34. * ``function`` / ``endfunction``
  35. * ``macro`` / ``endmacro``
  36. Evaluating Code
  37. ^^^^^^^^^^^^^^^
  38. .. _EVAL:
  39. .. code-block:: cmake
  40. cmake_language(EVAL CODE <code>...)
  41. Evaluates the ``<code>...`` as CMake code.
  42. For example, the code:
  43. .. code-block:: cmake
  44. set(A TRUE)
  45. set(B TRUE)
  46. set(C TRUE)
  47. set(condition "(A AND B) OR C")
  48. cmake_language(EVAL CODE "
  49. if (${condition})
  50. message(STATUS TRUE)
  51. else()
  52. message(STATUS FALSE)
  53. endif()"
  54. )
  55. is equivalent to
  56. .. code-block:: cmake
  57. set(A TRUE)
  58. set(B TRUE)
  59. set(C TRUE)
  60. set(condition "(A AND B) OR C")
  61. file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/eval.cmake "
  62. if (${condition})
  63. message(STATUS TRUE)
  64. else()
  65. message(STATUS FALSE)
  66. endif()"
  67. )
  68. include(${CMAKE_CURRENT_BINARY_DIR}/eval.cmake)
  69. Deferring Calls
  70. ^^^^^^^^^^^^^^^
  71. .. versionadded:: 3.19
  72. .. _DEFER:
  73. .. code-block:: cmake
  74. cmake_language(DEFER <options>... CALL <command> [<arg>...])
  75. Schedules a call to the named ``<command>`` with the given arguments (if any)
  76. to occur at a later time. By default, deferred calls are executed as if
  77. written at the end of the current directory's ``CMakeLists.txt`` file,
  78. except that they run even after a :command:`return` call. Variable
  79. references in arguments are evaluated at the time the deferred call is
  80. executed.
  81. The options are:
  82. ``DIRECTORY <dir>``
  83. Schedule the call for the end of the given directory instead of the
  84. current directory. The ``<dir>`` may reference either a source
  85. directory or its corresponding binary directory. Relative paths are
  86. treated as relative to the current source directory.
  87. The given directory must be known to CMake, being either the top-level
  88. directory or one added by :command:`add_subdirectory`. Furthermore,
  89. the given directory must not yet be finished processing. This means
  90. it can be the current directory or one of its ancestors.
  91. ``ID <id>``
  92. Specify an identification for the deferred call.
  93. The ``<id>`` may not be empty and may not begin with a capital letter ``A-Z``.
  94. The ``<id>`` may begin with an underscore (``_``) only if it was generated
  95. automatically by an earlier call that used ``ID_VAR`` to get the id.
  96. ``ID_VAR <var>``
  97. Specify a variable in which to store the identification for the
  98. deferred call. If ``ID <id>`` is not given, a new identification
  99. will be generated and the generated id will start with an underscore (``_``).
  100. The currently scheduled list of deferred calls may be retrieved:
  101. .. code-block:: cmake
  102. cmake_language(DEFER [DIRECTORY <dir>] GET_CALL_IDS <var>)
  103. This will store in ``<var>`` a :ref:`semicolon-separated list <CMake Language
  104. Lists>` of deferred call ids. The ids are for the directory scope in which
  105. the calls have been deferred to (i.e. where they will be executed), which can
  106. be different to the scope in which they were created. The ``DIRECTORY``
  107. option can be used to specify the scope for which to retrieve the call ids.
  108. If that option is not given, the call ids for the current directory scope will
  109. be returned.
  110. Details of a specific call may be retrieved from its id:
  111. .. code-block:: cmake
  112. cmake_language(DEFER [DIRECTORY <dir>] GET_CALL <id> <var>)
  113. This will store in ``<var>`` a :ref:`semicolon-separated list <CMake Language
  114. Lists>` in which the first element is the name of the command to be
  115. called, and the remaining elements are its unevaluated arguments (any
  116. contained ``;`` characters are included literally and cannot be distinguished
  117. from multiple arguments). If multiple calls are scheduled with the same id,
  118. this retrieves the first one. If no call is scheduled with the given id in
  119. the specified ``DIRECTORY`` scope (or the current directory scope if no
  120. ``DIRECTORY`` option is given), this stores an empty string in the variable.
  121. Deferred calls may be canceled by their id:
  122. .. code-block:: cmake
  123. cmake_language(DEFER [DIRECTORY <dir>] CANCEL_CALL <id>...)
  124. This cancels all deferred calls matching any of the given ids in the specified
  125. ``DIRECTORY`` scope (or the current directory scope if no ``DIRECTORY`` option
  126. is given). Unknown ids are silently ignored.
  127. Deferred Call Examples
  128. """"""""""""""""""""""
  129. For example, the code:
  130. .. code-block:: cmake
  131. cmake_language(DEFER CALL message "${deferred_message}")
  132. cmake_language(DEFER ID_VAR id CALL message "Canceled Message")
  133. cmake_language(DEFER CANCEL_CALL ${id})
  134. message("Immediate Message")
  135. set(deferred_message "Deferred Message")
  136. prints::
  137. Immediate Message
  138. Deferred Message
  139. The ``Cancelled Message`` is never printed because its command is
  140. canceled. The ``deferred_message`` variable reference is not evaluated
  141. until the call site, so it can be set after the deferred call is scheduled.
  142. In order to evaluate variable references immediately when scheduling a
  143. deferred call, wrap it using ``cmake_language(EVAL)``. However, note that
  144. arguments will be re-evaluated in the deferred call, though that can be
  145. avoided by using bracket arguments. For example:
  146. .. code-block:: cmake
  147. set(deferred_message "Deferred Message 1")
  148. set(re_evaluated [[${deferred_message}]])
  149. cmake_language(EVAL CODE "
  150. cmake_language(DEFER CALL message [[${deferred_message}]])
  151. cmake_language(DEFER CALL message \"${re_evaluated}\")
  152. ")
  153. message("Immediate Message")
  154. set(deferred_message "Deferred Message 2")
  155. also prints::
  156. Immediate Message
  157. Deferred Message 1
  158. Deferred Message 2