[sanitizer/coverage] Add AFL-style coverage counters (search heuristic for fuzzing).
[oota-llvm.git] / cmake / modules / HandleLLVMOptions.cmake
1 # This CMake module is responsible for interpreting the user defined LLVM_
2 # options and executing the appropriate CMake commands to realize the users'
3 # selections.
4
5 # This is commonly needed so make sure it's defined before we include anything
6 # else.
7 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
8
9 include(HandleLLVMStdlib)
10 include(AddLLVMDefinitions)
11 include(CheckCCompilerFlag)
12 include(CheckCXXCompilerFlag)
13
14 if(NOT LLVM_FORCE_USE_OLD_TOOLCHAIN)
15   if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
16     if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
17       message(FATAL_ERROR "Host GCC version must be at least 4.7!")
18     endif()
19   elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
20     if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.1)
21       message(FATAL_ERROR "Host Clang version must be at least 3.1!")
22     endif()
23
24     if (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
25       if (CMAKE_CXX_SIMULATE_VERSION VERSION_LESS 18.0)
26         message(FATAL_ERROR "Host Clang must have at least -fms-compatibility-version=18.0")
27       endif()
28       set(CLANG_CL 1)
29     elseif(NOT LLVM_ENABLE_LIBCXX)
30       # Otherwise, test that we aren't using too old of a version of libstdc++
31       # with the Clang compiler. This is tricky as there is no real way to
32       # check the version of libstdc++ directly. Instead we test for a known
33       # bug in libstdc++4.6 that is fixed in libstdc++4.7.
34       set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
35       set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
36       set(CMAKE_REQUIRED_FLAGS "-std=c++0x")
37       check_cxx_source_compiles("
38 #include <atomic>
39 std::atomic<float> x(0.0f);
40 int main() { return (float)x; }"
41         LLVM_NO_OLD_LIBSTDCXX)
42       if(NOT LLVM_NO_OLD_LIBSTDCXX)
43         message(FATAL_ERROR "Host Clang must be able to find libstdc++4.7 or newer!")
44       endif()
45       set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
46       set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
47     endif()
48   elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
49     if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18.0)
50       message(FATAL_ERROR "Host Visual Studio must be at least 2013")
51     elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18.0.31101)
52       message(WARNING "Host Visual Studio should at least be 2013 Update 4 (MSVC 18.0.31101)"
53         "  due to miscompiles from earlier versions")
54     endif()
55   endif()
56 endif()
57
58 if( LLVM_ENABLE_ASSERTIONS )
59   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
60   if( NOT MSVC )
61     add_definitions( -D_DEBUG )
62   endif()
63   # On non-Debug builds cmake automatically defines NDEBUG, so we
64   # explicitly undefine it:
65   if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
66     add_definitions( -UNDEBUG )
67     # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
68     foreach (flags_var_to_scrub
69         CMAKE_CXX_FLAGS_RELEASE
70         CMAKE_CXX_FLAGS_RELWITHDEBINFO
71         CMAKE_CXX_FLAGS_MINSIZEREL
72         CMAKE_C_FLAGS_RELEASE
73         CMAKE_C_FLAGS_RELWITHDEBINFO
74         CMAKE_C_FLAGS_MINSIZEREL)
75       string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
76         "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
77     endforeach()
78   endif()
79 endif()
80
81 if(WIN32)
82   set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
83   if(CYGWIN)
84     set(LLVM_ON_WIN32 0)
85     set(LLVM_ON_UNIX 1)
86   else(CYGWIN)
87     set(LLVM_ON_WIN32 1)
88     set(LLVM_ON_UNIX 0)
89   endif(CYGWIN)
90 else(WIN32)
91   if(UNIX)
92     set(LLVM_ON_WIN32 0)
93     set(LLVM_ON_UNIX 1)
94     if(APPLE)
95       set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
96     else(APPLE)
97       set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
98     endif(APPLE)
99   else(UNIX)
100     MESSAGE(SEND_ERROR "Unable to determine platform")
101   endif(UNIX)
102 endif(WIN32)
103
104 set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX})
105 set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
106
107 # We use *.dylib rather than *.so on darwin.
108 set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
109
110 if(APPLE)
111   # Darwin-specific linker flags for loadable modules.
112   set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
113 endif()
114
115 # Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise a DSO
116 # build might work on ELF but fail on MachO/COFF.
117 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" OR WIN32 OR
118         ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") AND
119    NOT LLVM_USE_SANITIZER)
120   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs")
121 endif()
122
123
124 function(append value)
125   foreach(variable ${ARGN})
126     set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
127   endforeach(variable)
128 endfunction()
129
130 function(append_if condition value)
131   if (${condition})
132     foreach(variable ${ARGN})
133       set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
134     endforeach(variable)
135   endif()
136 endfunction()
137
138 macro(add_flag_if_supported flag name)
139   check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
140   append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS)
141   check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
142   append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS)
143 endmacro()
144
145 function(add_flag_or_print_warning flag name)
146   check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
147   check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
148   if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name})
149     message(STATUS "Building with ${flag}")
150     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
151     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
152   else()
153     message(WARNING "${flag} is not supported.")
154   endif()
155 endfunction()
156
157 if( LLVM_ENABLE_PIC )
158   if( XCODE )
159     # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
160     # know how to disable this, so just force ENABLE_PIC off for now.
161     message(WARNING "-fPIC not supported with Xcode.")
162   elseif( WIN32 OR CYGWIN)
163     # On Windows all code is PIC. MinGW warns if -fPIC is used.
164   else()
165     add_flag_or_print_warning("-fPIC" FPIC)
166
167     if( WIN32 OR CYGWIN)
168       # MinGW warns if -fvisibility-inlines-hidden is used.
169     else()
170       check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
171       append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS)
172     endif()
173   endif()
174 endif()
175
176 if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
177   # TODO: support other platforms and toolchains.
178   if( LLVM_BUILD_32_BITS )
179     message(STATUS "Building 32 bits executables and libraries.")
180     add_llvm_definitions( -m32 )
181     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
182     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
183     set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
184   endif( LLVM_BUILD_32_BITS )
185 endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
186
187 if (LLVM_BUILD_STATIC)
188   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
189 endif()
190
191 if( XCODE )
192   # For Xcode enable several build settings that correspond to
193   # many warnings that are on by default in Clang but are
194   # not enabled for historical reasons.  For versions of Xcode
195   # that do not support these options they will simply
196   # be ignored.
197   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_RETURN_TYPE "YES")
198   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_MISSING_NEWLINE "YES")
199   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VALUE "YES")
200   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VARIABLE "YES")
201   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_SIGN_COMPARE "YES")
202   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_FUNCTION "YES")
203   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED "YES")
204   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS "YES")
205   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNINITIALIZED_AUTOS "YES")
206   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_BOOL_CONVERSION "YES")
207   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_EMPTY_BODY "YES")
208   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_ENUM_CONVERSION "YES")
209   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_INT_CONVERSION "YES")
210   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_CONSTANT_CONVERSION "YES")
211   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_NON_VIRTUAL_DESTRUCTOR "YES")
212 endif()
213
214 # On Win32 using MS tools, provide an option to set the number of parallel jobs
215 # to use.
216 if( MSVC_IDE )
217   set(LLVM_COMPILER_JOBS "0" CACHE STRING
218     "Number of parallel compiler jobs. 0 means use all processors. Default is 0.")
219   if( NOT LLVM_COMPILER_JOBS STREQUAL "1" )
220     if( LLVM_COMPILER_JOBS STREQUAL "0" )
221       add_llvm_definitions( /MP )
222     else()
223       message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS})
224       add_llvm_definitions( /MP${LLVM_COMPILER_JOBS} )
225     endif()
226   else()
227     message(STATUS "Parallel compilation disabled")
228   endif()
229 endif()
230
231 if( MSVC )
232   include(ChooseMSVCCRT)
233
234   if( NOT (${CMAKE_VERSION} VERSION_LESS 2.8.11) )
235     # set stack reserved size to ~10MB
236     # CMake previously automatically set this value for MSVC builds, but the
237     # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default
238     # value (1 MB) which is not enough for us in tasks such as parsing recursive
239     # C++ templates in Clang.
240     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000")
241   endif()
242
243   if( MSVC11 )
244     add_llvm_definitions(-D_VARIADIC_MAX=10)
245   endif()
246   
247   # Add definitions that make MSVC much less annoying.
248   add_llvm_definitions(
249     # For some reason MS wants to deprecate a bunch of standard functions...
250     -D_CRT_SECURE_NO_DEPRECATE
251     -D_CRT_SECURE_NO_WARNINGS
252     -D_CRT_NONSTDC_NO_DEPRECATE
253     -D_CRT_NONSTDC_NO_WARNINGS
254     -D_SCL_SECURE_NO_DEPRECATE
255     -D_SCL_SECURE_NO_WARNINGS
256
257     # Disabled warnings.
258     -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
259     -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
260     -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
261     -wd4258 # Suppress ''var' : definition from the for loop is ignored; the definition from the enclosing scope is used'
262     -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
263     -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
264     -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
265     -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
266     -wd4355 # Suppress ''this' : used in base member initializer list'
267     -wd4456 # Suppress 'declaration of 'var' hides local variable'
268     -wd4457 # Suppress 'declaration of 'var' hides function parameter'
269     -wd4458 # Suppress 'declaration of 'var' hides class member'
270     -wd4459 # Suppress 'declaration of 'var' hides global declaration'
271     -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
272     -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
273     -wd4722 # Suppress 'function' : destructor never returns, potential memory leak
274     -wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)'
275     
276     # Promoted warnings.
277     -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
278
279     # Promoted warnings to errors.
280     -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
281     )
282
283   # Enable warnings
284   if (LLVM_ENABLE_WARNINGS)
285     add_llvm_definitions( /W4 )
286     if (LLVM_ENABLE_PEDANTIC)
287       # No MSVC equivalent available
288     endif (LLVM_ENABLE_PEDANTIC)
289   endif (LLVM_ENABLE_WARNINGS)
290   if (LLVM_ENABLE_WERROR)
291     add_llvm_definitions( /WX )
292   endif (LLVM_ENABLE_WERROR)
293 elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
294   if (LLVM_ENABLE_WARNINGS)
295     append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
296     append("-Wcast-qual" CMAKE_CXX_FLAGS)
297
298     # Turn off missing field initializer warnings for gcc to avoid noise from
299     # false positives with empty {}. Turn them on otherwise (they're off by
300     # default for clang).
301     check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
302     if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
303       if (CMAKE_COMPILER_IS_GNUCXX)
304         append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
305       else()
306         append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
307       endif()
308     endif()
309
310     append_if(LLVM_ENABLE_PEDANTIC "-pedantic -Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
311     add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG)
312     append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
313     append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
314
315     # Check if -Wnon-virtual-dtor warns even though the class is marked final.
316     # If it does, don't add it. So it won't be added on clang 3.4 and older.
317     # This also catches cases when -Wnon-virtual-dtor isn't supported by
318     # the compiler at all.
319     set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
320     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11 -Werror=non-virtual-dtor")
321     CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();protected: ~base();};
322                                class derived final : public base { public: ~derived();};
323                                int main() { return 0; }"
324                               CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR)
325     set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
326     append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR
327               "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
328
329     # Check if -Wcomment is OK with an // comment ending with '\' if the next
330     # line is also a // comment.
331     set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
332     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment")
333     CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main() {return 0;}"
334                             C_WCOMMENT_ALLOWS_LINE_WRAP)
335     set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
336     if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP)
337       append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
338     endif()
339   endif (LLVM_ENABLE_WARNINGS)
340   append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
341   if (NOT LLVM_ENABLE_TIMESTAMPS)
342     add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME)
343   endif ()
344   if (LLVM_ENABLE_CXX1Y)
345     check_cxx_compiler_flag("-std=c++1y" CXX_SUPPORTS_CXX1Y)
346     append_if(CXX_SUPPORTS_CXX1Y "-std=c++1y" CMAKE_CXX_FLAGS)
347   else()
348     check_cxx_compiler_flag("-std=c++11" CXX_SUPPORTS_CXX11)
349     if (CXX_SUPPORTS_CXX11)
350       if (CYGWIN OR MINGW)
351         # MinGW and Cygwin are a bit stricter and lack things like
352         # 'strdup', 'stricmp', etc in c++11 mode.
353         append("-std=gnu++11" CMAKE_CXX_FLAGS)
354       else()
355         append("-std=c++11" CMAKE_CXX_FLAGS)
356       endif()
357     else()
358       message(FATAL_ERROR "LLVM requires C++11 support but the '-std=c++11' flag isn't supported.")
359     endif()
360   endif()
361   if (LLVM_ENABLE_MODULES)
362     set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
363     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fmodules -fcxx-modules")
364     # Check that we can build code with modules enabled, and that repeatedly
365     # including <cassert> still manages to respect NDEBUG properly.
366     CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG
367                                #include <cassert>
368                                #define NDEBUG
369                                #include <cassert>
370                                int main() { assert(this code is not compiled); }"
371                                CXX_SUPPORTS_MODULES)
372     set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
373     if (CXX_SUPPORTS_MODULES)
374       append_if(CXX_SUPPORTS_MODULES "-fmodules" CMAKE_C_FLAGS)
375       append_if(CXX_SUPPORTS_MODULES "-fmodules -fcxx-modules" CMAKE_CXX_FLAGS)
376     else()
377       message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler")
378     endif()
379   endif(LLVM_ENABLE_MODULES)
380 endif( MSVC )
381
382 macro(append_common_sanitizer_flags)
383   # Append -fno-omit-frame-pointer and turn on debug info to get better
384   # stack traces.
385   add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER)
386   if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
387       NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
388     add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY)
389   endif()
390   # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
391   if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
392     add_flag_if_supported("-O1" O1)
393   endif()
394 endmacro()
395
396 # Turn on sanitizers if necessary.
397 if(LLVM_USE_SANITIZER)
398   if (LLVM_ON_UNIX)
399     if (LLVM_USE_SANITIZER STREQUAL "Address")
400       append_common_sanitizer_flags()
401       append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
402     elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
403       append_common_sanitizer_flags()
404       append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
405       if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
406         append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
407       endif()
408     elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
409       append_common_sanitizer_flags()
410       append("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover"
411               CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
412     elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
413       append_common_sanitizer_flags()
414       append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
415     elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
416             LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
417       append_common_sanitizer_flags()
418       append("-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover"
419               CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
420     else()
421       message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
422     endif()
423   else()
424     message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
425   endif()
426   if (LLVM_USE_SANITIZE_COVERAGE)
427     append("-fsanitize-coverage=4 -mllvm -sanitizer-coverage-8bit-counters=1" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
428   endif()
429 endif()
430
431 # Turn on -gsplit-dwarf if requested
432 if(LLVM_USE_SPLIT_DWARF)
433   add_definitions("-gsplit-dwarf")
434 endif()
435
436 add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
437 add_llvm_definitions( -D__STDC_FORMAT_MACROS )
438 add_llvm_definitions( -D__STDC_LIMIT_MACROS )
439
440 # clang doesn't print colored diagnostics when invoked from Ninja
441 if (UNIX AND
442     CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
443     CMAKE_GENERATOR STREQUAL "Ninja")
444   append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
445 endif()
446
447 # Add flags for add_dead_strip().
448 # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
449 # But MinSizeRel seems to add that automatically, so maybe disable these
450 # flags instead if LLVM_NO_DEAD_STRIP is set.
451 if(NOT CYGWIN AND NOT WIN32)
452   if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
453     check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS)
454     if (C_SUPPORTS_FNO_FUNCTION_SECTIONS)
455       # Don't add -ffunction-section if it can be disabled with -fno-function-sections.
456       # Doing so will break sanitizers.
457       add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS)
458     endif()
459     add_flag_if_supported("-fdata-sections" FDATA_SECTIONS)
460   endif()
461 endif()
462
463 if(CYGWIN OR MINGW)
464   # Prune --out-implib from executables. It doesn't make sense even
465   # with --export-all-symbols.
466   string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " "
467     CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
468   string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " "
469     CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
470 endif()
471
472 if(MSVC)
473   # Remove flags here, for exceptions and RTTI.
474   # Each target property or source property should be responsible to control
475   # them.
476   # CL.EXE complains to override flags like "/GR /GR-".
477   string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
478   string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
479 endif()
480
481 # Provide public options to globally control RTTI and EH
482 option(LLVM_ENABLE_EH "Enable Exception handling" OFF)
483 option(LLVM_ENABLE_RTTI "Enable run time type information" OFF)
484 if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI)
485   message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON")
486 endif()
487
488 # Plugin support
489 # FIXME: Make this configurable.
490 if(WIN32 OR CYGWIN)
491   if(BUILD_SHARED_LIBS)
492     set(LLVM_ENABLE_PLUGINS ON)
493   else()
494     set(LLVM_ENABLE_PLUGINS OFF)
495   endif()
496 else()
497   set(LLVM_ENABLE_PLUGINS ON)
498 endif()