CMake/Tests/RunCMake/MSVCRuntimeChecks/CMP0184-common.cmake
AJIOB 2b2344b412 MSVC: Add abstraction for runtime checks
Replace our hard-coded default for `/RTC1` with a first-class
abstraction to select runtime checks from an enumeration of logical
names.  Add a `MSVC_RUNTIME_CHECKS` target property and corresponding
`CMAKE_MSVC_RUNTIME_CHECKS` variable.

Removing the old default flag requires a policy because existing
projects may rely on string processing to edit them and choose
runtime checks under the old behavior.  Add policy CMP0184 to
provide compatibility.

Fixes: #26614
2025-01-29 13:07:41 -05:00

26 lines
839 B
CMake

enable_language(C)
cmake_policy(GET CMP0184 cmp0184)
if(cmp0184 STREQUAL "NEW")
if(NOT CMAKE_MSVC_RUNTIME_CHECKS_DEFAULT)
message(SEND_ERROR "CMAKE_MSVC_RUNTIME_CHECKS_DEFAULT not set under NEW behavior")
endif()
else()
if(CMAKE_MSVC_RUNTIME_CHECKS_DEFAULT)
message(SEND_ERROR "CMAKE_MSVC_RUNTIME_CHECKS_DEFAULT is set under OLD behavior")
endif()
endif()
if(cmp0184 STREQUAL "NEW")
if(CMAKE_C_FLAGS_DEBUG MATCHES "[/-](RTC1|GZ)( |$)")
message(SEND_ERROR "CMAKE_C_FLAGS_DEBUG has -RTC1 flag under NEW behavior:\n ${CMAKE_C_FLAGS_DEBUG}")
endif()
else()
if(NOT (CMAKE_C_FLAGS_DEBUG MATCHES "/(RTC1|GZ)( |$)"))
message(SEND_ERROR "CMAKE_C_FLAGS_DEBUG does not have /RTC1 flag under OLD behavior:\n ${CMAKE_C_FLAGS_DEBUG}")
endif()
endif()
set(CMAKE_MSVC_RUNTIME_CHECKS BogusValue)
add_library(foo empty.c)