IfTest.cmake.in 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Prepare variable definitions.
  2. set(VAR_UNDEFINED)
  3. set(VAR_PATH /some/path/to/a/file.txt)
  4. set(FALSE_NAMES OFF NO FALSE N FOO-NOTFOUND IGNORE Off No False Ignore off n no false ignore)
  5. set(TRUE_NAMES ON YES TRUE Y On Yes True on yes true y)
  6. foreach(_arg "" 0 1 2 ${TRUE_NAMES} ${FALSE_NAMES})
  7. set(VAR_${_arg} "${_arg}")
  8. endforeach()
  9. include("@CMAKE_CURRENT_SOURCE_DIR@/../RegexEscapeString.cmake")
  10. REGEX_ESCAPE_STRING(CTEST_ESCAPED_CMAKE_CURRENT_SOURCE_DIR "@CMAKE_CURRENT_SOURCE_DIR@")
  11. macro(test_vars _old)
  12. # Variables set to false or not set.
  13. foreach(_var "" 0 ${FALSE_NAMES} UNDEFINED)
  14. if(VAR_${_var})
  15. message(FATAL_ERROR "${_old}if(VAR_${_var}) is true!")
  16. else()
  17. message(STATUS "${_old}if(VAR_${_var}) is false")
  18. endif()
  19. if(NOT VAR_${_var})
  20. message(STATUS "${_old}if(NOT VAR_${_var}) is true")
  21. else()
  22. message(FATAL_ERROR "${_old}if(NOT VAR_${_var}) is false!")
  23. endif()
  24. endforeach()
  25. # Variables set to true.
  26. foreach(_var 1 2 ${TRUE_NAMES} PATH)
  27. if(VAR_${_var})
  28. message(STATUS "${_old}if(VAR_${_var}) is true")
  29. else()
  30. message(FATAL_ERROR "${_old}if(VAR_${_var}) is false!")
  31. endif()
  32. if(NOT VAR_${_var})
  33. message(FATAL_ERROR "${_old}if(NOT VAR_${_var}) is true!")
  34. else()
  35. message(STATUS "${_old}if(NOT VAR_${_var}) is false")
  36. endif()
  37. endforeach()
  38. endmacro()
  39. #-----------------------------------------------------------------------------
  40. # Test the OLD behavior of CMP0012.
  41. cmake_policy(SET CMP0012 OLD)
  42. # False constants not recognized (still false).
  43. foreach(_false "" ${FALSE_NAMES})
  44. if("${_false}")
  45. message(FATAL_ERROR "OLD if(${_false}) is true!")
  46. else()
  47. message(STATUS "OLD if(${_false}) is false")
  48. endif()
  49. if(NOT "${_false}")
  50. message(STATUS "OLD if(NOT ${_false}) is true")
  51. else()
  52. message(FATAL_ERROR "OLD if(NOT ${_false}) is false!")
  53. endif()
  54. endforeach()
  55. # True constants not recognized.
  56. foreach(_false ${TRUE_NAMES})
  57. if(${_false})
  58. message(FATAL_ERROR "OLD if(${_false}) is true!")
  59. else()
  60. message(STATUS "OLD if(${_false}) is false")
  61. endif()
  62. if(NOT ${_false})
  63. message(STATUS "OLD if(NOT ${_false}) is true")
  64. else()
  65. message(FATAL_ERROR "OLD if(NOT ${_false}) is false!")
  66. endif()
  67. endforeach()
  68. # Numbers not recognized properly.
  69. foreach(_num 2 -2 2.0 -2.0 2x -2x)
  70. if(${_num})
  71. message(FATAL_ERROR "OLD if(${_num}) is true!")
  72. else()
  73. message(STATUS "OLD if(${_num}) is false")
  74. endif()
  75. if(NOT ${_num})
  76. message(FATAL_ERROR "OLD if(NOT ${_num}) is true!")
  77. else()
  78. message(STATUS "OLD if(NOT ${_num}) is false")
  79. endif()
  80. endforeach()
  81. test_vars("OLD ")
  82. #-----------------------------------------------------------------------------
  83. # Test the NEW behavior of CMP0012.
  84. cmake_policy(SET CMP0012 NEW)
  85. # Test false constants.
  86. foreach(_false "" 0 ${FALSE_NAMES})
  87. if("${_false}")
  88. message(FATAL_ERROR "if(${_false}) is true!")
  89. else()
  90. message(STATUS "if(${_false}) is false")
  91. endif()
  92. if(NOT "${_false}")
  93. message(STATUS "if(NOT ${_false}) is true")
  94. else()
  95. message(FATAL_ERROR "if(NOT ${_false}) is false!")
  96. endif()
  97. endforeach()
  98. # Test true constants.
  99. foreach(_true 1 ${TRUE_NAMES})
  100. if(${_true})
  101. message(STATUS "if(${_true}) is true")
  102. else()
  103. message(FATAL_ERROR "if(${_true}) is false!")
  104. endif()
  105. if(NOT ${_true})
  106. message(FATAL_ERROR "if(NOT ${_true}) is true!")
  107. else()
  108. message(STATUS "if(NOT ${_true}) is false")
  109. endif()
  110. endforeach()
  111. # Numbers recognized properly.
  112. foreach(_num 2 -2 2.0 -2.0)
  113. if(${_num})
  114. message(STATUS "if(${_num}) is true")
  115. else()
  116. message(FATAL_ERROR "if(${_num}) is false!")
  117. endif()
  118. if(NOT ${_num})
  119. message(FATAL_ERROR "if(NOT ${_num}) is true!")
  120. else()
  121. message(STATUS "if(NOT ${_num}) is false")
  122. endif()
  123. endforeach()
  124. # Bad numbers not recognized.
  125. foreach(_bad 2x -2x)
  126. if(${_bad})
  127. message(FATAL_ERROR "if(${_bad}) is true!")
  128. else()
  129. message(STATUS "if(${_bad}) is false")
  130. endif()
  131. if(NOT ${_bad})
  132. message(STATUS "if(NOT ${_bad}) is true")
  133. else()
  134. message(FATAL_ERROR "if(NOT ${_bad}) is false!")
  135. endif()
  136. endforeach()
  137. test_vars("")
  138. set(Invalid-Argument-RESULT 1)
  139. set(Invalid-Argument-STDERR ".*CMake Error at (${CTEST_ESCAPED_CMAKE_CURRENT_SOURCE_DIR}/)?If-Invalid-Argument.cmake:1 \\(if\\):.*Unknown arguments specified.*")
  140. include("@CMAKE_CURRENT_SOURCE_DIR@/CheckCMakeTest.cmake")
  141. check_cmake_test(If
  142. Invalid-Argument
  143. )