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 # Needed for __VA_OPT__() in macros.
12 add_compile_options(/Zc:preprocessor)
14 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
16 /MP # Enable multi-threaded compilation.
17 /FC # Display the full path of source code files passed to the compiler in diagnostics.
22 # Our strings are UTF-8.
24 add_compile_options(/utf-8)
26 add_compile_options(-finput-charset=utf-8)
29 # Add some -D flags for Debug builds. We cannot use add_definitions(), because
30 # it does not appear to support the $<> tags.
32 "$<$<CONFIG:Debug>:-D_DEBUG>"
33 "$<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=2>" # FORTIFY_SOURCE should only be used in non-debug builds (requires -O1+)
37 "$<$<NOT:$<CONFIG:Debug>>:-fstack-protector>" # Prevent undefined references when _FORTIFY_SOURCE > 0
39 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
41 "$<$<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, ...)
46 # Prepare a generator that checks if we are not a debug, and don't have asserts
47 # on. We need this later on to set some compile options for stable releases.
48 set(IS_STABLE_RELEASE "$<AND:$<NOT:$<CONFIG:Debug>>,$<NOT:$<BOOL:${OPTION_USE_ASSERTS}>>>")
53 /w34100 # 'identifier' : unreferenced formal parameter
54 /w34189 # 'identifier' : local variable is initialized but not referenced
56 if(MSVC_VERSION GREATER 1929 AND MSVC_VERSION LESS 1937 AND CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
57 # Starting with version 19.30 (fixed in version 19.37), there is an optimisation bug, see #9966 for details
58 # This flag disables the broken optimisation to work around the bug
59 add_compile_options(/d2ssa-rse-)
61 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
78 # We use 'ABCD' multichar for SaveLoad chunks identifiers
82 # Ninja processes the output so the output from the compiler
83 # isn't directly to a terminal; hence, the default is
84 # non-coloured output. We can override this to get nicely
85 # coloured output, but since that might yield odd results with
86 # IDEs, we extract it to an option.
87 if(OPTION_FORCE_COLORED_OUTPUT)
88 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
89 add_compile_options (-fdiagnostics-color=always)
90 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
91 add_compile_options (-fcolor-diagnostics)
95 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
96 include(CheckCXXCompilerFlag)
97 check_cxx_compiler_flag("-flifetime-dse=1" LIFETIME_DSE_FOUND)
100 # GCC 4.2+ automatically assumes that signed overflows do
101 # not occur in signed arithmetics, whereas we are not
102 # sure that they will not happen. It furthermore complains
103 # about its own optimized code in some places.
104 "-fno-strict-overflow"
106 # Prevent optimisation supposing enums are in a range specified by the standard
107 # For details, see http://gcc.gnu.org/PR43680
110 # -flifetime-dse=2 (default since GCC 6) doesn't play
111 # well with our custom pool item allocator
112 "$<$<BOOL:${LIFETIME_DSE_FOUND}>:-flifetime-dse=1>"
114 # We have a fight between clang wanting std::move() and gcc not wanting it
115 # and of course they both warn when the other compiler is happy
116 "-Wno-redundant-move"
120 if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
121 if (NOT CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
122 include(CheckCXXCompilerFlag)
123 check_cxx_compiler_flag("-mno-sse4" NO_SSE4_FOUND)
127 # Don't use SSE4 for general sources to increase compatibility.
133 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
136 # warning #873: function ... ::operator new ... has no corresponding operator delete ...
138 # warning #1292: unknown attribute "fallthrough"
140 # warning #1899: multicharacter character literal (potential portability problem)
142 # warning #2160: anonymous union qualifier is ignored
146 message(FATAL_ERROR "No warning flags are set for this compiler yet; please consider creating a Pull Request to add support for this compiler.")
149 if(NOT WIN32 AND NOT HAIKU)
150 # rdynamic is used to get useful stack traces from crash reports.
151 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")