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