From 8b42467226bfe269ba0a38c0ac7c36184d5ddd4c Mon Sep 17 00:00:00 2001 From: Menno Vink <26071817+MennoVink@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:21:04 +0100 Subject: [PATCH] MSVC warning fixes due to multiple values being provided for the same compiler setting --- scripts/CMake/CommonCppFlags.cmake | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/CMake/CommonCppFlags.cmake b/scripts/CMake/CommonCppFlags.cmake index 08b6af5c..e94f588c 100644 --- a/scripts/CMake/CommonCppFlags.cmake +++ b/scripts/CMake/CommonCppFlags.cmake @@ -73,7 +73,17 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") message(FATAL_ERROR "Building with a gcc version less than 4.7.3 is not supported.") endif() elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /W4 /permissive-") + # MSVC generates warnings when multiple /W# arguments are provided. If the parent project already specified a + # desired warning level we need to replace that instead of adding another warning level. + if(CMAKE_CXX_FLAGS MATCHES "[-/]W[0-4]") + string(REGEX REPLACE "[-/]W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() + # MSVC generates warnings when multiple standards are defined. The parent project may use the CMAKE_CXX_STANDARD + # variable to select te standard. Unset it so that our library code is still compiled with latest. + unset(CMAKE_CXX_STANDARD) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /permissive-") endif()