Move out of line
[llvm-project.git] / llvm / lib / Support / CMakeLists.txt
blobf653379e30334944a5538f29d54ff8c638e529d5
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("
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()
22 endif()
24 if(LLVM_ENABLE_ZLIB)
25   list(APPEND imported_libs ZLIB::ZLIB)
26 endif()
28 if(LLVM_ENABLE_ZSTD)
29   if(TARGET zstd::libzstd_shared AND NOT LLVM_USE_STATIC_ZSTD)
30     set(zstd_target zstd::libzstd_shared)
31   else()
32     set(zstd_target zstd::libzstd_static)
33   endif()
34 endif()
36 if(LLVM_ENABLE_ZSTD)
37   list(APPEND imported_libs ${zstd_target})
38 endif()
40 if( WIN32 )
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   # ntdll required for RtlGetLastNtStatus in lib/Support/ErrorHandling.cpp.
44   set(system_libs ${system_libs} psapi shell32 ole32 uuid advapi32 ws2_32 ntdll)
45 elseif( CMAKE_HOST_UNIX )
46   if( HAVE_LIBRT )
47     set(system_libs ${system_libs} rt)
48   endif()
49   if( HAVE_LIBDL )
50     set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
51   endif()
52   if( HAVE_BACKTRACE AND NOT "${Backtrace_LIBRARIES}" STREQUAL "" )
53     # On BSDs, CMake returns a fully qualified path to the backtrace library.
54     # We need to remove the path and the 'lib' prefix, to make it look like a
55     # regular short library name, suitable for appending to a -l link flag.
56     get_filename_component(Backtrace_LIBFILE ${Backtrace_LIBRARIES} NAME_WE)
57     STRING(REGEX REPLACE "^lib" "" Backtrace_LIBFILE ${Backtrace_LIBFILE})
58     set(system_libs ${system_libs} ${Backtrace_LIBFILE})
59   endif()
60   set(system_libs ${system_libs} ${LLVM_ATOMIC_LIB})
61   set(system_libs ${system_libs} ${LLVM_PTHREAD_LIB})
62   if( UNIX AND NOT (BEOS OR HAIKU) )
63     set(system_libs ${system_libs} m)
64   endif()
65   if( UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "SunOS" )
66     set(system_libs ${system_libs} kstat socket)
67   endif()
68   if( FUCHSIA )
69     set(system_libs ${system_libs} zircon)
70   endif()
71   if ( HAIKU )
72     add_compile_definitions(_BSD_SOURCE)
73     set(system_libs ${system_libs} bsd network)
74   endif()
75 endif( WIN32 )
77 # Delay load shell32.dll if possible to speed up process startup.
78 set (delayload_flags)
79 if (MSVC)
80   # When linking with Swift, `swiftc.exe` is used as the linker drive rather
81   # than invoking `link.exe` directly.  In such a case, the flags should be
82   # marked as `-Xlinker` to pass them directly to the linker.  As a temporary
83   # workaround simply elide the delay loading.
84   set (delayload_flags $<$<NOT:$<LINK_LANGUAGE:Swift>>:delayimp -delayload:shell32.dll -delayload:ole32.dll>)
85 endif()
87 # Link Z3 if the user wants to build it.
88 if(LLVM_WITH_Z3)
89   set(system_libs ${system_libs} ${Z3_LIBRARIES})
90 endif()
92 # Override the C runtime allocator on Windows and embed it into LLVM tools & libraries
93 if(LLVM_INTEGRATED_CRT_ALLOC)
94   if (NOT CMAKE_MSVC_RUNTIME_LIBRARY OR CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "DLL$")
95     message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC only works with CMAKE_MSVC_RUNTIME_LIBRARY set to MultiThreaded or MultiThreadedDebug.")
96   endif()
98   string(REGEX REPLACE "(/|\\\\)$" "" LLVM_INTEGRATED_CRT_ALLOC "${LLVM_INTEGRATED_CRT_ALLOC}")
100   if(NOT EXISTS "${LLVM_INTEGRATED_CRT_ALLOC}")
101     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.")
102   endif()
104   if((LLVM_INTEGRATED_CRT_ALLOC MATCHES "rpmalloc$") OR LLVM_ENABLE_RPMALLOC)
105     add_compile_definitions(ENABLE_OVERRIDE ENABLE_PRELOAD)
106     set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/rpmalloc/rpmalloc.c")
107     set(delayload_flags "${delayload_flags} -INCLUDE:malloc")
108   elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "snmalloc$")
109     set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/src/snmalloc/override/new.cc")
110     set(system_libs ${system_libs} "mincore.lib" "-INCLUDE:malloc")
111   elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "mimalloc$")
112     set(MIMALLOC_LIB "${LLVM_INTEGRATED_CRT_ALLOC}/out/msvc-x64/Release/mimalloc-static.lib")
113     if(NOT EXISTS "${MIMALLOC_LIB}")
114           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")
115     endif()
116     set(system_libs ${system_libs} "${MIMALLOC_LIB}" "-INCLUDE:malloc")
117   endif()
118 endif()
120 # FIXME: We are currently guarding AIX headers with _XOPEN_SOURCE=700.
121 # See llvm/CMakeLists.txt. However, we need _SC_NPROCESSORS_ONLN in
122 # unistd.h and it is guarded by _ALL_SOURCE, so we remove the _XOPEN_SOURCE
123 # guard here. We should remove the guards all together once AIX cleans up
124 # the system headers.
125 if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
126   remove_definitions("-D_XOPEN_SOURCE=700")
127 endif()
129 add_subdirectory(BLAKE3)
131 add_llvm_component_library(LLVMSupport
132   ABIBreak.cpp
133   AMDGPUMetadata.cpp
134   APFixedPoint.cpp
135   APFloat.cpp
136   APInt.cpp
137   APSInt.cpp
138   ARMBuildAttrs.cpp
139   ARMAttributeParser.cpp
140   ARMWinEH.cpp
141   Allocator.cpp
142   AutoConvert.cpp
143   Base64.cpp
144   BalancedPartitioning.cpp
145   BinaryStreamError.cpp
146   BinaryStreamReader.cpp
147   BinaryStreamRef.cpp
148   BinaryStreamWriter.cpp
149   BlockFrequency.cpp
150   BranchProbability.cpp
151   BuryPointer.cpp
152   CachePruning.cpp
153   Caching.cpp
154   circular_raw_ostream.cpp
155   Chrono.cpp
156   COM.cpp
157   CodeGenCoverage.cpp
158   CommandLine.cpp
159   Compression.cpp
160   CRC.cpp
161   ConvertUTF.cpp
162   ConvertEBCDIC.cpp
163   ConvertUTFWrapper.cpp
164   CrashRecoveryContext.cpp
165   CSKYAttributes.cpp
166   CSKYAttributeParser.cpp
167   DataExtractor.cpp
168   Debug.cpp
169   DebugCounter.cpp
170   DeltaAlgorithm.cpp
171   DivisionByConstantInfo.cpp
172   DAGDeltaAlgorithm.cpp
173   DJB.cpp
174   DynamicAPInt.cpp
175   ELFAttributeParser.cpp
176   ELFAttributes.cpp
177   Error.cpp
178   ErrorHandling.cpp
179   ExponentialBackoff.cpp
180   ExtensibleRTTI.cpp
181   FileCollector.cpp
182   FileUtilities.cpp
183   FileOutputBuffer.cpp
184   FloatingPointMode.cpp
185   FoldingSet.cpp
186   FormattedStream.cpp
187   FormatVariadic.cpp
188   GlobPattern.cpp
189   GraphWriter.cpp
190   HexagonAttributeParser.cpp
191   HexagonAttributes.cpp
192   InitLLVM.cpp
193   InstructionCost.cpp
194   IntEqClasses.cpp
195   IntervalMap.cpp
196   JSON.cpp
197   KnownBits.cpp
198   LEB128.cpp
199   LineIterator.cpp
200   Locale.cpp
201   LockFileManager.cpp
202   ManagedStatic.cpp
203   MathExtras.cpp
204   MemAlloc.cpp
205   MemoryBuffer.cpp
206   MemoryBufferRef.cpp
207   MD5.cpp
208   MSP430Attributes.cpp
209   MSP430AttributeParser.cpp
210   NativeFormatting.cpp
211   OptimizedStructLayout.cpp
212   Optional.cpp
213   PGOOptions.cpp
214   Parallel.cpp
215   PluginLoader.cpp
216   PrettyStackTrace.cpp
217   RandomNumberGenerator.cpp
218   Regex.cpp
219   RISCVAttributes.cpp
220   RISCVAttributeParser.cpp
221   RISCVISAUtils.cpp
222   ScaledNumber.cpp
223   ScopedPrinter.cpp
224   SHA1.cpp
225   SHA256.cpp
226   Signposts.cpp
227   SipHash.cpp
228   SlowDynamicAPInt.cpp
229   SmallPtrSet.cpp
230   SmallVector.cpp
231   SourceMgr.cpp
232   SpecialCaseList.cpp
233   Statistic.cpp
234   StringExtras.cpp
235   StringMap.cpp
236   StringSaver.cpp
237   StringRef.cpp
238   SuffixTreeNode.cpp
239   SuffixTree.cpp
240   SystemUtils.cpp
241   TarWriter.cpp
242   ThreadPool.cpp
243   TimeProfiler.cpp
244   Timer.cpp
245   ToolOutputFile.cpp
246   Twine.cpp
247   TypeSize.cpp
248   Unicode.cpp
249   UnicodeCaseFold.cpp
250   UnicodeNameToCodepoint.cpp
251   UnicodeNameToCodepointGenerated.cpp
252   VersionTuple.cpp
253   VirtualFileSystem.cpp
254   WithColor.cpp
255   YAMLParser.cpp
256   YAMLTraits.cpp
257   raw_os_ostream.cpp
258   raw_ostream.cpp
259   raw_socket_stream.cpp
260   regcomp.c
261   regerror.c
262   regexec.c
263   regfree.c
264   regstrlcpy.c
265   xxhash.cpp
266   Z3Solver.cpp
268   ${ALLOCATOR_FILES}
269   $<TARGET_OBJECTS:LLVMSupportBlake3>
271 # System
272   Atomic.cpp
273   DynamicLibrary.cpp
274   Errno.cpp
275   Memory.cpp
276   Path.cpp
277   Process.cpp
278   Program.cpp
279   RWMutex.cpp
280   Signals.cpp
281   Threading.cpp
282   Valgrind.cpp
283   Watchdog.cpp
285   ADDITIONAL_HEADER_DIRS
286   Unix
287   Windows
288   ${LLVM_MAIN_INCLUDE_DIR}/llvm/ADT
289   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Support
290   ${Backtrace_INCLUDE_DIRS}
292   LINK_LIBS
293   ${system_libs} ${imported_libs} ${delayload_flags}
295   LINK_COMPONENTS
296   Demangle
297   )
299 set(llvm_system_libs ${system_libs})
301 # This block is only needed for llvm-config. When we deprecate llvm-config and
302 # move to using CMake export, this block can be removed.
303 if(LLVM_ENABLE_ZLIB)
304   # CMAKE_BUILD_TYPE is only meaningful to single-configuration generators.
305   if(CMAKE_BUILD_TYPE)
306     string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
307     get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION_${build_type})
308   endif()
309   if(NOT zlib_library)
310     get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION)
311   endif()
312   get_library_name(${zlib_library} zlib_library)
313   set(llvm_system_libs ${llvm_system_libs} "${zlib_library}")
314 endif()
316 if(LLVM_ENABLE_ZSTD)
317   # CMAKE_BUILD_TYPE is only meaningful to single-configuration generators.
318   if(CMAKE_BUILD_TYPE)
319     string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
320     get_property(zstd_library TARGET ${zstd_target} PROPERTY LOCATION_${build_type})
321   endif()
322   if(NOT zstd_library)
323     get_property(zstd_library TARGET ${zstd_target} PROPERTY LOCATION)
324   endif()
325   if (zstd_target STREQUAL zstd::libzstd_shared)
326     get_library_name(${zstd_library} zstd_library)
327     set(llvm_system_libs ${llvm_system_libs} "${zstd_library}")
328   else()
329     set(llvm_system_libs ${llvm_system_libs} "${zstd_STATIC_LIBRARY}")
330   endif()
331 endif()
333 set_property(TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS "${llvm_system_libs}")
336 if(LLVM_INTEGRATED_CRT_ALLOC)
337   if(LLVM_INTEGRATED_CRT_ALLOC MATCHES "snmalloc$")
338     set_property(TARGET LLVMSupport PROPERTY CXX_STANDARD 17)
339     add_compile_definitions(_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING)
340     if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND
341         "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "x86_64")
342       set_property(TARGET LLVMSupport PROPERTY COMPILE_FLAGS "-mcx16")
343     endif()
344   endif()
345 endif()
347 if(LLVM_WITH_Z3)
348   target_include_directories(LLVMSupport SYSTEM
349     PRIVATE
350     ${Z3_INCLUDE_DIR}
351     )
352 endif()