CMake: Fix MSVC flags for Release/RelWithDebInfo

See https://gitlab.kitware.com/cmake/cmake/-/issues/20812

Manually redefine MSVC flags to match Visual Studio defaults
and ensure that Release builds generate debug info.
This commit is contained in:
Léo Lam 2021-04-25 19:41:07 +02:00
parent 20d00dfc79
commit d0484a9ea9
No known key found for this signature in database
GPG Key ID: 0DF30F9081000741
1 changed files with 10 additions and 6 deletions

View File

@ -1,7 +1,11 @@
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
foreach(f CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELWITHDEBINFO) # The default MSVC flags for Release and RelWithDebInfo are poorly chosen
if("${${f}}" MATCHES "/Zi") # (see issue https://gitlab.kitware.com/cmake/cmake/-/issues/20812)
string(REGEX REPLACE "/Zi" "/Z7" "${f}" "${${f}}") # By default, inlining is disabled for RelWithDebInfo.
endif() # Manually redefine MSVC flags to match Visual Studio defaults
# and ensure that Release builds generate debug info.
foreach(f CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELEASE)
# optimize, define NDEBUG, generate debug info
set(${f} "/O2 /DNDEBUG /Z7")
endforeach() endforeach()
endif() endif()