1 # This CMake module is responsible for interpreting the user defined LLVM_
2 # options and executing the appropriate CMake commands to realize the users'
5 # This is commonly needed so make sure it's defined before we include anything
7 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
9 include(CheckCompilerVersion)
10 include(HandleLLVMStdlib)
11 include(CheckCCompilerFlag)
12 include(CheckCXXCompilerFlag)
13 include(CheckSymbolExists)
15 if(CMAKE_LINKER MATCHES "lld-link" OR (WIN32 AND LLVM_USE_LINKER STREQUAL "lld") OR LLVM_ENABLE_LLD)
16 set(LINKER_IS_LLD_LINK TRUE)
18 set(LINKER_IS_LLD_LINK FALSE)
21 set(LLVM_CXX_STD_default "c++11")
22 # Preserve behaviour of legacy cache variables
23 if (LLVM_ENABLE_CXX1Y)
24 set(LLVM_CXX_STD_default "c++1y")
25 elseif (LLVM_ENABLE_CXX1Z)
26 set(LLVM_CXX_STD_default "c++1z")
28 set(LLVM_CXX_STD ${LLVM_CXX_STD_default}
29 CACHE STRING "C++ standard to use for compilation.")
31 set(LLVM_ENABLE_LTO OFF CACHE STRING "Build LLVM with LTO. May be specified as Thin or Full to use a particular kind of LTO")
32 string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO)
34 # Ninja Job Pool support
35 # The following only works with the Ninja generator in CMake >= 3.0.
36 set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING
37 "Define the maximum number of concurrent compilation jobs (Ninja only).")
38 if(LLVM_PARALLEL_COMPILE_JOBS)
39 if(NOT CMAKE_MAKE_PROGRAM MATCHES "ninja")
40 message(WARNING "Job pooling is only available with Ninja generators.")
42 set_property(GLOBAL APPEND PROPERTY JOB_POOLS compile_job_pool=${LLVM_PARALLEL_COMPILE_JOBS})
43 set(CMAKE_JOB_POOL_COMPILE compile_job_pool)
47 set(LLVM_PARALLEL_LINK_JOBS "" CACHE STRING
48 "Define the maximum number of concurrent link jobs (Ninja only).")
49 if(CMAKE_MAKE_PROGRAM MATCHES "ninja")
50 if(NOT LLVM_PARALLEL_LINK_JOBS AND uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
51 message(STATUS "ThinLTO provides its own parallel linking - limiting parallel link jobs to 2.")
52 set(LLVM_PARALLEL_LINK_JOBS "2")
54 if(LLVM_PARALLEL_LINK_JOBS)
55 set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=${LLVM_PARALLEL_LINK_JOBS})
56 set(CMAKE_JOB_POOL_LINK link_job_pool)
58 elseif(LLVM_PARALLEL_LINK_JOBS)
59 message(WARNING "Job pooling is only available with Ninja generators.")
62 if( LLVM_ENABLE_ASSERTIONS )
63 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
65 add_definitions( -D_DEBUG )
67 # On non-Debug builds cmake automatically defines NDEBUG, so we
68 # explicitly undefine it:
69 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
70 add_definitions( -UNDEBUG )
71 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
72 foreach (flags_var_to_scrub
73 CMAKE_CXX_FLAGS_RELEASE
74 CMAKE_CXX_FLAGS_RELWITHDEBINFO
75 CMAKE_CXX_FLAGS_MINSIZEREL
77 CMAKE_C_FLAGS_RELWITHDEBINFO
78 CMAKE_C_FLAGS_MINSIZEREL)
79 string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
80 "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
85 if(LLVM_ENABLE_EXPENSIVE_CHECKS)
86 add_definitions(-DEXPENSIVE_CHECKS)
87 add_definitions(-D_GLIBCXX_DEBUG)
90 string(TOUPPER "${LLVM_ABI_BREAKING_CHECKS}" uppercase_LLVM_ABI_BREAKING_CHECKS)
92 if( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "WITH_ASSERTS" )
93 if( LLVM_ENABLE_ASSERTIONS )
94 set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
96 elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_ON" )
97 set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
98 elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_OFF" )
99 # We don't need to do anything special to turn off ABI breaking checks.
100 elseif( NOT DEFINED LLVM_ABI_BREAKING_CHECKS )
101 # Treat LLVM_ABI_BREAKING_CHECKS like "FORCE_OFF" when it has not been
104 message(FATAL_ERROR "Unknown value for LLVM_ABI_BREAKING_CHECKS: \"${LLVM_ABI_BREAKING_CHECKS}\"!")
107 if( LLVM_REVERSE_ITERATION )
108 set( LLVM_ENABLE_REVERSE_ITERATION 1 )
112 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
124 if(APPLE OR ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
125 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
127 set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
129 else(FUCHSIA OR UNIX)
130 MESSAGE(SEND_ERROR "Unable to determine platform")
131 endif(FUCHSIA OR UNIX)
134 set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX})
135 set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
137 # We use *.dylib rather than *.so on darwin.
138 set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
141 if(LLVM_ENABLE_LLD AND LLVM_ENABLE_LTO)
142 message(FATAL_ERROR "lld does not support LTO on Darwin")
144 # Darwin-specific linker flags for loadable modules.
145 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
148 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
149 if(NOT LLVM_BUILD_32_BITS)
150 if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
151 append("-q64" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
153 append("-maix64" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
155 set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> -X64 qc <TARGET> <LINK_FLAGS> <OBJECTS>")
156 set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> -X64 q <TARGET> <LINK_FLAGS> <OBJECTS>")
157 set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -X64 <TARGET>")
158 set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -X64 <TARGET>")
160 # -fPIC does not enable the large code model for GCC on AIX but does for XL.
161 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
162 append("-mcmodel=large" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
163 elseif(CMAKE_CXX_COMPILER_ID MATCHES "XL")
164 # XL generates a small number of relocations not of the large model, -bbigtoc is needed.
165 append("-Wl,-bbigtoc"
166 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
170 # Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise a DSO
171 # build might work on ELF but fail on MachO/COFF.
172 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|FreeBSD|OpenBSD|DragonFly|AIX|SunOS" OR
174 NOT LLVM_USE_SANITIZER)
175 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs")
178 # Pass -Wl,-z,nodelete. This makes sure our shared libraries are not unloaded
179 # by dlclose(). We need that since the CLI API relies on cross-references
180 # between global objects which became horribly broken when one of the libraries
182 if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
183 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,nodelete")
184 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,nodelete")
188 function(append value)
189 foreach(variable ${ARGN})
190 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
194 function(append_if condition value)
196 foreach(variable ${ARGN})
197 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
202 macro(add_flag_if_supported flag name)
203 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
204 append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS)
205 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
206 append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS)
209 function(add_flag_or_print_warning flag name)
210 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
211 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
212 if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name})
213 message(STATUS "Building with ${flag}")
214 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
215 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
216 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${flag}" PARENT_SCOPE)
218 message(WARNING "${flag} is not supported.")
222 if( LLVM_ENABLE_LLD )
223 if ( LLVM_USE_LINKER )
224 message(FATAL_ERROR "LLVM_ENABLE_LLD and LLVM_USE_LINKER can't be set at the same time")
226 set(LLVM_USE_LINKER "lld")
229 if( LLVM_USE_LINKER )
230 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
231 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fuse-ld=${LLVM_USE_LINKER}")
232 check_cxx_source_compiles("int main() { return 0; }" CXX_SUPPORTS_CUSTOM_LINKER)
233 if ( NOT CXX_SUPPORTS_CUSTOM_LINKER )
234 message(FATAL_ERROR "Host compiler does not support '-fuse-ld=${LLVM_USE_LINKER}'")
236 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
237 append("-fuse-ld=${LLVM_USE_LINKER}"
238 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
241 if( LLVM_ENABLE_PIC )
243 # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
244 # know how to disable this, so just force ENABLE_PIC off for now.
245 message(WARNING "-fPIC not supported with Xcode.")
246 elseif( WIN32 OR CYGWIN)
247 # On Windows all code is PIC. MinGW warns if -fPIC is used.
249 add_flag_or_print_warning("-fPIC" FPIC)
251 # GCC for MIPS can miscompile LLVM due to PR37701.
252 if(CMAKE_COMPILER_IS_GNUCXX AND LLVM_NATIVE_ARCH STREQUAL "Mips" AND
253 NOT Uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
254 add_flag_or_print_warning("-fno-shrink-wrap" FNO_SHRINK_WRAP)
258 if(NOT WIN32 AND NOT CYGWIN AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
259 # MinGW warns if -fvisibility-inlines-hidden is used.
260 # GCC on AIX warns if -fvisibility-inlines-hidden is used.
261 check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
262 append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS)
265 if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND MINGW)
266 add_definitions( -D_FILE_OFFSET_BITS=64 )
269 if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
270 # TODO: support other platforms and toolchains.
271 if( LLVM_BUILD_32_BITS )
272 message(STATUS "Building 32 bits executables and libraries.")
273 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
274 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
275 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
276 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
277 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
279 # FIXME: CMAKE_SIZEOF_VOID_P is still 8
280 add_definitions(-D_LARGEFILE_SOURCE)
281 add_definitions(-D_FILE_OFFSET_BITS=64)
282 endif( LLVM_BUILD_32_BITS )
283 endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
285 # If building on a GNU specific 32-bit system, make sure off_t is 64 bits
286 # so that off_t can stored offset > 2GB.
287 # Android until version N (API 24) doesn't support it.
288 if (ANDROID AND (ANDROID_NATIVE_API_LEVEL LESS 24))
289 set(LLVM_FORCE_SMALLFILE_FOR_ANDROID TRUE)
291 if( CMAKE_SIZEOF_VOID_P EQUAL 4 AND NOT LLVM_FORCE_SMALLFILE_FOR_ANDROID)
292 # FIXME: It isn't handled in LLVM_BUILD_32_BITS.
293 add_definitions( -D_LARGEFILE_SOURCE )
294 add_definitions( -D_FILE_OFFSET_BITS=64 )
298 # For Xcode enable several build settings that correspond to
299 # many warnings that are on by default in Clang but are
300 # not enabled for historical reasons. For versions of Xcode
301 # that do not support these options they will simply
303 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_RETURN_TYPE "YES")
304 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_MISSING_NEWLINE "YES")
305 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VALUE "YES")
306 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VARIABLE "YES")
307 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_SIGN_COMPARE "YES")
308 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_FUNCTION "YES")
309 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED "YES")
310 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS "YES")
311 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNINITIALIZED_AUTOS "YES")
312 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_BOOL_CONVERSION "YES")
313 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_EMPTY_BODY "YES")
314 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_ENUM_CONVERSION "YES")
315 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_INT_CONVERSION "YES")
316 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_CONSTANT_CONVERSION "YES")
317 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_NON_VIRTUAL_DESTRUCTOR "YES")
320 # On Win32 using MS tools, provide an option to set the number of parallel jobs
323 set(LLVM_COMPILER_JOBS "0" CACHE STRING
324 "Number of parallel compiler jobs. 0 means use all processors. Default is 0.")
325 if( NOT LLVM_COMPILER_JOBS STREQUAL "1" )
326 if( LLVM_COMPILER_JOBS STREQUAL "0" )
327 add_definitions( /MP )
329 message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS})
330 add_definitions( /MP${LLVM_COMPILER_JOBS} )
333 message(STATUS "Parallel compilation disabled")
337 # set stack reserved size to ~10MB
339 # CMake previously automatically set this value for MSVC builds, but the
340 # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default
341 # value (1 MB) which is not enough for us in tasks such as parsing recursive
342 # C++ templates in Clang.
343 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000")
344 elseif(MINGW) # FIXME: Also cygwin?
345 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216")
347 # Pass -mbig-obj to mingw gas on Win64. COFF has a 2**16 section limit, and
348 # on Win64, every COMDAT function creates at least 3 sections: .text, .pdata,
350 if (CMAKE_SIZEOF_VOID_P EQUAL 8)
351 append("-Wa,-mbig-obj" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
356 if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.0 )
357 # For MSVC 2013, disable iterator null pointer checking in debug mode,
358 # especially so std::equal(nullptr, nullptr, nullptr) will not assert.
359 add_definitions("-D_DEBUG_POINTER_IMPL=")
362 include(ChooseMSVCCRT)
364 # Add definitions that make MSVC much less annoying.
366 # For some reason MS wants to deprecate a bunch of standard functions...
367 -D_CRT_SECURE_NO_DEPRECATE
368 -D_CRT_SECURE_NO_WARNINGS
369 -D_CRT_NONSTDC_NO_DEPRECATE
370 -D_CRT_NONSTDC_NO_WARNINGS
371 -D_SCL_SECURE_NO_DEPRECATE
372 -D_SCL_SECURE_NO_WARNINGS
375 # Tell MSVC to use the Unicode version of the Win32 APIs instead of ANSI.
381 if (LLVM_ENABLE_WERROR)
382 append("/WX" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
383 endif (LLVM_ENABLE_WERROR)
385 append("/Zc:inline" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
387 # Allow users to request PDBs in release mode. CMake offeres the
388 # RelWithDebInfo configuration, but it uses different optimization settings
389 # (/Ob1 vs /Ob2 or -O2 vs -O3). LLVM provides this flag so that users can get
390 # PDBs without changing codegen.
391 option(LLVM_ENABLE_PDB OFF)
392 if (LLVM_ENABLE_PDB AND uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
393 append("/Zi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
394 # /DEBUG disables linker GC and ICF, but we want those in Release mode.
395 append("/DEBUG /OPT:REF /OPT:ICF"
396 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
397 CMAKE_SHARED_LINKER_FLAGS)
400 # /Zc:strictStrings is incompatible with VS12's (Visual Studio 2013's)
401 # debug mode headers. Instead of only enabling them in VS2013's debug mode,
402 # we'll just enable them for Visual Studio 2015 (VS 14, MSVC_VERSION 1900)
404 if (NOT (MSVC_VERSION LESS 1900))
405 # Disable string literal const->non-const type conversion.
406 # "When specified, the compiler requires strict const-qualification
407 # conformance for pointers initialized by using string literals."
408 append("/Zc:strictStrings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
409 endif(NOT (MSVC_VERSION LESS 1900))
411 # "Generate Intrinsic Functions".
412 append("/Oi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
414 # "Enforce type conversion rules".
415 append("/Zc:rvalueCast" CMAKE_CXX_FLAGS)
417 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT LLVM_ENABLE_LTO)
418 # clang-cl and cl by default produce non-deterministic binaries because
419 # link.exe /incremental requires a timestamp in the .obj file. clang-cl
420 # has the flag /Brepro to force deterministic binaries. We want to pass that
421 # whenever you're building with clang unless you're passing /incremental
422 # or using LTO (/Brepro with LTO would result in a warning about the flag
423 # being unused, because we're not generating object files).
424 # This checks CMAKE_CXX_COMPILER_ID in addition to check_cxx_compiler_flag()
425 # because cl.exe does not emit an error on flags it doesn't understand,
426 # letting check_cxx_compiler_flag() claim it understands all flags.
427 check_cxx_compiler_flag("/Brepro" SUPPORTS_BREPRO)
429 # Check if /INCREMENTAL is passed to the linker and complain that it
430 # won't work with /Brepro.
431 string(TOUPPER "${CMAKE_EXE_LINKER_FLAGS}" upper_exe_flags)
432 string(TOUPPER "${CMAKE_MODULE_LINKER_FLAGS}" upper_module_flags)
433 string(TOUPPER "${CMAKE_SHARED_LINKER_FLAGS}" upper_shared_flags)
435 string(FIND "${upper_exe_flags} ${upper_module_flags} ${upper_shared_flags}"
436 "/INCREMENTAL" linker_flag_idx)
438 if (${linker_flag_idx} GREATER -1)
439 message(WARNING "/Brepro not compatible with /INCREMENTAL linking - builds will be non-deterministic")
441 append("/Brepro" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
446 elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
447 append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
448 append_if(LLVM_ENABLE_WERROR "-Wno-error" CMAKE_REQUIRED_FLAGS)
449 add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME)
450 add_flag_if_supported("-Werror=unguarded-availability-new" WERROR_UNGUARDED_AVAILABILITY_NEW)
451 check_cxx_compiler_flag("-std=${LLVM_CXX_STD}" CXX_SUPPORTS_CXX_STD)
452 if (CXX_SUPPORTS_CXX_STD)
454 # MinGW and Cygwin are a bit stricter and lack things like
455 # 'strdup', 'stricmp', etc in c++11 mode.
456 string(REPLACE "c++" "gnu++" gnu_LLVM_CXX_STD "${LLVM_CXX_STD}")
457 append("-std=${gnu_LLVM_CXX_STD}" CMAKE_CXX_FLAGS)
459 append("-std=${LLVM_CXX_STD}" CMAKE_CXX_FLAGS)
462 message(FATAL_ERROR "The host compiler does not support '-std=${LLVM_CXX_STD}'.")
464 if (LLVM_ENABLE_MODULES)
465 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
466 set(module_flags "-fmodules -fmodules-cache-path=${PROJECT_BINARY_DIR}/module.cache")
467 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
468 # On Darwin -fmodules does not imply -fcxx-modules.
469 set(module_flags "${module_flags} -fcxx-modules")
471 if (LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY)
472 set(module_flags "${module_flags} -Xclang -fmodules-local-submodule-visibility")
474 if (LLVM_ENABLE_MODULE_DEBUGGING AND
475 ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR
476 (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")))
477 set(module_flags "${module_flags} -gmodules")
479 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${module_flags}")
481 # Check that we can build code with modules enabled, and that repeatedly
482 # including <cassert> still manages to respect NDEBUG properly.
483 CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG
487 int main() { assert(this code is not compiled); }"
488 CXX_SUPPORTS_MODULES)
489 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
490 if (CXX_SUPPORTS_MODULES)
491 append("${module_flags}" CMAKE_CXX_FLAGS)
493 message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler")
495 endif(LLVM_ENABLE_MODULES)
500 set(msvc_warning_flags
502 -wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline)
503 -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
504 -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
505 -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
506 -wd4258 # Suppress ''var' : definition from the for loop is ignored; the definition from the enclosing scope is used'
507 -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
508 -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
509 -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
510 -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
511 -wd4456 # Suppress 'declaration of 'var' hides local variable'
512 -wd4457 # Suppress 'declaration of 'var' hides function parameter'
513 -wd4458 # Suppress 'declaration of 'var' hides class member'
514 -wd4459 # Suppress 'declaration of 'var' hides global declaration'
515 -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
516 -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
517 -wd4722 # Suppress 'function' : destructor never returns, potential memory leak
518 -wd4100 # Suppress 'unreferenced formal parameter'
519 -wd4127 # Suppress 'conditional expression is constant'
520 -wd4512 # Suppress 'assignment operator could not be generated'
521 -wd4505 # Suppress 'unreferenced local function has been removed'
522 -wd4610 # Suppress '<class> can never be instantiated'
523 -wd4510 # Suppress 'default constructor could not be generated'
524 -wd4702 # Suppress 'unreachable code'
525 -wd4245 # Suppress 'signed/unsigned mismatch'
526 -wd4706 # Suppress 'assignment within conditional expression'
527 -wd4310 # Suppress 'cast truncates constant value'
528 -wd4701 # Suppress 'potentially uninitialized local variable'
529 -wd4703 # Suppress 'potentially uninitialized local pointer variable'
530 -wd4389 # Suppress 'signed/unsigned mismatch'
531 -wd4611 # Suppress 'interaction between '_setjmp' and C++ object destruction is non-portable'
532 -wd4805 # Suppress 'unsafe mix of type <type> and type <type> in operation'
533 -wd4204 # Suppress 'nonstandard extension used : non-constant aggregate initializer'
534 -wd4577 # Suppress 'noexcept used with no exception handling mode specified; termination on exception is not guaranteed'
535 -wd4091 # Suppress 'typedef: ignored on left of '' when no variable is declared'
536 # C4592 is disabled because of false positives in Visual Studio 2015
537 # Update 1. Re-evaluate the usefulness of this diagnostic with Update 2.
538 -wd4592 # Suppress ''var': symbol will be dynamically initialized (implementation limitation)
539 -wd4319 # Suppress ''operator' : zero extending 'type' to 'type' of greater size'
540 # C4709 is disabled because of a bug with Visual Studio 2017 as of
541 # v15.8.8. Re-evaluate the usefulness of this diagnostic when the bug
543 -wd4709 # Suppress comma operator within array index expression
545 # Ideally, we'd like this warning to be enabled, but MSVC 2013 doesn't
546 # support the 'aligned' attribute in the way that clang sources requires (for
547 # any code that uses the LLVM_ALIGNAS macro), so this is must be disabled to
548 # avoid unwanted alignment warnings.
549 # When we switch to requiring a version of MSVC that supports the 'alignas'
550 # specifier (MSVC 2015?) this warning can be re-enabled.
551 -wd4324 # Suppress 'structure was padded due to __declspec(align())'
554 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
556 # Promoted warnings to errors.
557 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
562 if (LLVM_ENABLE_WARNINGS)
563 # Put /W4 in front of all the -we flags. cl.exe doesn't care, but for
564 # clang-cl having /W4 after the -we flags will re-enable the warnings
566 set(msvc_warning_flags "/W4 ${msvc_warning_flags}")
567 # CMake appends /W3 by default, and having /W3 followed by /W4 will result in
568 # cl : Command line warning D9025 : overriding '/W3' with '/W4'. Since this is
569 # a command line warning and not a compiler warning, it cannot be suppressed except
570 # by fixing the command line.
571 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
572 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
574 if (LLVM_ENABLE_PEDANTIC)
575 # No MSVC equivalent available
576 endif (LLVM_ENABLE_PEDANTIC)
577 endif (LLVM_ENABLE_WARNINGS)
579 foreach(flag ${msvc_warning_flags})
580 append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
584 if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
586 # Don't add -Wall for clang-cl, because it maps -Wall to -Weverything for
587 # MSVC compatibility. /W4 is added above instead.
589 append("-Wall" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
592 append("-Wextra -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
593 append("-Wcast-qual" CMAKE_CXX_FLAGS)
595 # Turn off missing field initializer warnings for gcc to avoid noise from
596 # false positives with empty {}. Turn them on otherwise (they're off by
597 # default for clang).
598 check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
599 if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
600 if (CMAKE_COMPILER_IS_GNUCXX)
601 append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
603 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
607 if (LLVM_ENABLE_PEDANTIC AND LLVM_COMPILER_IS_GCC_COMPATIBLE)
608 append("-pedantic" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
609 append("-Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
612 add_flag_if_supported("-Wimplicit-fallthrough" IMPLICIT_FALLTHROUGH_FLAG)
613 add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG)
614 append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
615 append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
617 # Disable -Wclass-memaccess, a C++-only warning from GCC 8 that fires on
618 # LLVM's ADT classes.
619 check_cxx_compiler_flag("-Wclass-memaccess" CXX_SUPPORTS_CLASS_MEMACCESS_FLAG)
620 append_if(CXX_SUPPORTS_CLASS_MEMACCESS_FLAG "-Wno-class-memaccess" CMAKE_CXX_FLAGS)
622 # The LLVM libraries have no stable C++ API, so -Wnoexcept-type is not useful.
623 check_cxx_compiler_flag("-Wnoexcept-type" CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG)
624 append_if(CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG "-Wno-noexcept-type" CMAKE_CXX_FLAGS)
626 # Check if -Wnon-virtual-dtor warns even though the class is marked final.
627 # If it does, don't add it. So it won't be added on clang 3.4 and older.
628 # This also catches cases when -Wnon-virtual-dtor isn't supported by
629 # the compiler at all. This flag is not activated for gcc since it will
630 # incorrectly identify a protected non-virtual base when there is a friend
631 # declaration. Don't activate this in general on Windows as this warning has
632 # too many false positives on COM-style classes, which are destroyed with
633 # Release() (PR32286).
634 if (NOT CMAKE_COMPILER_IS_GNUCXX AND NOT WIN32)
635 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
636 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11 -Werror=non-virtual-dtor")
637 CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();protected: ~base();};
638 class derived final : public base { public: ~derived();};
639 int main() { return 0; }"
640 CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR)
641 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
642 append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR
643 "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
646 # Enable -Wdelete-non-virtual-dtor if available.
647 add_flag_if_supported("-Wdelete-non-virtual-dtor" DELETE_NON_VIRTUAL_DTOR_FLAG)
649 # Check if -Wcomment is OK with an // comment ending with '\' if the next
650 # line is also a // comment.
651 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
652 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment")
653 CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main() {return 0;}"
654 C_WCOMMENT_ALLOWS_LINE_WRAP)
655 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
656 if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP)
657 append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
660 # Enable -Wstring-conversion to catch misuse of string literals.
661 add_flag_if_supported("-Wstring-conversion" STRING_CONVERSION_FLAG)
662 endif (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
664 if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND NOT LLVM_ENABLE_WARNINGS)
665 append("-w" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
668 macro(append_common_sanitizer_flags)
670 # Append -fno-omit-frame-pointer and turn on debug info to get better
672 add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER)
673 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
674 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
675 add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY)
677 # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
678 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND LLVM_OPTIMIZE_SANITIZED_BUILDS)
679 add_flag_if_supported("-O1" O1)
682 # Keep frame pointers around.
683 append("/Oy-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
684 # Always ask the linker to produce symbols with asan.
685 append("/Z7" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
686 append("-debug" CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
690 # Turn on sanitizers if necessary.
691 if(LLVM_USE_SANITIZER)
693 if (LLVM_USE_SANITIZER STREQUAL "Address")
694 append_common_sanitizer_flags()
695 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
696 elseif (LLVM_USE_SANITIZER STREQUAL "HWAddress")
697 append_common_sanitizer_flags()
698 append("-fsanitize=hwaddress" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
699 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
700 append_common_sanitizer_flags()
701 append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
702 if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
703 append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
705 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
706 append_common_sanitizer_flags()
707 append("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all"
708 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
709 elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
710 append_common_sanitizer_flags()
711 append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
712 elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
713 LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
714 append_common_sanitizer_flags()
715 append("-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all"
716 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
717 elseif (LLVM_USE_SANITIZER STREQUAL "Leaks")
718 append_common_sanitizer_flags()
719 append("-fsanitize=leak" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
721 message(FATAL_ERROR "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
724 if (LLVM_USE_SANITIZER STREQUAL "Address")
725 append_common_sanitizer_flags()
726 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
728 message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}")
731 message(FATAL_ERROR "LLVM_USE_SANITIZER is not supported on this platform.")
733 if (LLVM_USE_SANITIZER MATCHES "(Undefined;)?Address(;Undefined)?")
734 add_flag_if_supported("-fsanitize-address-use-after-scope"
735 FSANITIZE_USE_AFTER_SCOPE_FLAG)
737 if (LLVM_USE_SANITIZE_COVERAGE)
738 append("-fsanitize=fuzzer-no-link" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
740 if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*")
741 set(BLACKLIST_FILE "${CMAKE_SOURCE_DIR}/utils/sanitizers/ubsan_blacklist.txt")
742 if (EXISTS "${BLACKLIST_FILE}")
743 append("-fsanitize-blacklist=${BLACKLIST_FILE}"
744 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
749 # Turn on -gsplit-dwarf if requested
750 if(LLVM_USE_SPLIT_DWARF)
751 add_definitions("-gsplit-dwarf")
754 add_definitions( -D__STDC_CONSTANT_MACROS )
755 add_definitions( -D__STDC_FORMAT_MACROS )
756 add_definitions( -D__STDC_LIMIT_MACROS )
758 # clang and gcc don't default-print colored diagnostics when invoked from Ninja.
760 CMAKE_GENERATOR STREQUAL "Ninja" AND
761 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
762 (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
763 NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9))))
764 append("-fdiagnostics-color" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
767 # lld doesn't print colored diagnostics when invoked from Ninja
768 if (UNIX AND CMAKE_GENERATOR STREQUAL "Ninja")
769 include(CheckLinkerFlag)
770 check_linker_flag("-Wl,--color-diagnostics" LINKER_SUPPORTS_COLOR_DIAGNOSTICS)
771 append_if(LINKER_SUPPORTS_COLOR_DIAGNOSTICS "-Wl,--color-diagnostics"
772 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
775 # Add flags for add_dead_strip().
776 # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
777 # But MinSizeRel seems to add that automatically, so maybe disable these
778 # flags instead if LLVM_NO_DEAD_STRIP is set.
779 if(NOT CYGWIN AND NOT WIN32)
780 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND
781 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
782 check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS)
783 if (C_SUPPORTS_FNO_FUNCTION_SECTIONS)
784 # Don't add -ffunction-section if it can be disabled with -fno-function-sections.
785 # Doing so will break sanitizers.
786 add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS)
788 add_flag_if_supported("-fdata-sections" FDATA_SECTIONS)
793 # Remove flags here, for exceptions and RTTI.
794 # Each target property or source property should be responsible to control
796 # CL.EXE complains to override flags like "/GR /GR-".
797 string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
798 string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
801 # Provide public options to globally control RTTI and EH
802 option(LLVM_ENABLE_EH "Enable Exception handling" OFF)
803 option(LLVM_ENABLE_RTTI "Enable run time type information" OFF)
804 if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI)
805 message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON")
808 option(LLVM_USE_NEWPM "Build LLVM using the experimental new pass manager" Off)
809 mark_as_advanced(LLVM_USE_NEWPM)
811 append("-fexperimental-new-pass-manager"
814 CMAKE_EXE_LINKER_FLAGS
815 CMAKE_SHARED_LINKER_FLAGS)
818 option(LLVM_ENABLE_IR_PGO "Build LLVM and tools with IR PGO instrumentation (deprecated)" Off)
819 mark_as_advanced(LLVM_ENABLE_IR_PGO)
821 set(LLVM_BUILD_INSTRUMENTED OFF CACHE STRING "Build LLVM and tools with PGO instrumentation. May be specified as IR or Frontend")
822 mark_as_advanced(LLVM_BUILD_INSTRUMENTED)
823 string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" uppercase_LLVM_BUILD_INSTRUMENTED)
825 if (LLVM_BUILD_INSTRUMENTED)
826 if (LLVM_ENABLE_IR_PGO OR uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "IR")
827 append("-fprofile-generate='${LLVM_PROFILE_DATA_DIR}'"
830 CMAKE_EXE_LINKER_FLAGS
831 CMAKE_SHARED_LINKER_FLAGS)
832 elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR")
833 append("-fcs-profile-generate='${LLVM_CSPROFILE_DATA_DIR}'"
836 CMAKE_EXE_LINKER_FLAGS
837 CMAKE_SHARED_LINKER_FLAGS)
839 append("-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}'"
842 CMAKE_EXE_LINKER_FLAGS
843 CMAKE_SHARED_LINKER_FLAGS)
847 # Need to pass -fprofile-instr-use to linker for context-sensitive PGO
849 if(LLVM_PROFDATA_FILE AND EXISTS ${LLVM_PROFDATA_FILE})
850 append("-fprofile-instr-use='${LLVM_PROFDATA_FILE}'"
851 CMAKE_EXE_LINKER_FLAGS
852 CMAKE_SHARED_LINKER_FLAGS)
855 option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation" Off)
856 mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE)
857 append_if(LLVM_BUILD_INSTRUMENTED_COVERAGE "-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}' -fcoverage-mapping"
860 CMAKE_EXE_LINKER_FLAGS
861 CMAKE_SHARED_LINKER_FLAGS)
863 if (LLVM_BUILD_INSTRUMENTED AND LLVM_BUILD_INSTRUMENTED_COVERAGE)
864 message(FATAL_ERROR "LLVM_BUILD_INSTRUMENTED and LLVM_BUILD_INSTRUMENTED_COVERAGE cannot both be specified")
867 if(LLVM_ENABLE_LTO AND LLVM_ON_WIN32 AND NOT LINKER_IS_LLD_LINK)
868 message(FATAL_ERROR "When compiling for Windows, LLVM_ENABLE_LTO requires using lld as the linker (point CMAKE_LINKER at lld-link.exe)")
870 if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
871 append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
872 if(NOT LINKER_IS_LLD_LINK)
873 append("-flto=thin" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
875 # If the linker supports it, enable the lto cache. This improves initial build
876 # time a little since we re-link a lot of the same objects, and significantly
877 # improves incremental build time.
878 # FIXME: We should move all this logic into the clang driver.
880 append("-Wl,-cache_path_lto,${PROJECT_BINARY_DIR}/lto.cache"
881 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
882 elseif(UNIX AND LLVM_USE_LINKER STREQUAL "lld")
883 append("-Wl,--thinlto-cache-dir=${PROJECT_BINARY_DIR}/lto.cache"
884 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
885 elseif(LLVM_USE_LINKER STREQUAL "gold")
886 append("-Wl,--plugin-opt,cache-dir=${PROJECT_BINARY_DIR}/lto.cache"
887 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
889 elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL")
890 append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
891 if(NOT LINKER_IS_LLD_LINK)
892 append("-flto=full" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
894 elseif(LLVM_ENABLE_LTO)
895 append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
896 if(NOT LINKER_IS_LLD_LINK)
897 append("-flto" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
901 # This option makes utils/extract_symbols.py be used to determine the list of
902 # symbols to export from LLVM tools. This is necessary when using MSVC if you
903 # want to allow plugins, though note that the plugin has to explicitly link
904 # against (exactly one) tool so we can't unilaterally turn on
905 # LLVM_ENABLE_PLUGINS when it's enabled.
906 option(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS "Export symbols from LLVM tools so that plugins can import them" OFF)
907 if(BUILD_SHARED_LIBS AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
908 message(FATAL_ERROR "BUILD_SHARED_LIBS not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS")
910 if(LLVM_LINK_LLVM_DYLIB AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
911 message(FATAL_ERROR "LLVM_LINK_LLVM_DYLIB not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS")
914 # By default we should enable LLVM_ENABLE_IDE only for multi-configuration
915 # generators. This option disables optional build system features that make IDEs
917 set(LLVM_ENABLE_IDE_default OFF)
918 if (CMAKE_CONFIGURATION_TYPES)
919 set(LLVM_ENABLE_IDE_default ON)
921 option(LLVM_ENABLE_IDE
922 "Disable optional build system features that cause problems for IDE generators"
923 ${LLVM_ENABLE_IDE_default})
924 if (CMAKE_CONFIGURATION_TYPES AND NOT LLVM_ENABLE_IDE)
925 message(WARNING "Disabling LLVM_ENABLE_IDE on multi-configuration generators is not recommended.")
928 function(get_compile_definitions)
929 get_directory_property(top_dir_definitions DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS)
930 foreach(definition ${top_dir_definitions})
932 string(APPEND result " -D${definition}")
934 set(result "-D${definition}")
937 set(LLVM_DEFINITIONS "${result}" PARENT_SCOPE)
939 get_compile_definitions()
941 option(LLVM_FORCE_ENABLE_STATS "Enable statistics collection for builds that wouldn't normally enable it" OFF)
943 check_symbol_exists(os_signpost_interval_begin "os/signpost.h" macos_signposts_available)
944 if(macos_signposts_available)
945 check_cxx_source_compiles(
946 "#include <os/signpost.h>
947 int main() { os_signpost_interval_begin(nullptr, 0, \"\", \"\"); return 0; }"
948 macos_signposts_usable)
949 if(macos_signposts_usable)
950 set(LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS "WITH_ASSERTS" CACHE STRING
951 "Enable support for Xcode signposts. Can be WITH_ASSERTS, FORCE_ON, FORCE_OFF")
952 string(TOUPPER "${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}"
953 uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS)
954 if( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "WITH_ASSERTS" )
955 if( LLVM_ENABLE_ASSERTIONS )
956 set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 )
958 elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_ON" )
959 set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 )
960 elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_OFF" )
961 # We don't need to do anything special to turn off signposts.
962 elseif( NOT DEFINED LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS )
963 # Treat LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS like "FORCE_OFF" when it has not been
966 message(FATAL_ERROR "Unknown value for LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS:"
967 " \"${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}\"!")
972 option(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO "Use relative paths in debug info" OFF)
973 set(LLVM_SOURCE_PREFIX "" CACHE STRING "Use prefix for sources in debug info")
975 if(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO)
976 check_c_compiler_flag("-fdebug-prefix-map=foo=bar" SUPPORTS_FDEBUG_PREFIX_MAP)
977 if(LLVM_ENABLE_PROJECTS_USED)
978 get_filename_component(source_root "${LLVM_MAIN_SRC_DIR}/.." ABSOLUTE)
980 set(source_root "${LLVM_MAIN_SRC_DIR}")
982 file(RELATIVE_PATH relative_root "${source_root}" "${CMAKE_BINARY_DIR}")
983 append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
984 append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
985 add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES)