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