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