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(CheckFunctionExists)
10include(CheckStructHasMember)
11include(CheckCCompilerFlag)
12include(CMakePushCheckState)
13
14include(CheckCompilerVersion)
15include(HandleLLVMStdlib)
16
17if( UNIX AND NOT (APPLE OR BEOS OR HAIKU) )
18  # Used by check_symbol_exists:
19  list(APPEND CMAKE_REQUIRED_LIBRARIES "m")
20endif()
21# x86_64 FreeBSD 9.2 requires libcxxrt to be specified explicitly.
22if( CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE" AND
23    CMAKE_SIZEOF_VOID_P EQUAL 8 )
24  list(APPEND CMAKE_REQUIRED_LIBRARIES "cxxrt")
25endif()
26
27# Do checks with _XOPEN_SOURCE and large-file API on AIX, because we will build
28# with those too.
29if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
30          list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_XOPEN_SOURCE=700")
31          list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_LARGE_FILE_API")
32endif()
33
34# Do checks with _FILE_OFFSET_BITS=64 on Solaris, because we will build
35# with those too.
36if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
37          list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_FILE_OFFSET_BITS=64")
38endif()
39
40# include checks
41check_include_file(dlfcn.h HAVE_DLFCN_H)
42check_include_file(errno.h HAVE_ERRNO_H)
43check_include_file(fcntl.h HAVE_FCNTL_H)
44check_include_file(link.h HAVE_LINK_H)
45check_include_file(malloc/malloc.h HAVE_MALLOC_MALLOC_H)
46if( NOT PURE_WINDOWS )
47  check_include_file(pthread.h HAVE_PTHREAD_H)
48endif()
49check_include_file(signal.h HAVE_SIGNAL_H)
50check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
51check_include_file(sys/mman.h HAVE_SYS_MMAN_H)
52check_include_file(sys/param.h HAVE_SYS_PARAM_H)
53check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H)
54check_include_file(sys/stat.h HAVE_SYS_STAT_H)
55check_include_file(sys/time.h HAVE_SYS_TIME_H)
56check_include_file(sys/types.h HAVE_SYS_TYPES_H)
57check_include_file(sysexits.h HAVE_SYSEXITS_H)
58check_include_file(termios.h HAVE_TERMIOS_H)
59check_include_file(unistd.h HAVE_UNISTD_H)
60check_include_file(valgrind/valgrind.h HAVE_VALGRIND_VALGRIND_H)
61check_include_file(fenv.h HAVE_FENV_H)
62check_symbol_exists(FE_ALL_EXCEPT "fenv.h" HAVE_DECL_FE_ALL_EXCEPT)
63check_symbol_exists(FE_INEXACT "fenv.h" HAVE_DECL_FE_INEXACT)
64
65check_include_file(mach/mach.h HAVE_MACH_MACH_H)
66check_include_file(histedit.h HAVE_HISTEDIT_H)
67check_include_file(CrashReporterClient.h HAVE_CRASHREPORTERCLIENT_H)
68if(APPLE)
69  include(CheckCSourceCompiles)
70  CHECK_C_SOURCE_COMPILES("
71     static const char *__crashreporter_info__ = 0;
72     asm(\".desc ___crashreporter_info__, 0x10\");
73     int main() { return 0; }"
74    HAVE_CRASHREPORTER_INFO)
75endif()
76
77if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
78  check_include_file(linux/magic.h HAVE_LINUX_MAGIC_H)
79  if(NOT HAVE_LINUX_MAGIC_H)
80    # older kernels use split files
81    check_include_file(linux/nfs_fs.h HAVE_LINUX_NFS_FS_H)
82    check_include_file(linux/smb.h HAVE_LINUX_SMB_H)
83  endif()
84endif()
85
86# library checks
87if( NOT PURE_WINDOWS )
88  check_library_exists(pthread pthread_create "" HAVE_LIBPTHREAD)
89  if (HAVE_LIBPTHREAD)
90    check_library_exists(pthread pthread_getspecific "" HAVE_PTHREAD_GETSPECIFIC)
91    check_library_exists(pthread pthread_rwlock_init "" HAVE_PTHREAD_RWLOCK_INIT)
92    check_library_exists(pthread pthread_mutex_lock "" HAVE_PTHREAD_MUTEX_LOCK)
93  else()
94    # this could be Android
95    check_library_exists(c pthread_create "" PTHREAD_IN_LIBC)
96    if (PTHREAD_IN_LIBC)
97      check_library_exists(c pthread_getspecific "" HAVE_PTHREAD_GETSPECIFIC)
98      check_library_exists(c pthread_rwlock_init "" HAVE_PTHREAD_RWLOCK_INIT)
99      check_library_exists(c pthread_mutex_lock "" HAVE_PTHREAD_MUTEX_LOCK)
100    endif()
101  endif()
102  check_library_exists(dl dlopen "" HAVE_LIBDL)
103  check_library_exists(rt clock_gettime "" HAVE_LIBRT)
104endif()
105
106# Check for libpfm.
107include(FindLibpfm)
108
109if(HAVE_LIBPTHREAD)
110  # We want to find pthreads library and at the moment we do want to
111  # have it reported as '-l<lib>' instead of '-pthread'.
112  # TODO: switch to -pthread once the rest of the build system can deal with it.
113  set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
114  set(THREADS_HAVE_PTHREAD_ARG Off)
115  find_package(Threads REQUIRED)
116  set(LLVM_PTHREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
117endif()
118
119if(LLVM_ENABLE_ZLIB)
120  if(LLVM_ENABLE_ZLIB STREQUAL FORCE_ON)
121    find_package(ZLIB REQUIRED)
122  elseif(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
123    find_package(ZLIB)
124  endif()
125  if(ZLIB_FOUND)
126    # Check if zlib we found is usable; for example, we may have found a 32-bit
127    # library on a 64-bit system which would result in a link-time failure.
128    cmake_push_check_state()
129    list(APPEND CMAKE_REQUIRED_INCLUDES ${ZLIB_INCLUDE_DIRS})
130    list(APPEND CMAKE_REQUIRED_LIBRARIES ${ZLIB_LIBRARY})
131    check_symbol_exists(compress2 zlib.h HAVE_ZLIB)
132    cmake_pop_check_state()
133    if(LLVM_ENABLE_ZLIB STREQUAL FORCE_ON AND NOT HAVE_ZLIB)
134      message(FATAL_ERROR "Failed to configure zlib")
135    endif()
136  endif()
137  set(LLVM_ENABLE_ZLIB "${HAVE_ZLIB}")
138endif()
139
140if(LLVM_ENABLE_LIBXML2)
141  if(LLVM_ENABLE_LIBXML2 STREQUAL FORCE_ON)
142    find_package(LibXml2 REQUIRED)
143  elseif(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
144    find_package(LibXml2)
145  endif()
146  if(LibXml2_FOUND)
147    # Check if libxml2 we found is usable; for example, we may have found a 32-bit
148    # library on a 64-bit system which would result in a link-time failure.
149    cmake_push_check_state()
150    list(APPEND CMAKE_REQUIRED_INCLUDES ${LIBXML2_INCLUDE_DIRS})
151    list(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBXML2_LIBRARIES})
152    check_symbol_exists(xmlReadMemory libxml/xmlreader.h HAVE_LIBXML2)
153    cmake_pop_check_state()
154    if(LLVM_ENABLE_LIBXML2 STREQUAL FORCE_ON AND NOT HAVE_LIBXML2)
155      message(FATAL_ERROR "Failed to configure libxml2")
156    endif()
157  endif()
158  set(LLVM_ENABLE_LIBXML2 "${HAVE_LIBXML2}")
159endif()
160
161# Don't look for these libraries if we're using MSan, since uninstrumented third
162# party code may call MSan interceptors like strlen, leading to false positives.
163if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
164  # Don't look for these libraries on Windows.
165  if (NOT PURE_WINDOWS)
166    # Skip libedit if using ASan as it contains memory leaks.
167    if (LLVM_ENABLE_LIBEDIT AND HAVE_HISTEDIT_H AND NOT LLVM_USE_SANITIZER MATCHES ".*Address.*")
168      check_library_exists(edit el_init "" HAVE_LIBEDIT)
169    else()
170      set(HAVE_LIBEDIT 0)
171    endif()
172    if(LLVM_ENABLE_TERMINFO STREQUAL FORCE_ON)
173      set(MAYBE_REQUIRED REQUIRED)
174    else()
175      set(MAYBE_REQUIRED)
176    endif()
177    if(LLVM_ENABLE_TERMINFO)
178      find_library(TERMINFO_LIB NAMES terminfo tinfo curses ncurses ncursesw ${MAYBE_REQUIRED})
179    endif()
180    if(TERMINFO_LIB)
181      set(LLVM_ENABLE_TERMINFO 1)
182    else()
183      set(LLVM_ENABLE_TERMINFO 0)
184    endif()
185  else()
186    set(LLVM_ENABLE_TERMINFO 0)
187  endif()
188else()
189  set(LLVM_ENABLE_TERMINFO 0)
190endif()
191
192check_library_exists(xar xar_open "" LLVM_HAVE_LIBXAR)
193if(LLVM_HAVE_LIBXAR)
194  set(XAR_LIB xar)
195endif()
196
197# function checks
198check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
199find_package(Backtrace)
200set(HAVE_BACKTRACE ${Backtrace_FOUND})
201set(BACKTRACE_HEADER ${Backtrace_HEADER})
202
203# Prevent check_symbol_exists from using API that is not supported for a given
204# deployment target.
205check_c_compiler_flag("-Werror=unguarded-availability-new" "C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW")
206if(C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW)
207  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability-new")
208endif()
209
210# Determine whether we can register EH tables.
211check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
212check_symbol_exists(__deregister_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_DEREGISTER_FRAME)
213
214check_symbol_exists(_Unwind_Backtrace "unwind.h" HAVE__UNWIND_BACKTRACE)
215check_symbol_exists(getpagesize unistd.h HAVE_GETPAGESIZE)
216check_symbol_exists(sysconf unistd.h HAVE_SYSCONF)
217check_symbol_exists(getrusage sys/resource.h HAVE_GETRUSAGE)
218check_symbol_exists(setrlimit sys/resource.h HAVE_SETRLIMIT)
219check_symbol_exists(isatty unistd.h HAVE_ISATTY)
220check_symbol_exists(futimens sys/stat.h HAVE_FUTIMENS)
221check_symbol_exists(futimes sys/time.h HAVE_FUTIMES)
222check_symbol_exists(posix_fallocate fcntl.h HAVE_POSIX_FALLOCATE)
223# AddressSanitizer conflicts with lib/Support/Unix/Signals.inc
224# Avoid sigaltstack on Apple platforms, where backtrace() cannot handle it
225# (rdar://7089625) and _Unwind_Backtrace is unusable because it cannot unwind
226# past the signal handler after an assertion failure (rdar://29866587).
227if( HAVE_SIGNAL_H AND NOT LLVM_USE_SANITIZER MATCHES ".*Address.*" AND NOT APPLE )
228  check_symbol_exists(sigaltstack signal.h HAVE_SIGALTSTACK)
229endif()
230set(CMAKE_REQUIRED_DEFINITIONS "-D_LARGEFILE64_SOURCE")
231check_symbol_exists(lseek64 "sys/types.h;unistd.h" HAVE_LSEEK64)
232set(CMAKE_REQUIRED_DEFINITIONS "")
233check_symbol_exists(mallctl malloc_np.h HAVE_MALLCTL)
234check_symbol_exists(mallinfo malloc.h HAVE_MALLINFO)
235check_symbol_exists(mallinfo2 malloc.h HAVE_MALLINFO2)
236check_symbol_exists(malloc_zone_statistics malloc/malloc.h
237                    HAVE_MALLOC_ZONE_STATISTICS)
238check_symbol_exists(getrlimit "sys/types.h;sys/time.h;sys/resource.h" HAVE_GETRLIMIT)
239check_symbol_exists(posix_spawn spawn.h HAVE_POSIX_SPAWN)
240check_symbol_exists(pread unistd.h HAVE_PREAD)
241check_symbol_exists(sbrk unistd.h HAVE_SBRK)
242check_symbol_exists(strerror string.h HAVE_STRERROR)
243check_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
244check_symbol_exists(strerror_s string.h HAVE_DECL_STRERROR_S)
245check_symbol_exists(setenv stdlib.h HAVE_SETENV)
246if( PURE_WINDOWS )
247  check_symbol_exists(_chsize_s io.h HAVE__CHSIZE_S)
248
249  check_function_exists(_alloca HAVE__ALLOCA)
250  check_function_exists(__alloca HAVE___ALLOCA)
251  check_function_exists(__chkstk HAVE___CHKSTK)
252  check_function_exists(__chkstk_ms HAVE___CHKSTK_MS)
253  check_function_exists(___chkstk HAVE____CHKSTK)
254  check_function_exists(___chkstk_ms HAVE____CHKSTK_MS)
255
256  check_function_exists(__ashldi3 HAVE___ASHLDI3)
257  check_function_exists(__ashrdi3 HAVE___ASHRDI3)
258  check_function_exists(__divdi3 HAVE___DIVDI3)
259  check_function_exists(__fixdfdi HAVE___FIXDFDI)
260  check_function_exists(__fixsfdi HAVE___FIXSFDI)
261  check_function_exists(__floatdidf HAVE___FLOATDIDF)
262  check_function_exists(__lshrdi3 HAVE___LSHRDI3)
263  check_function_exists(__moddi3 HAVE___MODDI3)
264  check_function_exists(__udivdi3 HAVE___UDIVDI3)
265  check_function_exists(__umoddi3 HAVE___UMODDI3)
266
267  check_function_exists(__main HAVE___MAIN)
268  check_function_exists(__cmpdi2 HAVE___CMPDI2)
269endif()
270if( HAVE_DLFCN_H )
271  if( HAVE_LIBDL )
272    list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
273  endif()
274  check_symbol_exists(dlopen dlfcn.h HAVE_DLOPEN)
275  check_symbol_exists(dladdr dlfcn.h HAVE_DLADDR)
276  if( HAVE_LIBDL )
277    list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
278  endif()
279endif()
280
281CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtimespec.tv_nsec
282    "sys/types.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
283if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
284# The st_mtim.tv_nsec member of a `stat` structure is not reliable on some AIX
285# environments.
286  set(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC 0)
287else()
288  CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec
289      "sys/types.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
290endif()
291
292check_symbol_exists(__GLIBC__ stdio.h LLVM_USING_GLIBC)
293if( LLVM_USING_GLIBC )
294  add_definitions( -D_GNU_SOURCE )
295  list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
296endif()
297# This check requires _GNU_SOURCE
298if (NOT PURE_WINDOWS)
299  if (LLVM_PTHREAD_LIB)
300    list(APPEND CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
301  endif()
302  check_symbol_exists(pthread_getname_np pthread.h HAVE_PTHREAD_GETNAME_NP)
303  check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
304  if (LLVM_PTHREAD_LIB)
305    list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
306  endif()
307endif()
308
309# available programs checks
310function(llvm_find_program name)
311  string(TOUPPER ${name} NAME)
312  string(REGEX REPLACE "\\." "_" NAME ${NAME})
313
314  find_program(LLVM_PATH_${NAME} NAMES ${ARGV})
315  mark_as_advanced(LLVM_PATH_${NAME})
316  if(LLVM_PATH_${NAME})
317    set(HAVE_${NAME} 1 CACHE INTERNAL "Is ${name} available ?")
318    mark_as_advanced(HAVE_${NAME})
319  else(LLVM_PATH_${NAME})
320    set(HAVE_${NAME} "" CACHE INTERNAL "Is ${name} available ?")
321  endif(LLVM_PATH_${NAME})
322endfunction()
323
324if (LLVM_ENABLE_DOXYGEN)
325  llvm_find_program(dot)
326endif ()
327
328if( LLVM_ENABLE_FFI )
329  find_path(FFI_INCLUDE_PATH ffi.h PATHS ${FFI_INCLUDE_DIR})
330  if( EXISTS "${FFI_INCLUDE_PATH}/ffi.h" )
331    set(FFI_HEADER ffi.h CACHE INTERNAL "")
332    set(HAVE_FFI_H 1 CACHE INTERNAL "")
333  else()
334    find_path(FFI_INCLUDE_PATH ffi/ffi.h PATHS ${FFI_INCLUDE_DIR})
335    if( EXISTS "${FFI_INCLUDE_PATH}/ffi/ffi.h" )
336      set(FFI_HEADER ffi/ffi.h CACHE INTERNAL "")
337      set(HAVE_FFI_FFI_H 1 CACHE INTERNAL "")
338    endif()
339  endif()
340
341  if( NOT FFI_HEADER )
342    message(FATAL_ERROR "libffi includes are not found.")
343  endif()
344
345  find_library(FFI_LIBRARY_PATH ffi PATHS ${FFI_LIBRARY_DIR})
346  if( NOT FFI_LIBRARY_PATH )
347    message(FATAL_ERROR "libffi is not found.")
348  endif()
349
350  list(APPEND CMAKE_REQUIRED_LIBRARIES ${FFI_LIBRARY_PATH})
351  list(APPEND CMAKE_REQUIRED_INCLUDES ${FFI_INCLUDE_PATH})
352  check_symbol_exists(ffi_call ${FFI_HEADER} HAVE_FFI_CALL)
353  list(REMOVE_ITEM CMAKE_REQUIRED_INCLUDES ${FFI_INCLUDE_PATH})
354  list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${FFI_LIBRARY_PATH})
355else()
356  unset(HAVE_FFI_FFI_H CACHE)
357  unset(HAVE_FFI_H CACHE)
358  unset(HAVE_FFI_CALL CACHE)
359endif( LLVM_ENABLE_FFI )
360
361check_symbol_exists(proc_pid_rusage "libproc.h" HAVE_PROC_PID_RUSAGE)
362
363# Whether we can use std::is_trivially_copyable to verify llvm::is_trivially_copyable.
364CHECK_CXX_SOURCE_COMPILES("
365#include <type_traits>
366struct T { int val; };
367static_assert(std::is_trivially_copyable<T>::value, \"ok\");
368int main() { return 0;}
369" HAVE_STD_IS_TRIVIALLY_COPYABLE)
370
371
372# Define LLVM_HAS_ATOMICS if gcc or MSVC atomic builtins are supported.
373include(CheckAtomic)
374
375if( LLVM_ENABLE_PIC )
376  set(ENABLE_PIC 1)
377else()
378  set(ENABLE_PIC 0)
379  check_cxx_compiler_flag("-fno-pie" SUPPORTS_NO_PIE_FLAG)
380  if(SUPPORTS_NO_PIE_FLAG)
381    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-pie")
382  endif()
383endif()
384
385check_cxx_compiler_flag("-Wvariadic-macros" SUPPORTS_VARIADIC_MACROS_FLAG)
386check_cxx_compiler_flag("-Wgnu-zero-variadic-macro-arguments"
387                        SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
388
389set(USE_NO_MAYBE_UNINITIALIZED 0)
390set(USE_NO_UNINITIALIZED 0)
391
392# Disable gcc's potentially uninitialized use analysis as it presents lots of
393# false positives.
394if (CMAKE_COMPILER_IS_GNUCXX)
395  check_cxx_compiler_flag("-Wmaybe-uninitialized" HAS_MAYBE_UNINITIALIZED)
396  if (HAS_MAYBE_UNINITIALIZED)
397    set(USE_NO_MAYBE_UNINITIALIZED 1)
398  else()
399    # Only recent versions of gcc make the distinction between -Wuninitialized
400    # and -Wmaybe-uninitialized. If -Wmaybe-uninitialized isn't supported, just
401    # turn off all uninitialized use warnings.
402    check_cxx_compiler_flag("-Wuninitialized" HAS_UNINITIALIZED)
403    set(USE_NO_UNINITIALIZED ${HAS_UNINITIALIZED})
404  endif()
405endif()
406
407# By default, we target the host, but this can be overridden at CMake
408# invocation time.
409include(GetHostTriple)
410get_host_triple(LLVM_INFERRED_HOST_TRIPLE)
411
412set(LLVM_HOST_TRIPLE "${LLVM_INFERRED_HOST_TRIPLE}" CACHE STRING
413    "Host on which LLVM binaries will run")
414
415# Determine the native architecture.
416string(TOLOWER "${LLVM_TARGET_ARCH}" LLVM_NATIVE_ARCH)
417if( LLVM_NATIVE_ARCH STREQUAL "host" )
418  string(REGEX MATCH "^[^-]*" LLVM_NATIVE_ARCH ${LLVM_HOST_TRIPLE})
419endif ()
420
421if (LLVM_NATIVE_ARCH MATCHES "i[2-6]86")
422  set(LLVM_NATIVE_ARCH X86)
423elseif (LLVM_NATIVE_ARCH STREQUAL "x86")
424  set(LLVM_NATIVE_ARCH X86)
425elseif (LLVM_NATIVE_ARCH STREQUAL "amd64")
426  set(LLVM_NATIVE_ARCH X86)
427elseif (LLVM_NATIVE_ARCH STREQUAL "x86_64")
428  set(LLVM_NATIVE_ARCH X86)
429elseif (LLVM_NATIVE_ARCH MATCHES "sparc")
430  set(LLVM_NATIVE_ARCH Sparc)
431elseif (LLVM_NATIVE_ARCH MATCHES "powerpc")
432  set(LLVM_NATIVE_ARCH PowerPC)
433elseif (LLVM_NATIVE_ARCH MATCHES "ppc64le")
434  set(LLVM_NATIVE_ARCH PowerPC)
435elseif (LLVM_NATIVE_ARCH MATCHES "aarch64")
436  set(LLVM_NATIVE_ARCH AArch64)
437elseif (LLVM_NATIVE_ARCH MATCHES "arm64")
438  set(LLVM_NATIVE_ARCH AArch64)
439elseif (LLVM_NATIVE_ARCH MATCHES "arm")
440  set(LLVM_NATIVE_ARCH ARM)
441elseif (LLVM_NATIVE_ARCH MATCHES "avr")
442  set(LLVM_NATIVE_ARCH AVR)
443elseif (LLVM_NATIVE_ARCH MATCHES "mips")
444  set(LLVM_NATIVE_ARCH Mips)
445elseif (LLVM_NATIVE_ARCH MATCHES "xcore")
446  set(LLVM_NATIVE_ARCH XCore)
447elseif (LLVM_NATIVE_ARCH MATCHES "msp430")
448  set(LLVM_NATIVE_ARCH MSP430)
449elseif (LLVM_NATIVE_ARCH MATCHES "hexagon")
450  set(LLVM_NATIVE_ARCH Hexagon)
451elseif (LLVM_NATIVE_ARCH MATCHES "s390x")
452  set(LLVM_NATIVE_ARCH SystemZ)
453elseif (LLVM_NATIVE_ARCH MATCHES "wasm32")
454  set(LLVM_NATIVE_ARCH WebAssembly)
455elseif (LLVM_NATIVE_ARCH MATCHES "wasm64")
456  set(LLVM_NATIVE_ARCH WebAssembly)
457elseif (LLVM_NATIVE_ARCH MATCHES "riscv32")
458  set(LLVM_NATIVE_ARCH RISCV)
459elseif (LLVM_NATIVE_ARCH MATCHES "riscv64")
460  set(LLVM_NATIVE_ARCH RISCV)
461elseif (LLVM_NATIVE_ARCH STREQUAL "m68k")
462  set(LLVM_NATIVE_ARCH M68k)
463else ()
464  message(FATAL_ERROR "Unknown architecture ${LLVM_NATIVE_ARCH}")
465endif ()
466
467# If build targets includes "host" or "Native", then replace with native architecture.
468foreach (NATIVE_KEYWORD host Native)
469  list(FIND LLVM_TARGETS_TO_BUILD ${NATIVE_KEYWORD} idx)
470  if( NOT idx LESS 0 )
471    list(REMOVE_AT LLVM_TARGETS_TO_BUILD ${idx})
472    list(APPEND LLVM_TARGETS_TO_BUILD ${LLVM_NATIVE_ARCH})
473    list(REMOVE_DUPLICATES LLVM_TARGETS_TO_BUILD)
474  endif()
475endforeach()
476
477list(FIND LLVM_TARGETS_TO_BUILD ${LLVM_NATIVE_ARCH} NATIVE_ARCH_IDX)
478if (NATIVE_ARCH_IDX EQUAL -1)
479  message(STATUS
480    "Native target ${LLVM_NATIVE_ARCH} is not selected; lli will not JIT code")
481else ()
482  message(STATUS "Native target architecture is ${LLVM_NATIVE_ARCH}")
483  set(LLVM_NATIVE_TARGET LLVMInitialize${LLVM_NATIVE_ARCH}Target)
484  set(LLVM_NATIVE_TARGETINFO LLVMInitialize${LLVM_NATIVE_ARCH}TargetInfo)
485  set(LLVM_NATIVE_TARGETMC LLVMInitialize${LLVM_NATIVE_ARCH}TargetMC)
486  set(LLVM_NATIVE_ASMPRINTER LLVMInitialize${LLVM_NATIVE_ARCH}AsmPrinter)
487
488  # We don't have an ASM parser for all architectures yet.
489  if (EXISTS ${PROJECT_SOURCE_DIR}/lib/Target/${LLVM_NATIVE_ARCH}/AsmParser/CMakeLists.txt)
490    set(LLVM_NATIVE_ASMPARSER LLVMInitialize${LLVM_NATIVE_ARCH}AsmParser)
491  endif ()
492
493  # We don't have an disassembler for all architectures yet.
494  if (EXISTS ${PROJECT_SOURCE_DIR}/lib/Target/${LLVM_NATIVE_ARCH}/Disassembler/CMakeLists.txt)
495    set(LLVM_NATIVE_DISASSEMBLER LLVMInitialize${LLVM_NATIVE_ARCH}Disassembler)
496  endif ()
497endif ()
498
499if( MSVC )
500  set(SHLIBEXT ".lib")
501  set(stricmp "_stricmp")
502  set(strdup "_strdup")
503
504  # See if the DIA SDK is available and usable.
505  set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
506      "Path to the DIA SDK")
507
508  # Due to a bug in MSVC 2013's installation software, it is possible
509  # for MSVC 2013 to write the DIA SDK into the Visual Studio 2012
510  # install directory.  If this happens, the installation is corrupt
511  # and there's nothing we can do.  It happens with enough frequency
512  # though that we should handle it.  We do so by simply checking that
513  # the DIA SDK folder exists.  Should this happen you will need to
514  # uninstall VS 2012 and then re-install VS 2013.
515  if (IS_DIRECTORY "${MSVC_DIA_SDK_DIR}")
516    set(HAVE_DIA_SDK 1)
517  else()
518    set(HAVE_DIA_SDK 0)
519  endif()
520
521  option(LLVM_ENABLE_DIA_SDK "Use MSVC DIA SDK for debugging if available."
522                             ${HAVE_DIA_SDK})
523
524  if(LLVM_ENABLE_DIA_SDK AND NOT HAVE_DIA_SDK)
525    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.")
526  endif()
527else()
528  set(LLVM_ENABLE_DIA_SDK 0)
529endif( MSVC )
530
531# FIXME: Signal handler return type, currently hardcoded to 'void'
532set(RETSIGTYPE void)
533
534if( LLVM_ENABLE_THREADS )
535  # Check if threading primitives aren't supported on this platform
536  if( NOT HAVE_PTHREAD_H AND NOT WIN32 )
537    set(LLVM_ENABLE_THREADS 0)
538  endif()
539endif()
540
541if( LLVM_ENABLE_THREADS )
542  message(STATUS "Threads enabled.")
543else( LLVM_ENABLE_THREADS )
544  message(STATUS "Threads disabled.")
545endif()
546
547if (LLVM_ENABLE_DOXYGEN)
548  message(STATUS "Doxygen enabled.")
549  find_package(Doxygen REQUIRED)
550
551  if (DOXYGEN_FOUND)
552    # If we find doxygen and we want to enable doxygen by default create a
553    # global aggregate doxygen target for generating llvm and any/all
554    # subprojects doxygen documentation.
555    if (LLVM_BUILD_DOCS)
556      add_custom_target(doxygen ALL)
557    endif()
558
559    option(LLVM_DOXYGEN_EXTERNAL_SEARCH "Enable doxygen external search." OFF)
560    if (LLVM_DOXYGEN_EXTERNAL_SEARCH)
561      set(LLVM_DOXYGEN_SEARCHENGINE_URL "" CACHE STRING "URL to use for external search.")
562      set(LLVM_DOXYGEN_SEARCH_MAPPINGS "" CACHE STRING "Doxygen Search Mappings")
563    endif()
564  endif()
565else()
566  message(STATUS "Doxygen disabled.")
567endif()
568
569set(LLVM_BINDINGS "")
570find_program(GO_EXECUTABLE NAMES go DOC "go executable")
571if(WIN32 OR NOT LLVM_ENABLE_BINDINGS)
572  message(STATUS "Go bindings disabled.")
573else()
574  if(GO_EXECUTABLE STREQUAL "GO_EXECUTABLE-NOTFOUND")
575    message(STATUS "Go bindings disabled.")
576  else()
577    execute_process(COMMAND ${GO_EXECUTABLE} run ${PROJECT_SOURCE_DIR}/bindings/go/conftest.go
578                    RESULT_VARIABLE GO_CONFTEST)
579    if(GO_CONFTEST STREQUAL "0")
580      set(LLVM_BINDINGS "${LLVM_BINDINGS} go")
581      message(STATUS "Go bindings enabled.")
582    else()
583      message(STATUS "Go bindings disabled, need at least Go 1.2.")
584    endif()
585  endif()
586endif()
587
588find_program(GOLD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.gold ld.gold ${LLVM_DEFAULT_TARGET_TRIPLE}-ld ld DOC "The gold linker")
589set(LLVM_BINUTILS_INCDIR "" CACHE PATH
590	"PATH to binutils/include containing plugin-api.h for gold plugin.")
591
592if(CMAKE_GENERATOR STREQUAL "Ninja")
593  execute_process(COMMAND ${CMAKE_MAKE_PROGRAM} --version
594    OUTPUT_VARIABLE NINJA_VERSION
595    OUTPUT_STRIP_TRAILING_WHITESPACE)
596  set(NINJA_VERSION ${NINJA_VERSION} CACHE STRING "Ninja version number" FORCE)
597  message(STATUS "Ninja version: ${NINJA_VERSION}")
598endif()
599
600if(CMAKE_GENERATOR STREQUAL "Ninja" AND
601    NOT "${NINJA_VERSION}" VERSION_LESS "1.9.0" AND
602    CMAKE_HOST_APPLE AND CMAKE_HOST_SYSTEM_VERSION VERSION_GREATER "15.6.0")
603  set(LLVM_TOUCH_STATIC_LIBRARIES ON)
604endif()
605
606if(CMAKE_HOST_APPLE AND APPLE)
607  if(NOT CMAKE_XCRUN)
608    find_program(CMAKE_XCRUN NAMES xcrun)
609  endif()
610  if(CMAKE_XCRUN)
611    execute_process(COMMAND ${CMAKE_XCRUN} -find ld
612      OUTPUT_VARIABLE LD64_EXECUTABLE
613      OUTPUT_STRIP_TRAILING_WHITESPACE)
614  else()
615    find_program(LD64_EXECUTABLE NAMES ld DOC "The ld64 linker")
616  endif()
617
618  if(LD64_EXECUTABLE)
619    set(LD64_EXECUTABLE ${LD64_EXECUTABLE} CACHE PATH "ld64 executable")
620    message(STATUS "Found ld64 - ${LD64_EXECUTABLE}")
621  endif()
622endif()
623
624# Keep the version requirements in sync with bindings/ocaml/README.txt.
625include(FindOCaml)
626include(AddOCaml)
627if(WIN32 OR NOT LLVM_ENABLE_BINDINGS)
628  message(STATUS "OCaml bindings disabled.")
629else()
630  find_package(OCaml)
631  if( NOT OCAML_FOUND )
632    message(STATUS "OCaml bindings disabled.")
633  else()
634    if( OCAML_VERSION VERSION_LESS "4.00.0" )
635      message(STATUS "OCaml bindings disabled, need OCaml >=4.00.0.")
636    else()
637      find_ocamlfind_package(ctypes VERSION 0.4 OPTIONAL)
638      if( HAVE_OCAML_CTYPES )
639        message(STATUS "OCaml bindings enabled.")
640        find_ocamlfind_package(oUnit VERSION 2 OPTIONAL)
641        set(LLVM_BINDINGS "${LLVM_BINDINGS} ocaml")
642
643        set(LLVM_OCAML_INSTALL_PATH "${OCAML_STDLIB_PATH}" CACHE STRING
644            "Install directory for LLVM OCaml packages")
645      else()
646        message(STATUS "OCaml bindings disabled, need ctypes >=0.4.")
647      endif()
648    endif()
649  endif()
650endif()
651
652string(REPLACE " " ";" LLVM_BINDINGS_LIST "${LLVM_BINDINGS}")
653
654function(find_python_module module)
655  string(REPLACE "." "_" module_name ${module})
656  string(TOUPPER ${module_name} module_upper)
657  set(FOUND_VAR PY_${module_upper}_FOUND)
658  if (DEFINED ${FOUND_VAR})
659    return()
660  endif()
661
662  execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import ${module}"
663    RESULT_VARIABLE status
664    ERROR_QUIET)
665
666  if(status)
667    set(${FOUND_VAR} OFF CACHE BOOL "Failed to find python module '${module}'")
668    message(STATUS "Could NOT find Python module ${module}")
669  else()
670  set(${FOUND_VAR} ON CACHE BOOL "Found python module '${module}'")
671    message(STATUS "Found Python module ${module}")
672  endif()
673endfunction()
674
675set (PYTHON_MODULES
676  pygments
677  # Some systems still don't have pygments.lexers.c_cpp which was introduced in
678  # version 2.0 in 2014...
679  pygments.lexers.c_cpp
680  yaml
681  )
682foreach(module ${PYTHON_MODULES})
683  find_python_module(${module})
684endforeach()
685
686if(PY_PYGMENTS_FOUND AND PY_PYGMENTS_LEXERS_C_CPP_FOUND AND PY_YAML_FOUND)
687  set (LLVM_HAVE_OPT_VIEWER_MODULES 1)
688else()
689  set (LLVM_HAVE_OPT_VIEWER_MODULES 0)
690endif()
691