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++14")
22 # Preserve behaviour of legacy cache variables
23 if (LLVM_ENABLE_CXX1Z)
24 set(LLVM_CXX_STD_default "c++1z")
26 if (LLVM_CXX_STD STREQUAL "c++11")
27 set(LLVM_CXX_STD_force FORCE)
29 set(LLVM_CXX_STD ${LLVM_CXX_STD_default}
30 CACHE STRING "C++ standard to use for compilation." ${LLVM_CXX_STD_force})
32 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")
33 string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO)
35 # Ninja Job Pool support
36 # The following only works with the Ninja generator in CMake >= 3.0.
37 set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING
38 "Define the maximum number of concurrent compilation jobs (Ninja only).")
39 if(LLVM_PARALLEL_COMPILE_JOBS)
40 if(NOT CMAKE_MAKE_PROGRAM MATCHES "ninja")
41 message(WARNING "Job pooling is only available with Ninja generators.")
43 set_property(GLOBAL APPEND PROPERTY JOB_POOLS compile_job_pool=${LLVM_PARALLEL_COMPILE_JOBS})
44 set(CMAKE_JOB_POOL_COMPILE compile_job_pool)
48 set(LLVM_PARALLEL_LINK_JOBS "" CACHE STRING
49 "Define the maximum number of concurrent link jobs (Ninja only).")
50 if(CMAKE_MAKE_PROGRAM MATCHES "ninja")
51 if(NOT LLVM_PARALLEL_LINK_JOBS AND uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
52 message(STATUS "ThinLTO provides its own parallel linking - limiting parallel link jobs to 2.")
53 set(LLVM_PARALLEL_LINK_JOBS "2")
55 if(LLVM_PARALLEL_LINK_JOBS)
56 set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=${LLVM_PARALLEL_LINK_JOBS})
57 set(CMAKE_JOB_POOL_LINK link_job_pool)
59 elseif(LLVM_PARALLEL_LINK_JOBS)
60 message(WARNING "Job pooling is only available with Ninja generators.")
63 if( LLVM_ENABLE_ASSERTIONS )
64 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
66 add_definitions( -D_DEBUG )
68 # On non-Debug builds cmake automatically defines NDEBUG, so we
69 # explicitly undefine it:
70 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
71 add_definitions( -UNDEBUG )
72 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
73 foreach (flags_var_to_scrub
74 CMAKE_CXX_FLAGS_RELEASE
75 CMAKE_CXX_FLAGS_RELWITHDEBINFO
76 CMAKE_CXX_FLAGS_MINSIZEREL
78 CMAKE_C_FLAGS_RELWITHDEBINFO
79 CMAKE_C_FLAGS_MINSIZEREL)
80 string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
81 "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
86 if(LLVM_ENABLE_EXPENSIVE_CHECKS)
87 add_definitions(-DEXPENSIVE_CHECKS)
88 add_definitions(-D_GLIBCXX_DEBUG)
91 string(TOUPPER "${LLVM_ABI_BREAKING_CHECKS}" uppercase_LLVM_ABI_BREAKING_CHECKS)
93 if( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "WITH_ASSERTS" )
94 if( LLVM_ENABLE_ASSERTIONS )
95 set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
97 elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_ON" )
98 set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
99 elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_OFF" )
100 # We don't need to do anything special to turn off ABI breaking checks.
101 elseif( NOT DEFINED LLVM_ABI_BREAKING_CHECKS )
102 # Treat LLVM_ABI_BREAKING_CHECKS like "FORCE_OFF" when it has not been
105 message(FATAL_ERROR "Unknown value for LLVM_ABI_BREAKING_CHECKS: \"${LLVM_ABI_BREAKING_CHECKS}\"!")
108 if( LLVM_REVERSE_ITERATION )
109 set( LLVM_ENABLE_REVERSE_ITERATION 1 )
113 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
125 if(APPLE OR ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
126 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
128 set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
130 else(FUCHSIA OR UNIX)
131 MESSAGE(SEND_ERROR "Unable to determine platform")
132 endif(FUCHSIA OR UNIX)
135 set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX})
136 set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
138 # We use *.dylib rather than *.so on darwin.
139 set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
142 if(LLVM_ENABLE_LLD AND LLVM_ENABLE_LTO)
143 message(FATAL_ERROR "lld does not support LTO on Darwin")
145 # Darwin-specific linker flags for loadable modules.
146 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
149 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
150 if(NOT LLVM_BUILD_32_BITS)
151 if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
152 append("-q64" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
154 append("-maix64" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
156 set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> -X64 qc <TARGET> <LINK_FLAGS> <OBJECTS>")
157 set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> -X64 q <TARGET> <LINK_FLAGS> <OBJECTS>")
158 set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -X64 <TARGET>")
159 set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -X64 <TARGET>")
161 # -fPIC does not enable the large code model for GCC on AIX but does for XL.
162 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
163 append("-mcmodel=large" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
164 elseif(CMAKE_CXX_COMPILER_ID MATCHES "XL")
165 # XL generates a small number of relocations not of the large model, -bbigtoc is needed.
166 append("-Wl,-bbigtoc"
167 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
171 # Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise a DSO
172 # build might work on ELF but fail on MachO/COFF.
173 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|FreeBSD|OpenBSD|DragonFly|AIX|SunOS" OR
175 NOT LLVM_USE_SANITIZER)
176 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs")
179 # Pass -Wl,-z,nodelete. This makes sure our shared libraries are not unloaded
180 # by dlclose(). We need that since the CLI API relies on cross-references
181 # between global objects which became horribly broken when one of the libraries
183 if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
184 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,nodelete")
185 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,nodelete")
189 function(append value)
190 foreach(variable ${ARGN})
191 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
195 function(append_if condition value)
197 foreach(variable ${ARGN})
198 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
203 macro(add_flag_if_supported flag name)
204 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
205 append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS)
206 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
207 append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS)
210 function(add_flag_or_print_warning flag name)
211 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
212 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
213 if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name})
214 message(STATUS "Building with ${flag}")
215 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
216 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
217 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${flag}" PARENT_SCOPE)
219 message(WARNING "${flag} is not supported.")
223 if( LLVM_ENABLE_LLD )
224 if ( LLVM_USE_LINKER )
225 message(FATAL_ERROR "LLVM_ENABLE_LLD and LLVM_USE_LINKER can't be set at the same time")
227 set(LLVM_USE_LINKER "lld")
230 if( LLVM_USE_LINKER )
231 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
232 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fuse-ld=${LLVM_USE_LINKER}")
233 check_cxx_source_compiles("int main() { return 0; }" CXX_SUPPORTS_CUSTOM_LINKER)
234 if ( NOT CXX_SUPPORTS_CUSTOM_LINKER )
235 message(FATAL_ERROR "Host compiler does not support '-fuse-ld=${LLVM_USE_LINKER}'")
237 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
238 append("-fuse-ld=${LLVM_USE_LINKER}"
239 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
242 if( LLVM_ENABLE_PIC )
244 # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
245 # know how to disable this, so just force ENABLE_PIC off for now.
246 message(WARNING "-fPIC not supported with Xcode.")
247 elseif( WIN32 OR CYGWIN)
248 # On Windows all code is PIC. MinGW warns if -fPIC is used.
250 add_flag_or_print_warning("-fPIC" FPIC)
252 # GCC for MIPS can miscompile LLVM due to PR37701.
253 if(CMAKE_COMPILER_IS_GNUCXX AND LLVM_NATIVE_ARCH STREQUAL "Mips" AND
254 NOT Uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
255 add_flag_or_print_warning("-fno-shrink-wrap" FNO_SHRINK_WRAP)
259 if(NOT WIN32 AND NOT CYGWIN AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
260 # MinGW warns if -fvisibility-inlines-hidden is used.
261 # GCC on AIX warns if -fvisibility-inlines-hidden is used.
262 check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
263 append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS)
266 if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND MINGW)
267 add_definitions( -D_FILE_OFFSET_BITS=64 )
270 if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
271 # TODO: support other platforms and toolchains.
272 if( LLVM_BUILD_32_BITS )
273 message(STATUS "Building 32 bits executables and libraries.")
274 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
275 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
276 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
277 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
278 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
280 # FIXME: CMAKE_SIZEOF_VOID_P is still 8
281 add_definitions(-D_LARGEFILE_SOURCE)
282 add_definitions(-D_FILE_OFFSET_BITS=64)
283 endif( LLVM_BUILD_32_BITS )
284 endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
286 # If building on a GNU specific 32-bit system, make sure off_t is 64 bits
287 # so that off_t can stored offset > 2GB.
288 # Android until version N (API 24) doesn't support it.
289 if (ANDROID AND (ANDROID_NATIVE_API_LEVEL LESS 24))
290 set(LLVM_FORCE_SMALLFILE_FOR_ANDROID TRUE)
292 if( CMAKE_SIZEOF_VOID_P EQUAL 4 AND NOT LLVM_FORCE_SMALLFILE_FOR_ANDROID)
293 # FIXME: It isn't handled in LLVM_BUILD_32_BITS.
294 add_definitions( -D_LARGEFILE_SOURCE )
295 add_definitions( -D_FILE_OFFSET_BITS=64 )
299 # For Xcode enable several build settings that correspond to
300 # many warnings that are on by default in Clang but are
301 # not enabled for historical reasons. For versions of Xcode
302 # that do not support these options they will simply
304 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_RETURN_TYPE "YES")
305 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_MISSING_NEWLINE "YES")
306 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VALUE "YES")
307 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VARIABLE "YES")
308 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_SIGN_COMPARE "YES")
309 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_FUNCTION "YES")
310 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED "YES")
311 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS "YES")
312 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNINITIALIZED_AUTOS "YES")
313 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_BOOL_CONVERSION "YES")
314 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_EMPTY_BODY "YES")
315 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_ENUM_CONVERSION "YES")
316 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_INT_CONVERSION "YES")
317 set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_CONSTANT_CONVERSION "YES")
318 set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_NON_VIRTUAL_DESTRUCTOR "YES")
321 # On Win32 using MS tools, provide an option to set the number of parallel jobs
324 set(LLVM_COMPILER_JOBS "0" CACHE STRING
325 "Number of parallel compiler jobs. 0 means use all processors. Default is 0.")
326 if( NOT LLVM_COMPILER_JOBS STREQUAL "1" )
327 if( LLVM_COMPILER_JOBS STREQUAL "0" )
328 add_definitions( /MP )
330 message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS})
331 add_definitions( /MP${LLVM_COMPILER_JOBS} )
334 message(STATUS "Parallel compilation disabled")
338 # set stack reserved size to ~10MB
340 # CMake previously automatically set this value for MSVC builds, but the
341 # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default
342 # value (1 MB) which is not enough for us in tasks such as parsing recursive
343 # C++ templates in Clang.
344 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000")
345 elseif(MINGW) # FIXME: Also cygwin?
346 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216")
348 # Pass -mbig-obj to mingw gas on Win64. COFF has a 2**16 section limit, and
349 # on Win64, every COMDAT function creates at least 3 sections: .text, .pdata,
351 if (CMAKE_SIZEOF_VOID_P EQUAL 8)
352 append("-Wa,-mbig-obj" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
357 include(ChooseMSVCCRT)
359 # Add definitions that make MSVC much less annoying.
361 # For some reason MS wants to deprecate a bunch of standard functions...
362 -D_CRT_SECURE_NO_DEPRECATE
363 -D_CRT_SECURE_NO_WARNINGS
364 -D_CRT_NONSTDC_NO_DEPRECATE
365 -D_CRT_NONSTDC_NO_WARNINGS
366 -D_SCL_SECURE_NO_DEPRECATE
367 -D_SCL_SECURE_NO_WARNINGS
370 # Tell MSVC to use the Unicode version of the Win32 APIs instead of ANSI.
376 if (LLVM_ENABLE_WERROR)
377 append("/WX" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
378 endif (LLVM_ENABLE_WERROR)
380 append("/Zc:inline" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
382 # Allow users to request PDBs in release mode. CMake offeres the
383 # RelWithDebInfo configuration, but it uses different optimization settings
384 # (/Ob1 vs /Ob2 or -O2 vs -O3). LLVM provides this flag so that users can get
385 # PDBs without changing codegen.
386 option(LLVM_ENABLE_PDB OFF)
387 if (LLVM_ENABLE_PDB AND uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
388 append("/Zi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
389 # /DEBUG disables linker GC and ICF, but we want those in Release mode.
390 append("/DEBUG /OPT:REF /OPT:ICF"
391 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
392 CMAKE_SHARED_LINKER_FLAGS)
395 # Disable string literal const->non-const type conversion.
396 # "When specified, the compiler requires strict const-qualification
397 # conformance for pointers initialized by using string literals."
398 append("/Zc:strictStrings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
400 # "Generate Intrinsic Functions".
401 append("/Oi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
403 # "Enforce type conversion rules".
404 append("/Zc:rvalueCast" CMAKE_CXX_FLAGS)
406 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT LLVM_ENABLE_LTO)
407 # clang-cl and cl by default produce non-deterministic binaries because
408 # link.exe /incremental requires a timestamp in the .obj file. clang-cl
409 # has the flag /Brepro to force deterministic binaries. We want to pass that
410 # whenever you're building with clang unless you're passing /incremental
411 # or using LTO (/Brepro with LTO would result in a warning about the flag
412 # being unused, because we're not generating object files).
413 # This checks CMAKE_CXX_COMPILER_ID in addition to check_cxx_compiler_flag()
414 # because cl.exe does not emit an error on flags it doesn't understand,
415 # letting check_cxx_compiler_flag() claim it understands all flags.
416 check_cxx_compiler_flag("/Brepro" SUPPORTS_BREPRO)
418 # Check if /INCREMENTAL is passed to the linker and complain that it
419 # won't work with /Brepro.
420 string(TOUPPER "${CMAKE_EXE_LINKER_FLAGS}" upper_exe_flags)
421 string(TOUPPER "${CMAKE_MODULE_LINKER_FLAGS}" upper_module_flags)
422 string(TOUPPER "${CMAKE_SHARED_LINKER_FLAGS}" upper_shared_flags)
424 string(FIND "${upper_exe_flags} ${upper_module_flags} ${upper_shared_flags}"
425 "/INCREMENTAL" linker_flag_idx)
427 if (${linker_flag_idx} GREATER -1)
428 message(WARNING "/Brepro not compatible with /INCREMENTAL linking - builds will be non-deterministic")
430 append("/Brepro" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
435 elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
436 append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
437 append_if(LLVM_ENABLE_WERROR "-Wno-error" CMAKE_REQUIRED_FLAGS)
438 add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME)
439 add_flag_if_supported("-Werror=unguarded-availability-new" WERROR_UNGUARDED_AVAILABILITY_NEW)
440 check_cxx_compiler_flag("-std=${LLVM_CXX_STD}" CXX_SUPPORTS_CXX_STD)
441 if (CXX_SUPPORTS_CXX_STD)
443 # MinGW and Cygwin are a bit stricter and lack things like
444 # 'strdup', 'stricmp', etc in c++11 mode.
445 string(REPLACE "c++" "gnu++" gnu_LLVM_CXX_STD "${LLVM_CXX_STD}")
446 append("-std=${gnu_LLVM_CXX_STD}" CMAKE_CXX_FLAGS)
448 append("-std=${LLVM_CXX_STD}" CMAKE_CXX_FLAGS)
451 message(FATAL_ERROR "The host compiler does not support '-std=${LLVM_CXX_STD}'.")
453 if (LLVM_ENABLE_MODULES)
454 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
455 set(module_flags "-fmodules -fmodules-cache-path=${PROJECT_BINARY_DIR}/module.cache")
456 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
457 # On Darwin -fmodules does not imply -fcxx-modules.
458 set(module_flags "${module_flags} -fcxx-modules")
460 if (LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY)
461 set(module_flags "${module_flags} -Xclang -fmodules-local-submodule-visibility")
463 if (LLVM_ENABLE_MODULE_DEBUGGING AND
464 ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR
465 (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")))
466 set(module_flags "${module_flags} -gmodules")
468 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${module_flags}")
470 # Check that we can build code with modules enabled, and that repeatedly
471 # including <cassert> still manages to respect NDEBUG properly.
472 CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG
476 int main() { assert(this code is not compiled); }"
477 CXX_SUPPORTS_MODULES)
478 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
479 if (CXX_SUPPORTS_MODULES)
480 append("${module_flags}" CMAKE_CXX_FLAGS)
482 message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler")
484 endif(LLVM_ENABLE_MODULES)
489 set(msvc_warning_flags
491 -wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline)
492 -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
493 -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
494 -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
495 -wd4258 # Suppress ''var' : definition from the for loop is ignored; the definition from the enclosing scope is used'
496 -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
497 -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
498 -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
499 -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
500 -wd4456 # Suppress 'declaration of 'var' hides local variable'
501 -wd4457 # Suppress 'declaration of 'var' hides function parameter'
502 -wd4458 # Suppress 'declaration of 'var' hides class member'
503 -wd4459 # Suppress 'declaration of 'var' hides global declaration'
504 -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
505 -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
506 -wd4722 # Suppress 'function' : destructor never returns, potential memory leak
507 -wd4100 # Suppress 'unreferenced formal parameter'
508 -wd4127 # Suppress 'conditional expression is constant'
509 -wd4512 # Suppress 'assignment operator could not be generated'
510 -wd4505 # Suppress 'unreferenced local function has been removed'
511 -wd4610 # Suppress '<class> can never be instantiated'
512 -wd4510 # Suppress 'default constructor could not be generated'
513 -wd4702 # Suppress 'unreachable code'
514 -wd4245 # Suppress 'signed/unsigned mismatch'
515 -wd4706 # Suppress 'assignment within conditional expression'
516 -wd4310 # Suppress 'cast truncates constant value'
517 -wd4701 # Suppress 'potentially uninitialized local variable'
518 -wd4703 # Suppress 'potentially uninitialized local pointer variable'
519 -wd4389 # Suppress 'signed/unsigned mismatch'
520 -wd4611 # Suppress 'interaction between '_setjmp' and C++ object destruction is non-portable'
521 -wd4805 # Suppress 'unsafe mix of type <type> and type <type> in operation'
522 -wd4204 # Suppress 'nonstandard extension used : non-constant aggregate initializer'
523 -wd4577 # Suppress 'noexcept used with no exception handling mode specified; termination on exception is not guaranteed'
524 -wd4091 # Suppress 'typedef: ignored on left of '' when no variable is declared'
525 # C4592 is disabled because of false positives in Visual Studio 2015
526 # Update 1. Re-evaluate the usefulness of this diagnostic with Update 2.
527 -wd4592 # Suppress ''var': symbol will be dynamically initialized (implementation limitation)
528 -wd4319 # Suppress ''operator' : zero extending 'type' to 'type' of greater size'
529 # C4709 is disabled because of a bug with Visual Studio 2017 as of
530 # v15.8.8. Re-evaluate the usefulness of this diagnostic when the bug
532 -wd4709 # Suppress comma operator within array index expression
534 # Ideally, we'd like this warning to be enabled, but MSVC 2013 doesn't
535 # support the 'aligned' attribute in the way that clang sources requires (for
536 # any code that uses the LLVM_ALIGNAS macro), so this is must be disabled to
537 # avoid unwanted alignment warnings.
538 # When we switch to requiring a version of MSVC that supports the 'alignas'
539 # specifier (MSVC 2015?) this warning can be re-enabled.
540 -wd4324 # Suppress 'structure was padded due to __declspec(align())'
543 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
545 # Promoted warnings to errors.
546 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
551 if (LLVM_ENABLE_WARNINGS)
552 # Put /W4 in front of all the -we flags. cl.exe doesn't care, but for
553 # clang-cl having /W4 after the -we flags will re-enable the warnings
555 set(msvc_warning_flags "/W4 ${msvc_warning_flags}")
556 # CMake appends /W3 by default, and having /W3 followed by /W4 will result in
557 # cl : Command line warning D9025 : overriding '/W3' with '/W4'. Since this is
558 # a command line warning and not a compiler warning, it cannot be suppressed except
559 # by fixing the command line.
560 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
561 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
563 if (LLVM_ENABLE_PEDANTIC)
564 # No MSVC equivalent available
565 endif (LLVM_ENABLE_PEDANTIC)
566 endif (LLVM_ENABLE_WARNINGS)
568 foreach(flag ${msvc_warning_flags})
569 append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
573 if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
575 # Don't add -Wall for clang-cl, because it maps -Wall to -Weverything for
576 # MSVC compatibility. /W4 is added above instead.
578 append("-Wall" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
581 append("-Wextra -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
582 append("-Wcast-qual" CMAKE_CXX_FLAGS)
584 # Turn off missing field initializer warnings for gcc to avoid noise from
585 # false positives with empty {}. Turn them on otherwise (they're off by
586 # default for clang).
587 check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
588 if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
589 if (CMAKE_COMPILER_IS_GNUCXX)
590 append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
592 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
596 if (LLVM_ENABLE_PEDANTIC AND LLVM_COMPILER_IS_GCC_COMPATIBLE)
597 append("-pedantic" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
598 append("-Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
601 add_flag_if_supported("-Wimplicit-fallthrough" IMPLICIT_FALLTHROUGH_FLAG)
602 add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG)
603 append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
604 append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
606 # Disable -Wclass-memaccess, a C++-only warning from GCC 8 that fires on
607 # LLVM's ADT classes.
608 check_cxx_compiler_flag("-Wclass-memaccess" CXX_SUPPORTS_CLASS_MEMACCESS_FLAG)
609 append_if(CXX_SUPPORTS_CLASS_MEMACCESS_FLAG "-Wno-class-memaccess" CMAKE_CXX_FLAGS)
611 # The LLVM libraries have no stable C++ API, so -Wnoexcept-type is not useful.
612 check_cxx_compiler_flag("-Wnoexcept-type" CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG)
613 append_if(CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG "-Wno-noexcept-type" CMAKE_CXX_FLAGS)
615 # Check if -Wnon-virtual-dtor warns even though the class is marked final.
616 # If it does, don't add it. So it won't be added on clang 3.4 and older.
617 # This also catches cases when -Wnon-virtual-dtor isn't supported by
618 # the compiler at all. This flag is not activated for gcc since it will
619 # incorrectly identify a protected non-virtual base when there is a friend
620 # declaration. Don't activate this in general on Windows as this warning has
621 # too many false positives on COM-style classes, which are destroyed with
622 # Release() (PR32286).
623 if (NOT CMAKE_COMPILER_IS_GNUCXX AND NOT WIN32)
624 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
625 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11 -Werror=non-virtual-dtor")
626 CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();protected: ~base();};
627 class derived final : public base { public: ~derived();};
628 int main() { return 0; }"
629 CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR)
630 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
631 append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR
632 "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
635 # Enable -Wdelete-non-virtual-dtor if available.
636 add_flag_if_supported("-Wdelete-non-virtual-dtor" DELETE_NON_VIRTUAL_DTOR_FLAG)
638 # Check if -Wcomment is OK with an // comment ending with '\' if the next
639 # line is also a // comment.
640 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
641 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment")
642 CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main() {return 0;}"
643 C_WCOMMENT_ALLOWS_LINE_WRAP)
644 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
645 if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP)
646 append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
649 # Enable -Wstring-conversion to catch misuse of string literals.
650 add_flag_if_supported("-Wstring-conversion" STRING_CONVERSION_FLAG)
651 endif (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
653 if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND NOT LLVM_ENABLE_WARNINGS)
654 append("-w" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
657 macro(append_common_sanitizer_flags)
659 # Append -fno-omit-frame-pointer and turn on debug info to get better
661 add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER)
662 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
663 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
664 add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY)
666 # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
667 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND LLVM_OPTIMIZE_SANITIZED_BUILDS)
668 add_flag_if_supported("-O1" O1)
671 # Keep frame pointers around.
672 append("/Oy-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
673 # Always ask the linker to produce symbols with asan.
674 append("/Z7" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
675 append("-debug" CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
679 # Turn on sanitizers if necessary.
680 if(LLVM_USE_SANITIZER)
682 if (LLVM_USE_SANITIZER STREQUAL "Address")
683 append_common_sanitizer_flags()
684 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
685 elseif (LLVM_USE_SANITIZER STREQUAL "HWAddress")
686 append_common_sanitizer_flags()
687 append("-fsanitize=hwaddress" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
688 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
689 append_common_sanitizer_flags()
690 append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
691 if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
692 append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
694 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
695 append_common_sanitizer_flags()
696 append("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all"
697 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
698 elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
699 append_common_sanitizer_flags()
700 append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
701 elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
702 LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
703 append_common_sanitizer_flags()
704 append("-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all"
705 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
706 elseif (LLVM_USE_SANITIZER STREQUAL "Leaks")
707 append_common_sanitizer_flags()
708 append("-fsanitize=leak" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
710 message(FATAL_ERROR "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
713 if (LLVM_USE_SANITIZER STREQUAL "Address")
714 append_common_sanitizer_flags()
715 append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
717 message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}")
720 message(FATAL_ERROR "LLVM_USE_SANITIZER is not supported on this platform.")
722 if (LLVM_USE_SANITIZER MATCHES "(Undefined;)?Address(;Undefined)?")
723 add_flag_if_supported("-fsanitize-address-use-after-scope"
724 FSANITIZE_USE_AFTER_SCOPE_FLAG)
726 if (LLVM_USE_SANITIZE_COVERAGE)
727 append("-fsanitize=fuzzer-no-link" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
729 if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*")
730 set(BLACKLIST_FILE "${CMAKE_SOURCE_DIR}/utils/sanitizers/ubsan_blacklist.txt")
731 if (EXISTS "${BLACKLIST_FILE}")
732 append("-fsanitize-blacklist=${BLACKLIST_FILE}"
733 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
738 # Turn on -gsplit-dwarf if requested
739 if(LLVM_USE_SPLIT_DWARF)
740 add_definitions("-gsplit-dwarf")
743 add_definitions( -D__STDC_CONSTANT_MACROS )
744 add_definitions( -D__STDC_FORMAT_MACROS )
745 add_definitions( -D__STDC_LIMIT_MACROS )
747 # clang and gcc don't default-print colored diagnostics when invoked from Ninja.
749 CMAKE_GENERATOR STREQUAL "Ninja" AND
750 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
751 (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
752 NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9))))
753 append("-fdiagnostics-color" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
756 # lld doesn't print colored diagnostics when invoked from Ninja
757 if (UNIX AND CMAKE_GENERATOR STREQUAL "Ninja")
758 include(CheckLinkerFlag)
759 check_linker_flag("-Wl,--color-diagnostics" LINKER_SUPPORTS_COLOR_DIAGNOSTICS)
760 append_if(LINKER_SUPPORTS_COLOR_DIAGNOSTICS "-Wl,--color-diagnostics"
761 CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
764 # Add flags for add_dead_strip().
765 # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
766 # But MinSizeRel seems to add that automatically, so maybe disable these
767 # flags instead if LLVM_NO_DEAD_STRIP is set.
768 if(NOT CYGWIN AND NOT WIN32)
769 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND
770 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
771 check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS)
772 if (C_SUPPORTS_FNO_FUNCTION_SECTIONS)
773 # Don't add -ffunction-section if it can be disabled with -fno-function-sections.
774 # Doing so will break sanitizers.
775 add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS)
777 add_flag_if_supported("-fdata-sections" FDATA_SECTIONS)
782 # Remove flags here, for exceptions and RTTI.
783 # Each target property or source property should be responsible to control
785 # CL.EXE complains to override flags like "/GR /GR-".
786 string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
787 string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
790 # Provide public options to globally control RTTI and EH
791 option(LLVM_ENABLE_EH "Enable Exception handling" OFF)
792 option(LLVM_ENABLE_RTTI "Enable run time type information" OFF)
793 if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI)
794 message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON")
797 option(LLVM_USE_NEWPM "Build LLVM using the experimental new pass manager" Off)
798 mark_as_advanced(LLVM_USE_NEWPM)
800 append("-fexperimental-new-pass-manager"
803 CMAKE_EXE_LINKER_FLAGS
804 CMAKE_SHARED_LINKER_FLAGS)
807 option(LLVM_ENABLE_IR_PGO "Build LLVM and tools with IR PGO instrumentation (deprecated)" Off)
808 mark_as_advanced(LLVM_ENABLE_IR_PGO)
810 set(LLVM_BUILD_INSTRUMENTED OFF CACHE STRING "Build LLVM and tools with PGO instrumentation. May be specified as IR or Frontend")
811 mark_as_advanced(LLVM_BUILD_INSTRUMENTED)
812 string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" uppercase_LLVM_BUILD_INSTRUMENTED)
814 if (LLVM_BUILD_INSTRUMENTED)
815 if (LLVM_ENABLE_IR_PGO OR uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "IR")
816 append("-fprofile-generate='${LLVM_PROFILE_DATA_DIR}'"
819 CMAKE_EXE_LINKER_FLAGS
820 CMAKE_SHARED_LINKER_FLAGS)
821 elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR")
822 append("-fcs-profile-generate='${LLVM_CSPROFILE_DATA_DIR}'"
825 CMAKE_EXE_LINKER_FLAGS
826 CMAKE_SHARED_LINKER_FLAGS)
828 append("-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}'"
831 CMAKE_EXE_LINKER_FLAGS
832 CMAKE_SHARED_LINKER_FLAGS)
836 # Need to pass -fprofile-instr-use to linker for context-sensitive PGO
838 if(LLVM_PROFDATA_FILE AND EXISTS ${LLVM_PROFDATA_FILE})
839 append("-fprofile-instr-use='${LLVM_PROFDATA_FILE}'"
840 CMAKE_EXE_LINKER_FLAGS
841 CMAKE_SHARED_LINKER_FLAGS)
844 option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation" Off)
845 mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE)
846 append_if(LLVM_BUILD_INSTRUMENTED_COVERAGE "-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}' -fcoverage-mapping"
849 CMAKE_EXE_LINKER_FLAGS
850 CMAKE_SHARED_LINKER_FLAGS)
852 if (LLVM_BUILD_INSTRUMENTED AND LLVM_BUILD_INSTRUMENTED_COVERAGE)
853 message(FATAL_ERROR "LLVM_BUILD_INSTRUMENTED and LLVM_BUILD_INSTRUMENTED_COVERAGE cannot both be specified")
856 if(LLVM_ENABLE_LTO AND LLVM_ON_WIN32 AND NOT LINKER_IS_LLD_LINK)
857 message(FATAL_ERROR "When compiling for Windows, LLVM_ENABLE_LTO requires using lld as the linker (point CMAKE_LINKER at lld-link.exe)")
859 if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
860 append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
861 if(NOT LINKER_IS_LLD_LINK)
862 append("-flto=thin" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
864 # If the linker supports it, enable the lto cache. This improves initial build
865 # time a little since we re-link a lot of the same objects, and significantly
866 # improves incremental build time.
867 # FIXME: We should move all this logic into the clang driver.
869 append("-Wl,-cache_path_lto,${PROJECT_BINARY_DIR}/lto.cache"
870 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
871 elseif(UNIX AND LLVM_USE_LINKER STREQUAL "lld")
872 append("-Wl,--thinlto-cache-dir=${PROJECT_BINARY_DIR}/lto.cache"
873 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
874 elseif(LLVM_USE_LINKER STREQUAL "gold")
875 append("-Wl,--plugin-opt,cache-dir=${PROJECT_BINARY_DIR}/lto.cache"
876 CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
878 elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL")
879 append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
880 if(NOT LINKER_IS_LLD_LINK)
881 append("-flto=full" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
883 elseif(LLVM_ENABLE_LTO)
884 append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
885 if(NOT LINKER_IS_LLD_LINK)
886 append("-flto" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
890 # This option makes utils/extract_symbols.py be used to determine the list of
891 # symbols to export from LLVM tools. This is necessary when using MSVC if you
892 # want to allow plugins, though note that the plugin has to explicitly link
893 # against (exactly one) tool so we can't unilaterally turn on
894 # LLVM_ENABLE_PLUGINS when it's enabled.
895 option(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS "Export symbols from LLVM tools so that plugins can import them" OFF)
896 if(BUILD_SHARED_LIBS AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
897 message(FATAL_ERROR "BUILD_SHARED_LIBS not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS")
899 if(LLVM_LINK_LLVM_DYLIB AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
900 message(FATAL_ERROR "LLVM_LINK_LLVM_DYLIB not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS")
903 # By default we should enable LLVM_ENABLE_IDE only for multi-configuration
904 # generators. This option disables optional build system features that make IDEs
906 set(LLVM_ENABLE_IDE_default OFF)
907 if (CMAKE_CONFIGURATION_TYPES)
908 set(LLVM_ENABLE_IDE_default ON)
910 option(LLVM_ENABLE_IDE
911 "Disable optional build system features that cause problems for IDE generators"
912 ${LLVM_ENABLE_IDE_default})
913 if (CMAKE_CONFIGURATION_TYPES AND NOT LLVM_ENABLE_IDE)
914 message(WARNING "Disabling LLVM_ENABLE_IDE on multi-configuration generators is not recommended.")
917 function(get_compile_definitions)
918 get_directory_property(top_dir_definitions DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS)
919 foreach(definition ${top_dir_definitions})
921 string(APPEND result " -D${definition}")
923 set(result "-D${definition}")
926 set(LLVM_DEFINITIONS "${result}" PARENT_SCOPE)
928 get_compile_definitions()
930 option(LLVM_FORCE_ENABLE_STATS "Enable statistics collection for builds that wouldn't normally enable it" OFF)
932 check_symbol_exists(os_signpost_interval_begin "os/signpost.h" macos_signposts_available)
933 if(macos_signposts_available)
934 check_cxx_source_compiles(
935 "#include <os/signpost.h>
936 int main() { os_signpost_interval_begin(nullptr, 0, \"\", \"\"); return 0; }"
937 macos_signposts_usable)
938 if(macos_signposts_usable)
939 set(LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS "WITH_ASSERTS" CACHE STRING
940 "Enable support for Xcode signposts. Can be WITH_ASSERTS, FORCE_ON, FORCE_OFF")
941 string(TOUPPER "${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}"
942 uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS)
943 if( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "WITH_ASSERTS" )
944 if( LLVM_ENABLE_ASSERTIONS )
945 set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 )
947 elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_ON" )
948 set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 )
949 elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_OFF" )
950 # We don't need to do anything special to turn off signposts.
951 elseif( NOT DEFINED LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS )
952 # Treat LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS like "FORCE_OFF" when it has not been
955 message(FATAL_ERROR "Unknown value for LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS:"
956 " \"${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}\"!")
961 option(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO "Use relative paths in debug info" OFF)
962 set(LLVM_SOURCE_PREFIX "" CACHE STRING "Use prefix for sources in debug info")
964 if(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO)
965 check_c_compiler_flag("-fdebug-prefix-map=foo=bar" SUPPORTS_FDEBUG_PREFIX_MAP)
966 if(LLVM_ENABLE_PROJECTS_USED)
967 get_filename_component(source_root "${LLVM_MAIN_SRC_DIR}/.." ABSOLUTE)
969 set(source_root "${LLVM_MAIN_SRC_DIR}")
971 file(RELATIVE_PATH relative_root "${source_root}" "${CMAKE_BINARY_DIR}")
972 append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
973 append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
974 add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES)