cmake: Upgrade C/C++ compiler adjustments to modern CMake

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-09-28 02:07:53 +02:00
parent f3a57513ad
commit 808da4894b
1 changed files with 46 additions and 50 deletions

View File

@ -101,57 +101,53 @@ else()
set(ARCH "x64") set(ARCH "x64")
endif() endif()
# All Warnings, Extra Warnings, Pedantic ################################################################################
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") # C/C++ Compiler Adjustments
# using Clang ################################################################################
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-missing-braces -Wmissing-field-initializers -Wno-c++98-compat-pedantic -Wold-style-cast -Wno-documentation -Wno-documentation-unknown-command -Wno-covered-switch-default -Wno-switch-enum") if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR (WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-missing-braces -Wmissing-field-initializers -Wno-c++98-compat-pedantic -Wold-style-cast -Wno-documentation -Wno-documentation-unknown-command -Wno-covered-switch-default -Wno-switch-enum") message(STATUS "Applying custom flags for MSVC style build.")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# GCC: -fpermissive is required as GCC does not allow the same template to be in different namespaces.
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fpermissive -Wno-long-long -Wno-missing-braces -Wmissing-field-initializers")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fpermissive -Wno-long-long -Wno-missing-braces -Wmissing-field-initializers")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# using Intel C++
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(MSVC_EXTRA_FLAGS "/wd4061 /wd4100 /wd4180 /wd4201 /wd4464 /wd4505 /wd4514 /wd4571 /wd4623 /wd4625 /wd4626 /wd4668 /wd4710 /wd4774 /wd4820 /wd5026 /wd5027 /wd5039 /wd5045 /wd26812")
# Force to always compile with W4
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/Wall" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MSVC_EXTRA_FLAGS}")
if(CMAKE_C_FLAGS MATCHES "/W[0-4]") # MSVC/ClangCL
string(REGEX REPLACE "/W[0-4]" "/Wall" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") # - Dynamically link Microsoft C/C++ Redistributable.
else() # - Enable /W3 and disable useless warnings.
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Wall") # - Enable C++ exceptions with SEH exceptions.
endif() # - Enable multi-processor compiling.
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MSVC_EXTRA_FLAGS}")
if("${PropertyPrefix}" STREQUAL "") # Enable most useful warnings.
# Speed Optimized Configuration set(DISABLED_WARNINGS
set(_SPEED_COMPILER_FLAGS "/O2 /Oi /Ot /Oy /GF /GS- /Qpar /arch:AVX /GR /GL") "/wd4061" "/wd4100" "/wd4180" "/wd4201" "/wd4464" "/wd4505" "/wd4514"
set(_SPEED_LINKER_FLAGS "/LTCG:incremental /OPT:ICF=4 /INCREMENTAL /OPT:REF") "/wd4571" "/wd4623" "/wd4625" "/wd4626" "/wd4668" "/wd4710" "/wd4774"
set(CMAKE_CXX_FLAGS_SPEED "${CMAKE_CXX_FLAGS_RELWTIHDEBINFO} ${_SPEED_COMPILER_FLAGS}") "/wd4820" "/wd5026" "/wd5027" "/wd5039" "/wd5045" "/wd26812"
set(CMAKE_C_FLAGS_SPEED "${CMAKE_C_FLAGS_RELWTIHDEBINFO} ${_SPEED_COMPILER_FLAGS}") )
set(CMAKE_EXE_LINKER_FLAGS_SPEED "${CMAKE_EXE_LINKER_FLAGS_RELWTIHDEBINFO} ${_SPEED_LINKER_FLAGS}") add_compile_options("/W3")
set(CMAKE_SHARED_LINKER_FLAGS_SPEED "${CMAKE_EXE_LINKER_FLAGS_RELWTIHDEBINFO} ${_SPEED_LINKER_FLAGS}") foreach(WARN ${DISABLED_WARNINGS})
set(CMAKE_MODULE_LINKER_FLAGS_SPEED "${CMAKE_EXE_LINKER_FLAGS_RELWTIHDEBINFO} ${_SPEED_LINKER_FLAGS}") add_compile_options("${WARN}")
mark_as_advanced( endforeach()
CMAKE_CXX_FLAGS_SPEED
CMAKE_C_FLAGS_SPEED # Build with dynamic MSVC linkage.
CMAKE_EXE_LINKER_FLAGS_SPEED add_compile_options(
CMAKE_SHARED_LINKER_FLAGS_SPEED $<$<CONFIG:>:/MD>
CMAKE_MODULE_LINKER_FLAGS_SPEED $<$<CONFIG:Debug>:/MDd>
$<$<CONFIG:Release>:/MD>
$<$<CONFIG:RelWithDebInfo>:/MD>
$<$<CONFIG:MinSizeRel>:/MD>
) )
if(CMAKE_CONFIGURATION_TYPES) # C++ Exceptions & SEH
list(APPEND CMAKE_CONFIGURATION_TYPES Speed) add_compile_options("/EHa")
list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
CacheSet(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}") # Multiprocessor compiling
endif() add_compile_options("/MP")
endif() elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(STATUS "Applying custom flags for GNU style build.")
# Clang/AppleClang/GNU
# - Don't export by default.
# - Enable all and extra warnings.
add_compile_options("-Wall")
add_compile_options("-Wextra")
add_compile_options("-fvisibility=hidden")
endif() endif()
# C++ Standard and Extensions # C++ Standard and Extensions