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