diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -58,8 +58,25 @@ # Must go after project(..) include(GNUInstallDirs) -set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to") +# This C++ standard is required to build LLVM. +set(LLVM_REQUIRED_CXX_STANDARD 17) + +# If we find that the cache contains CMAKE_CXX_STANDARD it means that it's a old CMakeCache.txt +# and we can just inform the user and then reset it. +if($CACHE{CMAKE_CXX_STANDARD} AND $CACHE{CMAKE_CXX_STANDARD} LESS ${LLVM_REQUIRED_CXX_STANDARD}) + message(WARNING "Resetting cache value for CMAKE_CXX_STANDARD to ${LLVM_REQUIRED_CXX_STANDARD}") + unset(CMAKE_CXX_STANDARD CACHE) +endif() + +# if CMAKE_CXX_STANDARD is still set after the cache unset above it means that the user requested it +# and we allow it to be set to something newer than the required standard but otherwise we fail. +if(DEFINED CMAKE_CXX_STANDARD AND CMAKE_CXX_STANDARD LESS ${LLVM_REQUIRED_CXX_STANDARD}) + message(FATAL_ERROR "Requested CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} which is less than the required ${LLVM_REQUIRED_CXX_STANDARD}.") +endif() + +set(CMAKE_CXX_STANDARD ${LLVM_REQUIRED_CXX_STANDARD} CACHE STRING "C++ standard to conform to") set(CMAKE_CXX_STANDARD_REQUIRED YES) + if (CYGWIN) # Cygwin is a bit stricter and lack things like 'strdup', 'stricmp', etc in # c++xx mode.