From a5e85627b04ed8d05ccccfaf0aac5f83ec6ace0c Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Sat, 21 May 2022 14:26:39 +0200 Subject: [PATCH] cmake: Use Policy CMP0117 for more sensible RTTI flag configuration on MSVC. --- CMake/RemoveCompileFlag.cmake | 16 +++++++++++++ CMakeLists.txt | 34 +++++++++++++--------------- Source/Core/DolphinQt/CMakeLists.txt | 5 ++-- 3 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 CMake/RemoveCompileFlag.cmake diff --git a/CMake/RemoveCompileFlag.cmake b/CMake/RemoveCompileFlag.cmake new file mode 100644 index 0000000000..1bb9c167be --- /dev/null +++ b/CMake/RemoveCompileFlag.cmake @@ -0,0 +1,16 @@ +# from https://stackoverflow.com/a/49216539 +# The linked answer does some weird preconfiguring by manually splitting the CMAKE_CXX_FLAGS variable and applying it to a target, +# but as far as I can tell none of that is necessary, this works just fine as-is. + +# +# Removes the specified compile flag from the specified target. +# _target - The target to remove the compile flag from +# _flag - The compile flag to remove +# +macro(remove_cxx_flag_from_target _target _flag) + get_target_property(_target_cxx_flags ${_target} COMPILE_OPTIONS) + if(_target_cxx_flags) + list(REMOVE_ITEM _target_cxx_flags ${_flag}) + set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "${_target_cxx_flags}") + endif() +endmacro() diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bbb6d3b69..2428a093b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,14 @@ # cmake_minimum_required(VERSION 3.10) +# Weird chicken-and-egg problem: We can't check the compiler before the project() call, but we have to set the policies before it. +# So we do this in two steps: Set the policies if they exist, then error out afterwards if we end up being MSVC and they don't exist. +if (POLICY CMP0117) + cmake_policy(SET CMP0091 NEW) # MSVC runtime library flags are selected by an abstraction. + cmake_policy(SET CMP0092 NEW) # MSVC warning flags are not in CMAKE_{C,CXX}_FLAGS by default. + cmake_policy(SET CMP0117 NEW) # MSVC RTTI flag will not be added by default. +endif() + # Minimum OS X version. # This is inserted into the Info.plist as well. @@ -16,16 +24,15 @@ set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14.0" CACHE STRING "") set(CMAKE_USER_MAKE_RULES_OVERRIDE "CMake/FlagsOverride.cmake") -# Optionally enable these polcies so older versions of cmake don't break. -# we only need them for MSVC -if(POLICY CMP0091) - cmake_policy(SET CMP0091 NEW) # MSVC runtime library flags are selected by an abstraction. - cmake_policy(SET CMP0092 NEW) # MSVC warning flags are not in CMAKE_{C,CXX}_FLAGS by default. -endif() - project(dolphin-emu) if (MSVC) + if (POLICY CMP0117) + # cmake is a weird language. You can't do if(not POLICY) + else() + message(FATAL_ERROR "Please update to CMake 3.20 or higher.") + endif() + set(CMAKE_C_STANDARD 99) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -121,6 +128,7 @@ include(CheckAndAddFlag) include(CheckCCompilerFlag) include(CheckVendoringApproved) include(DolphinCompileDefinitions) +include(RemoveCompileFlag) # Enable folders for IDE set_property(GLOBAL PROPERTY USE_FOLDERS ON) @@ -258,21 +266,11 @@ elseif(CMAKE_GENERATOR MATCHES "Visual Studio") endif() if(MSVC) - if(POLICY CMP0091) - # cmake is a weird language. You can't do if(not POLICY) - else() - # We really kind of want this policy - message(FATAL_ERROR "please update cmake to at least 3.15") - endif() - check_and_add_flag(EXCEPTIONS /EHsc) dolphin_compile_definitions(_DEBUG DEBUG_ONLY) # Disable RTTI - # Unfortunately /GR is in the default compile flags for MSVC so we have to find and replace it. - foreach (flag CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) - string(REGEX REPLACE " /GR " " /GR- " ${flag} "${${flag}}") - endforeach() + add_compile_options(/GR-) # Set warning level 4 (the highest) add_compile_options(/W4) diff --git a/Source/Core/DolphinQt/CMakeLists.txt b/Source/Core/DolphinQt/CMakeLists.txt index 3398bb2695..3e19006952 100644 --- a/Source/Core/DolphinQt/CMakeLists.txt +++ b/Source/Core/DolphinQt/CMakeLists.txt @@ -394,9 +394,8 @@ if (MSVC) if ("${QT_VERSION_MAJOR}" GREATER_EQUAL 6) # Qt6 requires RTTI - foreach (flag CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) - string(REGEX REPLACE " /GR- " " /GR " ${flag} "${${flag}}") - endforeach() + remove_cxx_flag_from_target(dolphin-emu "/GR-") + target_compile_options(dolphin-emu PRIVATE "/GR") endif() endif()