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