1 # This CMake module is responsible for interpreting the user defined LLVM_
2 # options and executing the appropriate CMake commands to realize the users'
5 # This is commonly needed so make sure it's defined before we include anything
7 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
9 include(HandleLLVMStdlib)
10 include(AddLLVMDefinitions)
11 include(CheckCCompilerFlag)
12 include(CheckCXXCompilerFlag)
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!")
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!")
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")
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("
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!")
45 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
46 set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
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")
58 if( LLVM_ENABLE_ASSERTIONS )
59 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
61 add_definitions( -D_DEBUG )
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
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}}")
82 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
95 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
97 set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
100 MESSAGE(SEND_ERROR "Unable to determine platform")
104 set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX})
105 set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
107 # We use *.dylib rather than *.so on darwin.
108 set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
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")
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")
124 function(append value)
125 foreach(variable ${ARGN})
126 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
130 function(append_if condition value)
132 foreach(variable ${ARGN})
133 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
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)
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)
153 message(WARNING "${flag} is not supported.")
157 if( LLVM_ENABLE_PIC )
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.
165 add_flag_or_print_warning("-fPIC" FPIC)
168 # MinGW warns if -fvisibility-inlines-hidden is used.
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)
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 )
187 if (LLVM_BUILD_STATIC)
188 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
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
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")
214 # On Win32 using MS tools, provide an option to set the number of parallel jobs
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 )
223 message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS})
224 add_llvm_definitions( /MP${LLVM_COMPILER_JOBS} )
227 message(STATUS "Parallel compilation disabled")
232 include(ChooseMSVCCRT)
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")
244 add_llvm_definitions(-D_VARIADIC_MAX=10)
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
258 set(msvc_warning_flags
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'
295 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
297 # Promoted warnings to errors.
298 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
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)
312 foreach(flag ${msvc_warning_flags})
313 append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
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)
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)
329 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
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)
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)
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)
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)
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)
371 check_cxx_compiler_flag("-std=c++11" CXX_SUPPORTS_CXX11)
372 if (CXX_SUPPORTS_CXX11)
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)
378 append("-std=c++11" CMAKE_CXX_FLAGS)
381 message(FATAL_ERROR "LLVM requires C++11 support but the '-std=c++11' flag isn't supported.")
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
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)
400 message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler")
402 endif(LLVM_ENABLE_MODULES)
405 macro(append_common_sanitizer_flags)
406 # Append -fno-omit-frame-pointer and turn on debug info to get better
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)
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)
419 # Turn on sanitizers if necessary.
420 if(LLVM_USE_SANITIZER)
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)
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)
444 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
447 message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
449 if (LLVM_USE_SANITIZE_COVERAGE)
450 append("-fsanitize-coverage=4 -mllvm -sanitizer-coverage-8bit-counters=1" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
454 # Turn on -gsplit-dwarf if requested
455 if(LLVM_USE_SPLIT_DWARF)
456 add_definitions("-gsplit-dwarf")
459 add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
460 add_llvm_definitions( -D__STDC_FORMAT_MACROS )
461 add_llvm_definitions( -D__STDC_LIMIT_MACROS )
463 # clang doesn't print colored diagnostics when invoked from Ninja
465 CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
466 CMAKE_GENERATOR STREQUAL "Ninja")
467 append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
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)
482 add_flag_if_supported("-fdata-sections" FDATA_SECTIONS)
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}")
496 # Remove flags here, for exceptions and RTTI.
497 # Each target property or source property should be responsible to control
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}")
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")
512 # FIXME: Make this configurable.
514 if(BUILD_SHARED_LIBS)
515 set(LLVM_ENABLE_PLUGINS ON)
517 set(LLVM_ENABLE_PLUGINS OFF)
520 set(LLVM_ENABLE_PLUGINS ON)