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