CMakeLists.txt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. cmake_minimum_required(VERSION 2.8)
  2. project(CheckCompilerRelatedVariables)
  3. function(echo_var var)
  4. if(DEFINED ${var})
  5. message("${var}='${${var}}' is defined")
  6. else()
  7. message("${var}='${${var}}' is NOT defined")
  8. endif()
  9. endfunction()
  10. #
  11. # Check that the correct number of MSVC** variables are defined...
  12. #
  13. set(msvc_total 0)
  14. if(DEFINED MSVC60)
  15. math(EXPR msvc_total "${msvc_total} + 1")
  16. endif()
  17. if(DEFINED MSVC70)
  18. math(EXPR msvc_total "${msvc_total} + 1")
  19. endif()
  20. if(DEFINED MSVC71)
  21. math(EXPR msvc_total "${msvc_total} + 1")
  22. endif()
  23. if(DEFINED MSVC80)
  24. math(EXPR msvc_total "${msvc_total} + 1")
  25. endif()
  26. if(DEFINED MSVC90)
  27. math(EXPR msvc_total "${msvc_total} + 1")
  28. endif()
  29. if(DEFINED MSVC10)
  30. math(EXPR msvc_total "${msvc_total} + 1")
  31. endif()
  32. if(DEFINED MSVC11)
  33. math(EXPR msvc_total "${msvc_total} + 1")
  34. endif()
  35. if(DEFINED MSVC12)
  36. math(EXPR msvc_total "${msvc_total} + 1")
  37. endif()
  38. echo_var(MSVC)
  39. echo_var(MSVC60)
  40. echo_var(MSVC70)
  41. echo_var(MSVC71)
  42. echo_var(MSVC80)
  43. echo_var(MSVC90)
  44. echo_var(MSVC10)
  45. echo_var(MSVC11)
  46. echo_var(MSVC12)
  47. echo_var(MSVC_IDE)
  48. if(MSVC)
  49. #
  50. # MSVC is set in cl.cmake when cl is the compiler...
  51. #
  52. # Exactly one of the numbered variables should also be set
  53. # indicating which version of the cl compiler / Visual Studio
  54. # is in use...
  55. #
  56. if(msvc_total EQUAL 1)
  57. message("test passes: exactly one MSVC** variable is defined...")
  58. else()
  59. message(FATAL_ERROR "error: ${msvc_total} MSVC** variables are defined -- exactly 1 expected")
  60. endif()
  61. if(NOT DEFINED MSVC_IDE)
  62. message(FATAL_ERROR "MSVC_IDE not defined but should be!")
  63. elseif("${CMAKE_GENERATOR}" MATCHES "Visual Studio" AND NOT MSVC_IDE)
  64. message(FATAL_ERROR "MSVC_IDE is not true but should be (${CMAKE_GENERATOR})!")
  65. elseif(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio" AND MSVC_IDE)
  66. message(FATAL_ERROR "MSVC_IDE is true but should not be (${CMAKE_GENERATOR})!")
  67. endif()
  68. else()
  69. #
  70. # The compiler is something other than cl... None of the MSVC** variables
  71. # should be defined...
  72. #
  73. if(msvc_total EQUAL 0)
  74. message("test passes: no MSVC** variables are defined on non-MSVC build...")
  75. else()
  76. message(FATAL_ERROR "error: ${msvc_total} MSVC** variables are defined -- exactly 0 expected")
  77. endif()
  78. if(DEFINED MSVC_IDE)
  79. message(FATAL_ERROR "MSVC_IDE is defined but should not be!")
  80. endif()
  81. endif()
  82. #
  83. # This is a no-op executable... If this test is going to fail, it fails during
  84. # the configure step while cmake is configuring this CMakeLists.txt file...
  85. #
  86. file(WRITE
  87. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  88. "int main() { return 0; }
  89. "
  90. )
  91. add_executable(
  92. CheckCompilerRelatedVariables
  93. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  94. )