CheckCompilerVersion.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Check if the host compiler is new enough.
  2. # These versions are updated based on the following policy:
  3. # llvm.org/docs/DeveloperPolicy.html#toolchain
  4. include(CheckCXXSourceCompiles)
  5. set(GCC_MIN 5.1)
  6. set(GCC_SOFT_ERROR 5.1)
  7. set(CLANG_MIN 3.5)
  8. set(CLANG_SOFT_ERROR 3.5)
  9. set(APPLECLANG_MIN 6.0)
  10. set(APPLECLANG_SOFT_ERROR 6.0)
  11. # https://en.wikipedia.org/wiki/Microsoft_Visual_C#Internal_version_numbering
  12. # _MSC_VER == 1910 MSVC++ 14.1 (Visual Studio 2017 version 15.0)
  13. set(MSVC_MIN 19.1)
  14. set(MSVC_SOFT_ERROR 19.1)
  15. # Map the above GCC versions to dates: https://gcc.gnu.org/develop.html#timeline
  16. set(GCC_MIN_DATE 20150422)
  17. set(GCC_SOFT_ERROR_DATE 20150422)
  18. if(DEFINED LLVM_COMPILER_CHECKED)
  19. return()
  20. endif()
  21. set(LLVM_COMPILER_CHECKED ON)
  22. if(LLVM_FORCE_USE_OLD_TOOLCHAIN)
  23. return()
  24. endif()
  25. function(check_compiler_version NAME NICE_NAME MINIMUM_VERSION SOFT_ERROR_VERSION)
  26. if(NOT CMAKE_CXX_COMPILER_ID STREQUAL NAME)
  27. return()
  28. endif()
  29. if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS MINIMUM_VERSION)
  30. message(FATAL_ERROR "Host ${NICE_NAME} version must be at least ${MINIMUM_VERSION}, your version is ${CMAKE_CXX_COMPILER_VERSION}.")
  31. elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS SOFT_ERROR_VERSION)
  32. if(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN)
  33. message(WARNING "Host ${NICE_NAME} version should be at least ${SOFT_ERROR_VERSION} because LLVM will soon use new C++ features which your toolchain version doesn't support. Your version is ${CMAKE_CXX_COMPILER_VERSION}. Ignoring because you've set LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN, but very soon your toolchain won't be supported.")
  34. else()
  35. message(FATAL_ERROR "Host ${NICE_NAME} version should be at least ${SOFT_ERROR_VERSION} because LLVM will soon use new C++ features which your toolchain version doesn't support. Your version is ${CMAKE_CXX_COMPILER_VERSION}. You can temporarily opt out using LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN, but very soon your toolchain won't be supported.")
  36. endif()
  37. endif()
  38. endfunction(check_compiler_version)
  39. check_compiler_version("GNU" "GCC" ${GCC_MIN} ${GCC_SOFT_ERROR})
  40. check_compiler_version("Clang" "Clang" ${CLANG_MIN} ${CLANG_SOFT_ERROR})
  41. check_compiler_version("AppleClang" "Apple Clang" ${APPLECLANG_MIN} ${APPLECLANG_SOFT_ERROR})
  42. check_compiler_version("MSVC" "Visual Studio" ${MSVC_MIN} ${MSVC_SOFT_ERROR})
  43. if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  44. if (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
  45. if (CMAKE_CXX_SIMULATE_VERSION VERSION_LESS MSVC_MIN)
  46. message(FATAL_ERROR "Host Clang must have at least -fms-compatibility-version=${MSVC_MIN}, your version is ${CMAKE_CXX_SIMULATE_VERSION}.")
  47. endif()
  48. set(CLANG_CL 1)
  49. elseif(NOT LLVM_ENABLE_LIBCXX)
  50. # Test that we aren't using too old of a version of libstdc++.
  51. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  52. set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  53. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++0x")
  54. # Test for libstdc++ version of at least 4.8 by checking for _ZNKSt17bad_function_call4whatEv.
  55. # Note: We should check _GLIBCXX_RELEASE when possible (i.e., for GCC 7.1 and up).
  56. check_cxx_source_compiles("
  57. #include <iosfwd>
  58. #if defined(__GLIBCXX__)
  59. #if __GLIBCXX__ < ${GCC_MIN_DATE}
  60. #error Unsupported libstdc++ version
  61. #endif
  62. #endif
  63. #if defined(__GLIBCXX__)
  64. extern const char _ZNKSt17bad_function_call4whatEv[];
  65. const char *chk = _ZNKSt17bad_function_call4whatEv;
  66. #else
  67. const char *chk = \"\";
  68. #endif
  69. int main() { ++chk; return 0; }
  70. "
  71. LLVM_LIBSTDCXX_MIN)
  72. if(NOT LLVM_LIBSTDCXX_MIN)
  73. message(FATAL_ERROR "libstdc++ version must be at least ${GCC_MIN}.")
  74. endif()
  75. # Test for libstdc++ version of at least 5.1 by checking for std::iostream_category().
  76. # Note: We should check _GLIBCXX_RELEASE when possible (i.e., for GCC 7.1 and up).
  77. check_cxx_source_compiles("
  78. #include <iosfwd>
  79. #if defined(__GLIBCXX__)
  80. #if __GLIBCXX__ < ${GCC_SOFT_ERROR_DATE}
  81. #error Unsupported libstdc++ version
  82. #endif
  83. #endif
  84. #if defined(__GLIBCXX__)
  85. #include <ios>
  86. void foo(void) { (void) std::iostream_category(); }
  87. #endif
  88. int main() { return 0; }
  89. "
  90. LLVM_LIBSTDCXX_SOFT_ERROR)
  91. if(NOT LLVM_LIBSTDCXX_SOFT_ERROR)
  92. if(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN)
  93. message(WARNING "libstdc++ version should be at least ${GCC_SOFT_ERROR} because LLVM will soon use new C++ features which your toolchain version doesn't support. Ignoring because you've set LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN, but very soon your toolchain won't be supported.")
  94. else()
  95. message(FATAL_ERROR "libstdc++ version should be at least ${GCC_SOFT_ERROR} because LLVM will soon use new C++ features which your toolchain version doesn't support. You can temporarily opt out using LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN, but very soon your toolchain won't be supported.")
  96. endif()
  97. endif()
  98. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  99. set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
  100. endif()
  101. endif()