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 some libstdc++ versions, 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 add_flag_or_print_warning("-fno-shrink-wrap" FNO_SHRINK_WRAP) 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 check_cxx_compiler_flag("/Brepro" SUPPORTS_BREPRO) 639 if (SUPPORTS_BREPRO) 640 # Check if /INCREMENTAL is passed to the linker and complain that it 641 # won't work with /Brepro. 642 has_msvc_incremental_no_flag("${CMAKE_EXE_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${CMAKE_EXE_LINKER_FLAGS}" NO_INCR_EXE) 643 has_msvc_incremental_no_flag("${CMAKE_MODULE_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${CMAKE_MODULE_LINKER_FLAGS}" NO_INCR_MODULE) 644 has_msvc_incremental_no_flag("${CMAKE_SHARED_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${CMAKE_SHARED_LINKER_FLAGS}" NO_INCR_SHARED) 645 if (NO_INCR_EXE AND NO_INCR_MODULE AND NO_INCR_SHARED) 646 append("/Brepro" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 647 else() 648 message(WARNING "/Brepro not compatible with /INCREMENTAL linking - builds will be non-deterministic") 649 endif() 650 endif() 651 endif() 652 # By default MSVC has a 2^16 limit on the number of sections in an object file, 653 # but in many objects files need more than that. This flag is to increase the 654 # number of sections. 655 append("/bigobj" CMAKE_CXX_FLAGS) 656 657 # Enable standards conformance mode. 658 # This ensures handling of various C/C++ constructs is more similar to other compilers. 659 append("/permissive-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 660endif( MSVC ) 661 662# Warnings-as-errors handling for GCC-compatible compilers: 663if ( LLVM_COMPILER_IS_GCC_COMPATIBLE ) 664 append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 665 append_if(LLVM_ENABLE_WERROR "-Wno-error" CMAKE_REQUIRED_FLAGS) 666endif( LLVM_COMPILER_IS_GCC_COMPATIBLE ) 667 668# Specific default warnings-as-errors for compilers accepting GCC-compatible warning flags: 669if ( LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL" ) 670 add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME) 671 add_flag_if_supported("-Werror=unguarded-availability-new" WERROR_UNGUARDED_AVAILABILITY_NEW) 672endif( LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL" ) 673 674if ( LLVM_COMPILER_IS_GCC_COMPATIBLE ) 675 # LLVM data structures like llvm::User and llvm::MDNode rely on 676 # the value of object storage persisting beyond the lifetime of the 677 # object (#24952). This is not standard compliant and causes a runtime 678 # crash if LLVM is built with GCC and LTO enabled (#57740). Until 679 # these bugs are fixed, we need to disable dead store eliminations 680 # based on object lifetime. 681 add_flag_if_supported("-fno-lifetime-dse" CMAKE_CXX_FLAGS) 682endif ( LLVM_COMPILER_IS_GCC_COMPATIBLE ) 683 684# Modules enablement for GCC-compatible compilers: 685if ( LLVM_COMPILER_IS_GCC_COMPATIBLE AND LLVM_ENABLE_MODULES ) 686 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 687 set(module_flags "-fmodules -fmodules-cache-path=${PROJECT_BINARY_DIR}/module.cache") 688 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 689 # On Darwin -fmodules does not imply -fcxx-modules. 690 set(module_flags "${module_flags} -fcxx-modules") 691 endif() 692 if (LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY) 693 set(module_flags "${module_flags} -Xclang -fmodules-local-submodule-visibility") 694 endif() 695 if (LLVM_ENABLE_MODULE_DEBUGGING AND 696 ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR 697 (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))) 698 set(module_flags "${module_flags} -gmodules") 699 endif() 700 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${module_flags}") 701 702 # Check that we can build code with modules enabled, and that repeatedly 703 # including <cassert> still manages to respect NDEBUG properly. 704 CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG 705 #include <cassert> 706 #define NDEBUG 707 #include <cassert> 708 int main() { assert(this code is not compiled); }" 709 CXX_SUPPORTS_MODULES) 710 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 711 if (CXX_SUPPORTS_MODULES) 712 append("${module_flags}" CMAKE_CXX_FLAGS) 713 else() 714 message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler") 715 endif() 716endif( LLVM_COMPILER_IS_GCC_COMPATIBLE AND LLVM_ENABLE_MODULES ) 717 718if (MSVC) 719 if (NOT CLANG_CL) 720 set(msvc_warning_flags 721 # Disabled warnings. 722 -wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline) 723 -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned' 724 -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data' 725 -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data' 726 -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception' 727 -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized' 728 -wd4456 # Suppress 'declaration of 'var' hides local variable' 729 -wd4457 # Suppress 'declaration of 'var' hides function parameter' 730 -wd4458 # Suppress 'declaration of 'var' hides class member' 731 -wd4459 # Suppress 'declaration of 'var' hides global declaration' 732 -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated' 733 -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible' 734 -wd4722 # Suppress 'function' : destructor never returns, potential memory leak 735 -wd4100 # Suppress 'unreferenced formal parameter' 736 -wd4127 # Suppress 'conditional expression is constant' 737 -wd4512 # Suppress 'assignment operator could not be generated' 738 -wd4505 # Suppress 'unreferenced local function has been removed' 739 -wd4610 # Suppress '<class> can never be instantiated' 740 -wd4510 # Suppress 'default constructor could not be generated' 741 -wd4702 # Suppress 'unreachable code' 742 -wd4245 # Suppress ''conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch' 743 -wd4706 # Suppress 'assignment within conditional expression' 744 -wd4310 # Suppress 'cast truncates constant value' 745 -wd4701 # Suppress 'potentially uninitialized local variable' 746 -wd4703 # Suppress 'potentially uninitialized local pointer variable' 747 -wd4389 # Suppress 'signed/unsigned mismatch' 748 -wd4611 # Suppress 'interaction between '_setjmp' and C++ object destruction is non-portable' 749 -wd4805 # Suppress 'unsafe mix of type <type> and type <type> in operation' 750 -wd4204 # Suppress 'nonstandard extension used : non-constant aggregate initializer' 751 -wd4577 # Suppress 'noexcept used with no exception handling mode specified; termination on exception is not guaranteed' 752 -wd4091 # Suppress 'typedef: ignored on left of '' when no variable is declared' 753 # C4592 is disabled because of false positives in Visual Studio 2015 754 # Update 1. Re-evaluate the usefulness of this diagnostic with Update 2. 755 -wd4592 # Suppress ''var': symbol will be dynamically initialized (implementation limitation) 756 -wd4319 # Suppress ''operator' : zero extending 'type' to 'type' of greater size' 757 # C4709 is disabled because of a bug with Visual Studio 2017 as of 758 # v15.8.8. Re-evaluate the usefulness of this diagnostic when the bug 759 # is fixed. 760 -wd4709 # Suppress comma operator within array index expression 761 762 # We'd like this warning to be enabled, but it triggers from code in 763 # WinBase.h that we don't have control over. 764 -wd5105 # Suppress macro expansion producing 'defined' has undefined behavior 765 766 # Ideally, we'd like this warning to be enabled, but even MSVC 2019 doesn't 767 # support the 'aligned' attribute in the way that clang sources requires (for 768 # any code that uses the LLVM_ALIGNAS macro), so this is must be disabled to 769 # avoid unwanted alignment warnings. 770 -wd4324 # Suppress 'structure was padded due to __declspec(align())' 771 772 # Promoted warnings. 773 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning. 774 775 # Promoted warnings to errors. 776 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error. 777 ) 778 endif(NOT CLANG_CL) 779 780 # Enable warnings 781 if (LLVM_ENABLE_WARNINGS) 782 # Put /W4 in front of all the -we flags. cl.exe doesn't care, but for 783 # clang-cl having /W4 after the -we flags will re-enable the warnings 784 # disabled by -we. 785 set(msvc_warning_flags "/W4 ${msvc_warning_flags}") 786 # CMake appends /W3 by default, and having /W3 followed by /W4 will result in 787 # cl : Command line warning D9025 : overriding '/W3' with '/W4'. Since this is 788 # a command line warning and not a compiler warning, it cannot be suppressed except 789 # by fixing the command line. 790 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") 791 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 792 793 if (LLVM_ENABLE_PEDANTIC) 794 # No MSVC equivalent available 795 endif (LLVM_ENABLE_PEDANTIC) 796 endif (LLVM_ENABLE_WARNINGS) 797 798 foreach(flag ${msvc_warning_flags}) 799 append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 800 endforeach(flag) 801endif (MSVC) 802 803if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)) 804 805 # Don't add -Wall for clang-cl, because it maps -Wall to -Weverything for 806 # MSVC compatibility. /W4 is added above instead. 807 if (NOT CLANG_CL) 808 append("-Wall" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 809 endif() 810 811 append("-Wextra -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 812 append("-Wcast-qual" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 813 814 # Turn off missing field initializer warnings for gcc to avoid noise from 815 # false positives with empty {}. Turn them on otherwise (they're off by 816 # default for clang). 817 check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG) 818 if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG) 819 if (CMAKE_COMPILER_IS_GNUCXX) 820 append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 821 else() 822 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 823 endif() 824 endif() 825 826 if (LLVM_ENABLE_PEDANTIC AND LLVM_COMPILER_IS_GCC_COMPATIBLE) 827 append("-pedantic" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 828 append("-Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 829 830 # GCC warns about redundant toplevel semicolons (enabled by -pedantic 831 # above), while Clang doesn't. Enable the corresponding Clang option to 832 # pick up on these even in builds with Clang. 833 add_flag_if_supported("-Wc++98-compat-extra-semi" CXX98_COMPAT_EXTRA_SEMI_FLAG) 834 endif() 835 836 add_flag_if_supported("-Wimplicit-fallthrough" IMPLICIT_FALLTHROUGH_FLAG) 837 add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG) 838 append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS) 839 append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS) 840 841 # Disable -Wnonnull for GCC warning as it is emitting a lot of false positives. 842 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 843 append("-Wno-nonnull" CMAKE_CXX_FLAGS) 844 endif() 845 846 # Disable -Wclass-memaccess, a C++-only warning from GCC 8 that fires on 847 # LLVM's ADT classes. 848 check_cxx_compiler_flag("-Wclass-memaccess" CXX_SUPPORTS_CLASS_MEMACCESS_FLAG) 849 append_if(CXX_SUPPORTS_CLASS_MEMACCESS_FLAG "-Wno-class-memaccess" CMAKE_CXX_FLAGS) 850 851 # Disable -Wredundant-move and -Wpessimizing-move on GCC>=9. GCC wants to 852 # remove std::move in code like "A foo(ConvertibleToA a) { 853 # return std::move(a); }", but this code does not compile (or uses the copy 854 # constructor instead) on clang<=3.8. Clang also has a -Wredundant-move and 855 # -Wpessimizing-move, but they only fire when the types match exactly, so we 856 # can keep them here. 857 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 858 check_cxx_compiler_flag("-Wredundant-move" CXX_SUPPORTS_REDUNDANT_MOVE_FLAG) 859 append_if(CXX_SUPPORTS_REDUNDANT_MOVE_FLAG "-Wno-redundant-move" CMAKE_CXX_FLAGS) 860 check_cxx_compiler_flag("-Wpessimizing-move" CXX_SUPPORTS_PESSIMIZING_MOVE_FLAG) 861 append_if(CXX_SUPPORTS_PESSIMIZING_MOVE_FLAG "-Wno-pessimizing-move" CMAKE_CXX_FLAGS) 862 endif() 863 864 # The LLVM libraries have no stable C++ API, so -Wnoexcept-type is not useful. 865 check_cxx_compiler_flag("-Wnoexcept-type" CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG) 866 append_if(CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG "-Wno-noexcept-type" CMAKE_CXX_FLAGS) 867 868 # Check if -Wnon-virtual-dtor warns for a class marked final, when it has a 869 # friend declaration. If it does, don't add -Wnon-virtual-dtor. The case is 870 # considered unhelpful (https://gcc.gnu.org/PR102168). 871 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 872 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=non-virtual-dtor") 873 CHECK_CXX_SOURCE_COMPILES("class f {}; 874 class base {friend f; public: virtual void anchor();protected: ~base();}; 875 int main() { return 0; }" 876 CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR) 877 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 878 append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS) 879 880 append("-Wdelete-non-virtual-dtor" CMAKE_CXX_FLAGS) 881 882 # Enable -Wsuggest-override if it's available, and only if it doesn't 883 # suggest adding 'override' to functions that are already marked 'final' 884 # (which means it is disabled for GCC < 9.2). 885 check_cxx_compiler_flag("-Wsuggest-override" CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG) 886 if (CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG) 887 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 888 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=suggest-override") 889 CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();}; 890 class derived : base {public: void anchor() final;}; 891 int main() { return 0; }" 892 CXX_WSUGGEST_OVERRIDE_ALLOWS_ONLY_FINAL) 893 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 894 append_if(CXX_WSUGGEST_OVERRIDE_ALLOWS_ONLY_FINAL "-Wsuggest-override" CMAKE_CXX_FLAGS) 895 endif() 896 897 # Check if -Wcomment is OK with an // comment ending with '\' if the next 898 # line is also a // comment. 899 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 900 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment") 901 CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main(void) {return 0;}" 902 C_WCOMMENT_ALLOWS_LINE_WRAP) 903 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 904 if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP) 905 append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 906 endif() 907 908 # Enable -Wstring-conversion to catch misuse of string literals. 909 add_flag_if_supported("-Wstring-conversion" STRING_CONVERSION_FLAG) 910 911 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 912 # Disable the misleading indentation warning with GCC; GCC can 913 # produce noisy notes about this getting disabled in large files. 914 # See e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89549 915 check_cxx_compiler_flag("-Wmisleading-indentation" CXX_SUPPORTS_MISLEADING_INDENTATION_FLAG) 916 append_if(CXX_SUPPORTS_MISLEADING_INDENTATION_FLAG "-Wno-misleading-indentation" CMAKE_CXX_FLAGS) 917 else() 918 # Prevent bugs that can happen with llvm's brace style. 919 add_flag_if_supported("-Wmisleading-indentation" MISLEADING_INDENTATION_FLAG) 920 endif() 921 922 # Enable -Wctad-maybe-unsupported to catch unintended use of CTAD. 923 add_flag_if_supported("-Wctad-maybe-unsupported" CTAD_MAYBE_UNSPPORTED_FLAG) 924endif (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)) 925 926if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND NOT LLVM_ENABLE_WARNINGS) 927 append("-w" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 928endif() 929 930macro(append_common_sanitizer_flags) 931 if (NOT MSVC OR CLANG_CL) 932 # Append -fno-omit-frame-pointer and turn on debug info to get better 933 # stack traces. 934 add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER) 935 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND 936 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO") 937 add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY) 938 endif() 939 # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large. 940 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND LLVM_OPTIMIZE_SANITIZED_BUILDS) 941 add_flag_if_supported("-O1" O1) 942 endif() 943 else() 944 # Always ask the linker to produce symbols with asan. 945 append("/Z7" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 946 append("/debug" CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 947 # Not compatible with /INCREMENTAL link. 948 foreach (flags_opt_to_scrub 949 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 950 string (REGEX REPLACE "(^| )/INCREMENTAL($| )" " /INCREMENTAL:NO " 951 "${flags_opt_to_scrub}" "${${flags_opt_to_scrub}}") 952 endforeach() 953 if (LLVM_HOST_TRIPLE MATCHES "i[2-6]86-.*") 954 # Keep frame pointers around. 955 append("/Oy-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 956 endif() 957 endif() 958endmacro() 959 960# Turn on sanitizers if necessary. 961if(LLVM_USE_SANITIZER) 962 if (LLVM_ON_UNIX) 963 if (LLVM_USE_SANITIZER STREQUAL "Address") 964 append_common_sanitizer_flags() 965 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 966 elseif (LLVM_USE_SANITIZER STREQUAL "HWAddress") 967 append_common_sanitizer_flags() 968 append("-fsanitize=hwaddress" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 969 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") 970 append_common_sanitizer_flags() 971 append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 972 if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") 973 append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 974 endif() 975 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined") 976 append_common_sanitizer_flags() 977 append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 978 elseif (LLVM_USE_SANITIZER STREQUAL "Thread") 979 append_common_sanitizer_flags() 980 append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 981 elseif (LLVM_USE_SANITIZER STREQUAL "DataFlow") 982 append("-fsanitize=dataflow" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 983 elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR 984 LLVM_USE_SANITIZER STREQUAL "Undefined;Address") 985 append_common_sanitizer_flags() 986 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 987 append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 988 elseif (LLVM_USE_SANITIZER STREQUAL "Leaks") 989 append_common_sanitizer_flags() 990 append("-fsanitize=leak" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 991 else() 992 message(FATAL_ERROR "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") 993 endif() 994 elseif(MINGW) 995 if (LLVM_USE_SANITIZER STREQUAL "Address") 996 append_common_sanitizer_flags() 997 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 998 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined") 999 append_common_sanitizer_flags() 1000 append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1001 elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR 1002 LLVM_USE_SANITIZER STREQUAL "Undefined;Address") 1003 append_common_sanitizer_flags() 1004 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1005 append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1006 else() 1007 message(FATAL_ERROR "This sanitizer not yet supported in a MinGW environment: ${LLVM_USE_SANITIZER}") 1008 endif() 1009 elseif(MSVC) 1010 if (NOT LLVM_USE_SANITIZER MATCHES "^(Address|Undefined|Address;Undefined|Undefined;Address)$") 1011 message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}") 1012 endif() 1013 append_common_sanitizer_flags() 1014 if (LINKER_IS_LLD_LINK) 1015 if (LLVM_HOST_TRIPLE MATCHES "i[2-6]86-.*") 1016 set(arch "i386") 1017 else() 1018 set(arch "x86_64") 1019 endif() 1020 # Prepare ASAN runtime if needed 1021 if (LLVM_USE_SANITIZER MATCHES ".*Address.*") 1022 # lld string tail merging interacts badly with ASAN on Windows, turn it off here 1023 # See https://github.com/llvm/llvm-project/issues/62078 1024 append("/opt:nolldtailmerge" CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1025 if (${CMAKE_MSVC_RUNTIME_LIBRARY} MATCHES "^(MultiThreaded|MultiThreadedDebug)$") 1026 append("/wholearchive:clang_rt.asan-${arch}.lib /wholearchive:clang_rt.asan_cxx-${arch}.lib" 1027 CMAKE_EXE_LINKER_FLAGS) 1028 append("/wholearchive:clang_rt.asan_dll_thunk-${arch}.lib" 1029 CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1030 else() 1031 append("clang_rt.asan_dynamic-${arch}.lib /wholearchive:clang_rt.asan_dynamic_runtime_thunk-${arch}.lib" 1032 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1033 endif() 1034 endif() 1035 endif() 1036 if (LLVM_USE_SANITIZER MATCHES ".*Address.*") 1037 if (NOT CLANG_CL) 1038 append("/fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1039 # Not compatible with /RTC flags. 1040 foreach (flags_opt_to_scrub 1041 CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE}) 1042 string (REGEX REPLACE "(^| )/RTC[1csu]*($| )" " " 1043 "${flags_opt_to_scrub}" "${${flags_opt_to_scrub}}") 1044 endforeach() 1045 else() 1046 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1047 endif() 1048 endif() 1049 if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*") 1050 if (NOT CLANG_CL) 1051 message(FATAL_ERROR "This sanitizer is only supported by clang-cl: Undefined") 1052 endif() 1053 append(${LLVM_UBSAN_FLAGS} CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1054 endif() 1055 else() 1056 message(FATAL_ERROR "LLVM_USE_SANITIZER is not supported on this platform.") 1057 endif() 1058 if (LLVM_USE_SANITIZE_COVERAGE) 1059 append("-fsanitize=fuzzer-no-link" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1060 endif() 1061 if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*") 1062 set(IGNORELIST_FILE "${PROJECT_SOURCE_DIR}/utils/sanitizers/ubsan_ignorelist.txt") 1063 if (EXISTS "${IGNORELIST_FILE}") 1064 # Use this option name version since -fsanitize-ignorelist is only 1065 # accepted with clang 13.0 or newer. 1066 append("-fsanitize-blacklist=${IGNORELIST_FILE}" 1067 CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1068 endif() 1069 endif() 1070endif() 1071 1072# Turn on -gsplit-dwarf if requested in debug builds. 1073if (LLVM_USE_SPLIT_DWARF AND 1074 ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR 1075 (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))) 1076 # Limit to clang and gcc so far. Add compilers supporting this option. 1077 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR 1078 CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 1079 add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-gsplit-dwarf>) 1080 include(LLVMCheckLinkerFlag) 1081 llvm_check_linker_flag(CXX "-Wl,--gdb-index" LINKER_SUPPORTS_GDB_INDEX) 1082 append_if(LINKER_SUPPORTS_GDB_INDEX "-Wl,--gdb-index" 1083 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1084 endif() 1085endif() 1086 1087add_compile_definitions(__STDC_CONSTANT_MACROS) 1088add_compile_definitions(__STDC_FORMAT_MACROS) 1089add_compile_definitions(__STDC_LIMIT_MACROS) 1090 1091# clang and gcc don't default-print colored diagnostics when invoked from Ninja. 1092if (UNIX AND 1093 CMAKE_GENERATOR MATCHES "Ninja" AND 1094 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR 1095 (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND 1096 NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)))) 1097 append("-fdiagnostics-color" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1098endif() 1099 1100# lld doesn't print colored diagnostics when invoked from Ninja 1101if (UNIX AND CMAKE_GENERATOR MATCHES "Ninja") 1102 include(LLVMCheckLinkerFlag) 1103 llvm_check_linker_flag(CXX "-Wl,--color-diagnostics" LINKER_SUPPORTS_COLOR_DIAGNOSTICS) 1104 append_if(LINKER_SUPPORTS_COLOR_DIAGNOSTICS "-Wl,--color-diagnostics" 1105 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1106endif() 1107 1108# Add flags for add_dead_strip(). 1109# FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF? 1110# But MinSizeRel seems to add that automatically, so maybe disable these 1111# flags instead if LLVM_NO_DEAD_STRIP is set. 1112if(NOT CYGWIN AND NOT MSVC) 1113 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND 1114 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 1115 check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS) 1116 if (C_SUPPORTS_FNO_FUNCTION_SECTIONS) 1117 # Don't add -ffunction-sections if it can't be disabled with -fno-function-sections. 1118 # Doing so will break sanitizers. 1119 add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS) 1120 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL") 1121 append("-qfuncsect" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1122 endif() 1123 add_flag_if_supported("-fdata-sections" FDATA_SECTIONS) 1124 endif() 1125elseif(MSVC) 1126 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" ) 1127 append("/Gw" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1128 endif() 1129endif() 1130 1131if(MSVC) 1132 # Remove flags here, for exceptions and RTTI. 1133 # Each target property or source property should be responsible to control 1134 # them. 1135 # CL.EXE complains to override flags like "/GR /GR-". 1136 string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 1137 string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 1138endif() 1139 1140# Provide public options to globally control RTTI and EH 1141option(LLVM_ENABLE_EH "Enable Exception handling" OFF) 1142option(LLVM_ENABLE_RTTI "Enable run time type information" OFF) 1143if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI) 1144 message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON") 1145endif() 1146 1147option(LLVM_ENABLE_IR_PGO "Build LLVM and tools with IR PGO instrumentation (deprecated)" Off) 1148mark_as_advanced(LLVM_ENABLE_IR_PGO) 1149 1150set(LLVM_BUILD_INSTRUMENTED OFF CACHE STRING "Build LLVM and tools with PGO instrumentation. May be specified as IR or Frontend") 1151set(LLVM_VP_COUNTERS_PER_SITE "1.5" CACHE STRING "Value profile counters to use per site for IR PGO with Clang") 1152mark_as_advanced(LLVM_BUILD_INSTRUMENTED LLVM_VP_COUNTERS_PER_SITE) 1153string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" uppercase_LLVM_BUILD_INSTRUMENTED) 1154 1155if (LLVM_BUILD_INSTRUMENTED) 1156 if (LLVM_ENABLE_IR_PGO OR uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "IR") 1157 append("-fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\"" 1158 CMAKE_CXX_FLAGS 1159 CMAKE_C_FLAGS) 1160 if(NOT LINKER_IS_LLD_LINK) 1161 append("-fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\"" 1162 CMAKE_EXE_LINKER_FLAGS 1163 CMAKE_SHARED_LINKER_FLAGS) 1164 endif() 1165 # Set this to avoid running out of the value profile node section 1166 # under clang in dynamic linking mode. 1167 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND 1168 CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 11 AND 1169 LLVM_LINK_LLVM_DYLIB) 1170 append("-Xclang -mllvm -Xclang -vp-counters-per-site=${LLVM_VP_COUNTERS_PER_SITE}" 1171 CMAKE_CXX_FLAGS 1172 CMAKE_C_FLAGS) 1173 endif() 1174 elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR") 1175 append("-fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\"" 1176 CMAKE_CXX_FLAGS 1177 CMAKE_C_FLAGS) 1178 if(NOT LINKER_IS_LLD_LINK) 1179 append("-fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\"" 1180 CMAKE_EXE_LINKER_FLAGS 1181 CMAKE_SHARED_LINKER_FLAGS) 1182 endif() 1183 else() 1184 append("-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\"" 1185 CMAKE_CXX_FLAGS 1186 CMAKE_C_FLAGS) 1187 if(NOT LINKER_IS_LLD_LINK) 1188 append("-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\"" 1189 CMAKE_EXE_LINKER_FLAGS 1190 CMAKE_SHARED_LINKER_FLAGS) 1191 endif() 1192 endif() 1193endif() 1194 1195# When using clang-cl with an instrumentation-based tool, add clang's library 1196# resource directory to the library search path. Because cmake invokes the 1197# linker directly, it isn't sufficient to pass -fsanitize=* to the linker. 1198if (CLANG_CL AND (LLVM_BUILD_INSTRUMENTED OR LLVM_USE_SANITIZER)) 1199 execute_process( 1200 COMMAND ${CMAKE_CXX_COMPILER} /clang:-print-libgcc-file-name /clang:--rtlib=compiler-rt 1201 OUTPUT_VARIABLE clang_compiler_rt_file 1202 ERROR_VARIABLE clang_cl_stderr 1203 OUTPUT_STRIP_TRAILING_WHITESPACE 1204 ERROR_STRIP_TRAILING_WHITESPACE 1205 RESULT_VARIABLE clang_cl_exit_code) 1206 if (NOT "${clang_cl_exit_code}" STREQUAL "0") 1207 message(FATAL_ERROR 1208 "Unable to invoke clang-cl to find resource dir: ${clang_cl_stderr}") 1209 endif() 1210 file(TO_CMAKE_PATH "${clang_compiler_rt_file}" clang_compiler_rt_file) 1211 get_filename_component(clang_runtime_dir "${clang_compiler_rt_file}" DIRECTORY) 1212 append("/libpath:\"${clang_runtime_dir}\"" 1213 CMAKE_EXE_LINKER_FLAGS 1214 CMAKE_MODULE_LINKER_FLAGS 1215 CMAKE_SHARED_LINKER_FLAGS) 1216endif() 1217 1218if(LLVM_PROFDATA_FILE AND EXISTS ${LLVM_PROFDATA_FILE}) 1219 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) 1220 append("-fprofile-instr-use=\"${LLVM_PROFDATA_FILE}\"" 1221 CMAKE_CXX_FLAGS 1222 CMAKE_C_FLAGS) 1223 if(NOT LINKER_IS_LLD_LINK) 1224 append("-fprofile-instr-use=\"${LLVM_PROFDATA_FILE}\"" 1225 CMAKE_EXE_LINKER_FLAGS 1226 CMAKE_SHARED_LINKER_FLAGS) 1227 endif() 1228 else() 1229 message(FATAL_ERROR "LLVM_PROFDATA_FILE can only be specified when compiling with clang") 1230 endif() 1231endif() 1232 1233option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation" Off) 1234option(LLVM_INDIVIDUAL_TEST_COVERAGE "Emit individual coverage file for each test case." OFF) 1235mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE) 1236append_if(LLVM_BUILD_INSTRUMENTED_COVERAGE "-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\" -fcoverage-mapping" 1237 CMAKE_CXX_FLAGS 1238 CMAKE_C_FLAGS 1239 CMAKE_EXE_LINKER_FLAGS 1240 CMAKE_SHARED_LINKER_FLAGS) 1241 1242if (LLVM_BUILD_INSTRUMENTED AND LLVM_BUILD_INSTRUMENTED_COVERAGE) 1243 message(FATAL_ERROR "LLVM_BUILD_INSTRUMENTED and LLVM_BUILD_INSTRUMENTED_COVERAGE cannot both be specified") 1244endif() 1245 1246set(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.") 1247 1248if(LLVM_ENABLE_LTO AND LLVM_ON_WIN32 AND NOT LINKER_IS_LLD_LINK AND NOT MINGW) 1249 message(FATAL_ERROR "When compiling for Windows, LLVM_ENABLE_LTO requires using lld as the linker (point CMAKE_LINKER at lld-link.exe)") 1250endif() 1251if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") 1252 append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) 1253 if(NOT LINKER_IS_LLD_LINK) 1254 append("-flto=thin" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1255 endif() 1256 # If the linker supports it, enable the lto cache. This improves initial build 1257 # time a little since we re-link a lot of the same objects, and significantly 1258 # improves incremental build time. 1259 # FIXME: We should move all this logic into the clang driver. 1260 if(APPLE) 1261 append("-Wl,-cache_path_lto,${LLVM_THINLTO_CACHE_PATH}" 1262 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1263 elseif((UNIX OR MINGW) AND LLVM_USE_LINKER STREQUAL "lld") 1264 append("-Wl,--thinlto-cache-dir=${LLVM_THINLTO_CACHE_PATH}" 1265 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1266 elseif(LLVM_USE_LINKER STREQUAL "gold") 1267 append("-Wl,--plugin-opt,cache-dir=${LLVM_THINLTO_CACHE_PATH}" 1268 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1269 elseif(LINKER_IS_LLD_LINK) 1270 append("/lldltocache:${LLVM_THINLTO_CACHE_PATH}" 1271 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1272 endif() 1273elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL") 1274 append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) 1275 if(NOT LINKER_IS_LLD_LINK) 1276 append("-flto=full" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1277 endif() 1278elseif(LLVM_ENABLE_LTO) 1279 append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) 1280 if(NOT LINKER_IS_LLD_LINK) 1281 append("-flto" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) 1282 endif() 1283endif() 1284 1285if(LLVM_ENABLE_FATLTO AND UNIX AND NOT APPLE) 1286 append("-ffat-lto-objects" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1287 if(NOT LINKER_IS_LLD_LINK) 1288 append("-ffat-lto-objects" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS) 1289 endif() 1290endif() 1291 1292# Set an AIX default for LLVM_EXPORT_SYMBOLS_FOR_PLUGINS based on whether we are 1293# doing dynamic linking (see below). 1294set(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default OFF) 1295if (NOT (BUILD_SHARED_LIBS OR LLVM_LINK_LLVM_DYLIB)) 1296 set(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default ON) 1297endif() 1298 1299# This option makes utils/extract_symbols.py be used to determine the list of 1300# symbols to export from LLVM tools. This is necessary when on AIX or when using 1301# MSVC if you want to allow plugins. On AIX we don't show this option, and we 1302# enable it by default except when the LLVM libraries are set up for dynamic 1303# linking (due to incompatibility). With MSVC, note that the plugin has to 1304# explicitly link against (exactly one) tool so we can't unilaterally turn on 1305# LLVM_ENABLE_PLUGINS when it's enabled. 1306CMAKE_DEPENDENT_OPTION(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS 1307 "Export symbols from LLVM tools so that plugins can import them" OFF 1308 "NOT ${CMAKE_SYSTEM_NAME} MATCHES AIX" ${LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default}) 1309if(BUILD_SHARED_LIBS AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 1310 message(FATAL_ERROR "BUILD_SHARED_LIBS not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS") 1311endif() 1312if(LLVM_LINK_LLVM_DYLIB AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 1313 message(FATAL_ERROR "LLVM_LINK_LLVM_DYLIB not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS") 1314endif() 1315 1316# By default we should enable LLVM_ENABLE_IDE only for multi-configuration 1317# generators. This option disables optional build system features that make IDEs 1318# less usable. 1319set(LLVM_ENABLE_IDE_default OFF) 1320if (CMAKE_CONFIGURATION_TYPES) 1321 set(LLVM_ENABLE_IDE_default ON) 1322endif() 1323option(LLVM_ENABLE_IDE 1324 "Disable optional build system features that cause problems for IDE generators" 1325 ${LLVM_ENABLE_IDE_default}) 1326if (CMAKE_CONFIGURATION_TYPES AND NOT LLVM_ENABLE_IDE) 1327 message(WARNING "Disabling LLVM_ENABLE_IDE on multi-configuration generators is not recommended.") 1328endif() 1329 1330function(get_compile_definitions) 1331 get_directory_property(top_dir_definitions DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS) 1332 foreach(definition ${top_dir_definitions}) 1333 if(DEFINED result) 1334 string(APPEND result " -D${definition}") 1335 else() 1336 set(result "-D${definition}") 1337 endif() 1338 endforeach() 1339 set(LLVM_DEFINITIONS "${result}" PARENT_SCOPE) 1340endfunction() 1341get_compile_definitions() 1342 1343option(LLVM_FORCE_ENABLE_STATS "Enable statistics collection for builds that wouldn't normally enable it" OFF) 1344 1345check_symbol_exists(os_signpost_interval_begin "os/signpost.h" macos_signposts_available) 1346if(macos_signposts_available) 1347 check_cxx_source_compiles( 1348 "#include <os/signpost.h> 1349 int main() { os_signpost_interval_begin(nullptr, 0, \"\", \"\"); return 0; }" 1350 macos_signposts_usable) 1351 if(macos_signposts_usable) 1352 set(LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS "WITH_ASSERTS" CACHE STRING 1353 "Enable support for Xcode signposts. Can be WITH_ASSERTS, FORCE_ON, FORCE_OFF") 1354 string(TOUPPER "${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}" 1355 uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS) 1356 if( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "WITH_ASSERTS" ) 1357 if( LLVM_ENABLE_ASSERTIONS ) 1358 set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 ) 1359 endif() 1360 elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_ON" ) 1361 set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 ) 1362 elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_OFF" ) 1363 # We don't need to do anything special to turn off signposts. 1364 elseif( NOT DEFINED LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS ) 1365 # Treat LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS like "FORCE_OFF" when it has not been 1366 # defined. 1367 else() 1368 message(FATAL_ERROR "Unknown value for LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS:" 1369 " \"${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}\"!") 1370 endif() 1371 endif() 1372endif() 1373 1374set(LLVM_SOURCE_PREFIX "" CACHE STRING "Use prefix for sources") 1375 1376option(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO "Use relative paths in debug info" OFF) 1377 1378if(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO) 1379 check_c_compiler_flag("-fdebug-prefix-map=foo=bar" SUPPORTS_FDEBUG_PREFIX_MAP) 1380 if(LLVM_ENABLE_PROJECTS_USED) 1381 get_filename_component(source_root "${LLVM_MAIN_SRC_DIR}/.." ABSOLUTE) 1382 else() 1383 set(source_root "${LLVM_MAIN_SRC_DIR}") 1384 endif() 1385 file(RELATIVE_PATH relative_root "${CMAKE_BINARY_DIR}" "${source_root}") 1386 append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1387 append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1388 add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES) 1389endif() 1390 1391option(LLVM_USE_RELATIVE_PATHS_IN_FILES "Use relative paths in sources and debug info" OFF) 1392 1393if(LLVM_USE_RELATIVE_PATHS_IN_FILES) 1394 check_c_compiler_flag("-ffile-prefix-map=foo=bar" SUPPORTS_FFILE_PREFIX_MAP) 1395 if(LLVM_ENABLE_PROJECTS_USED) 1396 get_filename_component(source_root "${LLVM_MAIN_SRC_DIR}/.." ABSOLUTE) 1397 else() 1398 set(source_root "${LLVM_MAIN_SRC_DIR}") 1399 endif() 1400 file(RELATIVE_PATH relative_root "${CMAKE_BINARY_DIR}" "${source_root}") 1401 append_if(SUPPORTS_FFILE_PREFIX_MAP "-ffile-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1402 append_if(SUPPORTS_FFILE_PREFIX_MAP "-ffile-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 1403 add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES) 1404endif() 1405 1406set(LLVM_THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../third-party CACHE STRING 1407 "Directory containing third party software used by LLVM (e.g. googletest)") 1408 1409set(LLVM_UNITTEST_LINK_FLAGS "" CACHE STRING 1410 "Additional linker flags for unit tests") 1411 1412if(LLVM_ENABLE_LLVM_LIBC) 1413 check_library_exists(llvmlibc printf "" HAVE_LLVM_LIBC) 1414 if(NOT HAVE_LLVM_LIBC) 1415 message(WARNING "Unable to link against LLVM libc. LLVM will be built without linking against the LLVM libc overlay.") 1416 endif() 1417endif() 1418