Fix error: 'Bump' does not name a type (#9098)
[hiphop-php.git] / CMake / HPHPCompiler.cmake
blob1aa9d425953fb3c5003f9f7f3ea7849f9b3832bb
1 set(FREEBSD FALSE)
2 if("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
3   set(FREEBSD TRUE)
4 endif()
5 set(LINUX FALSE)
6 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
7   set(LINUX TRUE)
8 endif()
9 set(DARWIN FALSE)
10 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
11   set(DARWIN TRUE)
12 endif()
13 set(WINDOWS FALSE)
14 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
15   set(WINDOWS TRUE)
16 endif()
18 # Do this until cmake has a define for ARMv8
19 INCLUDE(CheckCXXSourceCompiles)
20 CHECK_CXX_SOURCE_COMPILES("
21 #ifndef __x86_64__
22 #error Not x64
23 #endif
24 int main() { return 0; }" IS_X64)
26 CHECK_CXX_SOURCE_COMPILES("
27 #ifndef __AARCH64EL__
28 #error Not ARMv8
29 #endif
30 int main() { return 0; }" IS_AARCH64)
32 # using Clang or GCC
33 if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
34   # Warnings to disable by name, -Wno-${name}
35   set(DISABLED_NAMED_WARNINGS)
36   list(APPEND DISABLED_NAMED_WARNINGS
37     "error=array-bounds"
38     "error=switch"
39     "attributes"
40     "deprecated"
41     "invalid-offsetof"
42     "register"
43     "sign-compare"
44     "strict-aliasing"
45     "unused-function"
46     "unused-local-typedefs"
47     "unused-result"
48     "write-strings"
49   )
51   # Warnings to disable by name when building C code.
52   set(DISABLED_C_NAMED_WARNINGS)
53   list(APPEND DISABLED_C_NAMED_WARNINGS
54     "missing-field-initializers"
55     "sign-compare"
56   )
58   # General options to pass to both C & C++ compilers
59   set(GENERAL_OPTIONS)
61   # General options to pass to the C++ compiler
62   set(GENERAL_CXX_OPTIONS)
63   list(APPEND GENERAL_CXX_OPTIONS
64     "std=gnu++1z"
65     "fno-omit-frame-pointer"
66     "fno-operator-names"
67     "Wall"
68     "Woverloaded-virtual"
69     "Werror=format-security"
70   )
72   # Options to pass for debug mode to the C++ compiler
73   set(DEBUG_CXX_OPTIONS)
75   # Options to pass for release mode to the C++ compiler
76   set(RELEASE_CXX_OPTIONS)
78   # Suboption of -g in debug mode
79   set(GDB_SUBOPTION)
81   # Enable GCC/LLVM stack-smashing protection
82   if(ENABLE_SSP)
83     list(APPEND GENERAL_OPTIONS
84       # This needs two dashes in the name, so put one here.
85       "-param=ssp-buffer-size=4"
86       "pie"
87       "fPIC"
88     )
89   endif()
91   if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang") # using Clang
92     if (IS_X64)
93       list(APPEND GENERAL_OPTIONS
94         # For unclear reasons, our detection for what crc32 intrinsics you have
95         # will cause clang to ICE. Specifying a baseline here works around the
96         # issue. (SSE4.2 has been available on processors for quite some time now.)
97         "msse4.2"
98       )
99       # Also need to pass the right option to ASM files to avoid inconsistencies
100       # in CRC hash function handling
101       set(CMAKE_ASM_FLAGS  "${CMAKE_ASM_FLAGS} -msse4.2")
102     endif()
104     list(APPEND GENERAL_CXX_OPTIONS
105       "Qunused-arguments"
106     )
107     list(APPEND DISABLED_C_NAMED_WARNINGS
108       "unused-command-line-argument"
109     )
110     list(APPEND DISABLED_NAMED_WARNINGS
111       "return-type-c-linkage"
112       "unknown-warning-option"
113       "unused-command-line-argument"
114     )
116     # Enabled GCC/LLVM stack-smashing protection
117     if(ENABLE_SSP)
118       if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.6 OR
119          CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 3.6)
120         list(APPEND GENERAL_OPTIONS "fstack-protector-strong")
121       else()
122         list(APPEND GENERAL_OPTIONS "fstack-protector")
123       endif()
124     endif()
126     if(CLANG_FORCE_LIBSTDCXX)
127       list(APPEND GENERAL_CXX_OPTIONS "stdlib=libstdc++")
128     else()
129       list(APPEND GENERAL_CXX_OPTIONS "stdlib=libc++")
130     endif()
131   else() # using GCC
132     list(APPEND DISABLED_NAMED_WARNINGS
133       "deprecated-declarations"
134       "maybe-uninitialized"
135     )
136     list(APPEND DISABLED_C_NAMED_WARNINGS
137       "maybe-uninitialized"
138       "old-style-declaration"
139     )
140     list(APPEND GENERAL_OPTIONS
141       "ffunction-sections"
142     )
143     list(APPEND GENERAL_CXX_OPTIONS
144       "fdata-sections"
145       "fno-gcse"
146       "fno-canonical-system-headers"
147       "Wvla"
148     )
149     list(APPEND RELEASE_CXX_OPTIONS
150       "-param max-inline-insns-auto=100"
151       "-param early-inlining-insns=200"
152       "-param max-early-inliner-iterations=50"
153       "-param=inline-unit-growth=200"
154       "-param=large-unit-insns=10000"
155     )
157     # Fix problem with GCC 4.9, https://kb.isc.org/article/AA-01167
158     if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 OR
159        CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.9)
160       list(APPEND GENERAL_OPTIONS "fno-delete-null-pointer-checks")
161     else()
162        message(FATAL_ERROR "${PROJECT_NAME} requires g++ 4.9 or greater.")
163     endif()
165     if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.4)
166       message(WARNING "HHVM is primarily tested on GCC 7.4-10.3. Using other versions may produce unexpected results, or may not even build at all.")
167     endif()
169     if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 10.3 OR
170        CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 10.3)
171      message(WARNING "HHVM is primarily tested on GCC 7.4-10.3. Using other versions may produce unexpected results, or may not even build at all.")
172     endif()
174     if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.1 OR
175        CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 5.1)
176       list(APPEND DISABLED_NAMED_WARNINGS "bool-compare")
177       list(APPEND GENERAL_OPTIONS "DFOLLY_HAVE_MALLOC_H")
178     endif()
180     if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0 OR
181        CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 6.0)
182       list(APPEND GENERAL_CXX_OPTIONS "Wno-misleading-indentation")
183     endif()
185     # Enabled GCC/LLVM stack-smashing protection
186     if(ENABLE_SSP)
187       if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8 OR
188          CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.8)
189         if(LINUX)
190           # https://isisblogs.poly.edu/2011/06/01/relro-relocation-read-only/
191           list(APPEND GENERAL_OPTIONS "Wl,-z,relro,-z,now")
192         endif()
193         if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 OR
194            CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.9)
195           list(APPEND GENERAL_OPTIONS "fstack-protector-strong")
196         endif()
197       else()
198         list(APPEND GENERAL_OPTIONS "fstack-protector")
199       endif()
200     endif()
202     # X64
203     if(IS_X64)
204       list(APPEND GENERAL_CXX_OPTIONS "mcrc32")
205         if(ENABLE_SSE4_2)
206           list(APPEND GENERAL_CXX_OPTIONS
207           # SSE4.2 has been available on processors for quite some time now. This
208           # allows enabling CRC hash function code
209           "msse4.2"
210           )
211           # Also pass the right option to ASM files to avoid inconsistencies
212           # in CRC hash function handling
213           set(CMAKE_ASM_FLAGS  "${CMAKE_ASM_FLAGS} -msse4.2")
214         endif()
215     endif()
217     # ARM64
218     if(IS_AARCH64)
219       # Force char type to be signed, which is not the case on aarch64.
220       list(APPEND GENERAL_OPTIONS "fsigned-char")
222       # If a CPU was specified, build a -mcpu option for the compiler.
223       set(AARCH64_TARGET_CPU "" CACHE STRING "CPU to tell gcc to optimize for (-mcpu)")
224       if(AARCH64_TARGET_CPU)
225         list(APPEND GENERAL_OPTIONS "mcpu=${AARCH64_TARGET_CPU}")
226         set(CMAKE_ASM_FLAGS  "${CMAKE_ASM_FLAGS} -mcpu=${AARCH64_TARGET_CPU}")
228         # Make sure GCC is not using the fix for errata 843419. This change
229         # interferes with the gold linker. Note that GCC applies this fix
230         # even if you specify an mcpu other than cortex-a53, which is why
231         # it's explicitly being disabled here for any cpu other than
232         # cortex-a53. If you're running a newer pass of the cortex-a53, then
233         # you can likely disable this fix with the following flag too. YMMV
234         if(NOT ${AARCH64_TARGET_CPU} STREQUAL "cortex-a53")
235           list(APPEND GENERAL_OPTIONS "mno-fix-cortex-a53-843419")
236         endif()
237       endif()
238     endif()
240     if(STATIC_CXX_LIB)
241       set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
242     endif()
244     if (ENABLE_SPLIT_DWARF)
245       set(GDB_SUBOPTION "split-dwarf")
246     endif()
247   endif()
249   # No optimizations for debug builds.
250   # -Og enables some optimizations, but makes debugging harder by optimizing
251   # away some functions and locals. -O0 is more debuggable.
252   # -O0-ggdb was reputed to cause gdb to crash (github #4450)
253   set(CMAKE_C_FLAGS_DEBUG            "-O0 -g${GDB_SUBOPTION}")
254   set(CMAKE_CXX_FLAGS_DEBUG          "-O0 -g${GDB_SUBOPTION}")
255   set(CMAKE_C_FLAGS_DEBUGOPT         "-O2 -g${GDB_SUBOPTION}")
256   set(CMAKE_CXX_FLAGS_DEBUGOPT       "-O2 -g${GDB_SUBOPTION}")
257   set(CMAKE_C_FLAGS_MINSIZEREL       "-Os -DNDEBUG")
258   set(CMAKE_CXX_FLAGS_MINSIZEREL     "-Os -DNDEBUG")
259   set(CMAKE_C_FLAGS_RELEASE          "-O3 -DNDEBUG")
260   set(CMAKE_CXX_FLAGS_RELEASE        "-O3 -DNDEBUG")
261   set(CMAKE_C_FLAGS_RELWITHDEBINFO   "-O2 -g${GDB_SUBOPTION} -DNDEBUG")
262   set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g${GDB_SUBOPTION} -DNDEBUG")
263   set(CMAKE_C_FLAGS                  "${CMAKE_C_FLAGS} -W -Werror=implicit-function-declaration")
265   mark_as_advanced(CMAKE_C_FLAGS_DEBUGOPT CMAKE_CXX_FLAGS_DEBUGOPT)
267   foreach(opt ${DISABLED_NAMED_WARNINGS})
268     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-${opt}")
269   endforeach()
271   foreach(opt ${DISABLED_C_NAMED_WARNINGS})
272     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-${opt}")
273   endforeach()
275   foreach(opt ${GENERAL_OPTIONS})
276     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -${opt}")
277     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -${opt}")
278   endforeach()
280   foreach(opt ${GENERAL_CXX_OPTIONS})
281     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -${opt}")
282   endforeach()
284   foreach(opt ${DEBUG_CXX_OPTIONS})
285     set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -${opt}")
286   endforeach()
288   foreach(opt ${RELEASE_CXX_OPTIONS})
289     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -${opt}")
290   endforeach()
292   # The ASM part of this makes it more effort than it's worth
293   # to add these to the general flags system.
294   if(ENABLE_AVX2)
295     set(CMAKE_C_FLAGS    "${CMAKE_C_FLAGS} -mavx2 -march=core-avx2")
296     set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -mavx2 -march=core-avx2")
297     set(CMAKE_ASM_FLAGS  "${CMAKE_ASM_FLAGS} -mavx2 -march=core-avx2")
298   endif()
299 # using Intel C++
300 elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel")
301   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -no-ipo -fp-model precise -wd584 -wd1418 -wd1918 -wd383 -wd869 -wd981 -wd424 -wd1419 -wd444 -wd271 -wd2259 -wd1572 -wd1599 -wd82 -wd177 -wd593 -w")
302   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -no-ipo -fp-model precise -wd584 -wd1418 -wd1918 -wd383 -wd869 -wd981 -wd424 -wd1419 -wd444 -wd271 -wd2259 -wd1572 -wd1599 -wd82 -wd177 -wd593 -fno-omit-frame-pointer -Wall -Woverloaded-virtual -Wno-deprecated -w1 -Wno-strict-aliasing -Wno-write-strings -Wno-invalid-offsetof -fno-operator-names")
303 # using Visual Studio C++
304 elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
305   message(WARNING "MSVC support is VERY experimental. It will likely not compile, and is intended for the utterly insane.")
307   ############################################################
308   # First we setup and account for the option sets.
309   ############################################################
311   set(MSVC_GENERAL_OPTIONS)
312   set(MSVC_DISABLED_WARNINGS)
313   set(MSVC_WARNINGS_AS_ERRORS)
314   set(MSVC_ADDITIONAL_DEFINES)
315   set(MSVC_EXE_LINKER_OPTIONS)
316   set(MSVC_DEBUG_OPTIONS)
317   set(MSVC_RELEASE_OPTIONS)
318   set(MSVC_RELEASE_LINKER_OPTIONS)
319   set(MSVC_DEBUG_EXE_LINKER_OPTIONS)
320   set(MSVC_RELEASE_EXE_LINKER_OPTIONS)
322   # Some additional configuration options.
323   set(MSVC_ENABLE_ALL_WARNINGS ON CACHE BOOL "If enabled, pass /Wall to the compiler.")
324   set(MSVC_ENABLE_DEBUG_INLINING ON CACHE BOOL "If enabled, enable inlining in the debug configuration. This allows /Zc:inline to be far more effective, resulting in hphp_runtime_static being ~450mb smaller.")
325   set(MSVC_ENABLE_LTCG OFF CACHE BOOL "If enabled, use Link Time Code Generation for Release builds.")
326   set(MSVC_ENABLE_PARALLEL_BUILD ON CACHE BOOL "If enabled, build multiple source files in parallel.")
327   set(MSVC_ENABLE_PCH ON CACHE BOOL "If enabled, use precompiled headers to speed up the build.")
328   set(MSVC_ENABLE_STATIC_ANALYSIS OFF CACHE BOOL "If enabled, do more complex static analysis and generate warnings appropriately.")
329   set(MSVC_FAVORED_ARCHITECTURE "blend" CACHE STRING "One of 'blend', 'AMD64', 'INTEL64', or 'ATOM'. This tells the compiler to generate code optimized to run best on the specified architecture.")
331   # The general options passed:
332   list(APPEND MSVC_GENERAL_OPTIONS
333     "bigobj" # Support objects with > 65k sections. Needed for folly due to templates.
334     "fp:precise" # Precise floating point model used in every other build, use it here as well.
335     "EHa" # Enable both SEH and C++ Exceptions.
336     "Oy-" # Disable elimination of stack frames.
337     "Zc:inline" # Have the compiler eliminate unreferenced COMDAT functions and data before emitting the object file. This produces significantly less input to the linker, resulting in MUCH faster linking.
338     "Zo" # Enable enhanced optimized debugging. Produces slightly larger pdb files, but the resulting optimized code is much much easier to debug.
339   )
341   # Enable all warnings if requested.
342   if (MSVC_ENABLE_ALL_WARNINGS)
343     list(APPEND MSVC_GENERAL_OPTIONS "Wall")
344   endif()
346   # Enable static analysis if requested.
347   if (MSVC_ENABLE_STATIC_ANALYSIS)
348     list(APPEND MSVC_GENERAL_OPTIONS "analyze")
349   endif()
351   # Enable multi-processor compilation if requested.
352   if (MSVC_ENABLE_PARALLEL_BUILD)
353     list(APPEND MSVC_GENERAL_OPTIONS "MP")
354   endif()
356   # Enable AVX2 codegen if available and requested.
357   if (ENABLE_AVX2)
358     list(APPEND MSVC_GENERAL_OPTIONS "arch:AVX2")
359   endif()
361   # Validate, and then add the favored architecture.
362   if (NOT MSVC_FAVORED_ARCHITECTURE STREQUAL "blend" AND NOT MSVC_FAVORED_ARCHITECTURE STREQUAL "AMD64" AND NOT MSVC_FAVORED_ARCHITECTURE STREQUAL "INTEL64" AND NOT MSVC_FAVORED_ARCHITECTURE STREQUAL "ATOM")
363     message(FATAL_ERROR "MSVC_FAVORED_ARCHITECTURE must be set to one of exactly, 'blend', 'AMD64', 'INTEL64', or 'ATOM'! Got '${MSVC_FAVORED_ARCHITECTURE}' instead!")
364   endif()
365   list(APPEND MSVC_GENERAL_OPTIONS "favor:${MSVC_FAVORED_ARCHITECTURE}")
367   # The warnings that are disabled:
368   list(APPEND MSVC_DISABLED_WARNINGS
369     "4068" # Unknown pragma.
370     "4091" # 'typedef' ignored on left of '' when no variable is declared.
371     "4101" # Unused variables
372     "4103" # Alignment changed after including header. This is needed because boost includes an ABI header that does some #pragma pack push/pop stuff, and we've passed our own packing
373     "4146" # Unary minus applied to unsigned type, result still unsigned.
374     "4250" # Function was inherited via dominance.
375     "4800" # Values being forced to bool, this happens many places, and is a "performance warning".
376   )
378   if (MSVC_ENABLE_ALL_WARNINGS)
379     # These warnings are disabled because we've
380     # enabled all warnings. If all warnings are
381     # not enabled, then these don't need to be
382     # disabled.
383     list(APPEND MSVC_DISABLED_WARNINGS
384       "4061" # Enum value not handled by a case in a switch on an enum. This isn't very helpful because it is produced even if a default statement is present.
385       "4100" # Unreferenced formal parameter.
386       "4127" # Conditional expression is constant.
387       "4131" # Old style declarator used. This is triggered by ext_bc's backend code.
388       "4189" # Local variable is initialized but not referenced.
389       "4191" # Unsafe type cast.
390       "4200" # Non-standard extension, zero sized array.
391       "4201" # Non-standard extension used: nameless struct/union.
392       "4232" # Non-standard extension used: 'pCurrent': address of dllimport.
393       "4245" # Implicit change from signed/unsigned when initializing.
394       "4255" # Implicitly converting function prototype from `()` to `(void)`.
395       "4265" # Class has virtual functions, but destructor is not virtual.
396       "4287" # Unsigned/negative constant mismatch.
397       "4296" # '<' Expression is always false.
398       "4315" # 'this' pointer for member may not be aligned to 8 bytes as expected by the constructor.
399       "4324" # Structure was padded due to alignment specifier.
400       "4355" # 'this' used in base member initializer list.
401       "4365" # Signed/unsigned mismatch.
402       "4371" # Layout of class may have changed due to fixes in packing.
403       "4388" # Signed/unsigned mismatch on relative comparison operator.
404       "4389" # Signed/unsigned mismatch on equality comparison operator.
405       "4435" # Object layout under /vd2 will change due to virtual base.
406       "4456" # Declaration of local hides previous definition of local by the same name.
407       "4457" # Declaration of local hides function parameter.
408       "4458" # Declaration of parameter hides class member.
409       "4459" # Declaration of parameter hides global declaration.
410       "4464" # Relative include path contains "..". This is triggered by the TBB headers.
411       "4505" # Unreferenced local function has been removed. This is mostly the result of things not being needed under MSVC.
412       "4514" # Unreferenced inline function has been removed. (caused by /Zc:inline)
413       "4548" # Expression before comma has no effect. I wouldn't disable this normally, but malloc.h triggers this warning.
414       "4555" # Expression has no effect; expected expression with side-effect. This is triggered by boost/variant.hpp.
415       "4574" # ifdef'd macro was defined to 0.
416       "4582" # Constructor is not implicitly called.
417       "4583" # Destructor is not implicitly called.
418       "4608" # Member has already been initialized by another union member initializer.
419       "4619" # Invalid warning number used in #pragma warning.
420       "4623" # Default constructor was implicitly defined as deleted.
421       "4625" # Copy constructor was implicitly defined as deleted.
422       "4626" # Assignment operator was implicitly defined as deleted.
423       "4647" # __is_pod() has a different value in pervious versions of MSVC.
424       "4668" # Macro was not defined, replacing with 0.
425       "4701" # Potentially uninitialized local variable used.
426       "4702" # Unreachable code.
427       "4706" # Assignment within conditional expression.
428       "4709" # Comma operator within array index expression. This currently just produces false-positives.
429       "4710" # Function was not inlined.
430       "4711" # Function was selected for automated inlining. This produces tens of thousands of warnings in release mode if you leave it enabled, which will completely break Visual Studio, so don't enable it.
431       "4714" # Function marked as __forceinline not inlined.
432       "4774" # Format string expected in argument is not a string literal.
433       "4820" # Padding added after data member.
434       "4917" # A GUID can only be associated with a class. This is triggered by some standard windows headers.
435       "4946" # reinterpret_cast used between related types.
436       "5026" # Move constructor was implicitly defined as deleted.
437       "5027" # Move assignment operator was implicitly defined as deleted.
438       "5031" # #pragma warning(pop): likely mismatch, popping warning state pushed in different file. This is needed because of how boost does things.
439     )
440   endif()
442   if (MSVC_ENABLE_STATIC_ANALYSIS)
443     # Warnings disabled for /analyze
444     list(APPEND MSVC_DISABLED_WARNINGS
445       "6001" # Using uninitialized memory. This is disabled because it is wrong 99% of the time.
446       "6011" # Dereferencing potentially NULL pointer.
447       "6031" # Return value ignored.
448       "6235" # (<non-zero constant> || <expression>) is always a non-zero constant.
449       "6237" # (<zero> && <expression>) is always zero. <expression> is never evaluated and may have side effects.
450       "6239" # (<non-zero constant> && <expression>) always evaluates to the result of <expression>.
451       "6240" # (<expression> && <non-zero constant>) always evaluates to the result of <expression>.
452       "6246" # Local declaration hides declaration of same name in outer scope.
453       "6248" # Setting a SECURITY_DESCRIPTOR's DACL to NULL will result in an unprotected object. This is done by one of the boost headers.
454       "6255" # _alloca indicates failure by raising a stack overflow exception.
455       "6262" # Function uses more than x bytes of stack space.
456       "6271" # Extra parameter passed to format function. The analysis pass doesn't recognize %j or %z, even though the runtime does.
457       "6285" # (<non-zero constant> || <non-zero constant>) is always true.
458       "6297" # 32-bit value is shifted then cast to 64-bits. The places this occurs never use more than 32 bits.
459       "6308" # Realloc might return null pointer: assigning null pointer to '<name>', which is passed as an argument to 'realloc', will cause the original memory to leak.
460       "6326" # Potential comparison of a constant with another constant.
461       "6330" # Unsigned/signed mismatch when passed as a parameter.
462       "6340" # Mismatch on sign when passed as format string value.
463       "6387" # '<value>' could be '0': This does not adhere to the specification for a function.
464       "28182" # Dereferencing NULL pointer. '<value>' contains the same NULL value as '<expression>'.
465       "28251" # Inconsistent annotation for function. This is because we only annotate the declaration and not the definition.
466       "28278" # Function appears with no prototype in scope.
467     )
468   endif()
470   # Warnings disabled to keep it quiet for now,
471   # most of these should be reviewed and re-enabled:
472   list(APPEND MSVC_DISABLED_WARNINGS
473     "4005" # Macro redefinition
474     "4018" # Signed/unsigned mismatch.
475     "4242" # Possible loss of data when returning a value.
476     "4244" # Implicit truncation of data.
477     "4267" # Implicit truncation of data. This really shouldn't be disabled.
478     "4291" # No matching destructor found.
479     "4302" # Pointer casting size difference
480     "4311" # Pointer casting size difference
481     "4312" # Pointer casting size difference
482     "4477" # Parameter to a formatting function isn't the same type as was passed in the format string.
483     "4624" # Destructor was implicitly undefined.
484     "4804" # Unsafe use of type 'bool' in operation. (comparing if bool is <=> scalar)
485     "4805" # Unsafe mix of scalar type and type 'bool' in operation. (comparing if bool is == scalar)
486   )
488   # Warnings to treat as errors:
489   list(APPEND MSVC_WARNINGS_AS_ERRORS
490     "4099" # Mixed use of struct and class on same type names. This was absolutely everywhere, and can cause errors at link-time if not fixed.
491     "4129" # Unknown escape sequence. This is usually caused by incorrect escaping.
492     "4566" # Character cannot be represented in current charset. This is remedied by prefixing string with "u8".
493   )
495   # And the extra defines:
496   list(APPEND MSVC_ADDITIONAL_DEFINES
497     "NOMINMAX" # This is needed because, for some absurd reason, one of the windows headers tries to define "min" and "max" as macros, which messes up most uses of std::numeric_limits.
498     "_CRT_NONSTDC_NO_WARNINGS" # Don't deprecate posix names of functions.
499     "_CRT_SECURE_NO_WARNINGS" # Don't deprecate the non _s versions of various standard library functions, because safety is for chumps.
500     "_SCL_SECURE_NO_WARNINGS" # Don't deprecate the non _s versions of various standard library functions, because safety is for chumps.
501     "_WINSOCK_DEPRECATED_NO_WARNINGS" # Don't deprecate pieces of winsock
502     "YY_NO_UNISTD_H" # Because MSVC doesn't have unistd.h, which is requested by the YACC generated code.
503   )
505   # The options passed to the linker for EXE targets:
506   list(APPEND MSVC_EXE_LINKER_OPTIONS
507     "BASE:0x10000" # Base the program at just over 64k in memory, to play nice with the JIT.
508     "DYNAMICBASE:NO" # Don't randomize the base address.
509     "FIXED" # The program can only be loaded at its preferred base address.
510     "STACK:8388608,8388608" # Set the stack reserve,commit to 8mb. Reserve should probably be higher.
511     "time" # Output some timing information about the link.
512   )
514   # The options to pass to the compiler for debug builds:
515   list(APPEND MSVC_DEBUG_OPTIONS
516     "Gy-" # Disable function level linking.
517     "GF-" # Disable string pooling.
518   )
520   # Add /Ob2 if allowing inlining in debug mode:
521   if (MSVC_ENABLE_DEBUG_INLINING)
522     list(APPEND MSVC_DEBUG_OPTIONS "Ob2")
523   endif()
525   # The options to pass to the compiler for release builds:
526   list(APPEND MSVC_RELEASE_OPTIONS
527     "GF" # Enable string pooling. (this is enabled by default by the optimization level, but we enable it here for clarity)
528     "Gw" # Optimize global data. (-fdata-sections)
529     "Gy" # Enable function level linking. (-ffunction-sections)
530     "Qpar" # Enable parallel code generation. HHVM itself doesn't currently use this, but it's dependencies, TBB for instance, might, so enable it.
531     "Oi" # Enable intrinsic functions.
532     "Ot" # Favor fast code.
533   )
535   # Add /GL to the compiler, and /LTCG to the linker
536   # if link time code generation is enabled.
537   if (MSVC_ENABLE_LTCG)
538     list(APPEND MSVC_RELEASE_OPTIONS "GL")
539     list(APPEND MSVC_RELEASE_LINKER_OPTIONS "LTCG")
540   endif()
542   # The options to pass to the linker for debug builds for EXE targets:
543   list(APPEND MSVC_DEBUG_EXE_LINKER_OPTIONS
544     "OPT:NOREF" # No unreferenced data elimination. (well, mostly)
545     "OPT:NOICF" # No Identical COMDAT folding.
546   )
548   # The options to pass to the linker for release builds for EXE targets:
549   list(APPEND MSVC_RELEASE_EXE_LINKER_OPTIONS
550     "OPT:REF" # Remove unreferenced functions and data.
551     "OPT:ICF" # Identical COMDAT folding.
552   )
554   ############################################################
555   # Now we need to adjust a couple of the default option sets.
556   ############################################################
558   # We need the static runtime.
559   foreach(flag_var
560       CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
561       CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
562       CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
563       CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
564     if (${flag_var} MATCHES "/MD")
565       string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
566     endif()
567   endforeach()
569   # In order for /Zc:inline, which speeds up the build significantly, to work
570   # we need to remove the /Ob0 parameter that CMake adds by default, because that
571   # would normally disable all inlining.
572   foreach(flag_var CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG)
573     if (${flag_var} MATCHES "/Ob0")
574       string(REGEX REPLACE "/Ob0" "" ${flag_var} "${${flag_var}}")
575     endif()
576   endforeach()
578   # Ignore a warning about an object file not defining any symbols,
579   # these are known, and we don't care.
580   set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221")
582   ############################################################
583   # And finally, we can set all the flags we've built up.
584   ############################################################
586   foreach(opt ${MSVC_GENERAL_OPTIONS})
587     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /${opt}")
588     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /${opt}")
589   endforeach()
591   foreach(opt ${MSVC_DISABLED_WARNINGS})
592     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd${opt}")
593     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd${opt}")
594   endforeach()
596   foreach(opt ${MSVC_WARNINGS_AS_ERRORS})
597     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /we${opt}")
598     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /we${opt}")
599   endforeach()
601   foreach(opt ${MSVC_ADDITIONAL_DEFINES})
602     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D ${opt}")
603     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D ${opt}")
604   endforeach()
606   foreach(opt ${MSVC_EXE_LINKER_OPTIONS})
607     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /${opt}")
608   endforeach()
610   foreach(opt ${MSVC_RELEASE_LINKER_OPTIONS})
611     foreach(flag_var
612         CMAKE_EXE_LINKER_FLAGS_RELEASE CMAKE_EXE_LINKER_FLAGS_MINSIZEREL CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO
613         CMAKE_SHARED_LINKER_FLAGS_RELEASE CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO
614         CMAKE_STATIC_LINKER_FLAGS_RELEASE CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO)
615       set(${flag_var} "${${flag_var}} /${opt}")
616     endforeach()
617   endforeach()
619   foreach(opt ${MSVC_DEBUG_OPTIONS})
620     set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /${opt}")
621     set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /${opt}")
622   endforeach()
624   foreach(opt ${MSVC_RELEASE_OPTIONS})
625     foreach(flag_var
626         CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
627         CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
628       set(${flag_var} "${${flag_var}} /${opt}")
629     endforeach()
630   endforeach()
632   foreach(opt ${MSVC_DEBUG_EXE_LINKER_OPTIONS})
633     set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /${opt}")
634   endforeach()
636   foreach(opt ${MSVC_RELEASE_EXE_LINKER_OPTIONS})
637     set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /${opt}")
638     set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /${opt}")
639     set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /${opt}")
640   endforeach()
641 else()
642   message("Warning: unknown/unsupported compiler, things may go wrong")
643 endif()
645 include(ThinArchives)