From d8a1886ccb3e9942a1b8ac2e2f9a0da415f9e3d3 Mon Sep 17 00:00:00 2001 From: Fabrice de Gans Date: Fri, 15 Mar 2024 11:32:38 -0700 Subject: [PATCH] [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. --- cmake/Toolchain-msvc.cmake | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/cmake/Toolchain-msvc.cmake b/cmake/Toolchain-msvc.cmake index 7cd0fbdb..d5cb12e9 100644 --- a/cmake/Toolchain-msvc.cmake +++ b/cmake/Toolchain-msvc.cmake @@ -32,13 +32,14 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug") add_compile_options(/Ob0 /Od /RTC1) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND NOT ENABLE_ASAN) # Use Edit and Continue with MSVC. - add_compile_options(/ZI) + set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "EditAndContinue" CACHE STRING "" FORCE) else() - add_compile_options(/Zi) + set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "ProgramDatabase" CACHE STRING "" FORCE) endif() else() - add_compile_options(/MT /Oi /Gy /Zi) + add_compile_options(/MT /Oi /Gy) add_link_options(/OPT:REF /OPT:ICF) + set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "ProgramDatabase" CACHE STRING "" FORCE) if (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") add_compile_options(/O1 /Ob1) @@ -49,6 +50,19 @@ else() 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) # We need to explicitly set all of these to override the CMake defaults.