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. 7string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) 8 9include(HandleLLVMStdlib) 10include(AddLLVMDefinitions) 11include(CheckCCompilerFlag) 12include(CheckCXXCompilerFlag) 13 14if(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> 39std::atomic<float> x(0.0f); 40int 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() 56endif() 57 58if (CMAKE_LINKER MATCHES "lld-link.exe") 59 # Pass /MANIFEST:NO so that CMake doesn't run mt.exe on our binaries. Adding 60 # manifests with mt.exe breaks LLD's symbol tables and takes as much time as 61 # the link. See PR24476. 62 append("/MANIFEST:NO" 63 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 64endif() 65 66if( LLVM_ENABLE_ASSERTIONS ) 67 # MSVC doesn't like _DEBUG on release builds. See PR 4379. 68 if( NOT MSVC ) 69 add_definitions( -D_DEBUG ) 70 endif() 71 # On non-Debug builds cmake automatically defines NDEBUG, so we 72 # explicitly undefine it: 73 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" ) 74 add_definitions( -UNDEBUG ) 75 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines. 76 foreach (flags_var_to_scrub 77 CMAKE_CXX_FLAGS_RELEASE 78 CMAKE_CXX_FLAGS_RELWITHDEBINFO 79 CMAKE_CXX_FLAGS_MINSIZEREL 80 CMAKE_C_FLAGS_RELEASE 81 CMAKE_C_FLAGS_RELWITHDEBINFO 82 CMAKE_C_FLAGS_MINSIZEREL) 83 string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " 84 "${flags_var_to_scrub}" "${${flags_var_to_scrub}}") 85 endforeach() 86 endif() 87endif() 88 89string(TOUPPER "${LLVM_ABI_BREAKING_CHECKS}" uppercase_LLVM_ABI_BREAKING_CHECKS) 90 91if( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "WITH_ASSERTS" ) 92 if( LLVM_ENABLE_ASSERTIONS ) 93 set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 ) 94 endif() 95elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_ON" ) 96 set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 ) 97elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_OFF" ) 98 # We don't need to do anything special to turn off ABI breaking checks. 99elseif( NOT DEFINED LLVM_ABI_BREAKING_CHECKS ) 100 # Treat LLVM_ABI_BREAKING_CHECKS like "FORCE_OFF" when it has not been 101 # defined. 102else() 103 message(FATAL_ERROR "Unknown value for LLVM_ABI_BREAKING_CHECKS: \"${LLVM_ABI_BREAKING_CHECKS}\"!") 104endif() 105 106if(WIN32) 107 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) 108 if(CYGWIN) 109 set(LLVM_ON_WIN32 0) 110 set(LLVM_ON_UNIX 1) 111 else(CYGWIN) 112 set(LLVM_ON_WIN32 1) 113 set(LLVM_ON_UNIX 0) 114 endif(CYGWIN) 115else(WIN32) 116 if(UNIX) 117 set(LLVM_ON_WIN32 0) 118 set(LLVM_ON_UNIX 1) 119 if(APPLE) 120 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) 121 else(APPLE) 122 set(LLVM_HAVE_LINK_VERSION_SCRIPT 1) 123 endif(APPLE) 124 else(UNIX) 125 MESSAGE(SEND_ERROR "Unable to determine platform") 126 endif(UNIX) 127endif(WIN32) 128 129set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX}) 130set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX}) 131 132# We use *.dylib rather than *.so on darwin. 133set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX}) 134 135if(APPLE) 136 # Darwin-specific linker flags for loadable modules. 137 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress") 138endif() 139 140# Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise a DSO 141# build might work on ELF but fail on MachO/COFF. 142if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" OR WIN32 OR CYGWIN OR 143 ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR 144 ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD") AND 145 NOT LLVM_USE_SANITIZER) 146 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs") 147endif() 148 149 150function(append value) 151 foreach(variable ${ARGN}) 152 set(${variable} "${${variable}} ${value}" PARENT_SCOPE) 153 endforeach(variable) 154endfunction() 155 156function(append_if condition value) 157 if (${condition}) 158 foreach(variable ${ARGN}) 159 set(${variable} "${${variable}} ${value}" PARENT_SCOPE) 160 endforeach(variable) 161 endif() 162endfunction() 163 164macro(add_flag_if_supported flag name) 165 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}") 166 append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS) 167 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}") 168 append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS) 169endmacro() 170 171function(add_flag_or_print_warning flag name) 172 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}") 173 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}") 174 if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name}) 175 message(STATUS "Building with ${flag}") 176 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE) 177 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE) 178 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${flag}" PARENT_SCOPE) 179 else() 180 message(WARNING "${flag} is not supported.") 181 endif() 182endfunction() 183 184if( LLVM_ENABLE_PIC ) 185 if( XCODE ) 186 # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't 187 # know how to disable this, so just force ENABLE_PIC off for now. 188 message(WARNING "-fPIC not supported with Xcode.") 189 elseif( WIN32 OR CYGWIN) 190 # On Windows all code is PIC. MinGW warns if -fPIC is used. 191 else() 192 add_flag_or_print_warning("-fPIC" FPIC) 193 endif() 194endif() 195 196if(NOT WIN32 AND NOT CYGWIN) 197 # MinGW warns if -fvisibility-inlines-hidden is used. 198 check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG) 199 append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS) 200endif() 201 202if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 ) 203 # TODO: support other platforms and toolchains. 204 if( LLVM_BUILD_32_BITS ) 205 message(STATUS "Building 32 bits executables and libraries.") 206 add_llvm_definitions( -m32 ) 207 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32") 208 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32") 209 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32") 210 endif( LLVM_BUILD_32_BITS ) 211endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 ) 212 213if (LLVM_BUILD_STATIC) 214 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") 215endif() 216 217if( XCODE ) 218 # For Xcode enable several build settings that correspond to 219 # many warnings that are on by default in Clang but are 220 # not enabled for historical reasons. For versions of Xcode 221 # that do not support these options they will simply 222 # be ignored. 223 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_RETURN_TYPE "YES") 224 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_MISSING_NEWLINE "YES") 225 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VALUE "YES") 226 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VARIABLE "YES") 227 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_SIGN_COMPARE "YES") 228 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_FUNCTION "YES") 229 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED "YES") 230 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS "YES") 231 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNINITIALIZED_AUTOS "YES") 232 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_BOOL_CONVERSION "YES") 233 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_EMPTY_BODY "YES") 234 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_ENUM_CONVERSION "YES") 235 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_INT_CONVERSION "YES") 236 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_CONSTANT_CONVERSION "YES") 237 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_NON_VIRTUAL_DESTRUCTOR "YES") 238endif() 239 240# On Win32 using MS tools, provide an option to set the number of parallel jobs 241# to use. 242if( MSVC_IDE ) 243 set(LLVM_COMPILER_JOBS "0" CACHE STRING 244 "Number of parallel compiler jobs. 0 means use all processors. Default is 0.") 245 if( NOT LLVM_COMPILER_JOBS STREQUAL "1" ) 246 if( LLVM_COMPILER_JOBS STREQUAL "0" ) 247 add_llvm_definitions( /MP ) 248 else() 249 message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS}) 250 add_llvm_definitions( /MP${LLVM_COMPILER_JOBS} ) 251 endif() 252 else() 253 message(STATUS "Parallel compilation disabled") 254 endif() 255endif() 256 257if( MSVC ) 258 if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.0 ) 259 # For MSVC 2013, disable iterator null pointer checking in debug mode, 260 # especially so std::equal(nullptr, nullptr, nullptr) will not assert. 261 add_llvm_definitions("-D_DEBUG_POINTER_IMPL=") 262 endif() 263 264 include(ChooseMSVCCRT) 265 266 if( NOT (${CMAKE_VERSION} VERSION_LESS 2.8.11) ) 267 # set stack reserved size to ~10MB 268 # CMake previously automatically set this value for MSVC builds, but the 269 # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default 270 # value (1 MB) which is not enough for us in tasks such as parsing recursive 271 # C++ templates in Clang. 272 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000") 273 endif() 274 275 if( MSVC11 ) 276 add_llvm_definitions(-D_VARIADIC_MAX=10) 277 endif() 278 279 # Add definitions that make MSVC much less annoying. 280 add_llvm_definitions( 281 # For some reason MS wants to deprecate a bunch of standard functions... 282 -D_CRT_SECURE_NO_DEPRECATE 283 -D_CRT_SECURE_NO_WARNINGS 284 -D_CRT_NONSTDC_NO_DEPRECATE 285 -D_CRT_NONSTDC_NO_WARNINGS 286 -D_SCL_SECURE_NO_DEPRECATE 287 -D_SCL_SECURE_NO_WARNINGS 288 ) 289 290 set(msvc_warning_flags 291 # Disabled warnings. 292 -wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline) 293 -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned' 294 -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored' 295 -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data' 296 -wd4258 # Suppress ''var' : definition from the for loop is ignored; the definition from the enclosing scope is used' 297 -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data' 298 -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception' 299 -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized' 300 -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized' 301 -wd4355 # Suppress ''this' : used in base member initializer list' 302 -wd4456 # Suppress 'declaration of 'var' hides local variable' 303 -wd4457 # Suppress 'declaration of 'var' hides function parameter' 304 -wd4458 # Suppress 'declaration of 'var' hides class member' 305 -wd4459 # Suppress 'declaration of 'var' hides global declaration' 306 -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated' 307 -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible' 308 -wd4722 # Suppress 'function' : destructor never returns, potential memory leak 309 -wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)' 310 -wd4100 # Suppress 'unreferenced formal parameter' 311 -wd4127 # Suppress 'conditional expression is constant' 312 -wd4512 # Suppress 'assignment operator could not be generated' 313 -wd4505 # Suppress 'unreferenced local function has been removed' 314 -wd4610 # Suppress '<class> can never be instantiated' 315 -wd4510 # Suppress 'default constructor could not be generated' 316 -wd4702 # Suppress 'unreachable code' 317 -wd4245 # Suppress 'signed/unsigned mismatch' 318 -wd4706 # Suppress 'assignment within conditional expression' 319 -wd4310 # Suppress 'cast truncates constant value' 320 -wd4701 # Suppress 'potentially uninitialized local variable' 321 -wd4703 # Suppress 'potentially uninitialized local pointer variable' 322 -wd4389 # Suppress 'signed/unsigned mismatch' 323 -wd4611 # Suppress 'interaction between '_setjmp' and C++ object destruction is non-portable' 324 -wd4805 # Suppress 'unsafe mix of type <type> and type <type> in operation' 325 -wd4204 # Suppress 'nonstandard extension used : non-constant aggregate initializer' 326 -wd4577 # Suppress 'noexcept used with no exception handling mode specified; termination on exception is not guaranteed' 327 -wd4091 # Suppress 'typedef: ignored on left of '' when no variable is declared' 328 # C4592 is disabled because of false positives in Visual Studio 2015 329 # Update 1. Re-evaluate the usefulness of this diagnostic with Update 2. 330 -wd4592 # Suppress ''var': symbol will be dynamically initialized (implementation limitation) 331 -wd4319 # Suppress ''operator' : zero extending 'type' to 'type' of greater size' 332 333 # Ideally, we'd like this warning to be enabled, but MSVC 2013 doesn't 334 # support the 'aligned' attribute in the way that clang sources requires (for 335 # any code that uses the LLVM_ALIGNAS macro), so this is must be disabled to 336 # avoid unwanted alignment warnings. 337 # When we switch to requiring a version of MSVC that supports the 'alignas' 338 # specifier (MSVC 2015?) this warning can be re-enabled. 339 -wd4324 # Suppress 'structure was padded due to __declspec(align())' 340 341 # Promoted warnings. 342 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning. 343 344 # Promoted warnings to errors. 345 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error. 346 ) 347 348 # Enable warnings 349 if (LLVM_ENABLE_WARNINGS) 350 # Put /W4 in front of all the -we flags. cl.exe doesn't care, but for 351 # clang-cl having /W4 after the -we flags will re-enable the warnings 352 # disabled by -we. 353 set(msvc_warning_flags "/W4 ${msvc_warning_flags}") 354 # CMake appends /W3 by default, and having /W3 followed by /W4 will result in 355 # cl : Command line warning D9025 : overriding '/W3' with '/W4'. Since this is 356 # a command line warning and not a compiler warning, it cannot be suppressed except 357 # by fixing the command line. 358 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") 359 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 360 361 if (LLVM_ENABLE_PEDANTIC) 362 # No MSVC equivalent available 363 endif (LLVM_ENABLE_PEDANTIC) 364 endif (LLVM_ENABLE_WARNINGS) 365 if (LLVM_ENABLE_WERROR) 366 append("/WX" msvc_warning_flags) 367 endif (LLVM_ENABLE_WERROR) 368 369 foreach(flag ${msvc_warning_flags}) 370 append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 371 endforeach(flag) 372 373 append("/Zc:inline" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 374 375 # /Zc:strictStrings is incompatible with VS12's (Visual Studio 2013's) 376 # debug mode headers. Instead of only enabling them in VS2013's debug mode, 377 # we'll just enable them for Visual Studio 2015 (VS 14, MSVC_VERSION 1900) 378 # and up. 379 if (NOT (MSVC_VERSION LESS 1900)) 380 # Disable string literal const->non-const type conversion. 381 # "When specified, the compiler requires strict const-qualification 382 # conformance for pointers initialized by using string literals." 383 append("/Zc:strictStrings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 384 endif(NOT (MSVC_VERSION LESS 1900)) 385 386 # "Generate Intrinsic Functions". 387 append("/Oi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 388 389 # "Enforce type conversion rules". 390 append("/Zc:rvalueCast" CMAKE_CXX_FLAGS) 391 392 if (NOT LLVM_ENABLE_TIMESTAMPS AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") 393 # clang-cl and cl by default produce non-deterministic binaries because 394 # link.exe /incremental requires a timestamp in the .obj file. clang-cl 395 # has the flag /Brepro to force deterministic binaries, so pass that when 396 # LLVM_ENABLE_TIMESTAMPS is turned off. 397 # This checks CMAKE_CXX_COMPILER_ID in addition to check_cxx_compiler_flag() 398 # because cl.exe does not emit an error on flags it doesn't understand, 399 # letting check_cxx_compiler_flag() claim it understands all flags. 400 check_cxx_compiler_flag("/Brepro" SUPPORTS_BREPRO) 401 append_if(SUPPORTS_BREPRO "/Brepro" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 402 403 if (SUPPORTS_BREPRO) 404 # Check if /INCREMENTAL is passed to the linker and complain that it 405 # won't work with /Brepro. 406 string(TOUPPER "${CMAKE_EXE_LINKER_FLAGS}" upper_exe_flags) 407 string(TOUPPER "${CMAKE_MODULE_LINKER_FLAGS}" upper_module_flags) 408 string(TOUPPER "${CMAKE_SHARED_LINKER_FLAGS}" upper_shared_flags) 409 410 string(FIND "${upper_exe_flags}" "/INCREMENTAL" exe_index) 411 string(FIND "${upper_module_flags}" "/INCREMENTAL" module_index) 412 string(FIND "${upper_shared_flags}" "/INCREMENTAL" shared_index) 413 414 if (${exe_index} GREATER -1 OR 415 ${module_index} GREATER -1 OR 416 ${shared_index} GREATER -1) 417 message(FATAL_ERROR "LLVM_ENABLE_TIMESTAMPS not compatible with /INCREMENTAL linking") 418 endif() 419 endif() 420 endif() 421 422 # Disable sized deallocation if the flag is supported. MSVC fails to compile 423 # the operator new overload in User otherwise. 424 check_c_compiler_flag("/WX /Zc:sizedDealloc-" SUPPORTS_SIZED_DEALLOC) 425 append_if(SUPPORTS_SIZED_DEALLOC "/Zc:sizedDealloc-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 426 427elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE ) 428 if (LLVM_ENABLE_WARNINGS) 429 append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 430 append("-Wcast-qual" CMAKE_CXX_FLAGS) 431 432 # Turn off missing field initializer warnings for gcc to avoid noise from 433 # false positives with empty {}. Turn them on otherwise (they're off by 434 # default for clang). 435 check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG) 436 if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG) 437 if (CMAKE_COMPILER_IS_GNUCXX) 438 append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 439 else() 440 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 441 endif() 442 endif() 443 444 append_if(LLVM_ENABLE_PEDANTIC "-pedantic" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 445 append_if(LLVM_ENABLE_PEDANTIC "-Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 446 add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG) 447 append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS) 448 append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS) 449 450 # Check if -Wnon-virtual-dtor warns even though the class is marked final. 451 # If it does, don't add it. So it won't be added on clang 3.4 and older. 452 # This also catches cases when -Wnon-virtual-dtor isn't supported by 453 # the compiler at all. This flag is not activated for gcc since it will 454 # incorrectly identify a protected non-virtual base when there is a friend 455 # declaration. 456 if (NOT CMAKE_COMPILER_IS_GNUCXX) 457 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 458 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11 -Werror=non-virtual-dtor") 459 CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();protected: ~base();}; 460 class derived final : public base { public: ~derived();}; 461 int main() { return 0; }" 462 CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR) 463 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 464 append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR 465 "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS) 466 endif() 467 468 # Enable -Wdelete-non-virtual-dtor if available. 469 add_flag_if_supported("-Wdelete-non-virtual-dtor" DELETE_NON_VIRTUAL_DTOR_FLAG) 470 471 # Check if -Wcomment is OK with an // comment ending with '\' if the next 472 # line is also a // comment. 473 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 474 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment") 475 CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main() {return 0;}" 476 C_WCOMMENT_ALLOWS_LINE_WRAP) 477 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 478 if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP) 479 append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 480 endif() 481 endif (LLVM_ENABLE_WARNINGS) 482 append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 483 if (NOT LLVM_ENABLE_TIMESTAMPS) 484 add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME) 485 endif () 486 if (LLVM_ENABLE_CXX1Y) 487 check_cxx_compiler_flag("-std=c++1y" CXX_SUPPORTS_CXX1Y) 488 append_if(CXX_SUPPORTS_CXX1Y "-std=c++1y" CMAKE_CXX_FLAGS) 489 else() 490 check_cxx_compiler_flag("-std=c++11" CXX_SUPPORTS_CXX11) 491 if (CXX_SUPPORTS_CXX11) 492 if (CYGWIN OR MINGW) 493 # MinGW and Cygwin are a bit stricter and lack things like 494 # 'strdup', 'stricmp', etc in c++11 mode. 495 append("-std=gnu++11" CMAKE_CXX_FLAGS) 496 else() 497 append("-std=c++11" CMAKE_CXX_FLAGS) 498 endif() 499 else() 500 message(FATAL_ERROR "LLVM requires C++11 support but the '-std=c++11' flag isn't supported.") 501 endif() 502 endif() 503 if (LLVM_ENABLE_MODULES) 504 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 505 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fmodules") 506 # Check that we can build code with modules enabled, and that repeatedly 507 # including <cassert> still manages to respect NDEBUG properly. 508 CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG 509 #include <cassert> 510 #define NDEBUG 511 #include <cassert> 512 int main() { assert(this code is not compiled); }" 513 CXX_SUPPORTS_MODULES) 514 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 515 if (CXX_SUPPORTS_MODULES) 516 append_if(CXX_SUPPORTS_MODULES "-fmodules" CMAKE_C_FLAGS) 517 append_if(CXX_SUPPORTS_MODULES "-fmodules -fcxx-modules" CMAKE_CXX_FLAGS) 518 else() 519 message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler") 520 endif() 521 endif(LLVM_ENABLE_MODULES) 522endif( MSVC ) 523 524macro(append_common_sanitizer_flags) 525 if (NOT MSVC) 526 # Append -fno-omit-frame-pointer and turn on debug info to get better 527 # stack traces. 528 add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER) 529 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND 530 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO") 531 add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY) 532 endif() 533 # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large. 534 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 535 add_flag_if_supported("-O1" O1) 536 endif() 537 elseif (CLANG_CL) 538 # Keep frame pointers around. 539 append("/Oy-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 540 if (CMAKE_LINKER MATCHES "lld-link.exe") 541 # Use DWARF debug info with LLD. 542 append("-gdwarf" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 543 else() 544 # Enable codeview otherwise. 545 append("/Z7" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 546 endif() 547 # Always ask the linker to produce symbols with asan. 548 append("-debug" CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 549 endif() 550endmacro() 551 552# Turn on sanitizers if necessary. 553if(LLVM_USE_SANITIZER) 554 if (LLVM_ON_UNIX) 555 if (LLVM_USE_SANITIZER STREQUAL "Address") 556 append_common_sanitizer_flags() 557 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 558 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") 559 append_common_sanitizer_flags() 560 append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 561 if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") 562 append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 563 endif() 564 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined") 565 append_common_sanitizer_flags() 566 append("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all" 567 CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 568 elseif (LLVM_USE_SANITIZER STREQUAL "Thread") 569 append_common_sanitizer_flags() 570 append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 571 elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR 572 LLVM_USE_SANITIZER STREQUAL "Undefined;Address") 573 append_common_sanitizer_flags() 574 append("-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all" 575 CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 576 else() 577 message(FATAL_ERROR "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") 578 endif() 579 elseif(MSVC) 580 if (LLVM_USE_SANITIZER STREQUAL "Address") 581 append_common_sanitizer_flags() 582 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 583 else() 584 message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}") 585 endif() 586 else() 587 message(FATAL_ERROR "LLVM_USE_SANITIZER is not supported on this platform.") 588 endif() 589 if (LLVM_USE_SANITIZE_COVERAGE) 590 append("-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 591 endif() 592endif() 593 594# Turn on -gsplit-dwarf if requested 595if(LLVM_USE_SPLIT_DWARF) 596 add_definitions("-gsplit-dwarf") 597endif() 598 599add_llvm_definitions( -D__STDC_CONSTANT_MACROS ) 600add_llvm_definitions( -D__STDC_FORMAT_MACROS ) 601add_llvm_definitions( -D__STDC_LIMIT_MACROS ) 602 603# clang doesn't print colored diagnostics when invoked from Ninja 604if (UNIX AND 605 CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND 606 CMAKE_GENERATOR STREQUAL "Ninja") 607 append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 608endif() 609 610# Add flags for add_dead_strip(). 611# FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF? 612# But MinSizeRel seems to add that automatically, so maybe disable these 613# flags instead if LLVM_NO_DEAD_STRIP is set. 614if(NOT CYGWIN AND NOT WIN32) 615 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND 616 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 617 check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS) 618 if (C_SUPPORTS_FNO_FUNCTION_SECTIONS) 619 # Don't add -ffunction-section if it can be disabled with -fno-function-sections. 620 # Doing so will break sanitizers. 621 add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS) 622 endif() 623 add_flag_if_supported("-fdata-sections" FDATA_SECTIONS) 624 endif() 625endif() 626 627if(CYGWIN OR MINGW) 628 # Prune --out-implib from executables. It doesn't make sense even 629 # with --export-all-symbols. 630 string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " " 631 CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}") 632 string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " " 633 CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}") 634endif() 635 636if(MSVC) 637 # Remove flags here, for exceptions and RTTI. 638 # Each target property or source property should be responsible to control 639 # them. 640 # CL.EXE complains to override flags like "/GR /GR-". 641 string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 642 string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 643endif() 644 645# Provide public options to globally control RTTI and EH 646option(LLVM_ENABLE_EH "Enable Exception handling" OFF) 647option(LLVM_ENABLE_RTTI "Enable run time type information" OFF) 648if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI) 649 message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON") 650endif() 651 652option(LLVM_BUILD_INSTRUMENTED "Build LLVM and tools with PGO instrumentation (experimental)" Off) 653mark_as_advanced(LLVM_BUILD_INSTRUMENTED) 654append_if(LLVM_BUILD_INSTRUMENTED "-fprofile-instr-generate" 655 CMAKE_CXX_FLAGS 656 CMAKE_C_FLAGS 657 CMAKE_EXE_LINKER_FLAGS 658 CMAKE_SHARED_LINKER_FLAGS) 659 660set(LLVM_ENABLE_LTO OFF CACHE STRING "Build LLVM with LTO. May be specified as Thin or Full to use a particular kind of LTO") 661string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO) 662if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") 663 append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS 664 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 665elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL") 666 append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS 667 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 668elseif(LLVM_ENABLE_LTO) 669 append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS 670 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 671endif() 672 673# Plugin support 674# FIXME: Make this configurable. 675if(WIN32 OR CYGWIN) 676 if(BUILD_SHARED_LIBS) 677 set(LLVM_ENABLE_PLUGINS ON) 678 else() 679 set(LLVM_ENABLE_PLUGINS OFF) 680 endif() 681else() 682 set(LLVM_ENABLE_PLUGINS ON) 683endif() 684