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