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