1 include(GetLibraryName)
3 # Ensure that libSupport does not carry any static global initializer.
4 # libSupport can be embedded in use cases where we don't want to load all
5 # cl::opt unless we want to parse the command line.
6 # ManagedStatic can be used to enable lazy-initialization of globals.
7 # We don't use `add_flag_if_supported` as instead of compiling an empty file we
8 # check if the current platform is able to compile global std::mutex with this
9 # flag (Linux can, Darwin can't for example).
10 check_cxx_compiler_flag("-Werror=global-constructors" HAS_WERROR_GLOBAL_CTORS)
11 if (HAS_WERROR_GLOBAL_CTORS)
12 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=global-constructors")
13 CHECK_CXX_SOURCE_COMPILES("
15 static std::mutex TestGlobalCtorDtor;
16 static std::recursive_mutex TestGlobalCtorDtor2;
17 int main() { (void)TestGlobalCtorDtor; (void)TestGlobalCtorDtor2; return 0;}
18 " LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
19 if (NOT LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
20 string(REPLACE "-Werror=global-constructors" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
25 list(APPEND imported_libs ZLIB::ZLIB)
29 if(TARGET zstd::libzstd_shared AND NOT LLVM_USE_STATIC_ZSTD)
30 set(zstd_target zstd::libzstd_shared)
32 set(zstd_target zstd::libzstd_static)
37 list(APPEND imported_libs ${zstd_target})
41 # libuuid required for FOLDERID_Profile usage in lib/Support/Windows/Path.inc.
42 # advapi32 required for CryptAcquireContextW in lib/Support/Windows/Path.inc.
43 set(system_libs ${system_libs} psapi shell32 ole32 uuid advapi32)
44 elseif( CMAKE_HOST_UNIX )
46 set(system_libs ${system_libs} rt)
49 set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
51 if( HAVE_BACKTRACE AND NOT "${Backtrace_LIBRARIES}" STREQUAL "" )
52 # On BSDs, CMake returns a fully qualified path to the backtrace library.
53 # We need to remove the path and the 'lib' prefix, to make it look like a
54 # regular short library name, suitable for appending to a -l link flag.
55 get_filename_component(Backtrace_LIBFILE ${Backtrace_LIBRARIES} NAME_WE)
56 STRING(REGEX REPLACE "^lib" "" Backtrace_LIBFILE ${Backtrace_LIBFILE})
57 set(system_libs ${system_libs} ${Backtrace_LIBFILE})
59 if( LLVM_ENABLE_TERMINFO )
60 set(imported_libs ${imported_libs} Terminfo::terminfo)
62 set(system_libs ${system_libs} ${LLVM_ATOMIC_LIB})
63 set(system_libs ${system_libs} ${LLVM_PTHREAD_LIB})
64 if( UNIX AND NOT (BEOS OR HAIKU) )
65 set(system_libs ${system_libs} m)
67 if( UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "SunOS" )
68 set(system_libs ${system_libs} kstat)
71 set(system_libs ${system_libs} zircon)
74 add_compile_definitions(_BSD_SOURCE)
75 set(system_libs ${system_libs} bsd)
77 endif( MSVC OR MINGW )
79 # Delay load shell32.dll if possible to speed up process startup.
82 # When linking with Swift, `swiftc.exe` is used as the linker drive rather
83 # than invoking `link.exe` directly. In such a case, the flags should be
84 # marked as `-Xlinker` to pass them directly to the linker. As a temporary
85 # workaround simply elide the delay loading.
86 set (delayload_flags $<$<NOT:$<LINK_LANGUAGE:Swift>>:delayimp -delayload:shell32.dll -delayload:ole32.dll>)
89 # Link Z3 if the user wants to build it.
91 set(system_libs ${system_libs} ${Z3_LIBRARIES})
94 # Override the C runtime allocator on Windows and embed it into LLVM tools & libraries
95 if(LLVM_INTEGRATED_CRT_ALLOC)
96 if (NOT CMAKE_MSVC_RUNTIME_LIBRARY OR CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "DLL$")
97 message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC only works with CMAKE_MSVC_RUNTIME_LIBRARY set to MultiThreaded or MultiThreadedDebug.")
100 string(REGEX REPLACE "(/|\\\\)$" "" LLVM_INTEGRATED_CRT_ALLOC "${LLVM_INTEGRATED_CRT_ALLOC}")
102 if(NOT EXISTS "${LLVM_INTEGRATED_CRT_ALLOC}")
103 message(FATAL_ERROR "Cannot find the path to `git clone` for the CRT allocator! (${LLVM_INTEGRATED_CRT_ALLOC}). Currently, rpmalloc, snmalloc and mimalloc are supported.")
106 if(LLVM_INTEGRATED_CRT_ALLOC MATCHES "rpmalloc$")
107 add_compile_definitions(ENABLE_OVERRIDE ENABLE_PRELOAD)
108 set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/rpmalloc/rpmalloc.c")
109 elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "snmalloc$")
110 set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/src/snmalloc/override/new.cc")
111 set(system_libs ${system_libs} "mincore.lib" "-INCLUDE:malloc")
112 elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "mimalloc$")
113 set(MIMALLOC_LIB "${LLVM_INTEGRATED_CRT_ALLOC}/out/msvc-x64/Release/mimalloc-static.lib")
114 if(NOT EXISTS "${MIMALLOC_LIB}")
115 message(FATAL_ERROR "Cannot find the mimalloc static library. To build it, first apply the patch from https://github.com/microsoft/mimalloc/issues/268 then build the Release x64 target through ${LLVM_INTEGRATED_CRT_ALLOC}\\ide\\vs2019\\mimalloc.sln")
117 set(system_libs ${system_libs} "${MIMALLOC_LIB}" "-INCLUDE:malloc")
121 # FIXME: We are currently guarding AIX headers with _XOPEN_SOURCE=700.
122 # See llvm/CMakeLists.txt. However, we need _SC_NPROCESSORS_ONLN in
123 # unistd.h and it is guarded by _ALL_SOURCE, so we remove the _XOPEN_SOURCE
124 # guard here. We should remove the guards all together once AIX cleans up
125 # the system headers.
126 if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
127 remove_definitions("-D_XOPEN_SOURCE=700")
130 add_subdirectory(BLAKE3)
132 add_llvm_component_library(LLVMSupport
140 ARMAttributeParser.cpp
145 BalancedPartitioning.cpp
146 BinaryStreamError.cpp
147 BinaryStreamReader.cpp
149 BinaryStreamWriter.cpp
151 BranchProbability.cpp
155 circular_raw_ostream.cpp
164 ConvertUTFWrapper.cpp
165 CrashRecoveryContext.cpp
167 CSKYAttributeParser.cpp
172 DivisionByConstantInfo.cpp
173 DAGDeltaAlgorithm.cpp
175 ELFAttributeParser.cpp
183 FloatingPointMode.cpp
207 MSP430AttributeParser.cpp
209 OptimizedStructLayout.cpp
215 RandomNumberGenerator.cpp
218 RISCVAttributeParser.cpp
246 UnicodeNameToCodepoint.cpp
247 UnicodeNameToCodepointGenerated.cpp
249 VirtualFileSystem.cpp
264 $<TARGET_OBJECTS:LLVMSupportBlake3>
280 ADDITIONAL_HEADER_DIRS
283 ${LLVM_MAIN_INCLUDE_DIR}/llvm/ADT
284 ${LLVM_MAIN_INCLUDE_DIR}/llvm/Support
285 ${Backtrace_INCLUDE_DIRS}
288 ${system_libs} ${imported_libs} ${delayload_flags}
294 set(llvm_system_libs ${system_libs})
296 # This block is only needed for llvm-config. When we deprecate llvm-config and
297 # move to using CMake export, this block can be removed.
299 # CMAKE_BUILD_TYPE is only meaningful to single-configuration generators.
301 string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
302 get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION_${build_type})
305 get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION)
307 get_library_name(${zlib_library} zlib_library)
308 set(llvm_system_libs ${llvm_system_libs} "${zlib_library}")
312 # CMAKE_BUILD_TYPE is only meaningful to single-configuration generators.
314 string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
315 get_property(zstd_library TARGET ${zstd_target} PROPERTY LOCATION_${build_type})
318 get_property(zstd_library TARGET ${zstd_target} PROPERTY LOCATION)
320 get_library_name(${zstd_library} zstd_library)
321 set(llvm_system_libs ${llvm_system_libs} "${zstd_library}")
324 if(LLVM_ENABLE_TERMINFO)
325 if(NOT terminfo_library)
326 get_property(terminfo_library TARGET Terminfo::terminfo PROPERTY LOCATION)
328 get_library_name(${terminfo_library} terminfo_library)
329 set(llvm_system_libs ${llvm_system_libs} "${terminfo_library}")
332 set_property(TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS "${llvm_system_libs}")
335 if(LLVM_INTEGRATED_CRT_ALLOC)
336 if(LLVM_INTEGRATED_CRT_ALLOC MATCHES "snmalloc$")
337 set_property(TARGET LLVMSupport PROPERTY CXX_STANDARD 17)
338 add_compile_definitions(_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING)
339 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND
340 "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "x86_64")
341 set_property(TARGET LLVMSupport PROPERTY COMPILE_FLAGS "-mcx16")
347 target_include_directories(LLVMSupport SYSTEM