cmake: Use Policy CMP0117 for more sensible RTTI flag configuration on MSVC.

This commit is contained in:
Admiral H. Curtiss 2022-05-21 14:26:39 +02:00
parent 7f2ea688d2
commit a5e85627b0
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
3 changed files with 34 additions and 21 deletions

View File

@ -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()

View File

@ -3,6 +3,14 @@
# #
cmake_minimum_required(VERSION 3.10) 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. # Minimum OS X version.
# This is inserted into the Info.plist as well. # 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") 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) project(dolphin-emu)
if (MSVC) 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_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
@ -121,6 +128,7 @@ include(CheckAndAddFlag)
include(CheckCCompilerFlag) include(CheckCCompilerFlag)
include(CheckVendoringApproved) include(CheckVendoringApproved)
include(DolphinCompileDefinitions) include(DolphinCompileDefinitions)
include(RemoveCompileFlag)
# Enable folders for IDE # Enable folders for IDE
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
@ -258,21 +266,11 @@ elseif(CMAKE_GENERATOR MATCHES "Visual Studio")
endif() endif()
if(MSVC) 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) check_and_add_flag(EXCEPTIONS /EHsc)
dolphin_compile_definitions(_DEBUG DEBUG_ONLY) dolphin_compile_definitions(_DEBUG DEBUG_ONLY)
# Disable RTTI # Disable RTTI
# Unfortunately /GR is in the default compile flags for MSVC so we have to find and replace it. add_compile_options(/GR-)
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()
# Set warning level 4 (the highest) # Set warning level 4 (the highest)
add_compile_options(/W4) add_compile_options(/W4)

View File

@ -394,9 +394,8 @@ if (MSVC)
if ("${QT_VERSION_MAJOR}" GREATER_EQUAL 6) if ("${QT_VERSION_MAJOR}" GREATER_EQUAL 6)
# Qt6 requires RTTI # Qt6 requires RTTI
foreach (flag CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) remove_cxx_flag_from_target(dolphin-emu "/GR-")
string(REGEX REPLACE " /GR- " " /GR " ${flag} "${${flag}}") target_compile_options(dolphin-emu PRIVATE "/GR")
endforeach()
endif() endif()
endif() endif()