[Build] Use new way of setting /Z flag with MSVC

CMake 3.25 introduced the CMAKE_MSVC_DEBUG_INFORMATION_FORMAT to set the
/Z flag for MSVC toolchains. Use it rather than modifying the compile
options if possible. We revert to setting the compile options manually
if the CMake version is too old.
This commit is contained in:
Fabrice de Gans 2024-03-15 11:32:38 -07:00
parent 1d051d0e6e
commit d8a1886ccb
1 changed files with 17 additions and 3 deletions

View File

@ -32,13 +32,14 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(/Ob0 /Od /RTC1) add_compile_options(/Ob0 /Od /RTC1)
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND NOT ENABLE_ASAN) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND NOT ENABLE_ASAN)
# Use Edit and Continue with MSVC. # Use Edit and Continue with MSVC.
add_compile_options(/ZI) set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "EditAndContinue" CACHE STRING "" FORCE)
else() else()
add_compile_options(/Zi) set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "ProgramDatabase" CACHE STRING "" FORCE)
endif() endif()
else() else()
add_compile_options(/MT /Oi /Gy /Zi) add_compile_options(/MT /Oi /Gy)
add_link_options(/OPT:REF /OPT:ICF) add_link_options(/OPT:REF /OPT:ICF)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "ProgramDatabase" CACHE STRING "" FORCE)
if (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") if (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
add_compile_options(/O1 /Ob1) add_compile_options(/O1 /Ob1)
@ -49,6 +50,19 @@ else()
endif() endif()
endif() endif()
if(CMAKE_VERSION VERSION_LESS "3.25")
# Backwards-compatible way of setting the /Z option.
if(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT STREQUAL "EditAndContinue")
add_compile_options(/ZI)
elseif(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT STREQUAL "ProgramDatabase")
add_compile_options(/Zi)
elseif(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT STREQUAL "Embedded")
add_compile_options(/Z7)
else()
message(FATAL_ERROR "Unknown value for CMAKE_MSVC_DEBUG_INFORMATION_FORMAT: ${CMAKE_MSVC_DEBUG_INFORMATION_FORMAT}")
endif()
endif()
set(CMAKE_RC_FLAGS "-c65001 /DWIN32" CACHE STRING "" FORCE) set(CMAKE_RC_FLAGS "-c65001 /DWIN32" CACHE STRING "" FORCE)
# We need to explicitly set all of these to override the CMake defaults. # We need to explicitly set all of these to override the CMake defaults.