cmake_policy.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. cmake_policy
  2. ------------
  3. Manage CMake Policy settings. See the :manual:`cmake-policies(7)`
  4. manual for defined policies.
  5. As CMake evolves it is sometimes necessary to change existing behavior
  6. in order to fix bugs or improve implementations of existing features.
  7. The CMake Policy mechanism is designed to help keep existing projects
  8. building as new versions of CMake introduce changes in behavior. Each
  9. new policy (behavioral change) is given an identifier of the form
  10. ``CMP<NNNN>`` where ``<NNNN>`` is an integer index. Documentation
  11. associated with each policy describes the ``OLD`` and ``NEW`` behavior
  12. and the reason the policy was introduced. Projects may set each policy
  13. to select the desired behavior. When CMake needs to know which behavior
  14. to use it checks for a setting specified by the project. If no
  15. setting is available the ``OLD`` behavior is assumed and a warning is
  16. produced requesting that the policy be set.
  17. Setting Policies by CMake Version
  18. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  19. The ``cmake_policy`` command is used to set policies to ``OLD`` or ``NEW``
  20. behavior. While setting policies individually is supported, we
  21. encourage projects to set policies based on CMake versions:
  22. .. signature:: cmake_policy(VERSION <min>[...<policy_max>])
  23. :target: VERSION
  24. .. versionadded:: 3.12
  25. The optional ``<policy_max>`` version.
  26. ``<min>`` and the optional ``<policy_max>`` are each CMake versions of the
  27. form ``major.minor[.patch[.tweak]]``, and the ``...`` is literal.
  28. The ``<min>`` version must be at least ``2.4`` and at most the running
  29. version of CMake. The ``<policy_max>`` version, if specified, must be at
  30. least the ``<min>`` version but may exceed the running version of CMake.
  31. If the running version of CMake is older than 3.12, the extra ``...``
  32. dots will be seen as version component separators, resulting in the
  33. ``...<policy_max>`` part being ignored and preserving the pre-3.12 behavior
  34. of basing policies on ``<min>``.
  35. .. include:: include/POLICY_VERSION.rst
  36. Note that the :command:`cmake_minimum_required(VERSION)`
  37. command implicitly calls ``cmake_policy(VERSION)`` too.
  38. .. include:: include/DEPRECATED_POLICY_VERSIONS.rst
  39. Setting Policies Explicitly
  40. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  41. .. signature:: cmake_policy(SET CMP<NNNN> NEW|OLD)
  42. :target: SET
  43. Tell CMake to use the ``OLD`` or ``NEW`` behavior for a given policy.
  44. Projects depending on the old behavior of a given policy may silence a
  45. policy warning by setting the policy state to ``OLD``. Alternatively
  46. one may fix the project to work with the new behavior and set the
  47. policy state to ``NEW``.
  48. .. include:: ../policy/include/DEPRECATED.rst
  49. Checking Policy Settings
  50. ^^^^^^^^^^^^^^^^^^^^^^^^
  51. .. signature:: cmake_policy(GET CMP<NNNN> <variable>)
  52. :target: GET
  53. Check whether a given policy is set to ``OLD`` or ``NEW`` behavior.
  54. The output ``<variable>`` value will be ``OLD`` or ``NEW`` if the
  55. policy is set, and empty otherwise.
  56. CMake Policy Stack
  57. ^^^^^^^^^^^^^^^^^^
  58. CMake keeps policy settings on a stack, so changes made by the
  59. ``cmake_policy`` command affect only the top of the stack. A new entry on
  60. the policy stack is managed automatically for each subdirectory to
  61. protect its parents and siblings. CMake also manages a new entry for
  62. scripts loaded by :command:`include` and :command:`find_package` commands
  63. except when invoked with the ``NO_POLICY_SCOPE`` option
  64. (see also policy :policy:`CMP0011`).
  65. The ``cmake_policy`` command provides an interface to manage custom
  66. entries on the policy stack:
  67. .. signature:: cmake_policy(PUSH)
  68. :target: PUSH
  69. Create a new entry on the policy stack.
  70. .. signature:: cmake_policy(POP)
  71. :target: POP
  72. Remove the last policy stack entry created with ``cmake_policy(PUSH)``.
  73. Each ``PUSH`` must have a matching ``POP`` to erase any changes.
  74. This is useful to make temporary changes to policy settings.
  75. Calls to the :command:`cmake_minimum_required(VERSION)`,
  76. :command:`cmake_policy(VERSION)`, or :command:`cmake_policy(SET)` commands
  77. influence only the current top of the policy stack.
  78. .. versionadded:: 3.25
  79. The :command:`block(SCOPE_FOR POLICIES)` command offers a more flexible
  80. and more secure way to manage the policy stack. The pop action is done
  81. automatically when leaving the block scope, so there is no need to
  82. precede each :command:`return` with a call to :command:`cmake_policy(POP)`.
  83. .. code-block:: cmake
  84. # stack management with cmake_policy()
  85. function(my_func)
  86. cmake_policy(PUSH)
  87. cmake_policy(SET ...)
  88. if (<cond1>)
  89. ...
  90. cmake_policy(POP)
  91. return()
  92. elseif(<cond2>)
  93. ...
  94. cmake_policy(POP)
  95. return()
  96. endif()
  97. ...
  98. cmake_policy(POP)
  99. endfunction()
  100. # stack management with block()/endblock()
  101. function(my_func)
  102. block(SCOPE_FOR POLICIES)
  103. cmake_policy(SET ...)
  104. if (<cond1>)
  105. ...
  106. return()
  107. elseif(<cond2>)
  108. ...
  109. return()
  110. endif()
  111. ...
  112. endblock()
  113. endfunction()
  114. Commands created by the :command:`function` and :command:`macro`
  115. commands record policy settings when they are created and
  116. use the pre-record policies when they are invoked. If the function or
  117. macro implementation sets policies, the changes automatically
  118. propagate up through callers until they reach the closest nested
  119. policy stack entry.
  120. See Also
  121. ^^^^^^^^
  122. * :command:`cmake_minimum_required`