CMakeLists.txt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if(DEFINED MSVC14)
  39. math(EXPR msvc_total "${msvc_total} + 1")
  40. endif()
  41. echo_var(MSVC)
  42. echo_var(MSVC60)
  43. echo_var(MSVC70)
  44. echo_var(MSVC71)
  45. echo_var(MSVC80)
  46. echo_var(MSVC90)
  47. echo_var(MSVC10)
  48. echo_var(MSVC11)
  49. echo_var(MSVC12)
  50. echo_var(MSVC14)
  51. echo_var(MSVC_IDE)
  52. if(MSVC)
  53. #
  54. # MSVC is set in cl.cmake when cl is the compiler...
  55. #
  56. # Exactly one of the numbered variables should also be set
  57. # indicating which version of the cl compiler / Visual Studio
  58. # is in use...
  59. #
  60. if(msvc_total EQUAL 1)
  61. message("test passes: exactly one MSVC** variable is defined...")
  62. else()
  63. message(FATAL_ERROR "error: ${msvc_total} MSVC** variables are defined -- exactly 1 expected")
  64. endif()
  65. if(NOT DEFINED MSVC_IDE)
  66. message(FATAL_ERROR "MSVC_IDE not defined but should be!")
  67. elseif("${CMAKE_GENERATOR}" MATCHES "Visual Studio" AND NOT MSVC_IDE)
  68. message(FATAL_ERROR "MSVC_IDE is not true but should be (${CMAKE_GENERATOR})!")
  69. elseif(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio" AND MSVC_IDE)
  70. message(FATAL_ERROR "MSVC_IDE is true but should not be (${CMAKE_GENERATOR})!")
  71. endif()
  72. else()
  73. #
  74. # The compiler is something other than cl... None of the MSVC** variables
  75. # should be defined...
  76. #
  77. if(msvc_total EQUAL 0)
  78. message("test passes: no MSVC** variables are defined on non-MSVC build...")
  79. else()
  80. message(FATAL_ERROR "error: ${msvc_total} MSVC** variables are defined -- exactly 0 expected")
  81. endif()
  82. if(DEFINED MSVC_IDE)
  83. message(FATAL_ERROR "MSVC_IDE is defined but should not be!")
  84. endif()
  85. endif()
  86. #
  87. # This is a no-op executable... If this test is going to fail, it fails during
  88. # the configure step while cmake is configuring this CMakeLists.txt file...
  89. #
  90. file(WRITE
  91. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  92. "int main() { return 0; }
  93. "
  94. )
  95. add_executable(
  96. CheckCompilerRelatedVariables
  97. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  98. )