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