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
5include(CheckCXXSourceCompiles)
6
7set(GCC_MIN 4.8)
8set(GCC_SOFT_ERROR 5.1)
9set(CLANG_MIN 3.1)
10set(CLANG_SOFT_ERROR 3.5)
11set(APPLECLANG_MIN 3.1)
12set(APPLECLANG_SOFT_ERROR 6.0)
13set(MSVC_MIN 19.00.24213.1)
14set(MSVC_SOFT_ERROR 19.1)
15
16# Map the above GCC versions to dates: https://gcc.gnu.org/develop.html#timeline
17set(GCC_MIN_DATE 20130322)
18set(GCC_SOFT_ERROR_DATE 20150422)
19
20
21if(DEFINED LLVM_COMPILER_CHECKED)
22  return()
23endif()
24set(LLVM_COMPILER_CHECKED ON)
25
26if(LLVM_FORCE_USE_OLD_TOOLCHAIN)
27  return()
28endif()
29
30function(check_compiler_version NAME NICE_NAME MINIMUM_VERSION SOFT_ERROR_VERSION)
31  if(NOT CMAKE_CXX_COMPILER_ID STREQUAL NAME)
32    return()
33  endif()
34  if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS MINIMUM_VERSION)
35    message(FATAL_ERROR "Host ${NICE_NAME} version must be at least ${MINIMUM_VERSION}, your version is ${CMAKE_CXX_COMPILER_VERSION}.")
36  elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS SOFT_ERROR_VERSION)
37    if(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN)
38      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.")
39    else()
40      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.")
41    endif()
42  endif()
43endfunction(check_compiler_version)
44
45check_compiler_version("GNU" "GCC" ${GCC_MIN} ${GCC_SOFT_ERROR})
46check_compiler_version("Clang" "Clang" ${CLANG_MIN} ${CLANG_SOFT_ERROR})
47check_compiler_version("AppleClang" "Apple Clang" ${APPLECLANG_MIN} ${APPLECLANG_SOFT_ERROR})
48check_compiler_version("MSVC" "Visual Studio" ${MSVC_MIN} ${MSVC_SOFT_ERROR})
49
50if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
51  if (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
52    if (CMAKE_CXX_SIMULATE_VERSION VERSION_LESS MSVC_MIN)
53      message(FATAL_ERROR "Host Clang must have at least -fms-compatibility-version=${MSVC_MIN}, your version is ${CMAKE_CXX_SIMULATE_VERSION}.")
54    endif()
55    set(CLANG_CL 1)
56  elseif(NOT LLVM_ENABLE_LIBCXX)
57    # Test that we aren't using too old of a version of libstdc++.
58    set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
59    set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
60    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++0x")
61    # Test for libstdc++ version of at least 4.8 by checking for _ZNKSt17bad_function_call4whatEv.
62    # Note: We should check _GLIBCXX_RELEASE when possible (i.e., for GCC 7.1 and up).
63    check_cxx_source_compiles("
64#include <iosfwd>
65#if defined(__GLIBCXX__)
66#if __GLIBCXX__ < ${GCC_MIN_DATE}
67#error Unsupported libstdc++ version
68#endif
69#endif
70#if defined(__GLIBCXX__)
71extern const char _ZNKSt17bad_function_call4whatEv[];
72const char *chk = _ZNKSt17bad_function_call4whatEv;
73#else
74const char *chk = \"\";
75#endif
76int main() { ++chk; return 0; }
77"
78      LLVM_LIBSTDCXX_MIN)
79    if(NOT LLVM_LIBSTDCXX_MIN)
80      message(FATAL_ERROR "libstdc++ version must be at least ${GCC_MIN}.")
81    endif()
82    # Test for libstdc++ version of at least 5.1 by checking for std::iostream_category().
83    # Note: We should check _GLIBCXX_RELEASE when possible (i.e., for GCC 7.1 and up).
84    check_cxx_source_compiles("
85#include <iosfwd>
86#if defined(__GLIBCXX__)
87#if __GLIBCXX__ < ${GCC_SOFT_ERROR_DATE}
88#error Unsupported libstdc++ version
89#endif
90#endif
91#if defined(__GLIBCXX__)
92#include <ios>
93void foo(void) { (void) std::iostream_category(); }
94#endif
95int main() { return 0; }
96"
97      LLVM_LIBSTDCXX_SOFT_ERROR)
98    if(NOT LLVM_LIBSTDCXX_SOFT_ERROR)
99      if(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN)
100        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.")
101      else()
102        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.")
103      endif()
104    endif()
105    set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
106    set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
107  endif()
108endif()
109