[CMake] Do all availability checks with -D_GNU_SOURCE (#116640)
[llvm-project.git] / llvm / cmake / config-ix.cmake
blob64878d28d9e1e5e97dafd7a26df8d78c2e7d893c
1 if( WIN32 AND NOT CYGWIN )
2   # We consider Cygwin as another Unix
3   set(PURE_WINDOWS 1)
4 endif()
6 include(CheckIncludeFile)
7 include(CheckLibraryExists)
8 include(CheckSymbolExists)
9 include(CheckCXXSymbolExists)
10 include(CheckFunctionExists)
11 include(CheckStructHasMember)
12 include(CheckCCompilerFlag)
13 include(CheckCSourceCompiles)
14 include(CMakePushCheckState)
16 include(CheckCompilerVersion)
17 include(CheckProblematicConfigurations)
18 include(HandleLLVMStdlib)
20 if( UNIX AND NOT (APPLE OR BEOS OR HAIKU) )
21   # Used by check_symbol_exists:
22   list(APPEND CMAKE_REQUIRED_LIBRARIES "m")
23 endif()
24 # x86_64 FreeBSD 9.2 requires libcxxrt to be specified explicitly.
25 if( CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE" AND
26     CMAKE_SIZEOF_VOID_P EQUAL 8 )
27   list(APPEND CMAKE_REQUIRED_LIBRARIES "cxxrt")
28 endif()
30 # Do checks with _XOPEN_SOURCE and large-file API on AIX, because we will build
31 # with those too.
32 if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
33           list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_XOPEN_SOURCE=700")
34           list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_LARGE_FILE_API")
35 endif()
37 # Do checks with _FILE_OFFSET_BITS=64 on Solaris, because we will build
38 # with those too.
39 if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
40           list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_FILE_OFFSET_BITS=64")
41 endif()
43 # Newer POSIX functions aren't available without the appropriate defines.
44 # Usually those are set by the use of -std=gnuXX, but one can also use the
45 # newer functions with -std=c(++)XX, i.e. without the GNU language extensions.
46 # Keep this at the top to make sure we don't add _GNU_SOURCE dependent checks
47 # before adding it.
48 check_symbol_exists(__GLIBC__ stdio.h LLVM_USING_GLIBC)
49 if(LLVM_USING_GLIBC)
50   add_compile_definitions(_GNU_SOURCE)
51   list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
53   # enable 64bit off_t on 32bit systems using glibc
54   if(CMAKE_SIZEOF_VOID_P EQUAL 4)
55     add_compile_definitions(_FILE_OFFSET_BITS=64)
56     list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_FILE_OFFSET_BITS=64")
57   endif()
58 endif()
60 # include checks
61 check_include_file(dlfcn.h HAVE_DLFCN_H)
62 check_include_file(errno.h HAVE_ERRNO_H)
63 check_include_file(fcntl.h HAVE_FCNTL_H)
64 check_include_file(malloc/malloc.h HAVE_MALLOC_MALLOC_H)
65 if( NOT PURE_WINDOWS )
66   check_include_file(pthread.h HAVE_PTHREAD_H)
67 endif()
68 check_include_file(signal.h HAVE_SIGNAL_H)
69 check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
70 check_include_file(sys/mman.h HAVE_SYS_MMAN_H)
71 check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H)
72 check_include_file(sys/stat.h HAVE_SYS_STAT_H)
73 check_include_file(sys/time.h HAVE_SYS_TIME_H)
74 check_include_file(sysexits.h HAVE_SYSEXITS_H)
75 check_include_file(termios.h HAVE_TERMIOS_H)
76 check_include_file(unistd.h HAVE_UNISTD_H)
77 check_include_file(valgrind/valgrind.h HAVE_VALGRIND_VALGRIND_H)
78 check_include_file(fenv.h HAVE_FENV_H)
79 check_symbol_exists(FE_ALL_EXCEPT "fenv.h" HAVE_DECL_FE_ALL_EXCEPT)
80 check_symbol_exists(FE_INEXACT "fenv.h" HAVE_DECL_FE_INEXACT)
81 check_c_source_compiles("
82         #if __has_attribute(used)
83         #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
84         #else
85         #define LLVM_ATTRIBUTE_USED
86         #endif
87         LLVM_ATTRIBUTE_USED void *foo() {
88           return __builtin_thread_pointer();
89         }
90         int main(void) { return 0; }"
91         HAVE_BUILTIN_THREAD_POINTER)
93 check_include_file(mach/mach.h HAVE_MACH_MACH_H)
94 check_include_file(CrashReporterClient.h HAVE_CRASHREPORTERCLIENT_H)
95 if(APPLE)
96   check_c_source_compiles("
97      static const char *__crashreporter_info__ = 0;
98      asm(\".desc ___crashreporter_info__, 0x10\");
99      int main(void) { return 0; }"
100     HAVE_CRASHREPORTER_INFO)
101 endif()
103 if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
104   check_include_file(linux/magic.h HAVE_LINUX_MAGIC_H)
105   if(NOT HAVE_LINUX_MAGIC_H)
106     # older kernels use split files
107     check_include_file(linux/nfs_fs.h HAVE_LINUX_NFS_FS_H)
108     check_include_file(linux/smb.h HAVE_LINUX_SMB_H)
109   endif()
110 endif()
112 # library checks
113 if( NOT PURE_WINDOWS )
114   check_library_exists(pthread pthread_create "" HAVE_LIBPTHREAD)
115   if (HAVE_LIBPTHREAD)
116     check_library_exists(pthread pthread_rwlock_init "" HAVE_PTHREAD_RWLOCK_INIT)
117     check_library_exists(pthread pthread_mutex_lock "" HAVE_PTHREAD_MUTEX_LOCK)
118   else()
119     # this could be Android
120     check_library_exists(c pthread_create "" PTHREAD_IN_LIBC)
121     if (PTHREAD_IN_LIBC)
122       check_library_exists(c pthread_rwlock_init "" HAVE_PTHREAD_RWLOCK_INIT)
123       check_library_exists(c pthread_mutex_lock "" HAVE_PTHREAD_MUTEX_LOCK)
124     endif()
125   endif()
126   check_library_exists(dl dlopen "" HAVE_LIBDL)
127   check_library_exists(rt clock_gettime "" HAVE_LIBRT)
128 endif()
130 # Check for libpfm.
131 include(FindLibpfm)
133 if(HAVE_LIBPTHREAD)
134   # We want to find pthreads library and at the moment we do want to
135   # have it reported as '-l<lib>' instead of '-pthread'.
136   # TODO: switch to -pthread once the rest of the build system can deal with it.
137   set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
138   set(THREADS_HAVE_PTHREAD_ARG Off)
139   find_package(Threads REQUIRED)
140   set(LLVM_PTHREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
141 endif()
143 if(LLVM_ENABLE_ZLIB)
144   if(LLVM_ENABLE_ZLIB STREQUAL FORCE_ON)
145     find_package(ZLIB REQUIRED)
146   elseif(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
147     find_package(ZLIB)
148   endif()
149   if(ZLIB_FOUND)
150     # Check if zlib we found is usable; for example, we may have found a 32-bit
151     # library on a 64-bit system which would result in a link-time failure.
152     cmake_push_check_state()
153     list(APPEND CMAKE_REQUIRED_INCLUDES ${ZLIB_INCLUDE_DIRS})
154     list(APPEND CMAKE_REQUIRED_LIBRARIES ${ZLIB_LIBRARY})
155     check_symbol_exists(compress2 zlib.h HAVE_ZLIB)
156     cmake_pop_check_state()
157     if(LLVM_ENABLE_ZLIB STREQUAL FORCE_ON AND NOT HAVE_ZLIB)
158       message(FATAL_ERROR "Failed to configure zlib")
159     endif()
160   endif()
161   set(LLVM_ENABLE_ZLIB "${HAVE_ZLIB}")
162 else()
163   set(LLVM_ENABLE_ZLIB 0)
164 endif()
166 set(zstd_FOUND 0)
167 if(LLVM_ENABLE_ZSTD)
168   if(LLVM_ENABLE_ZSTD STREQUAL FORCE_ON)
169     find_package(zstd REQUIRED)
170     if(NOT zstd_FOUND)
171       message(FATAL_ERROR "Failed to configure zstd, but LLVM_ENABLE_ZSTD is FORCE_ON")
172     endif()
173   elseif(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
174     find_package(zstd QUIET)
175   endif()
176 endif()
177 set(LLVM_ENABLE_ZSTD ${zstd_FOUND})
179 if(LLVM_ENABLE_LIBXML2)
180   if(LLVM_ENABLE_LIBXML2 STREQUAL FORCE_ON)
181     find_package(LibXml2 REQUIRED)
182   elseif(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
183     find_package(LibXml2)
184   endif()
185   if(LibXml2_FOUND)
186     # Check if libxml2 we found is usable; for example, we may have found a 32-bit
187     # library on a 64-bit system which would result in a link-time failure.
188     cmake_push_check_state()
189     list(APPEND CMAKE_REQUIRED_INCLUDES ${LIBXML2_INCLUDE_DIRS})
190     list(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBXML2_LIBRARIES})
191     list(APPEND CMAKE_REQUIRED_DEFINITIONS ${LIBXML2_DEFINITIONS})
192     check_symbol_exists(xmlReadMemory libxml/xmlreader.h HAVE_LIBXML2)
193     cmake_pop_check_state()
194     if(LLVM_ENABLE_LIBXML2 STREQUAL FORCE_ON AND NOT HAVE_LIBXML2)
195       message(FATAL_ERROR "Failed to configure libxml2")
196     endif()
197   endif()
198   set(LLVM_ENABLE_LIBXML2 "${HAVE_LIBXML2}")
199 endif()
201 if(LLVM_ENABLE_CURL)
202   if(LLVM_ENABLE_CURL STREQUAL FORCE_ON)
203     find_package(CURL REQUIRED)
204   else()
205     find_package(CURL)
206   endif()
207   if(CURL_FOUND)
208     # Check if curl we found is usable; for example, we may have found a 32-bit
209     # library on a 64-bit system which would result in a link-time failure.
210     cmake_push_check_state()
211     list(APPEND CMAKE_REQUIRED_LIBRARIES CURL::libcurl)
212     check_symbol_exists(curl_easy_init curl/curl.h HAVE_CURL)
213     cmake_pop_check_state()
214     if(LLVM_ENABLE_CURL STREQUAL FORCE_ON AND NOT HAVE_CURL)
215       message(FATAL_ERROR "Failed to configure curl")
216     endif()
217   endif()
218   set(LLVM_ENABLE_CURL "${HAVE_CURL}")
219 endif()
221 if(LLVM_ENABLE_HTTPLIB)
222   if(LLVM_ENABLE_HTTPLIB STREQUAL FORCE_ON)
223     find_package(httplib REQUIRED)
224   else()
225     find_package(httplib)
226   endif()
227   if(HTTPLIB_FOUND)
228     # Check if the "httplib" we found is usable; for example there may be another
229     # library with the same name.
230     cmake_push_check_state()
231     list(APPEND CMAKE_REQUIRED_LIBRARIES ${HTTPLIB_LIBRARY})
232     check_cxx_symbol_exists(CPPHTTPLIB_HTTPLIB_H ${HTTPLIB_HEADER_PATH} HAVE_HTTPLIB)
233     cmake_pop_check_state()
234     if(LLVM_ENABLE_HTTPLIB STREQUAL FORCE_ON AND NOT HAVE_HTTPLIB)
235       message(FATAL_ERROR "Failed to configure cpp-httplib")
236     endif()
237   endif()
238   set(LLVM_ENABLE_HTTPLIB "${HAVE_HTTPLIB}")
239 endif()
241 # Don't look for these libraries if we're using MSan, since uninstrumented third
242 # party code may call MSan interceptors like strlen, leading to false positives.
243 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
244   # Don't look for these libraries on Windows.
245   if (NOT PURE_WINDOWS)
246     # Skip libedit if using ASan as it contains memory leaks.
247     if (LLVM_ENABLE_LIBEDIT AND NOT LLVM_USE_SANITIZER MATCHES ".*Address.*")
248       if(LLVM_ENABLE_LIBEDIT STREQUAL FORCE_ON)
249         find_package(LibEdit REQUIRED)
250       else()
251         find_package(LibEdit)
252       endif()
253       set(HAVE_LIBEDIT "${LibEdit_FOUND}")
254     else()
255       set(HAVE_LIBEDIT 0)
256     endif()
257   else()
258     set(HAVE_LIBEDIT 0)
259   endif()
260 else()
261   set(HAVE_LIBEDIT 0)
262 endif()
264 if(LLVM_HAS_LOGF128)
265   include(CheckCXXSymbolExists)
266   check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)
268   if(LLVM_HAS_LOGF128 STREQUAL FORCE_ON AND NOT HAS_LOGF128)
269     message(FATAL_ERROR "Failed to configure logf128")
270   endif()
272   set(LLVM_HAS_LOGF128 "${HAS_LOGF128}")
273 endif()
275 # function checks
276 check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
277 find_package(Backtrace)
278 set(HAVE_BACKTRACE ${Backtrace_FOUND})
279 set(BACKTRACE_HEADER ${Backtrace_HEADER})
281 # Prevent check_symbol_exists from using API that is not supported for a given
282 # deployment target.
283 check_c_compiler_flag("-Werror=unguarded-availability-new" "C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW")
284 if(C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW)
285   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability-new")
286 endif()
288 # Determine whether we can register EH tables.
289 check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
290 check_symbol_exists(__deregister_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_DEREGISTER_FRAME)
291 check_symbol_exists(__unw_add_dynamic_fde "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_UNW_ADD_DYNAMIC_FDE)
293 check_symbol_exists(_Unwind_Backtrace "unwind.h" HAVE__UNWIND_BACKTRACE)
294 check_symbol_exists(getpagesize unistd.h HAVE_GETPAGESIZE)
295 check_symbol_exists(sysconf unistd.h HAVE_SYSCONF)
296 check_symbol_exists(getrusage sys/resource.h HAVE_GETRUSAGE)
297 check_symbol_exists(setrlimit sys/resource.h HAVE_SETRLIMIT)
298 check_symbol_exists(isatty unistd.h HAVE_ISATTY)
299 check_symbol_exists(futimens sys/stat.h HAVE_FUTIMENS)
300 check_symbol_exists(futimes sys/time.h HAVE_FUTIMES)
301 # AddressSanitizer conflicts with lib/Support/Unix/Signals.inc
302 # Avoid sigaltstack on Apple platforms, where backtrace() cannot handle it
303 # (rdar://7089625) and _Unwind_Backtrace is unusable because it cannot unwind
304 # past the signal handler after an assertion failure (rdar://29866587).
305 if( HAVE_SIGNAL_H AND NOT LLVM_USE_SANITIZER MATCHES ".*Address.*" AND NOT APPLE )
306   check_symbol_exists(sigaltstack signal.h HAVE_SIGALTSTACK)
307 endif()
308 check_symbol_exists(mallctl malloc_np.h HAVE_MALLCTL)
309 check_symbol_exists(mallinfo malloc.h HAVE_MALLINFO)
310 check_symbol_exists(mallinfo2 malloc.h HAVE_MALLINFO2)
311 check_symbol_exists(malloc_zone_statistics malloc/malloc.h
312                     HAVE_MALLOC_ZONE_STATISTICS)
313 check_symbol_exists(getrlimit "sys/types.h;sys/time.h;sys/resource.h" HAVE_GETRLIMIT)
314 check_symbol_exists(posix_spawn spawn.h HAVE_POSIX_SPAWN)
315 check_symbol_exists(pread unistd.h HAVE_PREAD)
316 check_symbol_exists(sbrk unistd.h HAVE_SBRK)
317 check_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
318 check_symbol_exists(strerror_s string.h HAVE_DECL_STRERROR_S)
319 check_symbol_exists(setenv stdlib.h HAVE_SETENV)
320 if( PURE_WINDOWS )
321   check_symbol_exists(_chsize_s io.h HAVE__CHSIZE_S)
323   check_function_exists(_alloca HAVE__ALLOCA)
324   check_function_exists(__alloca HAVE___ALLOCA)
325   check_function_exists(__chkstk HAVE___CHKSTK)
326   check_function_exists(__chkstk_ms HAVE___CHKSTK_MS)
327   check_function_exists(___chkstk HAVE____CHKSTK)
328   check_function_exists(___chkstk_ms HAVE____CHKSTK_MS)
330   check_function_exists(__ashldi3 HAVE___ASHLDI3)
331   check_function_exists(__ashrdi3 HAVE___ASHRDI3)
332   check_function_exists(__divdi3 HAVE___DIVDI3)
333   check_function_exists(__fixdfdi HAVE___FIXDFDI)
334   check_function_exists(__fixsfdi HAVE___FIXSFDI)
335   check_function_exists(__floatdidf HAVE___FLOATDIDF)
336   check_function_exists(__lshrdi3 HAVE___LSHRDI3)
337   check_function_exists(__moddi3 HAVE___MODDI3)
338   check_function_exists(__udivdi3 HAVE___UDIVDI3)
339   check_function_exists(__umoddi3 HAVE___UMODDI3)
341   check_function_exists(__main HAVE___MAIN)
342   check_function_exists(__cmpdi2 HAVE___CMPDI2)
343 endif()
345 CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtimespec.tv_nsec
346     "sys/types.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
347 if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
348 # The st_mtim.tv_nsec member of a `stat` structure is not reliable on some AIX
349 # environments.
350   set(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC 0)
351 else()
352   CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec
353       "sys/types.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
354 endif()
356 # This check requires _GNU_SOURCE.
357 if (NOT PURE_WINDOWS)
358   if (LLVM_PTHREAD_LIB)
359     list(APPEND CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
360   endif()
361   check_symbol_exists(pthread_getname_np pthread.h HAVE_PTHREAD_GETNAME_NP)
362   check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
363   check_symbol_exists(pthread_get_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_GET_NAME_NP)
364   check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SET_NAME_NP)
365   if (LLVM_PTHREAD_LIB)
366     list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
367   endif()
368 endif()
370 # This check requires _GNU_SOURCE.
371 if( HAVE_DLFCN_H )
372   if( HAVE_LIBDL )
373     list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
374   endif()
375   check_symbol_exists(dlopen dlfcn.h HAVE_DLOPEN)
376   check_symbol_exists(dladdr dlfcn.h HAVE_DLADDR)
377   if( HAVE_LIBDL )
378     list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
379   endif()
380 endif()
382 # available programs checks
383 function(llvm_find_program name)
384   string(TOUPPER ${name} NAME)
385   string(REGEX REPLACE "\\." "_" NAME ${NAME})
387   find_program(LLVM_PATH_${NAME} NAMES ${ARGV})
388   mark_as_advanced(LLVM_PATH_${NAME})
389   if(LLVM_PATH_${NAME})
390     set(HAVE_${NAME} 1 CACHE INTERNAL "Is ${name} available ?")
391     mark_as_advanced(HAVE_${NAME})
392   else(LLVM_PATH_${NAME})
393     set(HAVE_${NAME} "" CACHE INTERNAL "Is ${name} available ?")
394   endif(LLVM_PATH_${NAME})
395 endfunction()
397 if (LLVM_ENABLE_DOXYGEN)
398   llvm_find_program(dot)
399 endif ()
401 if(LLVM_ENABLE_FFI)
402   set(FFI_REQUIRE_INCLUDE On)
403   if(LLVM_ENABLE_FFI STREQUAL FORCE_ON)
404     find_package(FFI REQUIRED)
405   else()
406     find_package(FFI)
407   endif()
408   set(LLVM_ENABLE_FFI "${FFI_FOUND}")
409 else()
410   unset(HAVE_FFI_FFI_H CACHE)
411   unset(HAVE_FFI_H CACHE)
412   unset(HAVE_FFI_CALL CACHE)
413 endif()
415 check_symbol_exists(proc_pid_rusage "libproc.h" HAVE_PROC_PID_RUSAGE)
417 # Define LLVM_HAS_ATOMICS if gcc or MSVC atomic builtins are supported.
418 include(CheckAtomic)
420 if( LLVM_ENABLE_PIC )
421   set(ENABLE_PIC 1)
422 else()
423   set(ENABLE_PIC 0)
424   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-pie")
425 endif()
427 set(SUPPORTS_VARIADIC_MACROS_FLAG 0)
428 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
429   set(SUPPORTS_VARIADIC_MACROS_FLAG 1)
430 endif()
431 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
432   set(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG 1)
433 else()
434   set(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG 0)
435 endif()
437 set(USE_NO_MAYBE_UNINITIALIZED 0)
438 set(USE_NO_UNINITIALIZED 0)
440 # Disable gcc's potentially uninitialized use analysis as it presents lots of
441 # false positives.
442 if (CMAKE_COMPILER_IS_GNUCXX)
443   # Disable all -Wuninitialized warning for old GCC versions.
444   if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12.0)
445     set(USE_NO_UNINITIALIZED 1)
446   else()
447     set(USE_NO_MAYBE_UNINITIALIZED 1)
448   endif()
449 endif()
451 if(LLVM_INCLUDE_TESTS)
452   include(GetErrcMessages)
453   get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
454 endif()
456 # By default, we target the host, but this can be overridden at CMake
457 # invocation time.
458 include(GetHostTriple)
459 get_host_triple(LLVM_INFERRED_HOST_TRIPLE)
461 set(LLVM_HOST_TRIPLE "${LLVM_INFERRED_HOST_TRIPLE}" CACHE STRING
462     "Host on which LLVM binaries will run")
463 message(STATUS "LLVM host triple: ${LLVM_HOST_TRIPLE}")
465 # Determine the native architecture.
466 string(TOLOWER "${LLVM_TARGET_ARCH}" LLVM_NATIVE_ARCH)
467 if( LLVM_NATIVE_ARCH STREQUAL "host" )
468   string(REGEX MATCH "^[^-]*" LLVM_NATIVE_ARCH ${LLVM_HOST_TRIPLE})
469 endif ()
471 if (LLVM_NATIVE_ARCH MATCHES "i[2-6]86")
472   set(LLVM_NATIVE_ARCH X86)
473 elseif (LLVM_NATIVE_ARCH STREQUAL "x86")
474   set(LLVM_NATIVE_ARCH X86)
475 elseif (LLVM_NATIVE_ARCH STREQUAL "amd64")
476   set(LLVM_NATIVE_ARCH X86)
477 elseif (LLVM_NATIVE_ARCH STREQUAL "x86_64")
478   set(LLVM_NATIVE_ARCH X86)
479 elseif (LLVM_NATIVE_ARCH MATCHES "sparc")
480   set(LLVM_NATIVE_ARCH Sparc)
481 elseif (LLVM_NATIVE_ARCH MATCHES "powerpc")
482   set(LLVM_NATIVE_ARCH PowerPC)
483 elseif (LLVM_NATIVE_ARCH MATCHES "ppc64le")
484   set(LLVM_NATIVE_ARCH PowerPC)
485 elseif (LLVM_NATIVE_ARCH MATCHES "aarch64")
486   set(LLVM_NATIVE_ARCH AArch64)
487 elseif (LLVM_NATIVE_ARCH MATCHES "arm64")
488   set(LLVM_NATIVE_ARCH AArch64)
489 elseif (LLVM_NATIVE_ARCH MATCHES "arm")
490   set(LLVM_NATIVE_ARCH ARM)
491 elseif (LLVM_NATIVE_ARCH MATCHES "avr")
492   set(LLVM_NATIVE_ARCH AVR)
493 elseif (LLVM_NATIVE_ARCH MATCHES "mips")
494   set(LLVM_NATIVE_ARCH Mips)
495 elseif (LLVM_NATIVE_ARCH MATCHES "xcore")
496   set(LLVM_NATIVE_ARCH XCore)
497 elseif (LLVM_NATIVE_ARCH MATCHES "msp430")
498   set(LLVM_NATIVE_ARCH MSP430)
499 elseif (LLVM_NATIVE_ARCH MATCHES "hexagon")
500   set(LLVM_NATIVE_ARCH Hexagon)
501 elseif (LLVM_NATIVE_ARCH MATCHES "s390x")
502   set(LLVM_NATIVE_ARCH SystemZ)
503 elseif (LLVM_NATIVE_ARCH MATCHES "wasm32")
504   set(LLVM_NATIVE_ARCH WebAssembly)
505 elseif (LLVM_NATIVE_ARCH MATCHES "wasm64")
506   set(LLVM_NATIVE_ARCH WebAssembly)
507 elseif (LLVM_NATIVE_ARCH MATCHES "riscv32")
508   set(LLVM_NATIVE_ARCH RISCV)
509 elseif (LLVM_NATIVE_ARCH MATCHES "riscv64")
510   set(LLVM_NATIVE_ARCH RISCV)
511 elseif (LLVM_NATIVE_ARCH STREQUAL "m68k")
512   set(LLVM_NATIVE_ARCH M68k)
513 elseif (LLVM_NATIVE_ARCH MATCHES "loongarch")
514   set(LLVM_NATIVE_ARCH LoongArch)
515 else ()
516   message(FATAL_ERROR "Unknown architecture ${LLVM_NATIVE_ARCH}")
517 endif ()
519 # If build targets includes "host" or "Native", then replace with native architecture.
520 foreach (NATIVE_KEYWORD host Native)
521   list(FIND LLVM_TARGETS_TO_BUILD ${NATIVE_KEYWORD} idx)
522   if( NOT idx LESS 0 )
523     list(REMOVE_AT LLVM_TARGETS_TO_BUILD ${idx})
524     list(APPEND LLVM_TARGETS_TO_BUILD ${LLVM_NATIVE_ARCH})
525     list(REMOVE_DUPLICATES LLVM_TARGETS_TO_BUILD)
526   endif()
527 endforeach()
529 if (NOT ${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
530   message(STATUS
531     "Native target ${LLVM_NATIVE_ARCH} is not selected; lli will not JIT code")
532 else ()
533   message(STATUS "Native target architecture is ${LLVM_NATIVE_ARCH}")
534   set(LLVM_NATIVE_TARGET LLVMInitialize${LLVM_NATIVE_ARCH}Target)
535   set(LLVM_NATIVE_TARGETINFO LLVMInitialize${LLVM_NATIVE_ARCH}TargetInfo)
536   set(LLVM_NATIVE_TARGETMC LLVMInitialize${LLVM_NATIVE_ARCH}TargetMC)
537   set(LLVM_NATIVE_ASMPRINTER LLVMInitialize${LLVM_NATIVE_ARCH}AsmPrinter)
539   # We don't have an ASM parser for all architectures yet.
540   if (EXISTS ${PROJECT_SOURCE_DIR}/lib/Target/${LLVM_NATIVE_ARCH}/AsmParser/CMakeLists.txt)
541     set(LLVM_NATIVE_ASMPARSER LLVMInitialize${LLVM_NATIVE_ARCH}AsmParser)
542   endif ()
544   # We don't have an disassembler for all architectures yet.
545   if (EXISTS ${PROJECT_SOURCE_DIR}/lib/Target/${LLVM_NATIVE_ARCH}/Disassembler/CMakeLists.txt)
546     set(LLVM_NATIVE_DISASSEMBLER LLVMInitialize${LLVM_NATIVE_ARCH}Disassembler)
547   endif ()
548 endif ()
550 if( MSVC )
551   set(SHLIBEXT ".lib")
552   set(stricmp "_stricmp")
553   set(strdup "_strdup")
555   # Allow setting clang-cl's /winsysroot flag.
556   set(LLVM_WINSYSROOT "" CACHE STRING
557     "If set, argument to clang-cl's /winsysroot")
559   if (LLVM_WINSYSROOT)
560     set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
561         "Path to the DIA SDK")
562   else()
563     set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
564         "Path to the DIA SDK")
565   endif()
567   # See if the DIA SDK is available and usable.
568   # Due to a bug in MSVC 2013's installation software, it is possible
569   # for MSVC 2013 to write the DIA SDK into the Visual Studio 2012
570   # install directory.  If this happens, the installation is corrupt
571   # and there's nothing we can do.  It happens with enough frequency
572   # though that we should handle it.  We do so by simply checking that
573   # the DIA SDK folder exists.  Should this happen you will need to
574   # uninstall VS 2012 and then re-install VS 2013.
575   if (IS_DIRECTORY "${MSVC_DIA_SDK_DIR}")
576     set(HAVE_DIA_SDK 1)
577   else()
578     set(HAVE_DIA_SDK 0)
579   endif()
581   option(LLVM_ENABLE_DIA_SDK "Use MSVC DIA SDK for debugging if available."
582                              ${HAVE_DIA_SDK})
584   if(LLVM_ENABLE_DIA_SDK AND NOT HAVE_DIA_SDK)
585     message(FATAL_ERROR "DIA SDK not found. If you have both VS 2012 and 2013 installed, you may need to uninstall the former and re-install the latter afterwards.")
586   endif()
587 else()
588   set(LLVM_ENABLE_DIA_SDK 0)
589 endif( MSVC )
591 if( LLVM_ENABLE_THREADS )
592   # Check if threading primitives aren't supported on this platform
593   if( NOT HAVE_PTHREAD_H AND NOT WIN32 )
594     set(LLVM_ENABLE_THREADS 0)
595   endif()
596 endif()
598 if( LLVM_ENABLE_THREADS )
599   message(STATUS "Threads enabled.")
600 else( LLVM_ENABLE_THREADS )
601   message(STATUS "Threads disabled.")
602 endif()
604 if (LLVM_ENABLE_DOXYGEN)
605   message(STATUS "Doxygen enabled.")
606   find_package(Doxygen REQUIRED)
608   if (DOXYGEN_FOUND)
609     # If we find doxygen and we want to enable doxygen by default create a
610     # global aggregate doxygen target for generating llvm and any/all
611     # subprojects doxygen documentation.
612     if (LLVM_BUILD_DOCS)
613       add_custom_target(doxygen ALL)
614     endif()
616     option(LLVM_DOXYGEN_EXTERNAL_SEARCH "Enable doxygen external search." OFF)
617     if (LLVM_DOXYGEN_EXTERNAL_SEARCH)
618       set(LLVM_DOXYGEN_SEARCHENGINE_URL "" CACHE STRING "URL to use for external search.")
619       set(LLVM_DOXYGEN_SEARCH_MAPPINGS "" CACHE STRING "Doxygen Search Mappings")
620     endif()
621   endif()
622 else()
623   message(STATUS "Doxygen disabled.")
624 endif()
626 find_program(GOLD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.gold ld.gold ${LLVM_DEFAULT_TARGET_TRIPLE}-ld ld DOC "The gold linker")
627 set(LLVM_BINUTILS_INCDIR "" CACHE PATH
628     "PATH to binutils/include containing plugin-api.h for gold plugin.")
630 if(CMAKE_GENERATOR MATCHES "Ninja")
631   execute_process(COMMAND ${CMAKE_MAKE_PROGRAM} --version
632     OUTPUT_VARIABLE NINJA_VERSION
633     OUTPUT_STRIP_TRAILING_WHITESPACE)
634   set(NINJA_VERSION ${NINJA_VERSION} CACHE STRING "Ninja version number" FORCE)
635   message(STATUS "Ninja version: ${NINJA_VERSION}")
636 endif()
638 if(CMAKE_GENERATOR MATCHES "Ninja" AND
639     NOT "${NINJA_VERSION}" VERSION_LESS "1.9.0" AND
640     CMAKE_HOST_APPLE AND CMAKE_HOST_SYSTEM_VERSION VERSION_GREATER "15.6.0")
641   set(LLVM_TOUCH_STATIC_LIBRARIES ON)
642 endif()
644 if(CMAKE_HOST_APPLE AND APPLE)
645   if(NOT LD64_EXECUTABLE)
646     if(NOT CMAKE_XCRUN)
647       find_program(CMAKE_XCRUN NAMES xcrun)
648     endif()
650     # First, check if there's ld-classic, which is ld64 in newer SDKs.
651     if(CMAKE_XCRUN)
652       execute_process(COMMAND ${CMAKE_XCRUN} -find ld-classic
653         OUTPUT_VARIABLE LD64_EXECUTABLE
654         OUTPUT_STRIP_TRAILING_WHITESPACE)
655     else()
656       find_program(LD64_EXECUTABLE NAMES ld-classic DOC "The ld64 linker")
657     endif()
659     # Otherwise look for ld directly.
660     if(NOT LD64_EXECUTABLE)
661         if(CMAKE_XCRUN)
662           execute_process(COMMAND ${CMAKE_XCRUN} -find ld
663             OUTPUT_VARIABLE LD64_EXECUTABLE
664             OUTPUT_STRIP_TRAILING_WHITESPACE)
665         else()
666           find_program(LD64_EXECUTABLE NAMES ld DOC "The ld64 linker")
667         endif()
668     endif()
669   endif()
671   if(LD64_EXECUTABLE)
672     set(LD64_EXECUTABLE ${LD64_EXECUTABLE} CACHE PATH "ld64 executable")
673     message(STATUS "Found ld64 - ${LD64_EXECUTABLE}")
674   endif()
675 endif()
677 # Keep the version requirements in sync with bindings/ocaml/README.txt.
678 set(LLVM_BINDINGS "")
679 include(FindOCaml)
680 include(AddOCaml)
681 if(WIN32 OR NOT LLVM_ENABLE_BINDINGS)
682   message(STATUS "OCaml bindings disabled.")
683 else()
684   find_package(OCaml)
685   if( NOT OCAML_FOUND )
686     message(STATUS "OCaml bindings disabled.")
687   else()
688     if( OCAML_VERSION VERSION_LESS "4.00.0" )
689       message(STATUS "OCaml bindings disabled, need OCaml >=4.00.0.")
690     else()
691       find_ocamlfind_package(ctypes VERSION 0.4 OPTIONAL)
692       if( HAVE_OCAML_CTYPES )
693         message(STATUS "OCaml bindings enabled.")
694         set(LLVM_BINDINGS "${LLVM_BINDINGS} ocaml")
696         set(LLVM_OCAML_INSTALL_PATH "${OCAML_STDLIB_PATH}" CACHE STRING
697             "Install directory for LLVM OCaml packages")
698       else()
699         message(STATUS "OCaml bindings disabled, need ctypes >=0.4.")
700       endif()
701     endif()
702   endif()
703 endif()
705 string(REPLACE " " ";" LLVM_BINDINGS_LIST "${LLVM_BINDINGS}")
707 function(find_python_module module)
708   string(REPLACE "." "_" module_name ${module})
709   string(TOUPPER ${module_name} module_upper)
710   set(FOUND_VAR PY_${module_upper}_FOUND)
711   if (DEFINED ${FOUND_VAR})
712     return()
713   endif()
715   execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import ${module}"
716     RESULT_VARIABLE status
717     ERROR_QUIET)
719   if(status)
720     set(${FOUND_VAR} OFF CACHE BOOL "Failed to find python module '${module}'")
721     message(STATUS "Could NOT find Python module ${module}")
722   else()
723   set(${FOUND_VAR} ON CACHE BOOL "Found python module '${module}'")
724     message(STATUS "Found Python module ${module}")
725   endif()
726 endfunction()
728 set (PYTHON_MODULES
729   pygments
730   # Some systems still don't have pygments.lexers.c_cpp which was introduced in
731   # version 2.0 in 2014...
732   pygments.lexers.c_cpp
733   yaml
734   )
735 foreach(module ${PYTHON_MODULES})
736   find_python_module(${module})
737 endforeach()
739 if(PY_PYGMENTS_FOUND AND PY_PYGMENTS_LEXERS_C_CPP_FOUND AND PY_YAML_FOUND)
740   set (LLVM_HAVE_OPT_VIEWER_MODULES 1)
741 else()
742   set (LLVM_HAVE_OPT_VIEWER_MODULES 0)
743 endif()
745 function(llvm_get_host_prefixes_and_suffixes)
746   # Not all platform files will set these variables (relying on them being
747   # implicitly empty if they're unset), so unset the variables before including
748   # the platform file, to prevent any values from the target system leaking.
749   unset(CMAKE_STATIC_LIBRARY_PREFIX)
750   unset(CMAKE_STATIC_LIBRARY_SUFFIX)
751   unset(CMAKE_SHARED_LIBRARY_PREFIX)
752   unset(CMAKE_SHARED_LIBRARY_SUFFIX)
753   unset(CMAKE_IMPORT_LIBRARY_PREFIX)
754   unset(CMAKE_IMPORT_LIBRARY_SUFFIX)
755   unset(CMAKE_EXECUTABLE_SUFFIX)
756   unset(CMAKE_LINK_LIBRARY_SUFFIX)
757   include(Platform/${CMAKE_HOST_SYSTEM_NAME} OPTIONAL RESULT_VARIABLE _includedFile)
758   if (_includedFile)
759     set(LLVM_HOST_STATIC_LIBRARY_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX} PARENT_SCOPE)
760     set(LLVM_HOST_STATIC_LIBRARY_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX} PARENT_SCOPE)
761     set(LLVM_HOST_SHARED_LIBRARY_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX} PARENT_SCOPE)
762     set(LLVM_HOST_SHARED_LIBRARY_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX} PARENT_SCOPE)
763     set(LLVM_HOST_IMPORT_LIBRARY_PREFIX ${CMAKE_IMPORT_LIBRARY_PREFIX} PARENT_SCOPE)
764     set(LLVM_HOST_IMPORT_LIBRARY_SUFFIX ${CMAKE_IMPORT_LIBRARY_SUFFIX} PARENT_SCOPE)
765     set(LLVM_HOST_EXECUTABLE_SUFFIX ${CMAKE_EXECUTABLE_SUFFIX} PARENT_SCOPE)
766     set(LLVM_HOST_LINK_LIBRARY_SUFFIX ${CMAKE_LINK_LIBRARY_SUFFIX} PARENT_SCOPE)
767   endif()
768 endfunction()
770 llvm_get_host_prefixes_and_suffixes()