Fix reliance on the sign of char in fnv32 and fnv64
[folly.git] / CMake / FollyCompiler.cmake
1 # Some additional configuration options.\r
2 option(MSVC_ENABLE_ALL_WARNINGS "If enabled, pass /Wall to the compiler." ON)\r
3 option(MSVC_ENABLE_CPP_LATEST "If enabled, pass /std:c++latest to the compiler" ON)\r
4 option(MSVC_ENABLE_DEBUG_INLINING "If enabled, enable inlining in the debug configuration. This allows /Zc:inline to be far more effective." OFF)\r
5 option(MSVC_ENABLE_FAST_LINK "If enabled, pass /DEBUG:FASTLINK to the linker. This makes linking faster, but the gtest integration for Visual Studio can't currently handle the .pdbs generated." OFF)\r
6 option(MSVC_ENABLE_LEAN_AND_MEAN_WINDOWS "If enabled, define WIN32_LEAN_AND_MEAN to include a smaller subset of Windows.h" ON)\r
7 option(MSVC_ENABLE_LTCG "If enabled, use Link Time Code Generation for Release builds." OFF)\r
8 option(MSVC_ENABLE_PARALLEL_BUILD "If enabled, build multiple source files in parallel." ON)\r
9 option(MSVC_ENABLE_STATIC_ANALYSIS "If enabled, do more complex static analysis and generate warnings appropriately." OFF)\r
10 option(MSVC_USE_STATIC_RUNTIME "If enabled, build against the static, rather than the dynamic, runtime." OFF)\r
11 \r
12 # Alas, option() doesn't support string values.\r
13 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.")\r
14 # Add a pretty drop-down selector for these values when using the GUI.\r
15 set_property(\r
16   CACHE MSVC_FAVORED_ARCHITECTURE\r
17   PROPERTY STRINGS\r
18     blend\r
19     AMD64\r
20     ATOM\r
21     INTEL64\r
22 )\r
23 # Validate, and then add the favored architecture.\r
24 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")\r
25   message(FATAL_ERROR "MSVC_FAVORED_ARCHITECTURE must be set to one of exactly, 'blend', 'AMD64', 'INTEL64', or 'ATOM'! Got '${MSVC_FAVORED_ARCHITECTURE}' instead!")\r
26 endif()\r
27 \r
28 ############################################################\r
29 # We need to adjust a couple of the default option sets.\r
30 ############################################################\r
31 \r
32 # If the static runtime is requested, we have to\r
33 # overwrite some of CMake's defaults.\r
34 if (MSVC_USE_STATIC_RUNTIME)\r
35   foreach(flag_var\r
36       CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE\r
37       CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO\r
38       CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE\r
39       CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)\r
40     if (${flag_var} MATCHES "/MD")\r
41       string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")\r
42     endif()\r
43   endforeach()\r
44 endif()\r
45 \r
46 # In order for /Zc:inline, which speeds up the build significantly, to work\r
47 # we need to remove the /Ob0 parameter that CMake adds by default, because that\r
48 # would normally disable all inlining.\r
49 foreach(flag_var CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG)\r
50   if (${flag_var} MATCHES "/Ob0")\r
51     string(REGEX REPLACE "/Ob0" "" ${flag_var} "${${flag_var}}")\r
52   endif()\r
53 endforeach()\r
54 \r
55 # Apply the option set for Folly to the specified target.\r
56 function(apply_folly_compile_options_to_target THETARGET)\r
57   # The general options passed:\r
58   target_compile_options(${THETARGET}\r
59     PUBLIC\r
60       /EHa # Enable both SEH and C++ Exceptions.\r
61       /Zc:referenceBinding # Disallow temporaries from binding to non-const lvalue references.\r
62       /Zc:rvalueCast # Enforce the standard rules for explicit type conversion.\r
63       /Zc:implicitNoexcept # Enable implicit noexcept specifications where required, such as destructors.\r
64       /Zc:strictStrings # Don't allow conversion from a string literal to mutable characters.\r
65       /Zc:threadSafeInit # Enable thread-safe function-local statics initialization.\r
66       /Zc:throwingNew # Assume operator new throws on failure.\r
67 \r
68       $<$<BOOL:${MSVC_ENABLE_CPP_LATEST}>:/std:c++latest> # Build in C++ Latest mode if requested.\r
69 \r
70       # This is only supported by MSVC 2017\r
71       $<$<BOOL:${MSVC_IS_2017}>:/permissive-> # Be mean, don't allow bad non-standard stuff (C++/CLI, __declspec, etc. are all left intact).\r
72     PRIVATE\r
73       /bigobj # Support objects with > 65k sections. Needed due to templates.\r
74       /favor:${MSVC_FAVORED_ARCHITECTURE} # Architecture to prefer when generating code.\r
75       /Zc:inline # Have the compiler eliminate unreferenced COMDAT functions and data before emitting the object file.\r
76 \r
77       $<$<BOOL:${MSVC_ENABLE_ALL_WARNINGS}>:/Wall> # Enable all warnings if requested.\r
78       $<$<BOOL:${MSVC_ENABLE_PARALLEL_BUILD}>:/MP> # Enable multi-processor compilation if requested.\r
79       $<$<BOOL:${MSVC_ENABLE_STATIC_ANALYSIS}>:/analyze> # Enable static analysis if requested.\r
80 \r
81       # Debug builds\r
82       $<$<CONFIG:DEBUG>:\r
83         /Gy- # Disable function level linking.\r
84         /GF- # Disable string pooling.\r
85 \r
86         $<$<BOOL:${MSVC_ENABLE_DEBUG_INLINING}>:/Ob2> # Add /Ob2 if allowing inlining in debug mode.\r
87       >\r
88 \r
89       # Non-debug builds\r
90       $<$<NOT:$<CONFIG:DEBUG>>:\r
91         /GF # Enable string pooling. (this is enabled by default by the optimization level, but we enable it here for clarity)\r
92         /Gw # Optimize global data. (-fdata-sections)\r
93         /Gy # Enable function level linking. (-ffunction-sections)\r
94         /Qpar # Enable parallel code generation.\r
95         /Oi # Enable intrinsic functions.\r
96         /Ot # Favor fast code.\r
97 \r
98         $<$<BOOL:${MSVC_ENABLE_LTCG}>:/GL> # Enable link time code generation.\r
99       >\r
100   )\r
101 \r
102   target_compile_options(${THETARGET}\r
103     PUBLIC\r
104       /wd4191 # 'type cast' unsafe conversion of function pointers\r
105       /wd4291 # no matching operator delete found\r
106       /wd4309 # '=' truncation of constant value\r
107       /wd4310 # cast truncates constant value\r
108       /wd4366 # result of unary '&' operator may be unaligned\r
109       /wd4587 # behavior change; constructor no longer implicitly called\r
110       /wd4592 # symbol will be dynamically initialized (implementation limitation)\r
111       /wd4723 # potential divide by 0\r
112       /wd4724 # potential mod by 0\r
113       /wd4868 # compiler may not enforce left-to-right evaluation order\r
114       /wd4996 # user deprecated\r
115 \r
116       # The warnings that are disabled:\r
117       /wd4068 # Unknown pragma.\r
118       /wd4091 # 'typedef' ignored on left of '' when no variable is declared.\r
119       /wd4146 # Unary minus applied to unsigned type, result still unsigned.\r
120       /wd4800 # Values being forced to bool, this happens many places, and is a "performance warning".\r
121 \r
122       # NOTE: glog/logging.h:1116 change to `size_t pcount() const { return size_t(pptr() - pbase()); }`\r
123       # NOTE: gmock/gmock-spec-builders.h:1177 change to `*static_cast<const Action<F>*>(untyped_actions_[size_t(count - 1)]) :`\r
124       # NOTE: gmock/gmock-spec-builders.h:1749 change to `const size_t count = untyped_expectations_.size();`\r
125       # NOTE: gmock/gmock-spec-builders.h:1754 change to `for (size_t i = 0; i < count; i++) {`\r
126       # NOTE: gtest/gtest-printers.h:173 change to `const internal::BiggestInt kBigInt = internal::BiggestInt(value);`\r
127       # NOTE: gtest/internal/gtest-internal.h:890 add `GTEST_DISABLE_MSC_WARNINGS_PUSH_(4365)`\r
128       # NOTE: gtest/internal/gtest-internal.h:894 ass `GTEST_DISABLE_MSC_WARNINGS_POP_()`\r
129       # NOTE: boost/crc.hpp:578 change to `{ return static_cast<unsigned char>(x ^ rem); }`\r
130       # NOTE: boost/regex/v4/match_results.hpp:126 change to `return m_subs[size_type(sub)].length();`\r
131       # NOTE: boost/regex/v4/match_results.hpp:226 change to `return m_subs[size_type(sub)];`\r
132       # NOTE: boost/date_time/adjust_functors.hpp:67 change to `origDayOfMonth_ = short(ymd.day);`\r
133       # NOTE: boost/date_time/adjust_functors.hpp:75 change to `wrap_int2 wi(short(ymd.month));`\r
134       # NOTE: boost/date_time/adjust_functors.hpp:82 change to `day_type resultingEndOfMonthDay(cal_type::end_of_month_day(static_cast<unsigned short>(year), static_cast<unsigned short>(wi.as_int())));`\r
135       # NOTE: boost/date_time/adjust_functors.hpp:85 change to `return date_type(static_cast<unsigned short>(year), static_cast<unsigned short>(wi.as_int()), resultingEndOfMonthDay) - d;`\r
136       # NOTE: boost/date_time/adjust_functors.hpp:87 change to `day_type dayOfMonth = static_cast<unsigned short>(origDayOfMonth_);`\r
137       # NOTE: boost/date_time/adjust_functors.hpp:91 change to `return date_type(static_cast<unsigned short>(year), static_cast<unsigned short>(wi.as_int()), dayOfMonth) - d;`\r
138       # NOTE: boost/date_time/adjust_functors.hpp:98 change to `origDayOfMonth_ = short(ymd.day);`\r
139       # NOTE: boost/date_time/adjust_functors.hpp:106 change to `wrap_int2 wi(short(ymd.month));`\r
140       # NOTE: boost/date_time/adjust_functors.hpp:111 change to `day_type resultingEndOfMonthDay(cal_type::end_of_month_day(static_cast<unsigned short>(year), static_cast<unsigned short>(wi.as_int())));`\r
141       # NOTE: boost/date_time/adjust_functors.hpp:114 change to `return date_type(static_cast<unsigned short>(year), static_cast<unsigned short>(wi.as_int()), resultingEndOfMonthDay) - d;`\r
142       # NOTE: boost/date_time/adjust_functors.hpp:116 change to `day_type dayOfMonth = static_cast<unsigned short>(origDayOfMonth_);`\r
143       # NOTE: boost/date_time/adjust_functors.hpp:120 change to `return date_type(static_cast<unsigned short>(year), static_cast<unsigned short>(wi.as_int()), dayOfMonth) - d;`\r
144       # NOTE: boost/date_time/gregorian_calendar.ipp:81 change to `unsigned long  d = static_cast<unsigned long>(ymd.day + ((153*m + 2)/5) + 365*y + (y/4) - (y/100) + (y/400) - 32045);`\r
145       # NOTE: boost/date_time/gregorian/greg_date.hpp:122 change to `unsigned short eom_day =  gregorian_calendar::end_of_month_day(ymd.year, ymd.month);`\r
146       # NOTE: boost/thread/future.hpp:1050 change to `locks[std::ptrdiff_t(i)]=BOOST_THREAD_MAKE_RV_REF(boost::unique_lock<boost::mutex>(futures[i].future_->mutex));`\r
147       # NOTE: boost/thread/future.hpp:1063 change to `locks[std::ptrdiff_t(i)].unlock();`\r
148       # NOTE: boost/thread/win32/basic_recursive_mutex.hpp:47 change to `long const current_thread_id=long(win32::GetCurrentThreadId());`\r
149       # NOTE: boost/thread/win32/basic_recursive_mutex.hpp:53 change to `long const current_thread_id=long(win32::GetCurrentThreadId());`\r
150       # NOTE: boost/thread/win32/basic_recursive_mutex.hpp:64 change to `long const current_thread_id=long(win32::GetCurrentThreadId());`\r
151       # NOTE: boost/thread/win32/basic_recursive_mutex.hpp:78 change to `long const current_thread_id=long(win32::GetCurrentThreadId());`\r
152       # NOTE: boost/thread/win32/basic_recursive_mutex.hpp:84 change to `long const current_thread_id=long(win32::GetCurrentThreadId());`\r
153       # NOTE: boost/thread/win32/condition_variable.hpp:79 change to `detail::win32::ReleaseSemaphore(semaphore,long(count_to_release),0);`\r
154       # NOTE: boost/thread/win32/condition_variable.hpp:84 change to `release(unsigned(detail::interlocked_read_acquire(&waiters)));`\r
155       # NOTE: boost/algorithm/string/detail/classification.hpp:85 change to `std::size_t Size=std::size_t(::boost::distance(Range));`\r
156       /wd4018 # Signed/unsigned mismatch.\r
157       /wd4365 # Signed/unsigned mismatch.\r
158       /wd4388 # Signed/unsigned mismatch on relative comparison operator.\r
159       /wd4389 # Signed/unsigned mismatch on equality comparison operator.\r
160 \r
161       # TODO:\r
162       /wd4100 # Unreferenced formal parameter.\r
163       /wd4459 # Declaration of parameter hides global declaration.\r
164       /wd4505 # Unreferenced local function has been removed.\r
165       /wd4701 # Potentially uninitialized local variable used.\r
166       /wd4702 # Unreachable code.\r
167 \r
168       # These warnings are disabled because we've\r
169       # enabled all warnings. If all warnings are\r
170       # not enabled, we still need to disable them\r
171       # for consuming libs.\r
172       /wd4061 # 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.\r
173       /wd4127 # Conditional expression is constant.\r
174       /wd4200 # Non-standard extension, zero sized array.\r
175       /wd4201 # Non-standard extension used: nameless struct/union.\r
176       /wd4296 # '<' Expression is always false.\r
177       /wd4316 # Object allocated on the heap may not be aligned to 128.\r
178       /wd4324 # Structure was padded due to alignment specifier.\r
179       /wd4355 # 'this' used in base member initializer list.\r
180       /wd4371 # Layout of class may have changed due to fixes in packing.\r
181       /wd4435 # Object layout under /vd2 will change due to virtual base.\r
182       /wd4514 # Unreferenced inline function has been removed. (caused by /Zc:inline)\r
183       /wd4548 # Expression before comma has no effect. I wouldn't disable this normally, but malloc.h triggers this warning.\r
184       /wd4574 # ifdef'd macro was defined to 0.\r
185       /wd4582 # Constructor is not implicitly called.\r
186       /wd4583 # Destructor is not implicitly called.\r
187       /wd4619 # Invalid warning number used in #pragma warning.\r
188       /wd4623 # Default constructor was implicitly defined as deleted.\r
189       /wd4625 # Copy constructor was implicitly defined as deleted.\r
190       /wd4626 # Assignment operator was implicitly defined as deleted.\r
191       /wd4647 # Behavior change in __is_pod.\r
192       /wd4668 # Macro was not defined, replacing with 0.\r
193       /wd4706 # Assignment within conditional expression.\r
194       /wd4710 # Function was not inlined.\r
195       /wd4711 # Function was selected for automated inlining.\r
196       /wd4714 # Function marked as __forceinline not inlined.\r
197       /wd4820 # Padding added after data member.\r
198       /wd5026 # Move constructor was implicitly defined as deleted.\r
199       /wd5027 # Move assignment operator was implicitly defined as deleted.\r
200       /wd5031 # #pragma warning(pop): likely mismatch, popping warning state pushed in different file. This is needed because of how boost does things.\r
201 \r
202       # Warnings to treat as errors:\r
203       /we4099 # Mixed use of struct and class on same type names.\r
204       /we4129 # Unknown escape sequence. This is usually caused by incorrect escaping.\r
205       /we4566 # Character cannot be represented in current charset. This is remidied by prefixing string with "u8".\r
206 \r
207     PRIVATE\r
208       # Warnings disabled for /analyze\r
209       $<$<BOOL:${MSVC_ENABLE_STATIC_ANALYSIS}>:\r
210         /wd6001 # Using uninitialized memory. This is disabled because it is wrong 99% of the time.\r
211         /wd6011 # Dereferencing potentially NULL pointer.\r
212         /wd6031 # Return value ignored.\r
213         /wd6235 # (<non-zero constant> || <expression>) is always a non-zero constant.\r
214         /wd6237 # (<zero> && <expression>) is always zero. <expression> is never evaluated and may have side effects.\r
215         /wd6239 # (<non-zero constant> && <expression>) always evaluates to the result of <expression>.\r
216         /wd6240 # (<expression> && <non-zero constant>) always evaluates to the result of <expression>.\r
217         /wd6246 # Local declaration hides declaration of same name in outer scope.\r
218         /wd6248 # Setting a SECURITY_DESCRIPTOR's DACL to NULL will result in an unprotected object. This is done by one of the boost headers.\r
219         /wd6255 # _alloca indicates failure by raising a stack overflow exception.\r
220         /wd6262 # Function uses more than x bytes of stack space.\r
221         /wd6271 # Extra parameter passed to format function. The analysis pass doesn't recognize %j or %z, even though the runtime does.\r
222         /wd6285 # (<non-zero constant> || <non-zero constant>) is always true.\r
223         /wd6297 # 32-bit value is shifted then cast to 64-bits. The places this occurs never use more than 32 bits.\r
224         /wd6308 # 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.\r
225         /wd6326 # Potential comparison of a constant with another constant.\r
226         /wd6330 # Unsigned/signed mismatch when passed as a parameter.\r
227         /wd6340 # Mismatch on sign when passed as format string value.\r
228         /wd6387 # '<value>' could be '0': This does not adhere to the specification for a function.\r
229         /wd28182 # Dereferencing NULL pointer. '<value>' contains the same NULL value as '<expression>'.\r
230         /wd28251 # Inconsistent annotation for function. This is because we only annotate the declaration and not the definition.\r
231         /wd28278 # Function appears with no prototype in scope.\r
232       >\r
233   )\r
234 \r
235   # And the extra defines:\r
236   target_compile_definitions(${THETARGET}\r
237     PUBLIC\r
238       _CRT_NONSTDC_NO_WARNINGS # Don't deprecate posix names of functions.\r
239       _CRT_SECURE_NO_WARNINGS # Don't deprecate the non _s versions of various standard library functions, because safety is for chumps.\r
240       _SCL_SECURE_NO_WARNINGS # Don't deprecate the non _s versions of various standard library functions, because safety is for chumps.\r
241       \r
242       _STL_EXTRA_DISABLED_WARNINGS=4774\ 4987\r
243 \r
244       $<$<BOOL:${MSVC_ENABLE_CPP_LATEST}>:"_HAS_AUTO_PTR_ETC=1"> # We're building in C++ 17 or greater mode, but certain dependencies (Boost) still have dependencies on unary_function and binary_function, so we have to make sure not to remove them.\r
245       $<$<BOOL:${MSVC_ENABLE_LEAN_AND_MEAN_WINDOWS}>:"WIN32_LEAN_AND_MEAN"> # Don't include most of Windows.h\r
246   )\r
247 \r
248   # Ignore a warning about an object file not defining any symbols,\r
249   # these are known, and we don't care.\r
250   set_property(TARGET ${THETARGET} APPEND_STRING PROPERTY STATIC_LIBRARY_FLAGS " /ignore:4221")\r
251 \r
252   # The options to pass to the linker:\r
253   set_property(TARGET ${THETARGET} APPEND_STRING PROPERTY LINK_FLAGS_DEBUG " /INCREMENTAL") # Do incremental linking.\r
254   if (NOT $<TARGET_PROPERTY:${THETARGET},TYPE> STREQUAL "STATIC_LIBRARY")\r
255     set_property(TARGET ${THETARGET} APPEND_STRING PROPERTY LINK_FLAGS_DEBUG " /OPT:NOREF") # No unreferenced data elimination.\r
256     set_property(TARGET ${THETARGET} APPEND_STRING PROPERTY LINK_FLAGS_DEBUG " /OPT:NOICF") # No Identical COMDAT folding.\r
257 \r
258     set_property(TARGET ${THETARGET} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /OPT:REF") # Remove unreferenced functions and data.\r
259     set_property(TARGET ${THETARGET} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /OPT:ICF") # Identical COMDAT folding.\r
260   endif()\r
261 \r
262   if (MSVC_ENABLE_FAST_LINK)\r
263     set_property(TARGET ${THETARGET} APPEND_STRING PROPERTY LINK_FLAGS_DEBUG " /DEBUG:FASTLINK") # Generate a partial PDB file that simply references the original object and library files.\r
264   endif()\r
265 \r
266   # Add /GL to the compiler, and /LTCG to the linker\r
267   # if link time code generation is enabled.\r
268   if (MSVC_ENABLE_LTCG)\r
269     set_property(TARGET ${THETARGET} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " /LTCG")\r
270   endif()\r
271 endfunction()\r