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(CheckCompilerVersion) 10include(HandleLLVMStdlib) 11include(CheckCCompilerFlag) 12include(CheckCXXCompilerFlag) 13include(CheckSymbolExists) 14 15if(CMAKE_LINKER MATCHES "lld-link" OR (WIN32 AND LLVM_USE_LINKER STREQUAL "lld") OR LLVM_ENABLE_LLD) 16 set(LINKER_IS_LLD_LINK TRUE) 17else() 18 set(LINKER_IS_LLD_LINK FALSE) 19endif() 20 21set(LLVM_ENABLE_LTO OFF CACHE STRING "Build LLVM with LTO. May be specified as Thin or Full to use a particular kind of LTO") 22string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO) 23 24# Ninja Job Pool support 25# The following only works with the Ninja generator in CMake >= 3.0. 26set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING 27 "Define the maximum number of concurrent compilation jobs (Ninja only).") 28if(LLVM_PARALLEL_COMPILE_JOBS) 29 if(NOT CMAKE_MAKE_PROGRAM MATCHES "ninja") 30 message(WARNING "Job pooling is only available with Ninja generators.") 31 else() 32 set_property(GLOBAL APPEND PROPERTY JOB_POOLS compile_job_pool=${LLVM_PARALLEL_COMPILE_JOBS}) 33 set(CMAKE_JOB_POOL_COMPILE compile_job_pool) 34 endif() 35endif() 36 37set(LLVM_PARALLEL_LINK_JOBS "" CACHE STRING 38 "Define the maximum number of concurrent link jobs (Ninja only).") 39if(CMAKE_MAKE_PROGRAM MATCHES "ninja") 40 if(NOT LLVM_PARALLEL_LINK_JOBS AND uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") 41 message(STATUS "ThinLTO provides its own parallel linking - limiting parallel link jobs to 2.") 42 set(LLVM_PARALLEL_LINK_JOBS "2") 43 endif() 44 if(LLVM_PARALLEL_LINK_JOBS) 45 set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=${LLVM_PARALLEL_LINK_JOBS}) 46 set(CMAKE_JOB_POOL_LINK link_job_pool) 47 endif() 48elseif(LLVM_PARALLEL_LINK_JOBS) 49 message(WARNING "Job pooling is only available with Ninja generators.") 50endif() 51 52if( LLVM_ENABLE_ASSERTIONS ) 53 # MSVC doesn't like _DEBUG on release builds. See PR 4379. 54 if( NOT MSVC ) 55 add_definitions( -D_DEBUG ) 56 endif() 57 # On non-Debug builds cmake automatically defines NDEBUG, so we 58 # explicitly undefine it: 59 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" ) 60 # NOTE: use `add_compile_options` rather than `add_definitions` since 61 # `add_definitions` does not support generator expressions. 62 add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-UNDEBUG>) 63 64 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines. 65 foreach (flags_var_to_scrub 66 CMAKE_CXX_FLAGS_RELEASE 67 CMAKE_CXX_FLAGS_RELWITHDEBINFO 68 CMAKE_CXX_FLAGS_MINSIZEREL 69 CMAKE_C_FLAGS_RELEASE 70 CMAKE_C_FLAGS_RELWITHDEBINFO 71 CMAKE_C_FLAGS_MINSIZEREL) 72 string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " 73 "${flags_var_to_scrub}" "${${flags_var_to_scrub}}") 74 endforeach() 75 endif() 76endif() 77 78if(LLVM_ENABLE_EXPENSIVE_CHECKS) 79 add_definitions(-DEXPENSIVE_CHECKS) 80 81 # In some libstdc++ versions, std::min_element is not constexpr when 82 # _GLIBCXX_DEBUG is enabled. 83 CHECK_CXX_SOURCE_COMPILES(" 84 #define _GLIBCXX_DEBUG 85 #include <algorithm> 86 int main(int argc, char** argv) { 87 static constexpr int data[] = {0, 1}; 88 constexpr const int* min_elt = std::min_element(&data[0], &data[2]); 89 return 0; 90 }" CXX_SUPPORTS_GLIBCXX_DEBUG) 91 if(CXX_SUPPORTS_GLIBCXX_DEBUG) 92 add_definitions(-D_GLIBCXX_DEBUG) 93 else() 94 add_definitions(-D_GLIBCXX_ASSERTIONS) 95 endif() 96endif() 97 98if (LLVM_ENABLE_STRICT_FIXED_SIZE_VECTORS) 99 add_definitions(-DSTRICT_FIXED_SIZE_VECTORS) 100endif() 101 102string(TOUPPER "${LLVM_ABI_BREAKING_CHECKS}" uppercase_LLVM_ABI_BREAKING_CHECKS) 103 104if( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "WITH_ASSERTS" ) 105 if( LLVM_ENABLE_ASSERTIONS ) 106 set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 ) 107 endif() 108elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_ON" ) 109 set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 ) 110elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_OFF" ) 111 # We don't need to do anything special to turn off ABI breaking checks. 112elseif( NOT DEFINED LLVM_ABI_BREAKING_CHECKS ) 113 # Treat LLVM_ABI_BREAKING_CHECKS like "FORCE_OFF" when it has not been 114 # defined. 115else() 116 message(FATAL_ERROR "Unknown value for LLVM_ABI_BREAKING_CHECKS: \"${LLVM_ABI_BREAKING_CHECKS}\"!") 117endif() 118 119if( LLVM_REVERSE_ITERATION ) 120 set( LLVM_ENABLE_REVERSE_ITERATION 1 ) 121endif() 122 123if(WIN32) 124 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) 125 if(CYGWIN) 126 set(LLVM_ON_WIN32 0) 127 set(LLVM_ON_UNIX 1) 128 else(CYGWIN) 129 set(LLVM_ON_WIN32 1) 130 set(LLVM_ON_UNIX 0) 131 endif(CYGWIN) 132else(WIN32) 133 if(FUCHSIA OR UNIX) 134 set(LLVM_ON_WIN32 0) 135 set(LLVM_ON_UNIX 1) 136 if(APPLE OR ${CMAKE_SYSTEM_NAME} MATCHES "AIX") 137 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) 138 else() 139 set(LLVM_HAVE_LINK_VERSION_SCRIPT 1) 140 endif() 141 else(FUCHSIA OR UNIX) 142 MESSAGE(SEND_ERROR "Unable to determine platform") 143 endif(FUCHSIA OR UNIX) 144endif(WIN32) 145 146set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX}) 147set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX}) 148 149# We use *.dylib rather than *.so on darwin. 150set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX}) 151 152if(APPLE) 153 if(LLVM_ENABLE_LLD AND LLVM_ENABLE_LTO) 154 message(FATAL_ERROR "lld does not support LTO on Darwin") 155 endif() 156 # Darwin-specific linker flags for loadable modules. 157 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress") 158endif() 159 160if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") 161 # RHEL7 has ar and ranlib being non-deterministic by default. The D flag forces determinism, 162 # however only GNU version of ar and ranlib (2.27) have this option. 163 # RHEL DTS7 is also affected by this, which uses GNU binutils 2.28 164 execute_process(COMMAND ${CMAKE_AR} rD t.a 165 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 166 RESULT_VARIABLE AR_RESULT 167 OUTPUT_QUIET 168 ERROR_QUIET 169 ) 170 if(${AR_RESULT} EQUAL 0) 171 execute_process(COMMAND ${CMAKE_RANLIB} -D t.a 172 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 173 RESULT_VARIABLE RANLIB_RESULT 174 OUTPUT_QUIET 175 ERROR_QUIET 176 ) 177 if(${RANLIB_RESULT} EQUAL 0) 178 set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>") 179 set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>") 180 set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>") 181 182 set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>") 183 set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>") 184 set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>") 185 endif() 186 file(REMOVE ${CMAKE_BINARY_DIR}/t.a) 187 endif() 188endif() 189 190if(${CMAKE_SYSTEM_NAME} MATCHES "AIX") 191 # -fPIC does not enable the large code model for GCC on AIX but does for XL. 192 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 193 append("-mcmodel=large" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) 194 elseif(CMAKE_CXX_COMPILER_ID MATCHES "XL") 195 # XL generates a small number of relocations not of the large model, -bbigtoc is needed. 196 append("-Wl,-bbigtoc" 197 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 198 endif() 199endif() 200 201# Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise a DSO 202# build might work on ELF but fail on MachO/COFF. 203if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|FreeBSD|OpenBSD|DragonFly|AIX|SunOS" OR 204 WIN32 OR CYGWIN) AND 205 NOT LLVM_USE_SANITIZER) 206 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs") 207endif() 208 209# Pass -Wl,-z,nodelete. This makes sure our shared libraries are not unloaded 210# by dlclose(). We need that since the CLI API relies on cross-references 211# between global objects which became horribly broken when one of the libraries 212# is unloaded. 213if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") 214 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,nodelete") 215 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,nodelete") 216endif() 217 218 219function(append value) 220 foreach(variable ${ARGN}) 221 set(${variable} "${${variable}} ${value}" PARENT_SCOPE) 222 endforeach(variable) 223endfunction() 224 225function(append_if condition value) 226 if (${condition}) 227 foreach(variable ${ARGN}) 228 set(${variable} "${${variable}} ${value}" PARENT_SCOPE) 229 endforeach(variable) 230 endif() 231endfunction() 232 233macro(add_flag_if_supported flag name) 234 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}") 235 append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS) 236 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}") 237 append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS) 238endmacro() 239 240function(add_flag_or_print_warning flag name) 241 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}") 242 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}") 243 if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name}) 244 message(STATUS "Building with ${flag}") 245 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE) 246 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE) 247 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${flag}" PARENT_SCOPE) 248 else() 249 message(WARNING "${flag} is not supported.") 250 endif() 251endfunction() 252 253if( LLVM_ENABLE_LLD ) 254 if ( LLVM_USE_LINKER ) 255 message(FATAL_ERROR "LLVM_ENABLE_LLD and LLVM_USE_LINKER can't be set at the same time") 256 endif() 257 set(LLVM_USE_LINKER "lld") 258endif() 259 260if( LLVM_USE_LINKER ) 261 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 262 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fuse-ld=${LLVM_USE_LINKER}") 263 check_cxx_source_compiles("int main() { return 0; }" CXX_SUPPORTS_CUSTOM_LINKER) 264 if ( NOT CXX_SUPPORTS_CUSTOM_LINKER ) 265 message(FATAL_ERROR "Host compiler does not support '-fuse-ld=${LLVM_USE_LINKER}'") 266 endif() 267 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 268 append("-fuse-ld=${LLVM_USE_LINKER}" 269 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 270endif() 271 272if( LLVM_ENABLE_PIC ) 273 if( XCODE ) 274 # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't 275 # know how to disable this, so just force ENABLE_PIC off for now. 276 message(WARNING "-fPIC not supported with Xcode.") 277 elseif( WIN32 OR CYGWIN) 278 # On Windows all code is PIC. MinGW warns if -fPIC is used. 279 else() 280 add_flag_or_print_warning("-fPIC" FPIC) 281 endif() 282 # GCC for MIPS can miscompile LLVM due to PR37701. 283 if(CMAKE_COMPILER_IS_GNUCXX AND LLVM_NATIVE_ARCH STREQUAL "Mips" AND 284 NOT Uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 285 add_flag_or_print_warning("-fno-shrink-wrap" FNO_SHRINK_WRAP) 286 endif() 287endif() 288 289if(NOT WIN32 AND NOT CYGWIN AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) 290 # MinGW warns if -fvisibility-inlines-hidden is used. 291 # GCC on AIX warns if -fvisibility-inlines-hidden is used. 292 check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG) 293 append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS) 294endif() 295 296if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND MINGW) 297 add_definitions( -D_FILE_OFFSET_BITS=64 ) 298endif() 299 300if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 ) 301 # TODO: support other platforms and toolchains. 302 if( LLVM_BUILD_32_BITS ) 303 message(STATUS "Building 32 bits executables and libraries.") 304 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32") 305 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32") 306 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32") 307 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32") 308 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32") 309 310 # FIXME: CMAKE_SIZEOF_VOID_P is still 8 311 add_definitions(-D_LARGEFILE_SOURCE) 312 add_definitions(-D_FILE_OFFSET_BITS=64) 313 endif( LLVM_BUILD_32_BITS ) 314endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 ) 315 316# If building on a GNU specific 32-bit system, make sure off_t is 64 bits 317# so that off_t can stored offset > 2GB. 318# Android until version N (API 24) doesn't support it. 319if (ANDROID AND (ANDROID_NATIVE_API_LEVEL LESS 24)) 320 set(LLVM_FORCE_SMALLFILE_FOR_ANDROID TRUE) 321endif() 322if( CMAKE_SIZEOF_VOID_P EQUAL 4 AND NOT LLVM_FORCE_SMALLFILE_FOR_ANDROID) 323 # FIXME: It isn't handled in LLVM_BUILD_32_BITS. 324 add_definitions( -D_LARGEFILE_SOURCE ) 325 add_definitions( -D_FILE_OFFSET_BITS=64 ) 326endif() 327 328if( XCODE ) 329 # For Xcode enable several build settings that correspond to 330 # many warnings that are on by default in Clang but are 331 # not enabled for historical reasons. For versions of Xcode 332 # that do not support these options they will simply 333 # be ignored. 334 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_RETURN_TYPE "YES") 335 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_MISSING_NEWLINE "YES") 336 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VALUE "YES") 337 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VARIABLE "YES") 338 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_SIGN_COMPARE "YES") 339 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_FUNCTION "YES") 340 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED "YES") 341 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS "YES") 342 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNINITIALIZED_AUTOS "YES") 343 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_BOOL_CONVERSION "YES") 344 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_EMPTY_BODY "YES") 345 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_ENUM_CONVERSION "YES") 346 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_INT_CONVERSION "YES") 347 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_CONSTANT_CONVERSION "YES") 348 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_NON_VIRTUAL_DESTRUCTOR "YES") 349endif() 350 351# On Win32 using MS tools, provide an option to set the number of parallel jobs 352# to use. 353if( MSVC_IDE ) 354 set(LLVM_COMPILER_JOBS "0" CACHE STRING 355 "Number of parallel compiler jobs. 0 means use all processors. Default is 0.") 356 if( NOT LLVM_COMPILER_JOBS STREQUAL "1" ) 357 if( LLVM_COMPILER_JOBS STREQUAL "0" ) 358 add_definitions( /MP ) 359 else() 360 message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS}) 361 add_definitions( /MP${LLVM_COMPILER_JOBS} ) 362 endif() 363 else() 364 message(STATUS "Parallel compilation disabled") 365 endif() 366endif() 367 368# set stack reserved size to ~10MB 369if(MSVC) 370 # CMake previously automatically set this value for MSVC builds, but the 371 # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default 372 # value (1 MB) which is not enough for us in tasks such as parsing recursive 373 # C++ templates in Clang. 374 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000") 375elseif(MINGW) # FIXME: Also cygwin? 376 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216") 377 378 # Pass -mbig-obj to mingw gas on Win64. COFF has a 2**16 section limit, and 379 # on Win64, every COMDAT function creates at least 3 sections: .text, .pdata, 380 # and .xdata. 381 if (CMAKE_SIZEOF_VOID_P EQUAL 8) 382 append("-Wa,-mbig-obj" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 383 endif() 384endif() 385 386if( MSVC ) 387 include(ChooseMSVCCRT) 388 389 # Add definitions that make MSVC much less annoying. 390 add_definitions( 391 # For some reason MS wants to deprecate a bunch of standard functions... 392 -D_CRT_SECURE_NO_DEPRECATE 393 -D_CRT_SECURE_NO_WARNINGS 394 -D_CRT_NONSTDC_NO_DEPRECATE 395 -D_CRT_NONSTDC_NO_WARNINGS 396 -D_SCL_SECURE_NO_DEPRECATE 397 -D_SCL_SECURE_NO_WARNINGS 398 ) 399 400 # Tell MSVC to use the Unicode version of the Win32 APIs instead of ANSI. 401 add_definitions( 402 -DUNICODE 403 -D_UNICODE 404 ) 405 406 if (LLVM_ENABLE_WERROR) 407 append("/WX" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 408 endif (LLVM_ENABLE_WERROR) 409 410 append("/Zc:inline" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 411 412 # Allow users to request PDBs in release mode. CMake offeres the 413 # RelWithDebInfo configuration, but it uses different optimization settings 414 # (/Ob1 vs /Ob2 or -O2 vs -O3). LLVM provides this flag so that users can get 415 # PDBs without changing codegen. 416 option(LLVM_ENABLE_PDB OFF) 417 if (LLVM_ENABLE_PDB AND uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") 418 append("/Zi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 419 # /DEBUG disables linker GC and ICF, but we want those in Release mode. 420 append("/DEBUG /OPT:REF /OPT:ICF" 421 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS 422 CMAKE_SHARED_LINKER_FLAGS) 423 endif() 424 425 # Disable string literal const->non-const type conversion. 426 # "When specified, the compiler requires strict const-qualification 427 # conformance for pointers initialized by using string literals." 428 append("/Zc:strictStrings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 429 430 # "Generate Intrinsic Functions". 431 append("/Oi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 432 433 # "Enforce type conversion rules". 434 append("/Zc:rvalueCast" CMAKE_CXX_FLAGS) 435 436 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT LLVM_ENABLE_LTO) 437 # clang-cl and cl by default produce non-deterministic binaries because 438 # link.exe /incremental requires a timestamp in the .obj file. clang-cl 439 # has the flag /Brepro to force deterministic binaries. We want to pass that 440 # whenever you're building with clang unless you're passing /incremental 441 # or using LTO (/Brepro with LTO would result in a warning about the flag 442 # being unused, because we're not generating object files). 443 # This checks CMAKE_CXX_COMPILER_ID in addition to check_cxx_compiler_flag() 444 # because cl.exe does not emit an error on flags it doesn't understand, 445 # letting check_cxx_compiler_flag() claim it understands all flags. 446 check_cxx_compiler_flag("/Brepro" SUPPORTS_BREPRO) 447 if (SUPPORTS_BREPRO) 448 # Check if /INCREMENTAL is passed to the linker and complain that it 449 # won't work with /Brepro. 450 string(TOUPPER "${CMAKE_EXE_LINKER_FLAGS}" upper_exe_flags) 451 string(TOUPPER "${CMAKE_MODULE_LINKER_FLAGS}" upper_module_flags) 452 string(TOUPPER "${CMAKE_SHARED_LINKER_FLAGS}" upper_shared_flags) 453 454 string(FIND "${upper_exe_flags} ${upper_module_flags} ${upper_shared_flags}" 455 "/INCREMENTAL" linker_flag_idx) 456 457 if (${linker_flag_idx} GREATER -1) 458 message(WARNING "/Brepro not compatible with /INCREMENTAL linking - builds will be non-deterministic") 459 else() 460 append("/Brepro" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 461 endif() 462 endif() 463 endif() 464endif( MSVC ) 465 466# Warnings-as-errors handling for GCC-compatible compilers: 467if ( LLVM_COMPILER_IS_GCC_COMPATIBLE ) 468 append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 469 append_if(LLVM_ENABLE_WERROR "-Wno-error" CMAKE_REQUIRED_FLAGS) 470endif( LLVM_COMPILER_IS_GCC_COMPATIBLE ) 471 472# Specific default warnings-as-errors for compilers accepting GCC-compatible warning flags: 473if ( LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL" ) 474 add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME) 475 add_flag_if_supported("-Werror=unguarded-availability-new" WERROR_UNGUARDED_AVAILABILITY_NEW) 476endif( LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL" ) 477 478# Modules enablement for GCC-compatible compilers: 479if ( LLVM_COMPILER_IS_GCC_COMPATIBLE AND LLVM_ENABLE_MODULES ) 480 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 481 set(module_flags "-fmodules -fmodules-cache-path=${PROJECT_BINARY_DIR}/module.cache") 482 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 483 # On Darwin -fmodules does not imply -fcxx-modules. 484 set(module_flags "${module_flags} -fcxx-modules") 485 endif() 486 if (LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY) 487 set(module_flags "${module_flags} -Xclang -fmodules-local-submodule-visibility") 488 endif() 489 if (LLVM_ENABLE_MODULE_DEBUGGING AND 490 ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR 491 (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))) 492 set(module_flags "${module_flags} -gmodules") 493 endif() 494 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${module_flags}") 495 496 # Check that we can build code with modules enabled, and that repeatedly 497 # including <cassert> still manages to respect NDEBUG properly. 498 CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG 499 #include <cassert> 500 #define NDEBUG 501 #include <cassert> 502 int main() { assert(this code is not compiled); }" 503 CXX_SUPPORTS_MODULES) 504 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 505 if (CXX_SUPPORTS_MODULES) 506 append("${module_flags}" CMAKE_CXX_FLAGS) 507 else() 508 message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler") 509 endif() 510endif( LLVM_COMPILER_IS_GCC_COMPATIBLE AND LLVM_ENABLE_MODULES ) 511 512if (MSVC) 513 if (NOT CLANG_CL) 514 set(msvc_warning_flags 515 # Disabled warnings. 516 -wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline) 517 -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned' 518 -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data' 519 -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data' 520 -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception' 521 -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized' 522 -wd4456 # Suppress 'declaration of 'var' hides local variable' 523 -wd4457 # Suppress 'declaration of 'var' hides function parameter' 524 -wd4458 # Suppress 'declaration of 'var' hides class member' 525 -wd4459 # Suppress 'declaration of 'var' hides global declaration' 526 -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated' 527 -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible' 528 -wd4722 # Suppress 'function' : destructor never returns, potential memory leak 529 -wd4100 # Suppress 'unreferenced formal parameter' 530 -wd4127 # Suppress 'conditional expression is constant' 531 -wd4512 # Suppress 'assignment operator could not be generated' 532 -wd4505 # Suppress 'unreferenced local function has been removed' 533 -wd4610 # Suppress '<class> can never be instantiated' 534 -wd4510 # Suppress 'default constructor could not be generated' 535 -wd4702 # Suppress 'unreachable code' 536 -wd4245 # Suppress ''conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch' 537 -wd4706 # Suppress 'assignment within conditional expression' 538 -wd4310 # Suppress 'cast truncates constant value' 539 -wd4701 # Suppress 'potentially uninitialized local variable' 540 -wd4703 # Suppress 'potentially uninitialized local pointer variable' 541 -wd4389 # Suppress 'signed/unsigned mismatch' 542 -wd4611 # Suppress 'interaction between '_setjmp' and C++ object destruction is non-portable' 543 -wd4805 # Suppress 'unsafe mix of type <type> and type <type> in operation' 544 -wd4204 # Suppress 'nonstandard extension used : non-constant aggregate initializer' 545 -wd4577 # Suppress 'noexcept used with no exception handling mode specified; termination on exception is not guaranteed' 546 -wd4091 # Suppress 'typedef: ignored on left of '' when no variable is declared' 547 # C4592 is disabled because of false positives in Visual Studio 2015 548 # Update 1. Re-evaluate the usefulness of this diagnostic with Update 2. 549 -wd4592 # Suppress ''var': symbol will be dynamically initialized (implementation limitation) 550 -wd4319 # Suppress ''operator' : zero extending 'type' to 'type' of greater size' 551 # C4709 is disabled because of a bug with Visual Studio 2017 as of 552 # v15.8.8. Re-evaluate the usefulness of this diagnostic when the bug 553 # is fixed. 554 -wd4709 # Suppress comma operator within array index expression 555 556 # Ideally, we'd like this warning to be enabled, but even MSVC 2019 doesn't 557 # support the 'aligned' attribute in the way that clang sources requires (for 558 # any code that uses the LLVM_ALIGNAS macro), so this is must be disabled to 559 # avoid unwanted alignment warnings. 560 -wd4324 # Suppress 'structure was padded due to __declspec(align())' 561 562 # Promoted warnings. 563 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning. 564 565 # Promoted warnings to errors. 566 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error. 567 ) 568 endif(NOT CLANG_CL) 569 570 # Enable warnings 571 if (LLVM_ENABLE_WARNINGS) 572 # Put /W4 in front of all the -we flags. cl.exe doesn't care, but for 573 # clang-cl having /W4 after the -we flags will re-enable the warnings 574 # disabled by -we. 575 set(msvc_warning_flags "/W4 ${msvc_warning_flags}") 576 # CMake appends /W3 by default, and having /W3 followed by /W4 will result in 577 # cl : Command line warning D9025 : overriding '/W3' with '/W4'. Since this is 578 # a command line warning and not a compiler warning, it cannot be suppressed except 579 # by fixing the command line. 580 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") 581 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 582 583 if (LLVM_ENABLE_PEDANTIC) 584 # No MSVC equivalent available 585 endif (LLVM_ENABLE_PEDANTIC) 586 endif (LLVM_ENABLE_WARNINGS) 587 588 foreach(flag ${msvc_warning_flags}) 589 append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 590 endforeach(flag) 591endif (MSVC) 592 593if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)) 594 595 # Don't add -Wall for clang-cl, because it maps -Wall to -Weverything for 596 # MSVC compatibility. /W4 is added above instead. 597 if (NOT CLANG_CL) 598 append("-Wall" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 599 endif() 600 601 append("-Wextra -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 602 append("-Wcast-qual" CMAKE_CXX_FLAGS) 603 604 # Turn off missing field initializer warnings for gcc to avoid noise from 605 # false positives with empty {}. Turn them on otherwise (they're off by 606 # default for clang). 607 check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG) 608 if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG) 609 if (CMAKE_COMPILER_IS_GNUCXX) 610 append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 611 else() 612 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 613 endif() 614 endif() 615 616 if (LLVM_ENABLE_PEDANTIC AND LLVM_COMPILER_IS_GCC_COMPATIBLE) 617 append("-pedantic" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 618 append("-Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 619 endif() 620 621 add_flag_if_supported("-Wimplicit-fallthrough" IMPLICIT_FALLTHROUGH_FLAG) 622 add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG) 623 append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS) 624 append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS) 625 626 # Disable -Wclass-memaccess, a C++-only warning from GCC 8 that fires on 627 # LLVM's ADT classes. 628 check_cxx_compiler_flag("-Wclass-memaccess" CXX_SUPPORTS_CLASS_MEMACCESS_FLAG) 629 append_if(CXX_SUPPORTS_CLASS_MEMACCESS_FLAG "-Wno-class-memaccess" CMAKE_CXX_FLAGS) 630 631 # Disable -Wredundant-move on GCC>=9. GCC wants to remove std::move in code 632 # like "A foo(ConvertibleToA a) { return std::move(a); }", but this code does 633 # not compile (or uses the copy constructor instead) on clang<=3.8. Clang also 634 # has a -Wredundant-move, but it only fires when the types match exactly, so 635 # we can keep it here. 636 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 637 check_cxx_compiler_flag("-Wredundant-move" CXX_SUPPORTS_REDUNDANT_MOVE_FLAG) 638 append_if(CXX_SUPPORTS_REDUNDANT_MOVE_FLAG "-Wno-redundant-move" CMAKE_CXX_FLAGS) 639 endif() 640 641 # The LLVM libraries have no stable C++ API, so -Wnoexcept-type is not useful. 642 check_cxx_compiler_flag("-Wnoexcept-type" CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG) 643 append_if(CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG "-Wno-noexcept-type" CMAKE_CXX_FLAGS) 644 645 # Check if -Wnon-virtual-dtor warns even though the class is marked final. 646 # If it does, don't add it. So it won't be added on clang 3.4 and older. 647 # This also catches cases when -Wnon-virtual-dtor isn't supported by 648 # the compiler at all. This flag is not activated for gcc since it will 649 # incorrectly identify a protected non-virtual base when there is a friend 650 # declaration. Don't activate this in general on Windows as this warning has 651 # too many false positives on COM-style classes, which are destroyed with 652 # Release() (PR32286). 653 if (NOT CMAKE_COMPILER_IS_GNUCXX AND NOT WIN32) 654 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 655 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11 -Werror=non-virtual-dtor") 656 CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();protected: ~base();}; 657 class derived final : public base { public: ~derived();}; 658 int main() { return 0; }" 659 CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR) 660 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 661 append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR 662 "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS) 663 endif() 664 665 # Enable -Wdelete-non-virtual-dtor if available. 666 add_flag_if_supported("-Wdelete-non-virtual-dtor" DELETE_NON_VIRTUAL_DTOR_FLAG) 667 668 # Check if -Wcomment is OK with an // comment ending with '\' if the next 669 # line is also a // comment. 670 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 671 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment") 672 CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main() {return 0;}" 673 C_WCOMMENT_ALLOWS_LINE_WRAP) 674 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 675 if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP) 676 append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 677 endif() 678 679 # Enable -Wstring-conversion to catch misuse of string literals. 680 add_flag_if_supported("-Wstring-conversion" STRING_CONVERSION_FLAG) 681endif (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)) 682 683if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND NOT LLVM_ENABLE_WARNINGS) 684 append("-w" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 685endif() 686 687macro(append_common_sanitizer_flags) 688 if (NOT MSVC) 689 # Append -fno-omit-frame-pointer and turn on debug info to get better 690 # stack traces. 691 add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER) 692 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND 693 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO") 694 add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY) 695 endif() 696 # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large. 697 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND LLVM_OPTIMIZE_SANITIZED_BUILDS) 698 add_flag_if_supported("-O1" O1) 699 endif() 700 elseif (CLANG_CL) 701 # Keep frame pointers around. 702 append("/Oy-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 703 # Always ask the linker to produce symbols with asan. 704 append("/Z7" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 705 append("-debug" CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 706 endif() 707endmacro() 708 709# Turn on sanitizers if necessary. 710if(LLVM_USE_SANITIZER) 711 if (LLVM_ON_UNIX) 712 if (LLVM_USE_SANITIZER STREQUAL "Address") 713 append_common_sanitizer_flags() 714 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 715 elseif (LLVM_USE_SANITIZER STREQUAL "HWAddress") 716 append_common_sanitizer_flags() 717 append("-fsanitize=hwaddress" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 718 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") 719 append_common_sanitizer_flags() 720 append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 721 if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") 722 append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 723 endif() 724 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined") 725 append_common_sanitizer_flags() 726 append("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all" 727 CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 728 elseif (LLVM_USE_SANITIZER STREQUAL "Thread") 729 append_common_sanitizer_flags() 730 append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 731 elseif (LLVM_USE_SANITIZER STREQUAL "DataFlow") 732 append("-fsanitize=dataflow" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 733 elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR 734 LLVM_USE_SANITIZER STREQUAL "Undefined;Address") 735 append_common_sanitizer_flags() 736 append("-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all" 737 CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 738 elseif (LLVM_USE_SANITIZER STREQUAL "Leaks") 739 append_common_sanitizer_flags() 740 append("-fsanitize=leak" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 741 else() 742 message(FATAL_ERROR "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") 743 endif() 744 elseif(MSVC) 745 if (LLVM_USE_SANITIZER STREQUAL "Address") 746 append_common_sanitizer_flags() 747 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 748 else() 749 message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}") 750 endif() 751 else() 752 message(FATAL_ERROR "LLVM_USE_SANITIZER is not supported on this platform.") 753 endif() 754 if (LLVM_USE_SANITIZER MATCHES "(Undefined;)?Address(;Undefined)?") 755 add_flag_if_supported("-fsanitize-address-use-after-scope" 756 FSANITIZE_USE_AFTER_SCOPE_FLAG) 757 endif() 758 if (LLVM_USE_SANITIZE_COVERAGE) 759 append("-fsanitize=fuzzer-no-link" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 760 endif() 761 if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*") 762 set(BLACKLIST_FILE "${CMAKE_SOURCE_DIR}/utils/sanitizers/ubsan_blacklist.txt") 763 if (EXISTS "${BLACKLIST_FILE}") 764 append("-fsanitize-blacklist=${BLACKLIST_FILE}" 765 CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 766 endif() 767 endif() 768endif() 769 770# Turn on -gsplit-dwarf if requested in debug builds. 771if (LLVM_USE_SPLIT_DWARF AND 772 ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR 773 (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))) 774 # Limit to clang and gcc so far. Add compilers supporting this option. 775 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR 776 CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 777 add_compile_options(-gsplit-dwarf) 778 endif() 779endif() 780 781add_definitions( -D__STDC_CONSTANT_MACROS ) 782add_definitions( -D__STDC_FORMAT_MACROS ) 783add_definitions( -D__STDC_LIMIT_MACROS ) 784 785# clang and gcc don't default-print colored diagnostics when invoked from Ninja. 786if (UNIX AND 787 CMAKE_GENERATOR STREQUAL "Ninja" AND 788 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR 789 (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND 790 NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)))) 791 append("-fdiagnostics-color" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 792endif() 793 794# lld doesn't print colored diagnostics when invoked from Ninja 795if (UNIX AND CMAKE_GENERATOR STREQUAL "Ninja") 796 include(CheckLinkerFlag) 797 check_linker_flag("-Wl,--color-diagnostics" LINKER_SUPPORTS_COLOR_DIAGNOSTICS) 798 append_if(LINKER_SUPPORTS_COLOR_DIAGNOSTICS "-Wl,--color-diagnostics" 799 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 800endif() 801 802# Add flags for add_dead_strip(). 803# FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF? 804# But MinSizeRel seems to add that automatically, so maybe disable these 805# flags instead if LLVM_NO_DEAD_STRIP is set. 806if(NOT CYGWIN AND NOT WIN32) 807 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND 808 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 809 check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS) 810 if (C_SUPPORTS_FNO_FUNCTION_SECTIONS) 811 # Don't add -ffunction-sections if it can't be disabled with -fno-function-sections. 812 # Doing so will break sanitizers. 813 add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS) 814 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL") 815 append("-qfuncsect" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 816 endif() 817 add_flag_if_supported("-fdata-sections" FDATA_SECTIONS) 818 endif() 819elseif(MSVC) 820 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" ) 821 append("/Gw" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 822 endif() 823endif() 824 825if(MSVC) 826 # Remove flags here, for exceptions and RTTI. 827 # Each target property or source property should be responsible to control 828 # them. 829 # CL.EXE complains to override flags like "/GR /GR-". 830 string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 831 string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 832endif() 833 834# Provide public options to globally control RTTI and EH 835option(LLVM_ENABLE_EH "Enable Exception handling" OFF) 836option(LLVM_ENABLE_RTTI "Enable run time type information" OFF) 837if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI) 838 message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON") 839endif() 840 841option(LLVM_USE_NEWPM "Build LLVM using the experimental new pass manager" Off) 842mark_as_advanced(LLVM_USE_NEWPM) 843if (LLVM_USE_NEWPM) 844 append("-fexperimental-new-pass-manager" 845 CMAKE_CXX_FLAGS 846 CMAKE_C_FLAGS 847 CMAKE_EXE_LINKER_FLAGS 848 CMAKE_SHARED_LINKER_FLAGS) 849endif() 850 851option(LLVM_ENABLE_IR_PGO "Build LLVM and tools with IR PGO instrumentation (deprecated)" Off) 852mark_as_advanced(LLVM_ENABLE_IR_PGO) 853 854set(LLVM_BUILD_INSTRUMENTED OFF CACHE STRING "Build LLVM and tools with PGO instrumentation. May be specified as IR or Frontend") 855mark_as_advanced(LLVM_BUILD_INSTRUMENTED) 856string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" uppercase_LLVM_BUILD_INSTRUMENTED) 857 858if (LLVM_BUILD_INSTRUMENTED) 859 if (LLVM_ENABLE_IR_PGO OR uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "IR") 860 append("-fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\"" 861 CMAKE_CXX_FLAGS 862 CMAKE_C_FLAGS) 863 if(NOT LINKER_IS_LLD_LINK) 864 append("-fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\"" 865 CMAKE_EXE_LINKER_FLAGS 866 CMAKE_SHARED_LINKER_FLAGS) 867 endif() 868 elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR") 869 append("-fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\"" 870 CMAKE_CXX_FLAGS 871 CMAKE_C_FLAGS) 872 if(NOT LINKER_IS_LLD_LINK) 873 append("-fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\"" 874 CMAKE_EXE_LINKER_FLAGS 875 CMAKE_SHARED_LINKER_FLAGS) 876 endif() 877 else() 878 append("-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\"" 879 CMAKE_CXX_FLAGS 880 CMAKE_C_FLAGS) 881 if(NOT LINKER_IS_LLD_LINK) 882 append("-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\"" 883 CMAKE_EXE_LINKER_FLAGS 884 CMAKE_SHARED_LINKER_FLAGS) 885 endif() 886 endif() 887endif() 888 889# When using clang-cl with an instrumentation-based tool, add clang's library 890# resource directory to the library search path. Because cmake invokes the 891# linker directly, it isn't sufficient to pass -fsanitize=* to the linker. 892if (CLANG_CL AND (LLVM_BUILD_INSTRUMENTED OR LLVM_USE_SANITIZER)) 893 execute_process( 894 COMMAND ${CMAKE_CXX_COMPILER} /clang:-print-resource-dir 895 OUTPUT_VARIABLE clang_resource_dir 896 ERROR_VARIABLE clang_cl_stderr 897 OUTPUT_STRIP_TRAILING_WHITESPACE 898 ERROR_STRIP_TRAILING_WHITESPACE 899 RESULT_VARIABLE clang_cl_exit_code) 900 if (NOT "${clang_cl_exit_code}" STREQUAL "0") 901 message(FATAL_ERROR 902 "Unable to invoke clang-cl to find resource dir: ${clang_cl_stderr}") 903 endif() 904 file(TO_CMAKE_PATH "${clang_resource_dir}" clang_resource_dir) 905 append("/libpath:${clang_resource_dir}/lib/windows" 906 CMAKE_EXE_LINKER_FLAGS 907 CMAKE_SHARED_LINKER_FLAGS) 908endif() 909 910if(LLVM_PROFDATA_FILE AND EXISTS ${LLVM_PROFDATA_FILE}) 911 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) 912 append("-fprofile-instr-use=\"${LLVM_PROFDATA_FILE}\"" 913 CMAKE_CXX_FLAGS 914 CMAKE_C_FLAGS) 915 if(NOT LINKER_IS_LLD_LINK) 916 append("-fprofile-instr-use=\"${LLVM_PROFDATA_FILE}\"" 917 CMAKE_EXE_LINKER_FLAGS 918 CMAKE_SHARED_LINKER_FLAGS) 919 endif() 920 else() 921 message(FATAL_ERROR "LLVM_PROFDATA_FILE can only be specified when compiling with clang") 922 endif() 923endif() 924 925option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation" Off) 926mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE) 927append_if(LLVM_BUILD_INSTRUMENTED_COVERAGE "-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\" -fcoverage-mapping" 928 CMAKE_CXX_FLAGS 929 CMAKE_C_FLAGS 930 CMAKE_EXE_LINKER_FLAGS 931 CMAKE_SHARED_LINKER_FLAGS) 932 933if (LLVM_BUILD_INSTRUMENTED AND LLVM_BUILD_INSTRUMENTED_COVERAGE) 934 message(FATAL_ERROR "LLVM_BUILD_INSTRUMENTED and LLVM_BUILD_INSTRUMENTED_COVERAGE cannot both be specified") 935endif() 936 937if(LLVM_ENABLE_LTO AND LLVM_ON_WIN32 AND NOT LINKER_IS_LLD_LINK) 938 message(FATAL_ERROR "When compiling for Windows, LLVM_ENABLE_LTO requires using lld as the linker (point CMAKE_LINKER at lld-link.exe)") 939endif() 940if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") 941 append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) 942 if(NOT LINKER_IS_LLD_LINK) 943 append("-flto=thin" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 944 endif() 945 # If the linker supports it, enable the lto cache. This improves initial build 946 # time a little since we re-link a lot of the same objects, and significantly 947 # improves incremental build time. 948 # FIXME: We should move all this logic into the clang driver. 949 if(APPLE) 950 append("-Wl,-cache_path_lto,${PROJECT_BINARY_DIR}/lto.cache" 951 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 952 elseif(UNIX AND LLVM_USE_LINKER STREQUAL "lld") 953 append("-Wl,--thinlto-cache-dir=${PROJECT_BINARY_DIR}/lto.cache" 954 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 955 elseif(LLVM_USE_LINKER STREQUAL "gold") 956 append("-Wl,--plugin-opt,cache-dir=${PROJECT_BINARY_DIR}/lto.cache" 957 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 958 elseif(LINKER_IS_LLD_LINK) 959 append("/lldltocache:${PROJECT_BINARY_DIR}/lto.cache" 960 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 961 endif() 962elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL") 963 append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) 964 if(NOT LINKER_IS_LLD_LINK) 965 append("-flto=full" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 966 endif() 967elseif(LLVM_ENABLE_LTO) 968 append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) 969 if(NOT LINKER_IS_LLD_LINK) 970 append("-flto" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 971 endif() 972endif() 973 974# This option makes utils/extract_symbols.py be used to determine the list of 975# symbols to export from LLVM tools. This is necessary when using MSVC if you 976# want to allow plugins, though note that the plugin has to explicitly link 977# against (exactly one) tool so we can't unilaterally turn on 978# LLVM_ENABLE_PLUGINS when it's enabled. 979option(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS "Export symbols from LLVM tools so that plugins can import them" OFF) 980if(BUILD_SHARED_LIBS AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 981 message(FATAL_ERROR "BUILD_SHARED_LIBS not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS") 982endif() 983if(LLVM_LINK_LLVM_DYLIB AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 984 message(FATAL_ERROR "LLVM_LINK_LLVM_DYLIB not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS") 985endif() 986 987# By default we should enable LLVM_ENABLE_IDE only for multi-configuration 988# generators. This option disables optional build system features that make IDEs 989# less usable. 990set(LLVM_ENABLE_IDE_default OFF) 991if (CMAKE_CONFIGURATION_TYPES) 992 set(LLVM_ENABLE_IDE_default ON) 993endif() 994option(LLVM_ENABLE_IDE 995 "Disable optional build system features that cause problems for IDE generators" 996 ${LLVM_ENABLE_IDE_default}) 997if (CMAKE_CONFIGURATION_TYPES AND NOT LLVM_ENABLE_IDE) 998 message(WARNING "Disabling LLVM_ENABLE_IDE on multi-configuration generators is not recommended.") 999endif() 1000 1001function(get_compile_definitions) 1002 get_directory_property(top_dir_definitions DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS) 1003 foreach(definition ${top_dir_definitions}) 1004 if(DEFINED result) 1005 string(APPEND result " -D${definition}") 1006 else() 1007 set(result "-D${definition}") 1008 endif() 1009 endforeach() 1010 set(LLVM_DEFINITIONS "${result}" PARENT_SCOPE) 1011endfunction() 1012get_compile_definitions() 1013 1014option(LLVM_FORCE_ENABLE_STATS "Enable statistics collection for builds that wouldn't normally enable it" OFF) 1015 1016check_symbol_exists(os_signpost_interval_begin "os/signpost.h" macos_signposts_available) 1017if(macos_signposts_available) 1018 check_cxx_source_compiles( 1019 "#include <os/signpost.h> 1020 int main() { os_signpost_interval_begin(nullptr, 0, \"\", \"\"); return 0; }" 1021 macos_signposts_usable) 1022 if(macos_signposts_usable) 1023 set(LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS "WITH_ASSERTS" CACHE STRING 1024 "Enable support for Xcode signposts. Can be WITH_ASSERTS, FORCE_ON, FORCE_OFF") 1025 string(TOUPPER "${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}" 1026 uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS) 1027 if( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "WITH_ASSERTS" ) 1028 if( LLVM_ENABLE_ASSERTIONS ) 1029 set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 ) 1030 endif() 1031 elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_ON" ) 1032 set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 ) 1033 elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_OFF" ) 1034 # We don't need to do anything special to turn off signposts. 1035 elseif( NOT DEFINED LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS ) 1036 # Treat LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS like "FORCE_OFF" when it has not been 1037 # defined. 1038 else() 1039 message(FATAL_ERROR "Unknown value for LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS:" 1040 " \"${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}\"!") 1041 endif() 1042 endif() 1043endif() 1044 1045set(LLVM_SOURCE_PREFIX "" CACHE STRING "Use prefix for sources") 1046 1047option(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO "Use relative paths in debug info" OFF) 1048 1049if(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO) 1050 check_c_compiler_flag("-fdebug-prefix-map=foo=bar" SUPPORTS_FDEBUG_PREFIX_MAP) 1051 if(LLVM_ENABLE_PROJECTS_USED) 1052 get_filename_component(source_root "${LLVM_MAIN_SRC_DIR}/.." ABSOLUTE) 1053 else() 1054 set(source_root "${LLVM_MAIN_SRC_DIR}") 1055 endif() 1056 file(RELATIVE_PATH relative_root "${source_root}" "${CMAKE_BINARY_DIR}") 1057 append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1058 append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1059 add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES) 1060endif() 1061 1062option(LLVM_USE_RELATIVE_PATHS_IN_FILES "Use relative paths in sources and debug info" OFF) 1063 1064if(LLVM_USE_RELATIVE_PATHS_IN_FILES) 1065 check_c_compiler_flag("-ffile-prefix-map=foo=bar" SUPPORTS_FFILE_PREFIX_MAP) 1066 if(LLVM_ENABLE_PROJECTS_USED) 1067 get_filename_component(source_root "${LLVM_MAIN_SRC_DIR}/.." ABSOLUTE) 1068 else() 1069 set(source_root "${LLVM_MAIN_SRC_DIR}") 1070 endif() 1071 file(RELATIVE_PATH relative_root "${source_root}" "${CMAKE_BINARY_DIR}") 1072 append_if(SUPPORTS_FFILE_PREFIX_MAP "-ffile-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1073 append_if(SUPPORTS_FFILE_PREFIX_MAP "-ffile-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1074 add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES) 1075endif() 1076