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