1 # Macro which contains all bits to setup the compile flags correctly.
7 # "If /Zc:rvalueCast is specified, the compiler follows section 5.4 of the
8 # C++11 standard". We need C++11 for the way we use threads.
9 add_compile_options(/Zc:rvalueCast)
11 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
13 /Zc:preprocessor # Needed for __VA_OPT__() in macros.
14 /MP # Enable multi-threaded compilation.
15 /FC # Display the full path of source code files passed to the compiler in diagnostics.
20 # Our strings are UTF-8.
22 add_compile_options(/utf-8)
24 add_compile_options(-finput-charset=utf-8)
27 # Add some -D flags for Debug builds. We cannot use add_definitions(), because
28 # it does not appear to support the $<> tags.
30 "$<$<CONFIG:Debug>:-D_DEBUG>"
31 "$<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=2>" # FORTIFY_SOURCE should only be used in non-debug builds (requires -O1+)
35 "$<$<NOT:$<CONFIG:Debug>>:-fstack-protector>" # Prevent undefined references when _FORTIFY_SOURCE > 0
37 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
39 "$<$<CONFIG:Debug>:-Wa,-mbig-obj>" # Switch to pe-bigobj-x86-64 as x64 Debug builds push pe-x86-64 to the limits (linking errors with ASLR, ...)
44 # Prepare a generator that checks if we are not a debug, and don't have asserts
45 # on. We need this later on to set some compile options for stable releases.
46 set(IS_STABLE_RELEASE "$<AND:$<NOT:$<CONFIG:Debug>>,$<NOT:$<BOOL:${OPTION_USE_ASSERTS}>>>")
51 /w34100 # 'identifier' : unreferenced formal parameter
52 /w34189 # 'identifier' : local variable is initialized but not referenced
54 if(MSVC_VERSION GREATER 1929 AND MSVC_VERSION LESS 1937 AND CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
55 # Starting with version 19.30 (fixed in version 19.37), there is an optimisation bug, see #9966 for details
56 # This flag disables the broken optimisation to work around the bug
57 add_compile_options(/d2ssa-rse-)
59 if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
64 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
81 # We use 'ABCD' multichar for SaveLoad chunks identifiers
85 # Ninja processes the output so the output from the compiler
86 # isn't directly to a terminal; hence, the default is
87 # non-coloured output. We can override this to get nicely
88 # coloured output, but since that might yield odd results with
89 # IDEs, we extract it to an option.
90 if(OPTION_FORCE_COLORED_OUTPUT)
91 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
92 add_compile_options (-fdiagnostics-color=always)
93 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
94 add_compile_options (-fcolor-diagnostics)
98 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
99 include(CheckCXXCompilerFlag)
100 check_cxx_compiler_flag("-flifetime-dse=1" LIFETIME_DSE_FOUND)
103 # GCC 4.2+ automatically assumes that signed overflows do
104 # not occur in signed arithmetics, whereas we are not
105 # sure that they will not happen. It furthermore complains
106 # about its own optimized code in some places.
107 "-fno-strict-overflow"
109 # Prevent optimisation supposing enums are in a range specified by the standard
110 # For details, see http://gcc.gnu.org/PR43680
113 # -flifetime-dse=2 (default since GCC 6) doesn't play
114 # well with our custom pool item allocator
115 "$<$<BOOL:${LIFETIME_DSE_FOUND}>:-flifetime-dse=1>"
117 # We have a fight between clang wanting std::move() and gcc not wanting it
118 # and of course they both warn when the other compiler is happy
119 "-Wno-redundant-move"
123 if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
124 if (NOT CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
125 include(CheckCXXCompilerFlag)
126 check_cxx_compiler_flag("-mno-sse4" NO_SSE4_FOUND)
130 # Don't use SSE4 for general sources to increase compatibility.
136 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
139 # warning #873: function ... ::operator new ... has no corresponding operator delete ...
141 # warning #1292: unknown attribute "fallthrough"
143 # warning #1899: multicharacter character literal (potential portability problem)
145 # warning #2160: anonymous union qualifier is ignored
149 message(FATAL_ERROR "No warning flags are set for this compiler yet; please consider creating a Pull Request to add support for this compiler.")
152 if(NOT WIN32 AND NOT HAIKU)
153 # rdynamic is used to get useful stack traces from crash reports.
154 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")