[WebAssembly] Add new target feature in support of 'extended-const' proposal
[llvm-project.git] / llvm / cmake / config-ix.cmake
blob18d78879c968f031ff740c11873214d1d3cf50b5
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)
12 include(CMakePushCheckState)
14 include(CheckCompilerVersion)
15 include(HandleLLVMStdlib)
17 if( UNIX AND NOT (APPLE OR BEOS OR HAIKU) )
18   # Used by check_symbol_exists:
19   list(APPEND CMAKE_REQUIRED_LIBRARIES "m")
20 endif()
21 # x86_64 FreeBSD 9.2 requires libcxxrt to be specified explicitly.
22 if( CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE" AND
23     CMAKE_SIZEOF_VOID_P EQUAL 8 )
24   list(APPEND CMAKE_REQUIRED_LIBRARIES "cxxrt")
25 endif()
27 # Do checks with _XOPEN_SOURCE and large-file API on AIX, because we will build
28 # with those too.
29 if (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")
32 endif()
34 # Do checks with _FILE_OFFSET_BITS=64 on Solaris, because we will build
35 # with those too.
36 if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
37           list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_FILE_OFFSET_BITS=64")
38 endif()
40 # include checks
41 check_include_file(dlfcn.h HAVE_DLFCN_H)
42 check_include_file(errno.h HAVE_ERRNO_H)
43 check_include_file(fcntl.h HAVE_FCNTL_H)
44 check_include_file(link.h HAVE_LINK_H)
45 check_include_file(malloc/malloc.h HAVE_MALLOC_MALLOC_H)
46 if( NOT PURE_WINDOWS )
47   check_include_file(pthread.h HAVE_PTHREAD_H)
48 endif()
49 check_include_file(signal.h HAVE_SIGNAL_H)
50 check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
51 check_include_file(sys/mman.h HAVE_SYS_MMAN_H)
52 check_include_file(sys/param.h HAVE_SYS_PARAM_H)
53 check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H)
54 check_include_file(sys/stat.h HAVE_SYS_STAT_H)
55 check_include_file(sys/time.h HAVE_SYS_TIME_H)
56 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
57 check_include_file(sysexits.h HAVE_SYSEXITS_H)
58 check_include_file(termios.h HAVE_TERMIOS_H)
59 check_include_file(unistd.h HAVE_UNISTD_H)
60 check_include_file(valgrind/valgrind.h HAVE_VALGRIND_VALGRIND_H)
61 check_include_file(fenv.h HAVE_FENV_H)
62 check_symbol_exists(FE_ALL_EXCEPT "fenv.h" HAVE_DECL_FE_ALL_EXCEPT)
63 check_symbol_exists(FE_INEXACT "fenv.h" HAVE_DECL_FE_INEXACT)
65 check_include_file(mach/mach.h HAVE_MACH_MACH_H)
66 check_include_file(histedit.h HAVE_HISTEDIT_H)
67 check_include_file(CrashReporterClient.h HAVE_CRASHREPORTERCLIENT_H)
68 if(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)
75 endif()
77 if(${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()
84 endif()
86 # library checks
87 if( 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)
104 endif()
106 # Check for libpfm.
107 include(FindLibpfm)
109 if(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})
117 endif()
119 if(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}")
138 endif()
140 if(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}")
160 endif()
162 if(LLVM_ENABLE_CURL)
163   if(LLVM_ENABLE_CURL STREQUAL FORCE_ON)
164     find_package(CURL REQUIRED)
165   else()
166     find_package(CURL)
167   endif()
168   if(CURL_FOUND)
169     # Check if curl we found is usable; for example, we may have found a 32-bit
170     # library on a 64-bit system which would result in a link-time failure.
171     cmake_push_check_state()
172     list(APPEND CMAKE_REQUIRED_INCLUDES ${CURL_INCLUDE_DIRS})
173     list(APPEND CMAKE_REQUIRED_LIBRARIES ${CURL_LIBRARY})
174     check_symbol_exists(curl_easy_init curl/curl.h HAVE_CURL)
175     cmake_pop_check_state()
176     if(LLVM_ENABLE_CURL STREQUAL FORCE_ON AND NOT HAVE_CURL)
177       message(FATAL_ERROR "Failed to configure curl")
178     endif()
179   endif()
180   set(LLVM_ENABLE_CURL "${HAVE_CURL}")
181 endif()
183 # Don't look for these libraries if we're using MSan, since uninstrumented third
184 # party code may call MSan interceptors like strlen, leading to false positives.
185 if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
186   # Don't look for these libraries on Windows.
187   if (NOT PURE_WINDOWS)
188     # Skip libedit if using ASan as it contains memory leaks.
189     if (LLVM_ENABLE_LIBEDIT AND HAVE_HISTEDIT_H AND NOT LLVM_USE_SANITIZER MATCHES ".*Address.*")
190       check_library_exists(edit el_init "" HAVE_LIBEDIT)
191     else()
192       set(HAVE_LIBEDIT 0)
193     endif()
194     if(LLVM_ENABLE_TERMINFO)
195       if(LLVM_ENABLE_TERMINFO STREQUAL FORCE_ON)
196         find_package(Terminfo REQUIRED)
197       else()
198         find_package(Terminfo)
199       endif()
200       set(LLVM_ENABLE_TERMINFO "${Terminfo_FOUND}")
201     endif()
202   else()
203     set(LLVM_ENABLE_TERMINFO 0)
204   endif()
205 else()
206   set(LLVM_ENABLE_TERMINFO 0)
207 endif()
209 check_library_exists(xar xar_open "" LLVM_HAVE_LIBXAR)
210 if(LLVM_HAVE_LIBXAR)
211   message(STATUS "The xar file format has been deprecated: LLVM_HAVE_LIBXAR might be removed in the future.")
212   # The xar file format has been deprecated since macOS 12.0.
213   if (CMAKE_OSX_DEPLOYMENT_TARGET VERSION_GREATER_EQUAL 12)
214     set(LLVM_HAVE_LIBXAR 0)
215   else()
216     set(XAR_LIB xar)
217   endif()
218 endif()
220 # function checks
221 check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
222 find_package(Backtrace)
223 set(HAVE_BACKTRACE ${Backtrace_FOUND})
224 set(BACKTRACE_HEADER ${Backtrace_HEADER})
226 # Prevent check_symbol_exists from using API that is not supported for a given
227 # deployment target.
228 check_c_compiler_flag("-Werror=unguarded-availability-new" "C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW")
229 if(C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW)
230   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability-new")
231 endif()
233 # Determine whether we can register EH tables.
234 check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
235 check_symbol_exists(__deregister_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_DEREGISTER_FRAME)
236 check_symbol_exists(__unw_add_dynamic_fde "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_UNW_ADD_DYNAMIC_FDE)
238 check_symbol_exists(_Unwind_Backtrace "unwind.h" HAVE__UNWIND_BACKTRACE)
239 check_symbol_exists(getpagesize unistd.h HAVE_GETPAGESIZE)
240 check_symbol_exists(sysconf unistd.h HAVE_SYSCONF)
241 check_symbol_exists(getrusage sys/resource.h HAVE_GETRUSAGE)
242 check_symbol_exists(setrlimit sys/resource.h HAVE_SETRLIMIT)
243 check_symbol_exists(isatty unistd.h HAVE_ISATTY)
244 check_symbol_exists(futimens sys/stat.h HAVE_FUTIMENS)
245 check_symbol_exists(futimes sys/time.h HAVE_FUTIMES)
246 # AddressSanitizer conflicts with lib/Support/Unix/Signals.inc
247 # Avoid sigaltstack on Apple platforms, where backtrace() cannot handle it
248 # (rdar://7089625) and _Unwind_Backtrace is unusable because it cannot unwind
249 # past the signal handler after an assertion failure (rdar://29866587).
250 if( HAVE_SIGNAL_H AND NOT LLVM_USE_SANITIZER MATCHES ".*Address.*" AND NOT APPLE )
251   check_symbol_exists(sigaltstack signal.h HAVE_SIGALTSTACK)
252 endif()
253 set(CMAKE_REQUIRED_DEFINITIONS "-D_LARGEFILE64_SOURCE")
254 check_symbol_exists(lseek64 "sys/types.h;unistd.h" HAVE_LSEEK64)
255 set(CMAKE_REQUIRED_DEFINITIONS "")
256 check_symbol_exists(mallctl malloc_np.h HAVE_MALLCTL)
257 check_symbol_exists(mallinfo malloc.h HAVE_MALLINFO)
258 check_symbol_exists(mallinfo2 malloc.h HAVE_MALLINFO2)
259 check_symbol_exists(malloc_zone_statistics malloc/malloc.h
260                     HAVE_MALLOC_ZONE_STATISTICS)
261 check_symbol_exists(getrlimit "sys/types.h;sys/time.h;sys/resource.h" HAVE_GETRLIMIT)
262 check_symbol_exists(posix_spawn spawn.h HAVE_POSIX_SPAWN)
263 check_symbol_exists(pread unistd.h HAVE_PREAD)
264 check_symbol_exists(sbrk unistd.h HAVE_SBRK)
265 check_symbol_exists(strerror string.h HAVE_STRERROR)
266 check_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
267 check_symbol_exists(strerror_s string.h HAVE_DECL_STRERROR_S)
268 check_symbol_exists(setenv stdlib.h HAVE_SETENV)
269 if( PURE_WINDOWS )
270   check_symbol_exists(_chsize_s io.h HAVE__CHSIZE_S)
272   check_function_exists(_alloca HAVE__ALLOCA)
273   check_function_exists(__alloca HAVE___ALLOCA)
274   check_function_exists(__chkstk HAVE___CHKSTK)
275   check_function_exists(__chkstk_ms HAVE___CHKSTK_MS)
276   check_function_exists(___chkstk HAVE____CHKSTK)
277   check_function_exists(___chkstk_ms HAVE____CHKSTK_MS)
279   check_function_exists(__ashldi3 HAVE___ASHLDI3)
280   check_function_exists(__ashrdi3 HAVE___ASHRDI3)
281   check_function_exists(__divdi3 HAVE___DIVDI3)
282   check_function_exists(__fixdfdi HAVE___FIXDFDI)
283   check_function_exists(__fixsfdi HAVE___FIXSFDI)
284   check_function_exists(__floatdidf HAVE___FLOATDIDF)
285   check_function_exists(__lshrdi3 HAVE___LSHRDI3)
286   check_function_exists(__moddi3 HAVE___MODDI3)
287   check_function_exists(__udivdi3 HAVE___UDIVDI3)
288   check_function_exists(__umoddi3 HAVE___UMODDI3)
290   check_function_exists(__main HAVE___MAIN)
291   check_function_exists(__cmpdi2 HAVE___CMPDI2)
292 endif()
293 if( HAVE_DLFCN_H )
294   if( HAVE_LIBDL )
295     list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
296   endif()
297   check_symbol_exists(dlopen dlfcn.h HAVE_DLOPEN)
298   check_symbol_exists(dladdr dlfcn.h HAVE_DLADDR)
299   if( HAVE_LIBDL )
300     list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
301   endif()
302 endif()
304 CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtimespec.tv_nsec
305     "sys/types.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
306 if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
307 # The st_mtim.tv_nsec member of a `stat` structure is not reliable on some AIX
308 # environments.
309   set(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC 0)
310 else()
311   CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec
312       "sys/types.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
313 endif()
315 check_symbol_exists(__GLIBC__ stdio.h LLVM_USING_GLIBC)
316 if( LLVM_USING_GLIBC )
317   add_definitions( -D_GNU_SOURCE )
318   list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
319 endif()
320 # This check requires _GNU_SOURCE
321 if (NOT PURE_WINDOWS)
322   if (LLVM_PTHREAD_LIB)
323     list(APPEND CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
324   endif()
325   check_symbol_exists(pthread_getname_np pthread.h HAVE_PTHREAD_GETNAME_NP)
326   check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
327   if (LLVM_PTHREAD_LIB)
328     list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
329   endif()
330 endif()
332 # available programs checks
333 function(llvm_find_program name)
334   string(TOUPPER ${name} NAME)
335   string(REGEX REPLACE "\\." "_" NAME ${NAME})
337   find_program(LLVM_PATH_${NAME} NAMES ${ARGV})
338   mark_as_advanced(LLVM_PATH_${NAME})
339   if(LLVM_PATH_${NAME})
340     set(HAVE_${NAME} 1 CACHE INTERNAL "Is ${name} available ?")
341     mark_as_advanced(HAVE_${NAME})
342   else(LLVM_PATH_${NAME})
343     set(HAVE_${NAME} "" CACHE INTERNAL "Is ${name} available ?")
344   endif(LLVM_PATH_${NAME})
345 endfunction()
347 if (LLVM_ENABLE_DOXYGEN)
348   llvm_find_program(dot)
349 endif ()
351 if(LLVM_ENABLE_FFI)
352   set(FFI_REQUIRE_INCLUDE On)
353   if(LLVM_ENABLE_FFI STREQUAL FORCE_ON)
354     find_package(FFI REQUIRED)
355   else()
356     find_package(FFI)
357   endif()
358   set(LLVM_ENABLE_FFI "${FFI_FOUND}")
359 else()
360   unset(HAVE_FFI_FFI_H CACHE)
361   unset(HAVE_FFI_H CACHE)
362   unset(HAVE_FFI_CALL CACHE)
363 endif()
365 check_symbol_exists(proc_pid_rusage "libproc.h" HAVE_PROC_PID_RUSAGE)
367 # Whether we can use std::is_trivially_copyable to verify llvm::is_trivially_copyable.
368 CHECK_CXX_SOURCE_COMPILES("
369 #include <type_traits>
370 struct T { int val; };
371 static_assert(std::is_trivially_copyable<T>::value, \"ok\");
372 int main() { return 0;}
373 " HAVE_STD_IS_TRIVIALLY_COPYABLE)
376 # Define LLVM_HAS_ATOMICS if gcc or MSVC atomic builtins are supported.
377 include(CheckAtomic)
379 if( LLVM_ENABLE_PIC )
380   set(ENABLE_PIC 1)
381 else()
382   set(ENABLE_PIC 0)
383   check_cxx_compiler_flag("-fno-pie" SUPPORTS_NO_PIE_FLAG)
384   if(SUPPORTS_NO_PIE_FLAG)
385     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-pie")
386   endif()
387 endif()
389 check_cxx_compiler_flag("-Wvariadic-macros" SUPPORTS_VARIADIC_MACROS_FLAG)
390 check_cxx_compiler_flag("-Wgnu-zero-variadic-macro-arguments"
391                         SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
393 set(USE_NO_MAYBE_UNINITIALIZED 0)
394 set(USE_NO_UNINITIALIZED 0)
396 # Disable gcc's potentially uninitialized use analysis as it presents lots of
397 # false positives.
398 if (CMAKE_COMPILER_IS_GNUCXX)
399   check_cxx_compiler_flag("-Wmaybe-uninitialized" HAS_MAYBE_UNINITIALIZED)
400   if (HAS_MAYBE_UNINITIALIZED)
401     set(USE_NO_MAYBE_UNINITIALIZED 1)
402   else()
403     # Only recent versions of gcc make the distinction between -Wuninitialized
404     # and -Wmaybe-uninitialized. If -Wmaybe-uninitialized isn't supported, just
405     # turn off all uninitialized use warnings.
406     check_cxx_compiler_flag("-Wuninitialized" HAS_UNINITIALIZED)
407     set(USE_NO_UNINITIALIZED ${HAS_UNINITIALIZED})
408   endif()
409 endif()
411 # By default, we target the host, but this can be overridden at CMake
412 # invocation time.
413 include(GetHostTriple)
414 get_host_triple(LLVM_INFERRED_HOST_TRIPLE)
416 set(LLVM_HOST_TRIPLE "${LLVM_INFERRED_HOST_TRIPLE}" CACHE STRING
417     "Host on which LLVM binaries will run")
419 # Determine the native architecture.
420 string(TOLOWER "${LLVM_TARGET_ARCH}" LLVM_NATIVE_ARCH)
421 if( LLVM_NATIVE_ARCH STREQUAL "host" )
422   string(REGEX MATCH "^[^-]*" LLVM_NATIVE_ARCH ${LLVM_HOST_TRIPLE})
423 endif ()
425 if (LLVM_NATIVE_ARCH MATCHES "i[2-6]86")
426   set(LLVM_NATIVE_ARCH X86)
427 elseif (LLVM_NATIVE_ARCH STREQUAL "x86")
428   set(LLVM_NATIVE_ARCH X86)
429 elseif (LLVM_NATIVE_ARCH STREQUAL "amd64")
430   set(LLVM_NATIVE_ARCH X86)
431 elseif (LLVM_NATIVE_ARCH STREQUAL "x86_64")
432   set(LLVM_NATIVE_ARCH X86)
433 elseif (LLVM_NATIVE_ARCH MATCHES "sparc")
434   set(LLVM_NATIVE_ARCH Sparc)
435 elseif (LLVM_NATIVE_ARCH MATCHES "powerpc")
436   set(LLVM_NATIVE_ARCH PowerPC)
437 elseif (LLVM_NATIVE_ARCH MATCHES "ppc64le")
438   set(LLVM_NATIVE_ARCH PowerPC)
439 elseif (LLVM_NATIVE_ARCH MATCHES "aarch64")
440   set(LLVM_NATIVE_ARCH AArch64)
441 elseif (LLVM_NATIVE_ARCH MATCHES "arm64")
442   set(LLVM_NATIVE_ARCH AArch64)
443 elseif (LLVM_NATIVE_ARCH MATCHES "arm")
444   set(LLVM_NATIVE_ARCH ARM)
445 elseif (LLVM_NATIVE_ARCH MATCHES "avr")
446   set(LLVM_NATIVE_ARCH AVR)
447 elseif (LLVM_NATIVE_ARCH MATCHES "mips")
448   set(LLVM_NATIVE_ARCH Mips)
449 elseif (LLVM_NATIVE_ARCH MATCHES "xcore")
450   set(LLVM_NATIVE_ARCH XCore)
451 elseif (LLVM_NATIVE_ARCH MATCHES "msp430")
452   set(LLVM_NATIVE_ARCH MSP430)
453 elseif (LLVM_NATIVE_ARCH MATCHES "hexagon")
454   set(LLVM_NATIVE_ARCH Hexagon)
455 elseif (LLVM_NATIVE_ARCH MATCHES "s390x")
456   set(LLVM_NATIVE_ARCH SystemZ)
457 elseif (LLVM_NATIVE_ARCH MATCHES "wasm32")
458   set(LLVM_NATIVE_ARCH WebAssembly)
459 elseif (LLVM_NATIVE_ARCH MATCHES "wasm64")
460   set(LLVM_NATIVE_ARCH WebAssembly)
461 elseif (LLVM_NATIVE_ARCH MATCHES "riscv32")
462   set(LLVM_NATIVE_ARCH RISCV)
463 elseif (LLVM_NATIVE_ARCH MATCHES "riscv64")
464   set(LLVM_NATIVE_ARCH RISCV)
465 elseif (LLVM_NATIVE_ARCH STREQUAL "m68k")
466   set(LLVM_NATIVE_ARCH M68k)
467 else ()
468   message(FATAL_ERROR "Unknown architecture ${LLVM_NATIVE_ARCH}")
469 endif ()
471 # If build targets includes "host" or "Native", then replace with native architecture.
472 foreach (NATIVE_KEYWORD host Native)
473   list(FIND LLVM_TARGETS_TO_BUILD ${NATIVE_KEYWORD} idx)
474   if( NOT idx LESS 0 )
475     list(REMOVE_AT LLVM_TARGETS_TO_BUILD ${idx})
476     list(APPEND LLVM_TARGETS_TO_BUILD ${LLVM_NATIVE_ARCH})
477     list(REMOVE_DUPLICATES LLVM_TARGETS_TO_BUILD)
478   endif()
479 endforeach()
481 list(FIND LLVM_TARGETS_TO_BUILD ${LLVM_NATIVE_ARCH} NATIVE_ARCH_IDX)
482 if (NATIVE_ARCH_IDX EQUAL -1)
483   message(STATUS
484     "Native target ${LLVM_NATIVE_ARCH} is not selected; lli will not JIT code")
485 else ()
486   message(STATUS "Native target architecture is ${LLVM_NATIVE_ARCH}")
487   set(LLVM_NATIVE_TARGET LLVMInitialize${LLVM_NATIVE_ARCH}Target)
488   set(LLVM_NATIVE_TARGETINFO LLVMInitialize${LLVM_NATIVE_ARCH}TargetInfo)
489   set(LLVM_NATIVE_TARGETMC LLVMInitialize${LLVM_NATIVE_ARCH}TargetMC)
490   set(LLVM_NATIVE_ASMPRINTER LLVMInitialize${LLVM_NATIVE_ARCH}AsmPrinter)
492   # We don't have an ASM parser for all architectures yet.
493   if (EXISTS ${PROJECT_SOURCE_DIR}/lib/Target/${LLVM_NATIVE_ARCH}/AsmParser/CMakeLists.txt)
494     set(LLVM_NATIVE_ASMPARSER LLVMInitialize${LLVM_NATIVE_ARCH}AsmParser)
495   endif ()
497   # We don't have an disassembler for all architectures yet.
498   if (EXISTS ${PROJECT_SOURCE_DIR}/lib/Target/${LLVM_NATIVE_ARCH}/Disassembler/CMakeLists.txt)
499     set(LLVM_NATIVE_DISASSEMBLER LLVMInitialize${LLVM_NATIVE_ARCH}Disassembler)
500   endif ()
501 endif ()
503 if( MSVC )
504   set(SHLIBEXT ".lib")
505   set(stricmp "_stricmp")
506   set(strdup "_strdup")
508   # Allow setting clang-cl's /winsysroot flag.
509   set(LLVM_WINSYSROOT "" CACHE STRING
510     "If set, argument to clang-cl's /winsysroot")
512   if (LLVM_WINSYSROOT)
513     set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
514         "Path to the DIA SDK")
515   else()
516     set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
517         "Path to the DIA SDK")
518   endif()
520   # See if the DIA SDK is available and usable.
521   # Due to a bug in MSVC 2013's installation software, it is possible
522   # for MSVC 2013 to write the DIA SDK into the Visual Studio 2012
523   # install directory.  If this happens, the installation is corrupt
524   # and there's nothing we can do.  It happens with enough frequency
525   # though that we should handle it.  We do so by simply checking that
526   # the DIA SDK folder exists.  Should this happen you will need to
527   # uninstall VS 2012 and then re-install VS 2013.
528   if (IS_DIRECTORY "${MSVC_DIA_SDK_DIR}")
529     set(HAVE_DIA_SDK 1)
530   else()
531     set(HAVE_DIA_SDK 0)
532   endif()
534   option(LLVM_ENABLE_DIA_SDK "Use MSVC DIA SDK for debugging if available."
535                              ${HAVE_DIA_SDK})
537   if(LLVM_ENABLE_DIA_SDK AND NOT HAVE_DIA_SDK)
538     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.")
539   endif()
540 else()
541   set(LLVM_ENABLE_DIA_SDK 0)
542 endif( MSVC )
544 # FIXME: Signal handler return type, currently hardcoded to 'void'
545 set(RETSIGTYPE void)
547 if( LLVM_ENABLE_THREADS )
548   # Check if threading primitives aren't supported on this platform
549   if( NOT HAVE_PTHREAD_H AND NOT WIN32 )
550     set(LLVM_ENABLE_THREADS 0)
551   endif()
552 endif()
554 if( LLVM_ENABLE_THREADS )
555   message(STATUS "Threads enabled.")
556 else( LLVM_ENABLE_THREADS )
557   message(STATUS "Threads disabled.")
558 endif()
560 if (LLVM_ENABLE_DOXYGEN)
561   message(STATUS "Doxygen enabled.")
562   find_package(Doxygen REQUIRED)
564   if (DOXYGEN_FOUND)
565     # If we find doxygen and we want to enable doxygen by default create a
566     # global aggregate doxygen target for generating llvm and any/all
567     # subprojects doxygen documentation.
568     if (LLVM_BUILD_DOCS)
569       add_custom_target(doxygen ALL)
570     endif()
572     option(LLVM_DOXYGEN_EXTERNAL_SEARCH "Enable doxygen external search." OFF)
573     if (LLVM_DOXYGEN_EXTERNAL_SEARCH)
574       set(LLVM_DOXYGEN_SEARCHENGINE_URL "" CACHE STRING "URL to use for external search.")
575       set(LLVM_DOXYGEN_SEARCH_MAPPINGS "" CACHE STRING "Doxygen Search Mappings")
576     endif()
577   endif()
578 else()
579   message(STATUS "Doxygen disabled.")
580 endif()
582 set(LLVM_BINDINGS "")
583 find_program(GO_EXECUTABLE NAMES go DOC "go executable")
584 if(WIN32 OR NOT LLVM_ENABLE_BINDINGS)
585   message(STATUS "Go bindings disabled.")
586 else()
587   if(GO_EXECUTABLE STREQUAL "GO_EXECUTABLE-NOTFOUND")
588     message(STATUS "Go bindings disabled.")
589   else()
590     execute_process(COMMAND ${GO_EXECUTABLE} run ${PROJECT_SOURCE_DIR}/bindings/go/conftest.go
591                     RESULT_VARIABLE GO_CONFTEST)
592     if(GO_CONFTEST STREQUAL "0")
593       set(LLVM_BINDINGS "${LLVM_BINDINGS} go")
594       message(STATUS "Go bindings enabled.")
595     else()
596       message(STATUS "Go bindings disabled, need at least Go 1.2.")
597     endif()
598   endif()
599 endif()
601 find_program(GOLD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.gold ld.gold ${LLVM_DEFAULT_TARGET_TRIPLE}-ld ld DOC "The gold linker")
602 set(LLVM_BINUTILS_INCDIR "" CACHE PATH
603     "PATH to binutils/include containing plugin-api.h for gold plugin.")
605 if(CMAKE_GENERATOR MATCHES "Ninja")
606   execute_process(COMMAND ${CMAKE_MAKE_PROGRAM} --version
607     OUTPUT_VARIABLE NINJA_VERSION
608     OUTPUT_STRIP_TRAILING_WHITESPACE)
609   set(NINJA_VERSION ${NINJA_VERSION} CACHE STRING "Ninja version number" FORCE)
610   message(STATUS "Ninja version: ${NINJA_VERSION}")
611 endif()
613 if(CMAKE_GENERATOR MATCHES "Ninja" AND
614     NOT "${NINJA_VERSION}" VERSION_LESS "1.9.0" AND
615     CMAKE_HOST_APPLE AND CMAKE_HOST_SYSTEM_VERSION VERSION_GREATER "15.6.0")
616   set(LLVM_TOUCH_STATIC_LIBRARIES ON)
617 endif()
619 if(CMAKE_HOST_APPLE AND APPLE)
620   if(NOT CMAKE_XCRUN)
621     find_program(CMAKE_XCRUN NAMES xcrun)
622   endif()
623   if(CMAKE_XCRUN)
624     execute_process(COMMAND ${CMAKE_XCRUN} -find ld
625       OUTPUT_VARIABLE LD64_EXECUTABLE
626       OUTPUT_STRIP_TRAILING_WHITESPACE)
627   else()
628     find_program(LD64_EXECUTABLE NAMES ld DOC "The ld64 linker")
629   endif()
631   if(LD64_EXECUTABLE)
632     set(LD64_EXECUTABLE ${LD64_EXECUTABLE} CACHE PATH "ld64 executable")
633     message(STATUS "Found ld64 - ${LD64_EXECUTABLE}")
634   endif()
635 endif()
637 # Keep the version requirements in sync with bindings/ocaml/README.txt.
638 include(FindOCaml)
639 include(AddOCaml)
640 if(WIN32 OR NOT LLVM_ENABLE_BINDINGS)
641   message(STATUS "OCaml bindings disabled.")
642 else()
643   find_package(OCaml)
644   if( NOT OCAML_FOUND )
645     message(STATUS "OCaml bindings disabled.")
646   else()
647     if( OCAML_VERSION VERSION_LESS "4.00.0" )
648       message(STATUS "OCaml bindings disabled, need OCaml >=4.00.0.")
649     else()
650       find_ocamlfind_package(ctypes VERSION 0.4 OPTIONAL)
651       if( HAVE_OCAML_CTYPES )
652         message(STATUS "OCaml bindings enabled.")
653         set(LLVM_BINDINGS "${LLVM_BINDINGS} ocaml")
655         set(LLVM_OCAML_INSTALL_PATH "${OCAML_STDLIB_PATH}" CACHE STRING
656             "Install directory for LLVM OCaml packages")
657       else()
658         message(STATUS "OCaml bindings disabled, need ctypes >=0.4.")
659       endif()
660     endif()
661   endif()
662 endif()
664 string(REPLACE " " ";" LLVM_BINDINGS_LIST "${LLVM_BINDINGS}")
666 function(find_python_module module)
667   string(REPLACE "." "_" module_name ${module})
668   string(TOUPPER ${module_name} module_upper)
669   set(FOUND_VAR PY_${module_upper}_FOUND)
670   if (DEFINED ${FOUND_VAR})
671     return()
672   endif()
674   execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import ${module}"
675     RESULT_VARIABLE status
676     ERROR_QUIET)
678   if(status)
679     set(${FOUND_VAR} OFF CACHE BOOL "Failed to find python module '${module}'")
680     message(STATUS "Could NOT find Python module ${module}")
681   else()
682   set(${FOUND_VAR} ON CACHE BOOL "Found python module '${module}'")
683     message(STATUS "Found Python module ${module}")
684   endif()
685 endfunction()
687 set (PYTHON_MODULES
688   pygments
689   # Some systems still don't have pygments.lexers.c_cpp which was introduced in
690   # version 2.0 in 2014...
691   pygments.lexers.c_cpp
692   yaml
693   )
694 foreach(module ${PYTHON_MODULES})
695   find_python_module(${module})
696 endforeach()
698 if(PY_PYGMENTS_FOUND AND PY_PYGMENTS_LEXERS_C_CPP_FOUND AND PY_YAML_FOUND)
699   set (LLVM_HAVE_OPT_VIEWER_MODULES 1)
700 else()
701   set (LLVM_HAVE_OPT_VIEWER_MODULES 0)
702 endif()
704 function(llvm_get_host_prefixes_and_suffixes)
705   # Not all platform files will set these variables (relying on them being
706   # implicitly empty if they're unset), so unset the variables before including
707   # the platform file, to prevent any values from the target system leaking.
708   unset(CMAKE_STATIC_LIBRARY_PREFIX)
709   unset(CMAKE_STATIC_LIBRARY_SUFFIX)
710   unset(CMAKE_SHARED_LIBRARY_PREFIX)
711   unset(CMAKE_SHARED_LIBRARY_SUFFIX)
712   unset(CMAKE_IMPORT_LIBRARY_PREFIX)
713   unset(CMAKE_IMPORT_LIBRARY_SUFFIX)
714   unset(CMAKE_EXECUTABLE_SUFFIX)
715   unset(CMAKE_LINK_LIBRARY_SUFFIX)
716   include(Platform/${CMAKE_HOST_SYSTEM_NAME} OPTIONAL RESULT_VARIABLE _includedFile)
717   if (_includedFile)
718     set(LLVM_HOST_STATIC_LIBRARY_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX} PARENT_SCOPE)
719     set(LLVM_HOST_STATIC_LIBRARY_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX} PARENT_SCOPE)
720     set(LLVM_HOST_SHARED_LIBRARY_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX} PARENT_SCOPE)
721     set(LLVM_HOST_SHARED_LIBRARY_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX} PARENT_SCOPE)
722     set(LLVM_HOST_IMPORT_LIBRARY_PREFIX ${CMAKE_IMPORT_LIBRARY_PREFIX} PARENT_SCOPE)
723     set(LLVM_HOST_IMPORT_LIBRARY_SUFFIX ${CMAKE_IMPORT_LIBRARY_SUFFIX} PARENT_SCOPE)
724     set(LLVM_HOST_EXECUTABLE_SUFFIX ${CMAKE_EXECUTABLE_SUFFIX} PARENT_SCOPE)
725     set(LLVM_HOST_LINK_LIBRARY_SUFFIX ${CMAKE_LINK_LIBRARY_SUFFIX} PARENT_SCOPE)
726   endif()
727 endfunction()
729 llvm_get_host_prefixes_and_suffixes()