cmake_language.rst 1.8 KB

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