cmake_language.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. Introduction
  11. ^^^^^^^^^^^^
  12. This command will call meta-operations on built-in CMake commands or
  13. those created via the :command:`macro` or :command:`function` commands.
  14. ``cmake_language`` does not introduce a new variable or policy scope.
  15. Calling Commands
  16. ^^^^^^^^^^^^^^^^
  17. .. _CALL:
  18. .. code-block:: cmake
  19. cmake_language(CALL <command> [<arg>...])
  20. Calls the named ``<command>`` with the given arguments (if any).
  21. For example, the code:
  22. .. code-block:: cmake
  23. set(message_command "message")
  24. cmake_language(CALL ${message_command} STATUS "Hello World!")
  25. is equivalent to
  26. .. code-block:: cmake
  27. message(STATUS "Hello World!")
  28. .. note::
  29. To ensure consistency of the code, the following commands are not allowed:
  30. * ``if`` / ``elseif`` / ``else`` / ``endif``
  31. * ``while`` / ``endwhile``
  32. * ``foreach`` / ``endforeach``
  33. * ``function`` / ``endfunction``
  34. * ``macro`` / ``endmacro``
  35. Evaluating Code
  36. ^^^^^^^^^^^^^^^
  37. .. _EVAL:
  38. .. code-block:: cmake
  39. cmake_language(EVAL CODE <code>...)
  40. Evaluates the ``<code>...`` as CMake code.
  41. For example, the code:
  42. .. code-block:: cmake
  43. set(A TRUE)
  44. set(B TRUE)
  45. set(C TRUE)
  46. set(condition "(A AND B) OR C")
  47. cmake_language(EVAL CODE "
  48. if (${condition})
  49. message(STATUS TRUE)
  50. else()
  51. message(STATUS FALSE)
  52. endif()"
  53. )
  54. is equivalent to
  55. .. code-block:: cmake
  56. set(A TRUE)
  57. set(B TRUE)
  58. set(C TRUE)
  59. set(condition "(A AND B) OR C")
  60. file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/eval.cmake "
  61. if (${condition})
  62. message(STATUS TRUE)
  63. else()
  64. message(STATUS FALSE)
  65. endif()"
  66. )
  67. include(${CMAKE_CURRENT_BINARY_DIR}/eval.cmake)