1 # Macro which contains all bits to setup the compile flags correctly.
7 if(VCPKG_TARGET_TRIPLET MATCHES "-static" AND NOT VCPKG_TARGET_TRIPLET MATCHES "-md")
8 # Switch to MT (static) instead of MD (dynamic) binary
10 # For MSVC two generators are available
11 # - a command line generator (Ninja) using CMAKE_BUILD_TYPE to specify the
12 # configuration of the build tree
13 # - an IDE generator (Visual Studio) using CMAKE_CONFIGURATION_TYPES to
14 # specify all configurations that will be available in the generated solution
15 list(APPEND MSVC_CONFIGS "${CMAKE_BUILD_TYPE}" "${CMAKE_CONFIGURATION_TYPES}")
17 # Set usage of static runtime for all configurations
18 foreach(MSVC_CONFIG ${MSVC_CONFIGS})
19 string(TOUPPER "CMAKE_CXX_FLAGS_${MSVC_CONFIG}" MSVC_FLAGS)
20 string(REPLACE "/MD" "/MT" ${MSVC_FLAGS} "${${MSVC_FLAGS}}")
24 # "If /Zc:rvalueCast is specified, the compiler follows section 5.4 of the
25 # C++11 standard". We need C++11 for the way we use threads.
26 add_compile_options(/Zc:rvalueCast)
28 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
30 /MP # Enable multi-threaded compilation.
31 /FC # Display the full path of source code files passed to the compiler in diagnostics.
36 # Add some -D flags for Debug builds. We cannot use add_definitions(), because
37 # it does not appear to support the $<> tags.
39 "$<$<CONFIG:Debug>:-D_DEBUG>"
40 "$<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=2>" # FORTIFY_SOURCE should only be used in non-debug builds (requires -O1+)
44 "$<$<NOT:$<CONFIG:Debug>>:-fstack-protector>" # Prevent undefined references when _FORTIFY_SOURCE > 0
48 # Prepare a generator that checks if we are not a debug, and don't have asserts
49 # on. We need this later on to set some compile options for stable releases.
50 set(IS_STABLE_RELEASE "$<AND:$<NOT:$<CONFIG:Debug>>,$<NOT:$<BOOL:${OPTION_USE_ASSERTS}>>>")
53 add_compile_options(/W3)
54 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
70 # Often parameters are unused, which is fine.
72 # We use 'ABCD' multichar for SaveLoad chunks identifiers
75 # Compilers complains about that we break strict-aliasing.
76 # On most places we don't see how to fix it, and it doesn't
77 # break anything. So disable strict-aliasing to make the
82 # When we are a stable release (Release build + USE_ASSERTS not set),
83 # assertations are off, which trigger a lot of warnings. We disable
84 # these warnings for these releases.
85 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
87 "$<${IS_STABLE_RELEASE}:-Wno-unused-variable>"
88 "$<${IS_STABLE_RELEASE}:-Wno-unused-but-set-parameter>"
89 "$<${IS_STABLE_RELEASE}:-Wno-unused-but-set-variable>"
93 "$<${IS_STABLE_RELEASE}:-Wno-unused-variable>"
94 "$<${IS_STABLE_RELEASE}:-Wno-unused-parameter>"
98 # Ninja processes the output so the output from the compiler
99 # isn't directly to a terminal; hence, the default is
100 # non-coloured output. We can override this to get nicely
101 # coloured output, but since that might yield odd results with
102 # IDEs, we extract it to an option.
103 if(OPTION_FORCE_COLORED_OUTPUT)
104 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
105 add_compile_options (-fdiagnostics-color=always)
106 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
107 add_compile_options (-fcolor-diagnostics)
111 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
112 include(CheckCXXCompilerFlag)
113 check_cxx_compiler_flag("-flifetime-dse=1" LIFETIME_DSE_FOUND)
116 # GCC 4.2+ automatically assumes that signed overflows do
117 # not occur in signed arithmetics, whereas we are not
118 # sure that they will not happen. It furthermore complains
119 # about its own optimized code in some places.
120 "-fno-strict-overflow"
122 # Prevent optimisation supposing enums are in a range specified by the standard
123 # For details, see http://gcc.gnu.org/PR43680
126 # -flifetime-dse=2 (default since GCC 6) doesn't play
127 # well with our custom pool item allocator
128 "$<$<BOOL:${LIFETIME_DSE_FOUND}>:-flifetime-dse=1>"
132 if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
133 if (NOT CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
134 include(CheckCXXCompilerFlag)
135 check_cxx_compiler_flag("-mno-sse4" NO_SSE4_FOUND)
139 # Don't use SSE4 for general sources to increase compatibility.
145 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
148 # warning #873: function ... ::operator new ... has no corresponding operator delete ...
150 # warning #1292: unknown attribute "fallthrough"
152 # warning #1899: multicharacter character literal (potential portability problem)
154 # warning #2160: anonymous union qualifier is ignored
158 message(FATAL_ERROR "No warning flags are set for this compiler yet; please consider creating a Pull Request to add support for this compiler.")
161 if(NOT WIN32 AND NOT HAIKU)
162 # rdynamic is used to get useful stack traces from crash reports.
163 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")