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