1include(GetLibraryName)
2
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).
10check_cxx_compiler_flag("-Werror=global-constructors" HAS_WERROR_GLOBAL_CTORS)
11if (HAS_WERROR_GLOBAL_CTORS)
12  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=global-constructors")
13  CHECK_CXX_SOURCE_COMPILES("
14  #include <mutex>
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})
21  endif()
22endif()
23
24if(LLVM_ENABLE_ZLIB)
25  set(imported_libs ZLIB::ZLIB)
26endif()
27
28if(LLVM_ENABLE_ZSTD)
29  list(APPEND imported_libs zstd::libzstd_shared)
30endif()
31
32if( MSVC OR MINGW )
33  # libuuid required for FOLDERID_Profile usage in lib/Support/Windows/Path.inc.
34  # advapi32 required for CryptAcquireContextW in lib/Support/Windows/Path.inc.
35  set(system_libs ${system_libs} psapi shell32 ole32 uuid advapi32)
36elseif( CMAKE_HOST_UNIX )
37  if( HAVE_LIBRT )
38    set(system_libs ${system_libs} rt)
39  endif()
40  if( HAVE_LIBDL )
41    set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
42  endif()
43  if( HAVE_BACKTRACE AND NOT "${Backtrace_LIBRARIES}" STREQUAL "" )
44    # On BSDs, CMake returns a fully qualified path to the backtrace library.
45    # We need to remove the path and the 'lib' prefix, to make it look like a
46    # regular short library name, suitable for appending to a -l link flag.
47    get_filename_component(Backtrace_LIBFILE ${Backtrace_LIBRARIES} NAME_WE)
48    STRING(REGEX REPLACE "^lib" "" Backtrace_LIBFILE ${Backtrace_LIBFILE})
49    set(system_libs ${system_libs} ${Backtrace_LIBFILE})
50  endif()
51  if( LLVM_ENABLE_TERMINFO )
52    set(imported_libs ${imported_libs} Terminfo::terminfo)
53  endif()
54  if( LLVM_ENABLE_THREADS AND (HAVE_LIBATOMIC OR HAVE_CXX_LIBATOMICS64) )
55    set(system_libs ${system_libs} atomic)
56  endif()
57  set(system_libs ${system_libs} ${LLVM_PTHREAD_LIB})
58  if( UNIX AND NOT (BEOS OR HAIKU) )
59    set(system_libs ${system_libs} m)
60  endif()
61  if( FUCHSIA )
62    set(system_libs ${system_libs} zircon)
63  endif()
64  if ( HAIKU )
65    add_definitions(-D_BSD_SOURCE)
66    set(system_libs ${system_libs} bsd)
67  endif()
68endif( MSVC OR MINGW )
69
70# Delay load shell32.dll if possible to speed up process startup.
71set (delayload_flags)
72if (MSVC)
73  set (delayload_flags delayimp -delayload:shell32.dll -delayload:ole32.dll)
74endif()
75
76# Link Z3 if the user wants to build it.
77if(LLVM_WITH_Z3)
78  set(system_libs ${system_libs} ${Z3_LIBRARIES})
79endif()
80
81# Override the C runtime allocator on Windows and embed it into LLVM tools & libraries
82if(LLVM_INTEGRATED_CRT_ALLOC)
83  if (CMAKE_BUILD_TYPE AND NOT ${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$")
84    message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC only works with /MT or /MTd. Use LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE} to set the appropriate option.")
85  endif()
86
87  string(REGEX REPLACE "(/|\\\\)$" "" LLVM_INTEGRATED_CRT_ALLOC "${LLVM_INTEGRATED_CRT_ALLOC}")
88
89  if(NOT EXISTS "${LLVM_INTEGRATED_CRT_ALLOC}")
90    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.")
91  endif()
92
93  if(LLVM_INTEGRATED_CRT_ALLOC MATCHES "rpmalloc$")
94    add_definitions(-DENABLE_OVERRIDE -DENABLE_PRELOAD)
95    set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/rpmalloc/rpmalloc.c")
96  elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "snmalloc$")
97    set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/src/snmalloc/override/new.cc")
98    set(system_libs ${system_libs} "mincore.lib" "-INCLUDE:malloc")
99  elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "mimalloc$")
100    set(MIMALLOC_LIB "${LLVM_INTEGRATED_CRT_ALLOC}/out/msvc-x64/Release/mimalloc-static.lib")
101    if(NOT EXISTS "${MIMALLOC_LIB}")
102	  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")
103    endif()
104    set(system_libs ${system_libs} "${MIMALLOC_LIB}" "-INCLUDE:malloc")
105  endif()
106endif()
107
108add_subdirectory(BLAKE3)
109
110add_llvm_component_library(LLVMSupport
111  AddressRanges.cpp
112  AArch64TargetParser.cpp
113  ABIBreak.cpp
114  ARMTargetParser.cpp
115  AMDGPUMetadata.cpp
116  APFixedPoint.cpp
117  APFloat.cpp
118  APInt.cpp
119  APSInt.cpp
120  ARMBuildAttrs.cpp
121  ARMAttributeParser.cpp
122  ARMWinEH.cpp
123  Allocator.cpp
124  AutoConvert.cpp
125  BinaryStreamError.cpp
126  BinaryStreamReader.cpp
127  BinaryStreamRef.cpp
128  BinaryStreamWriter.cpp
129  BlockFrequency.cpp
130  BranchProbability.cpp
131  BuryPointer.cpp
132  CachePruning.cpp
133  Caching.cpp
134  circular_raw_ostream.cpp
135  Chrono.cpp
136  COM.cpp
137  CodeGenCoverage.cpp
138  CommandLine.cpp
139  Compression.cpp
140  CRC.cpp
141  ConvertUTF.cpp
142  ConvertUTFWrapper.cpp
143  CrashRecoveryContext.cpp
144  CSKYAttributes.cpp
145  CSKYAttributeParser.cpp
146  CSKYTargetParser.cpp
147  DataExtractor.cpp
148  Debug.cpp
149  DebugCounter.cpp
150  DeltaAlgorithm.cpp
151  DivisionByConstantInfo.cpp
152  DAGDeltaAlgorithm.cpp
153  DJB.cpp
154  ELFAttributeParser.cpp
155  ELFAttributes.cpp
156  Error.cpp
157  ErrorHandling.cpp
158  ExtensibleRTTI.cpp
159  FileCollector.cpp
160  FileUtilities.cpp
161  FileOutputBuffer.cpp
162  FoldingSet.cpp
163  FormattedStream.cpp
164  FormatVariadic.cpp
165  GlobPattern.cpp
166  GraphWriter.cpp
167  Hashing.cpp
168  InitLLVM.cpp
169  InstructionCost.cpp
170  IntEqClasses.cpp
171  IntervalMap.cpp
172  ItaniumManglingCanonicalizer.cpp
173  JSON.cpp
174  KnownBits.cpp
175  LEB128.cpp
176  LineIterator.cpp
177  Locale.cpp
178  LockFileManager.cpp
179  LowLevelType.cpp
180  ManagedStatic.cpp
181  MathExtras.cpp
182  MemAlloc.cpp
183  MemoryBuffer.cpp
184  MemoryBufferRef.cpp
185  MD5.cpp
186  MSP430Attributes.cpp
187  MSP430AttributeParser.cpp
188  NativeFormatting.cpp
189  OptimizedStructLayout.cpp
190  Optional.cpp
191  Parallel.cpp
192  PluginLoader.cpp
193  PrettyStackTrace.cpp
194  RandomNumberGenerator.cpp
195  Regex.cpp
196  RISCVAttributes.cpp
197  RISCVAttributeParser.cpp
198  RISCVISAInfo.cpp
199  ScaledNumber.cpp
200  ScopedPrinter.cpp
201  SHA1.cpp
202  SHA256.cpp
203  Signposts.cpp
204  SmallPtrSet.cpp
205  SmallVector.cpp
206  SourceMgr.cpp
207  SpecialCaseList.cpp
208  Statistic.cpp
209  StringExtras.cpp
210  StringMap.cpp
211  StringSaver.cpp
212  StringRef.cpp
213  SuffixTree.cpp
214  SymbolRemappingReader.cpp
215  SystemUtils.cpp
216  TarWriter.cpp
217  TargetParser.cpp
218  ThreadPool.cpp
219  TimeProfiler.cpp
220  Timer.cpp
221  ToolOutputFile.cpp
222  TrigramIndex.cpp
223  Triple.cpp
224  Twine.cpp
225  TypeSize.cpp
226  Unicode.cpp
227  UnicodeCaseFold.cpp
228  UnicodeNameToCodepoint.cpp
229  UnicodeNameToCodepointGenerated.cpp
230  VersionTuple.cpp
231  VirtualFileSystem.cpp
232  WithColor.cpp
233  X86TargetParser.cpp
234  YAMLParser.cpp
235  YAMLTraits.cpp
236  raw_os_ostream.cpp
237  raw_ostream.cpp
238  regcomp.c
239  regerror.c
240  regexec.c
241  regfree.c
242  regstrlcpy.c
243  xxhash.cpp
244  Z3Solver.cpp
245
246  ${ALLOCATOR_FILES}
247  $<TARGET_OBJECTS:LLVMSupportBlake3>
248
249# System
250  Atomic.cpp
251  DynamicLibrary.cpp
252  Errno.cpp
253  Host.cpp
254  Memory.cpp
255  Path.cpp
256  Process.cpp
257  Program.cpp
258  RWMutex.cpp
259  Signals.cpp
260  ThreadLocal.cpp
261  Threading.cpp
262  Valgrind.cpp
263  Watchdog.cpp
264
265  ADDITIONAL_HEADER_DIRS
266  Unix
267  Windows
268  ${LLVM_MAIN_INCLUDE_DIR}/llvm/ADT
269  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Support
270  ${Backtrace_INCLUDE_DIRS}
271
272  LINK_LIBS
273  ${system_libs} ${imported_libs} ${delayload_flags}
274
275  LINK_COMPONENTS
276  Demangle
277  )
278
279set(llvm_system_libs ${system_libs})
280
281# This block is only needed for llvm-config. When we deprecate llvm-config and
282# move to using CMake export, this block can be removed.
283if(LLVM_ENABLE_ZLIB)
284  # CMAKE_BUILD_TYPE is only meaningful to single-configuration generators.
285  if(CMAKE_BUILD_TYPE)
286    string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
287    get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION_${build_type})
288  endif()
289  if(NOT zlib_library)
290    get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION)
291  endif()
292  get_library_name(${zlib_library} zlib_library)
293  set(llvm_system_libs ${llvm_system_libs} "${zlib_library}")
294endif()
295
296if(LLVM_ENABLE_ZSTD)
297  # CMAKE_BUILD_TYPE is only meaningful to single-configuration generators.
298  if(CMAKE_BUILD_TYPE)
299    string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
300    get_property(zstd_library TARGET zstd::libzstd_shared PROPERTY LOCATION_${build_type})
301  endif()
302  if(NOT zstd_library)
303    get_property(zstd_library TARGET zstd::libzstd_shared PROPERTY LOCATION)
304  endif()
305  set(llvm_system_libs ${llvm_system_libs} "${zstd_library}")
306endif()
307
308if(LLVM_ENABLE_TERMINFO)
309  if(NOT terminfo_library)
310    get_property(terminfo_library TARGET Terminfo::terminfo PROPERTY LOCATION)
311  endif()
312  get_library_name(${terminfo_library} terminfo_library)
313  set(llvm_system_libs ${llvm_system_libs} "${terminfo_library}")
314endif()
315
316set_property(TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS "${llvm_system_libs}")
317
318
319if(LLVM_INTEGRATED_CRT_ALLOC)
320  if(LLVM_INTEGRATED_CRT_ALLOC MATCHES "snmalloc$")
321    set_property(TARGET LLVMSupport PROPERTY CXX_STANDARD 17)
322    add_definitions(-D_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING)
323    if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND
324        "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "x86_64")
325      set_property(TARGET LLVMSupport PROPERTY COMPILE_FLAGS "-mcx16")
326    endif()
327  endif()
328endif()
329
330if(LLVM_WITH_Z3)
331  target_include_directories(LLVMSupport SYSTEM
332    PRIVATE
333    ${Z3_INCLUDE_DIR}
334    )
335endif()
336