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