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 5.1)
8set(GCC_SOFT_ERROR 7.1)
9set(CLANG_MIN 3.5)
10set(CLANG_SOFT_ERROR 5.0)
11set(APPLECLANG_MIN 6.0)
12set(APPLECLANG_SOFT_ERROR 9.3)
13
14# https://en.wikipedia.org/wiki/Microsoft_Visual_C#Internal_version_numbering
15# _MSC_VER == 1920 MSVC++ 14.20 Visual Studio 2019 Version 16.0
16# _MSC_VER == 1927 MSVC++ 14.27 Visual Studio 2019 Version 16.7
17set(MSVC_MIN 19.20)
18set(MSVC_SOFT_ERROR 19.27)
19
20# Map the above GCC versions to dates: https://gcc.gnu.org/develop.html#timeline
21set(GCC_MIN_DATE 20150422)
22set(LIBSTDCXX_SOFT_ERROR 7)
23
24
25if(DEFINED LLVM_COMPILER_CHECKED)
26  return()
27endif()
28set(LLVM_COMPILER_CHECKED ON)
29
30if(LLVM_FORCE_USE_OLD_TOOLCHAIN)
31  return()
32endif()
33
34function(check_compiler_version NAME NICE_NAME MINIMUM_VERSION SOFT_ERROR_VERSION)
35  if(NOT CMAKE_CXX_COMPILER_ID STREQUAL NAME)
36    return()
37  endif()
38  if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS MINIMUM_VERSION)
39    message(FATAL_ERROR "Host ${NICE_NAME} version must be at least ${MINIMUM_VERSION}, your version is ${CMAKE_CXX_COMPILER_VERSION}.")
40  elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS SOFT_ERROR_VERSION)
41    if(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN)
42      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.")
43    else()
44      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.")
45    endif()
46  endif()
47endfunction(check_compiler_version)
48
49check_compiler_version("GNU" "GCC" ${GCC_MIN} ${GCC_SOFT_ERROR})
50check_compiler_version("Clang" "Clang" ${CLANG_MIN} ${CLANG_SOFT_ERROR})
51check_compiler_version("AppleClang" "Apple Clang" ${APPLECLANG_MIN} ${APPLECLANG_SOFT_ERROR})
52check_compiler_version("MSVC" "Visual Studio" ${MSVC_MIN} ${MSVC_SOFT_ERROR})
53
54# See https://developercommunity.visualstudio.com/content/problem/845933/miscompile-boolean-condition-deduced-to-be-always.html
55# and thread "[llvm-dev] Longstanding failing tests - clang-tidy, MachO, Polly"
56# on llvm-dev Jan 21-23 2020.
57if ((${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) AND
58    (19.24 VERSION_LESS_EQUAL ${CMAKE_CXX_COMPILER_VERSION}) AND
59    (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 19.25))
60  if(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN)
61    message(WARNING "Host Visual Studio version 16.4 is known to miscompile part of LLVM")
62  else()
63    message(FATAL_ERROR "Host Visual Studio version 16.4 is known to miscompile part of LLVM, please use clang-cl or upgrade to 16.5 or above (use -DLLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN=ON to ignore)")
64  endif()
65endif()
66
67
68if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
69  if (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
70    if (CMAKE_CXX_SIMULATE_VERSION VERSION_LESS MSVC_MIN)
71      message(FATAL_ERROR "Host Clang must have at least -fms-compatibility-version=${MSVC_MIN}, your version is ${CMAKE_CXX_SIMULATE_VERSION}.")
72    endif()
73    set(CLANG_CL 1)
74  elseif(NOT LLVM_ENABLE_LIBCXX)
75    # Test that we aren't using too old of a version of libstdc++.
76    set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
77    set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
78    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++0x")
79    # Test for libstdc++ version of at least 4.8 by checking for _ZNKSt17bad_function_call4whatEv.
80    # Note: We should check _GLIBCXX_RELEASE when possible (i.e., for GCC 7.1 and up).
81    check_cxx_source_compiles("
82#include <iosfwd>
83#if defined(__GLIBCXX__)
84#if __GLIBCXX__ < ${GCC_MIN_DATE}
85#error Unsupported libstdc++ version
86#endif
87#endif
88#if defined(__GLIBCXX__)
89extern const char _ZNKSt17bad_function_call4whatEv[];
90const char *chk = _ZNKSt17bad_function_call4whatEv;
91#else
92const char *chk = \"\";
93#endif
94int main() { ++chk; return 0; }
95"
96      LLVM_LIBSTDCXX_MIN)
97    if(NOT LLVM_LIBSTDCXX_MIN)
98      message(FATAL_ERROR "libstdc++ version must be at least ${GCC_MIN}.")
99    endif()
100    check_cxx_source_compiles("
101#include <iosfwd>
102#if defined(__GLIBCXX__)
103#if !defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE < ${LIBSTDCXX_SOFT_ERROR}
104#error Unsupported libstdc++ version
105#endif
106#endif
107int main() { return 0; }
108"
109      LLVM_LIBSTDCXX_SOFT_ERROR)
110    if(NOT LLVM_LIBSTDCXX_SOFT_ERROR)
111      if(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN)
112        message(WARNING "libstdc++ version should be at least ${LIBSTDCXX_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.")
113      else()
114        message(FATAL_ERROR "libstdc++ version should be at least ${LIBSTDCXX_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.")
115      endif()
116    endif()
117    set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
118    set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
119  endif()
120endif()
121