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