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