[CMake] Check CMAKE_CXX_STANDARD and error if it's to old

Also nuke the old value out of the cache if it's there, otherwise
it could lead to problems when the project increases the standard
and the developer just runs the "make/ninja" program.

Reviewed By: royjacobson

Differential Revision: https://reviews.llvm.org/D131367
This commit is contained in:
Tobias Hieta 2022-08-07 21:52:37 +02:00
parent 0a5c344a84
commit d4abdd2e3d
No known key found for this signature in database
GPG Key ID: 44F2485E45D59042
1 changed files with 18 additions and 1 deletions

View File

@ -58,8 +58,25 @@ project(LLVM
# 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.