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