From 398f9016b6860480e43412efac9c938dbf933f2b Mon Sep 17 00:00:00 2001 From: Diego Dassie Date: Mon, 17 Jun 2024 16:48:33 +0200 Subject: [PATCH] fix(MSVC): Don't enforce /Zi if POLICY CMP0141 is available With the current implementation if CMAKE_MSVC_DEBUG_INFORMATION_FORMAT is set, it won't be respected as the /Zi flag is always set in enable_sanitizers. If POLICY CMP0141 is available, don't set the /Zi flag, instead use MSVC_DEBUG_INFORMATION_FORMAT, if the user didn't set CMAKE_MSVC_DEBUG_INFORMATION_FORMAT or set it to EditAndContinue, than only in that case set it to ProgramDatabase. --- src/Index.cmake | 5 +++++ src/Sanitizers.cmake | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Index.cmake b/src/Index.cmake index 94d4b936..644aff6f 100644 --- a/src/Index.cmake +++ b/src/Index.cmake @@ -11,6 +11,11 @@ if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") cmake_policy(SET CMP0138 NEW) endif() +if(POLICY CMP0141) + # MSVC debug information format flags are selected by an abstraction. + cmake_policy(SET CMP0141 NEW) +endif() + # only useable here set(ProjectOptions_SRC_DIR "${CMAKE_CURRENT_LIST_DIR}") diff --git a/src/Sanitizers.cmake b/src/Sanitizers.cmake index ea410eac..af1fde11 100644 --- a/src/Sanitizers.cmake +++ b/src/Sanitizers.cmake @@ -86,8 +86,19 @@ function( "Using MSVC sanitizers requires setting the MSVC environment before building the project. Please manually open the MSVC command prompt and rebuild the project." ) endif() + if(POLICY CMP0141) + if("${CMAKE_MSVC_DEBUG_INFORMATION_FORMAT}" STREQUAL "" + OR "${CMAKE_MSVC_DEBUG_INFORMATION_FORMAT}" STREQUAL "EditAndContinue" + ) + set_target_properties( + ${_project_name} PROPERTIES MSVC_DEBUG_INFORMATION_FORMAT ProgramDatabase + ) + endif() + else() + target_compile_options(${_project_name} INTERFACE /Zi) + endif() target_compile_options( - ${_project_name} INTERFACE /fsanitize=${LIST_OF_SANITIZERS} /Zi /INCREMENTAL:NO + ${_project_name} INTERFACE /fsanitize=${LIST_OF_SANITIZERS} /INCREMENTAL:NO ) target_link_options(${_project_name} INTERFACE /INCREMENTAL:NO) endif()