cmake_policy.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. cmake_policy
  2. ------------
  3. Manage CMake Policy settings.
  4. As CMake evolves it is sometimes necessary to change existing behavior
  5. in order to fix bugs or improve implementations of existing features.
  6. The CMake Policy mechanism is designed to help keep existing projects
  7. building as new versions of CMake introduce changes in behavior. Each
  8. new policy (behavioral change) is given an identifier of the form
  9. "CMP<NNNN>" where "<NNNN>" is an integer index. Documentation
  10. associated with each policy describes the OLD and NEW behavior and the
  11. reason the policy was introduced. Projects may set each policy to
  12. select the desired behavior. When CMake needs to know which behavior
  13. to use it checks for a setting specified by the project. If no
  14. setting is available the OLD behavior is assumed and a warning is
  15. produced requesting that the policy be set.
  16. The cmake_policy command is used to set policies to OLD or NEW
  17. behavior. While setting policies individually is supported, we
  18. encourage projects to set policies based on CMake versions.
  19. ::
  20. cmake_policy(VERSION major.minor[.patch[.tweak]])
  21. Specify that the current CMake list file is written for the given
  22. version of CMake. All policies introduced in the specified version or
  23. earlier will be set to use NEW behavior. All policies introduced
  24. after the specified version will be unset (unless variable
  25. CMAKE_POLICY_DEFAULT_CMP<NNNN> sets a default). This effectively
  26. requests behavior preferred as of a given CMake version and tells
  27. newer CMake versions to warn about their new policies. The policy
  28. version specified must be at least 2.4 or the command will report an
  29. error. In order to get compatibility features supporting versions
  30. earlier than 2.4 see documentation of policy CMP0001.
  31. ::
  32. cmake_policy(SET CMP<NNNN> NEW)
  33. cmake_policy(SET CMP<NNNN> OLD)
  34. Tell CMake to use the OLD or NEW behavior for a given policy.
  35. Projects depending on the old behavior of a given policy may silence a
  36. policy warning by setting the policy state to OLD. Alternatively one
  37. may fix the project to work with the new behavior and set the policy
  38. state to NEW.
  39. ::
  40. cmake_policy(GET CMP<NNNN> <variable>)
  41. Check whether a given policy is set to OLD or NEW behavior. The
  42. output variable value will be "OLD" or "NEW" if the policy is set, and
  43. empty otherwise.
  44. CMake keeps policy settings on a stack, so changes made by the
  45. cmake_policy command affect only the top of the stack. A new entry on
  46. the policy stack is managed automatically for each subdirectory to
  47. protect its parents and siblings. CMake also manages a new entry for
  48. scripts loaded by include() and find_package() commands except when
  49. invoked with the NO_POLICY_SCOPE option (see also policy CMP0011).
  50. The cmake_policy command provides an interface to manage custom
  51. entries on the policy stack:
  52. ::
  53. cmake_policy(PUSH)
  54. cmake_policy(POP)
  55. Each PUSH must have a matching POP to erase any changes. This is
  56. useful to make temporary changes to policy settings.
  57. Functions and macros record policy settings when they are created and
  58. use the pre-record policies when they are invoked. If the function or
  59. macro implementation sets policies, the changes automatically
  60. propagate up through callers until they reach the closest nested
  61. policy stack entry.