cmake_policy.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. .. code-block:: cmake
  23. cmake_policy(VERSION <min>[...<max>])
  24. ``<min>`` and the optional ``<max>`` are each CMake versions of the form
  25. ``major.minor[.patch[.tweak]]``, and the ``...`` is literal. The ``<min>``
  26. version must be at least ``2.4`` and at most the running version of CMake.
  27. The ``<max>`` version, if specified, must be at least the ``<min>`` version
  28. but may exceed the running version of CMake. If the running version of
  29. CMake is older than 3.12, the extra ``...`` dots will be seen as version
  30. component separators, resulting in the ``...<max>`` part being ignored and
  31. preserving the pre-3.12 behavior of basing policies on ``<min>``.
  32. This specifies that the current CMake code is written for the given
  33. range of CMake versions. All policies known to the running version of CMake
  34. and introduced in the ``<min>`` (or ``<max>``, if specified) version
  35. or earlier will be set to use ``NEW`` behavior. All policies
  36. introduced in later versions will be unset (unless the
  37. :variable:`CMAKE_POLICY_DEFAULT_CMP<NNNN>` variable sets a default).
  38. This effectively requests behavior preferred as of a given CMake
  39. version and tells newer CMake versions to warn about their new policies.
  40. Note that the :command:`cmake_minimum_required(VERSION)`
  41. command implicitly calls ``cmake_policy(VERSION)`` too.
  42. Setting Policies Explicitly
  43. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  44. .. code-block:: cmake
  45. cmake_policy(SET CMP<NNNN> NEW)
  46. cmake_policy(SET CMP<NNNN> OLD)
  47. Tell CMake to use the ``OLD`` or ``NEW`` behavior for a given policy.
  48. Projects depending on the old behavior of a given policy may silence a
  49. policy warning by setting the policy state to ``OLD``. Alternatively
  50. one may fix the project to work with the new behavior and set the
  51. policy state to ``NEW``.
  52. .. include:: ../policy/DEPRECATED.txt
  53. Checking Policy Settings
  54. ^^^^^^^^^^^^^^^^^^^^^^^^
  55. .. code-block:: cmake
  56. cmake_policy(GET CMP<NNNN> <variable>)
  57. Check whether a given policy is set to ``OLD`` or ``NEW`` behavior.
  58. The output ``<variable>`` value will be ``OLD`` or ``NEW`` if the
  59. policy is set, and empty otherwise.
  60. CMake Policy Stack
  61. ^^^^^^^^^^^^^^^^^^
  62. CMake keeps policy settings on a stack, so changes made by the
  63. cmake_policy command affect only the top of the stack. A new entry on
  64. the policy stack is managed automatically for each subdirectory to
  65. protect its parents and siblings. CMake also manages a new entry for
  66. scripts loaded by :command:`include` and :command:`find_package` commands
  67. except when invoked with the ``NO_POLICY_SCOPE`` option
  68. (see also policy :policy:`CMP0011`).
  69. The ``cmake_policy`` command provides an interface to manage custom
  70. entries on the policy stack:
  71. .. code-block:: cmake
  72. cmake_policy(PUSH)
  73. cmake_policy(POP)
  74. Each ``PUSH`` must have a matching ``POP`` to erase any changes.
  75. This is useful to make temporary changes to policy settings.
  76. Calls to the :command:`cmake_minimum_required(VERSION)`,
  77. ``cmake_policy(VERSION)``, or ``cmake_policy(SET)`` commands
  78. influence only the current top of the policy stack.
  79. Commands created by the :command:`function` and :command:`macro`
  80. commands record policy settings when they are created and
  81. use the pre-record policies when they are invoked. If the function or
  82. macro implementation sets policies, the changes automatically
  83. propagate up through callers until they reach the closest nested
  84. policy stack entry.