CMakeLists.txt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. project(PolicyScope C)
  2. # No cmake_minimum_required(VERSION), it's in FindFoo.
  3. #-----------------------------------------------------------------------------
  4. # Helper function to report results.
  5. function(check msg lhs rhs)
  6. if(NOT "${lhs}" STREQUAL "${rhs}")
  7. message(FATAL_ERROR "${msg}: expected [${lhs}], got [${rhs}]")
  8. endif()
  9. endfunction()
  10. #-----------------------------------------------------------------------------
  11. # Test using a development framework that sets policies for us.
  12. # Policy CMP0011 should not be set at this point.
  13. cmake_policy(GET CMP0011 cmp)
  14. check(CMP0011 "" "${cmp}")
  15. # Put the test modules in the search path.
  16. list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
  17. # The included file should set policies for us.
  18. find_package(Foo)
  19. # Check policies set by the package.
  20. cmake_policy(GET CMP0003 cmp)
  21. check(CMP0003 "OLD" "${cmp}")
  22. cmake_policy(GET CMP0002 cmp)
  23. check(CMP0002 "NEW" "${cmp}")
  24. cmake_policy(GET CMP0011 cmp)
  25. check(CMP0011 "NEW" "${cmp}")
  26. # Make sure an included file cannot change policies.
  27. include(Bar)
  28. cmake_policy(GET CMP0003 cmp)
  29. check(CMP0003 "OLD" "${cmp}")
  30. # Allow the included file to change policies.
  31. include(Bar NO_POLICY_SCOPE)
  32. cmake_policy(GET CMP0003 cmp)
  33. check(CMP0003 "NEW" "${cmp}")
  34. #-----------------------------------------------------------------------------
  35. # Test function and macro policy recording.
  36. # Create the functions in an isolated scope in which we change policies.
  37. cmake_policy(PUSH)
  38. if(1)
  39. # Change CMP0002
  40. cmake_policy(SET CMP0002 OLD)
  41. function(func1)
  42. # CMP0002 should be changed when this function is invoked
  43. cmake_policy(GET CMP0002 cmp)
  44. check(CMP0002 "OLD" "${cmp}")
  45. endfunction()
  46. # Unset CMP0002
  47. cmake_policy(VERSION 2.4)
  48. macro(macro1)
  49. # CMP0002 should be unset when this macro is invoked
  50. cmake_policy(GET CMP0002 cmp)
  51. check(CMP0002 "" "${cmp}")
  52. # Setting the policy should work here and also in the caller.
  53. cmake_policy(SET CMP0002 OLD)
  54. cmake_policy(GET CMP0002 cmp)
  55. check(CMP0002 "OLD" "${cmp}")
  56. endmacro()
  57. endif()
  58. cmake_policy(POP)
  59. # CMP0002 should still be NEW in this context.
  60. cmake_policy(GET CMP0002 cmp)
  61. check(CMP0002 "NEW" "${cmp}")
  62. # Check the recorded policies
  63. func1()
  64. macro1()
  65. # The macro should have changed CMP0002.
  66. cmake_policy(GET CMP0002 cmp)
  67. check(CMP0002 "OLD" "${cmp}")
  68. #-----------------------------------------------------------------------------
  69. # Test CMAKE_POLICY_DEFAULT_CMP<NNNN> variable.
  70. cmake_policy(PUSH)
  71. set(CMAKE_POLICY_DEFAULT_CMP0010 OLD) # ignored
  72. set(CMAKE_POLICY_DEFAULT_CMP0012 OLD) # honored
  73. set(CMAKE_POLICY_DEFAULT_CMP0013 NEW) # honored
  74. set(CMAKE_POLICY_DEFAULT_CMP0014 "") # noop
  75. cmake_policy(VERSION 2.6.3)
  76. cmake_policy(GET CMP0010 cmp)
  77. check(CMP0010 "NEW" "${cmp}")
  78. cmake_policy(GET CMP0012 cmp)
  79. check(CMP0012 "OLD" "${cmp}")
  80. cmake_policy(GET CMP0013 cmp)
  81. check(CMP0013 "NEW" "${cmp}")
  82. cmake_policy(GET CMP0014 cmp)
  83. check(CMP0014 "" "${cmp}")
  84. cmake_policy(POP)
  85. #-----------------------------------------------------------------------------
  86. # Dummy executable so the project can build and run.
  87. add_executable(PolicyScope main.c)