CMakeLists.txt 2.6 KB

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