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