[CMake] Don't pass in MSVC warning flags as definitions
[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
258   set(msvc_warning_flags
259     # Disabled warnings.
260     -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
261     -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
262     -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
263     -wd4258 # Suppress ''var' : definition from the for loop is ignored; the definition from the enclosing scope is used'
264     -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
265     -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
266     -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
267     -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
268     -wd4355 # Suppress ''this' : used in base member initializer list'
269     -wd4456 # Suppress 'declaration of 'var' hides local variable'
270     -wd4457 # Suppress 'declaration of 'var' hides function parameter'
271     -wd4458 # Suppress 'declaration of 'var' hides class member'
272     -wd4459 # Suppress 'declaration of 'var' hides global declaration'
273     -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
274     -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
275     -wd4722 # Suppress 'function' : destructor never returns, potential memory leak
276     -wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)'
277
278     # Promoted warnings.
279     -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
280
281     # Promoted warnings to errors.
282     -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
283     )
284
285   # Enable warnings
286   if (LLVM_ENABLE_WARNINGS)
287     append("/W4" msvc_warning_flags)
288     if (LLVM_ENABLE_PEDANTIC)
289       # No MSVC equivalent available
290     endif (LLVM_ENABLE_PEDANTIC)
291   endif (LLVM_ENABLE_WARNINGS)
292   if (LLVM_ENABLE_WERROR)
293     append("/WX" msvc_warning_flags)
294   endif (LLVM_ENABLE_WERROR)
295
296   foreach(flag ${msvc_warning_flags})
297     append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
298   endforeach(flag)
299
300 elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
301   if (LLVM_ENABLE_WARNINGS)
302     append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
303     append("-Wcast-qual" CMAKE_CXX_FLAGS)
304
305     # Turn off missing field initializer warnings for gcc to avoid noise from
306     # false positives with empty {}. Turn them on otherwise (they're off by
307     # default for clang).
308     check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
309     if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
310       if (CMAKE_COMPILER_IS_GNUCXX)
311         append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
312       else()
313         append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
314       endif()
315     endif()
316
317     append_if(LLVM_ENABLE_PEDANTIC "-pedantic -Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
318     add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG)
319     append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
320     append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
321
322     # Check if -Wnon-virtual-dtor warns even though the class is marked final.
323     # If it does, don't add it. So it won't be added on clang 3.4 and older.
324     # This also catches cases when -Wnon-virtual-dtor isn't supported by
325     # the compiler at all.
326     set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
327     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11 -Werror=non-virtual-dtor")
328     CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();protected: ~base();};
329                                class derived final : public base { public: ~derived();};
330                                int main() { return 0; }"
331                               CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR)
332     set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
333     append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR
334               "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
335
336     # Check if -Wcomment is OK with an // comment ending with '\' if the next
337     # line is also a // comment.
338     set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
339     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment")
340     CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main() {return 0;}"
341                             C_WCOMMENT_ALLOWS_LINE_WRAP)
342     set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
343     if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP)
344       append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
345     endif()
346   endif (LLVM_ENABLE_WARNINGS)
347   append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
348   if (NOT LLVM_ENABLE_TIMESTAMPS)
349     add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME)
350   endif ()
351   if (LLVM_ENABLE_CXX1Y)
352     check_cxx_compiler_flag("-std=c++1y" CXX_SUPPORTS_CXX1Y)
353     append_if(CXX_SUPPORTS_CXX1Y "-std=c++1y" CMAKE_CXX_FLAGS)
354   else()
355     check_cxx_compiler_flag("-std=c++11" CXX_SUPPORTS_CXX11)
356     if (CXX_SUPPORTS_CXX11)
357       if (CYGWIN OR MINGW)
358         # MinGW and Cygwin are a bit stricter and lack things like
359         # 'strdup', 'stricmp', etc in c++11 mode.
360         append("-std=gnu++11" CMAKE_CXX_FLAGS)
361       else()
362         append("-std=c++11" CMAKE_CXX_FLAGS)
363       endif()
364     else()
365       message(FATAL_ERROR "LLVM requires C++11 support but the '-std=c++11' flag isn't supported.")
366     endif()
367   endif()
368   if (LLVM_ENABLE_MODULES)
369     set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
370     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fmodules -fcxx-modules")
371     # Check that we can build code with modules enabled, and that repeatedly
372     # including <cassert> still manages to respect NDEBUG properly.
373     CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG
374                                #include <cassert>
375                                #define NDEBUG
376                                #include <cassert>
377                                int main() { assert(this code is not compiled); }"
378                                CXX_SUPPORTS_MODULES)
379     set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
380     if (CXX_SUPPORTS_MODULES)
381       append_if(CXX_SUPPORTS_MODULES "-fmodules" CMAKE_C_FLAGS)
382       append_if(CXX_SUPPORTS_MODULES "-fmodules -fcxx-modules" CMAKE_CXX_FLAGS)
383     else()
384       message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler")
385     endif()
386   endif(LLVM_ENABLE_MODULES)
387 endif( MSVC )
388
389 macro(append_common_sanitizer_flags)
390   # Append -fno-omit-frame-pointer and turn on debug info to get better
391   # stack traces.
392   add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER)
393   if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
394       NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
395     add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY)
396   endif()
397   # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
398   if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
399     add_flag_if_supported("-O1" O1)
400   endif()
401 endmacro()
402
403 # Turn on sanitizers if necessary.
404 if(LLVM_USE_SANITIZER)
405   if (LLVM_ON_UNIX)
406     if (LLVM_USE_SANITIZER STREQUAL "Address")
407       append_common_sanitizer_flags()
408       append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
409     elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
410       append_common_sanitizer_flags()
411       append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
412       if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
413         append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
414       endif()
415     elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
416       append_common_sanitizer_flags()
417       append("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover"
418               CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
419     elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
420       append_common_sanitizer_flags()
421       append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
422     elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
423             LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
424       append_common_sanitizer_flags()
425       append("-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover"
426               CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
427     else()
428       message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
429     endif()
430   else()
431     message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
432   endif()
433   if (LLVM_USE_SANITIZE_COVERAGE)
434     append("-fsanitize-coverage=4 -mllvm -sanitizer-coverage-8bit-counters=1" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
435   endif()
436 endif()
437
438 # Turn on -gsplit-dwarf if requested
439 if(LLVM_USE_SPLIT_DWARF)
440   add_definitions("-gsplit-dwarf")
441 endif()
442
443 add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
444 add_llvm_definitions( -D__STDC_FORMAT_MACROS )
445 add_llvm_definitions( -D__STDC_LIMIT_MACROS )
446
447 # clang doesn't print colored diagnostics when invoked from Ninja
448 if (UNIX AND
449     CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
450     CMAKE_GENERATOR STREQUAL "Ninja")
451   append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
452 endif()
453
454 # Add flags for add_dead_strip().
455 # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
456 # But MinSizeRel seems to add that automatically, so maybe disable these
457 # flags instead if LLVM_NO_DEAD_STRIP is set.
458 if(NOT CYGWIN AND NOT WIN32)
459   if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
460     check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS)
461     if (C_SUPPORTS_FNO_FUNCTION_SECTIONS)
462       # Don't add -ffunction-section if it can be disabled with -fno-function-sections.
463       # Doing so will break sanitizers.
464       add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS)
465     endif()
466     add_flag_if_supported("-fdata-sections" FDATA_SECTIONS)
467   endif()
468 endif()
469
470 if(CYGWIN OR MINGW)
471   # Prune --out-implib from executables. It doesn't make sense even
472   # with --export-all-symbols.
473   string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " "
474     CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
475   string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " "
476     CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
477 endif()
478
479 if(MSVC)
480   # Remove flags here, for exceptions and RTTI.
481   # Each target property or source property should be responsible to control
482   # them.
483   # CL.EXE complains to override flags like "/GR /GR-".
484   string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
485   string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
486 endif()
487
488 # Provide public options to globally control RTTI and EH
489 option(LLVM_ENABLE_EH "Enable Exception handling" OFF)
490 option(LLVM_ENABLE_RTTI "Enable run time type information" OFF)
491 if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI)
492   message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON")
493 endif()
494
495 # Plugin support
496 # FIXME: Make this configurable.
497 if(WIN32 OR CYGWIN)
498   if(BUILD_SHARED_LIBS)
499     set(LLVM_ENABLE_PLUGINS ON)
500   else()
501     set(LLVM_ENABLE_PLUGINS OFF)
502   endif()
503 else()
504   set(LLVM_ENABLE_PLUGINS ON)
505 endif()