CMake misses a check for sbrk on NetBSD.
[oota-llvm.git] / cmake / config-ix.cmake
1 include(CheckIncludeFile)
2 include(CheckLibraryExists)
3 include(CheckSymbolExists)
4 include(CheckFunctionExists)
5 include(CheckCXXSourceCompiles)
6
7 # Helper macros and functions
8 macro(add_cxx_include result files)
9   set(${result} "")
10   foreach (file_name ${files})
11      set(${result} "${${result}}#include<${file_name}>\n")
12   endforeach()
13 endmacro(add_cxx_include files result)
14
15 function(check_type_exists type files variable)
16   add_cxx_include(includes "${files}")
17   CHECK_CXX_SOURCE_COMPILES("
18     ${includes} ${type} typeVar;
19     int main() {
20         return 0;
21     }
22     " ${variable})
23 endfunction()
24
25 # include checks
26 check_include_file(argz.h HAVE_ARGZ_H)
27 check_include_file(assert.h HAVE_ASSERT_H)
28 check_include_file(dirent.h HAVE_DIRENT_H)
29 check_include_file(dl.h HAVE_DL_H)
30 check_include_file(dld.h HAVE_DLD_H)
31 check_include_file(dlfcn.h HAVE_DLFCN_H)
32 check_include_file(errno.h HAVE_ERRNO_H)
33 check_include_file(execinfo.h HAVE_EXECINFO_H)
34 check_include_file(fcntl.h HAVE_FCNTL_H)
35 check_include_file(inttypes.h HAVE_INTTYPES_H)
36 check_include_file(limits.h HAVE_LIMITS_H)
37 check_include_file(link.h HAVE_LINK_H)
38 check_include_file(malloc.h HAVE_MALLOC_H)
39 check_include_file(malloc/malloc.h HAVE_MALLOC_MALLOC_H)
40 check_include_file(memory.h HAVE_MEMORY_H)
41 check_include_file(ndir.h HAVE_NDIR_H)
42 check_include_file(pthread.h HAVE_PTHREAD_H)
43 check_include_file(setjmp.h HAVE_SETJMP_H)
44 check_include_file(signal.h HAVE_SIGNAL_H)
45 check_include_file(stdint.h HAVE_STDINT_H)
46 check_include_file(stdio.h HAVE_STDIO_H)
47 check_include_file(stdlib.h HAVE_STDLIB_H)
48 check_include_file(string.h HAVE_STRING_H)
49 check_include_file(sys/dir.h HAVE_SYS_DIR_H)
50 check_include_file(sys/dl.h HAVE_SYS_DL_H)
51 check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
52 check_include_file(sys/mman.h HAVE_SYS_MMAN_H)
53 check_include_file(sys/ndir.h HAVE_SYS_NDIR_H)
54 check_include_file(sys/param.h HAVE_SYS_PARAM_H)
55 check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H)
56 check_include_file(sys/stat.h HAVE_SYS_STAT_H)
57 check_include_file(sys/time.h HAVE_SYS_TIME_H)
58 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
59 check_include_file(sys/wait.h HAVE_SYS_WAIT_H)
60 check_include_file(termios.h HAVE_TERMIOS_H)
61 check_include_file(unistd.h HAVE_UNISTD_H)
62 check_include_file(utime.h HAVE_UTIME_H)
63 check_include_file(windows.h HAVE_WINDOWS_H)
64
65 # library checks
66 check_library_exists(pthread pthread_create "" HAVE_LIBPTHREAD)
67 check_library_exists(pthread pthread_getspecific "" HAVE_PTHREAD_GETSPECIFIC)
68 check_library_exists(pthread pthread_rwlock_init "" HAVE_PTHREAD_RWLOCK_INIT)
69 check_library_exists(dl dlopen "" HAVE_LIBDL)
70
71 # function checks
72 check_symbol_exists(getpagesize unistd.h HAVE_GETPAGESIZE)
73 check_symbol_exists(getrusage sys/resource.h HAVE_GETRUSAGE)
74 check_symbol_exists(setrlimit sys/resource.h HAVE_SETRLIMIT)
75 check_function_exists(isatty HAVE_ISATTY)
76 check_symbol_exists(isinf cmath HAVE_ISINF_IN_CMATH)
77 check_symbol_exists(isinf math.h HAVE_ISINF_IN_MATH_H)
78 check_symbol_exists(isnan cmath HAVE_ISNAN_IN_CMATH)
79 check_symbol_exists(isnan math.h HAVE_ISNAN_IN_MATH_H)
80 check_symbol_exists(ceilf math.h HAVE_CEILF)
81 check_symbol_exists(floorf math.h HAVE_FLOORF)
82 check_symbol_exists(mallinfo malloc.h HAVE_MALLINFO)
83 check_symbol_exists(malloc_zone_statistics malloc/malloc.h
84                     HAVE_MALLOC_ZONE_STATISTICS)
85 check_symbol_exists(mkdtemp unistd.h HAVE_MKDTEMP)
86 check_symbol_exists(mkstemp unistd.h HAVE_MKSTEMP)
87 check_symbol_exists(mktemp unistd.h HAVE_MKTEMP)
88 check_symbol_exists(pthread_mutex_lock pthread.h HAVE_PTHREAD_MUTEX_LOCK)
89 check_symbol_exists(sbrk unistd.h HAVE_SBRK)
90 check_symbol_exists(strtoll stdlib.h HAVE_STRTOLL)
91 check_symbol_exists(strerror string.h HAVE_STRERROR)
92 check_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
93 check_symbol_exists(strerror_s string.h HAVE_STRERROR_S)
94 check_symbol_exists(setenv stdlib.h HAVE_SETENV)
95
96 check_symbol_exists(__GLIBC__ stdio.h LLVM_USING_GLIBC)
97 if( LLVM_USING_GLIBC )
98   add_llvm_definitions( -D_GNU_SOURCE )
99 endif()
100
101 # Type checks
102 check_type_exists(std::bidirectional_iterator<int,int> "iterator;iostream" HAVE_BI_ITERATOR)
103 check_type_exists(std::iterator<int,int,int> iterator HAVE_STD_ITERATOR)
104 check_type_exists(std::forward_iterator<int,int> iterator HAVE_FWD_ITERATOR)
105
106 set(headers "")
107 if (HAVE_SYS_TYPES_H)
108   set(headers ${headers} "sys/types.h")
109 endif()
110
111 if (HAVE_INTTYPES_H)
112   set(headers ${headers} "inttypes.h")
113 endif()
114
115 if (HAVE_STDINT_H)
116   set(headers ${headers} "stdint.h")
117 endif()
118
119 check_type_exists(uint64_t "${headers}" HAVE_UINT64_T)
120 check_type_exists(u_int64_t "${headers}" HAVE_U_INT64_T)
121
122 # Define LLVM_MULTITHREADED if gcc atomic builtins exists.
123 include(CheckAtomic)
124
125 include(CheckCXXCompilerFlag)
126 # On windows all code is position-independent and mingw warns if -fPIC
127 # is in the command-line.
128 if( NOT WIN32 )
129   check_cxx_compiler_flag("-fPIC" SUPPORTS_FPIC_FLAG)
130 endif()
131
132 include(GetTargetTriple)
133 get_target_triple(LLVM_HOSTTRIPLE)
134 message(STATUS "LLVM_HOSTTRIPLE: ${LLVM_HOSTTRIPLE}")
135
136 # Determine the native architecture.
137 string(TOLOWER "${LLVM_TARGET_ARCH}" LLVM_NATIVE_ARCH)
138 if( LLVM_NATIVE_ARCH STREQUAL "host" )
139   string(REGEX MATCH "^[^-]*" LLVM_NATIVE_ARCH ${LLVM_HOSTTRIPLE})
140 endif ()
141
142 if (LLVM_NATIVE_ARCH MATCHES "i[2-6]86")
143   set(LLVM_NATIVE_ARCH X86)
144 elseif (LLVM_NATIVE_ARCH STREQUAL "x86")
145   set(LLVM_NATIVE_ARCH X86)
146 elseif (LLVM_NATIVE_ARCH STREQUAL "amd64")
147   set(LLVM_NATIVE_ARCH X86)
148 elseif (LLVM_NATIVE_ARCH STREQUAL "x86_64")
149   set(LLVM_NATIVE_ARCH X86)
150 elseif (LLVM_NATIVE_ARCH MATCHES "sparc")
151   set(LLVM_NATIVE_ARCH Sparc)
152 elseif (LLVM_NATIVE_ARCH MATCHES "powerpc")
153   set(LLVM_NATIVE_ARCH PowerPC)
154 elseif (LLVM_NATIVE_ARCH MATCHES "alpha")
155   set(LLVM_NATIVE_ARCH Alpha)
156 elseif (LLVM_NATIVE_ARCH MATCHES "arm")
157   set(LLVM_NATIVE_ARCH ARM)
158 elseif (LLVM_NATIVE_ARCH MATCHES "mips")
159   set(LLVM_NATIVE_ARCH Mips)
160 elseif (LLVM_NATIVE_ARCH MATCHES "pic16")
161   set(LLVM_NATIVE_ARCH "PIC16")
162 elseif (LLVM_NATIVE_ARCH MATCHES "xcore")
163   set(LLVM_NATIVE_ARCH XCore)
164 elseif (LLVM_NATIVE_ARCH MATCHES "msp430")
165   set(LLVM_NATIVE_ARCH MSP430)
166 else ()
167   message(STATUS 
168     "Unknown architecture ${LLVM_NATIVE_ARCH}; lli will not JIT code")
169   set(LLVM_NATIVE_ARCH)
170 endif ()
171   
172 if (LLVM_NATIVE_ARCH)
173   list(FIND LLVM_TARGETS_TO_BUILD ${LLVM_NATIVE_ARCH} NATIVE_ARCH_IDX)
174   if (NATIVE_ARCH_IDX EQUAL -1)
175     message(STATUS 
176       "Native target ${LLVM_NATIVE_ARCH} is not selected; lli will not JIT code")
177     set(LLVM_NATIVE_ARCH)
178   else ()
179     message(STATUS "Native target architecture is ${LLVM_NATIVE_ARCH}")
180   endif ()
181 endif()
182
183 if( MINGW )
184   set(HAVE_LIBIMAGEHLP 1)
185   set(HAVE_LIBPSAPI 1)
186   # TODO: Check existence of libraries.
187   #   include(CheckLibraryExists)
188   #   CHECK_LIBRARY_EXISTS(imagehlp ??? . HAVE_LIBIMAGEHLP)
189 endif( MINGW )
190
191 if( MSVC )
192   set(error_t int)
193   set(mode_t "unsigned short")
194   set(LTDL_SHLIBPATH_VAR "PATH")
195   set(LTDL_SYSSEARCHPATH "")
196   set(LTDL_DLOPEN_DEPLIBS 1)
197   set(SHLIBEXT ".lib")
198   set(LTDL_OBJDIR "_libs")
199   set(HAVE_STRTOLL 1)
200   set(strtoll "_strtoi64")
201   set(strtoull "_strtoui64")
202   set(stricmp "_stricmp")
203   set(strdup "_strdup")
204 else( MSVC )
205   set(LTDL_SHLIBPATH_VAR "LD_LIBRARY_PATH")
206   set(LTDL_SYSSEARCHPATH "") # TODO
207   set(LTDL_DLOPEN_DEPLIBS 0)  # TODO
208 endif( MSVC )
209
210 # FIXME: Signal handler return type, currently hardcoded to 'void'
211 set(RETSIGTYPE void)
212
213 if( LLVM_ENABLE_THREADS )
214   if( HAVE_PTHREAD_H OR WIN32 )
215     set(ENABLE_THREADS 1)
216   endif()
217 endif()
218
219 if( ENABLE_THREADS )
220   message(STATUS "Threads enabled.")
221 else( ENABLE_THREADS )
222   message(STATUS "Threads disabled.")
223 endif()
224
225 configure_file(
226   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/config.h.cmake
227   ${LLVM_BINARY_DIR}/include/llvm/Config/config.h
228   )
229
230 configure_file(
231   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Support/DataTypes.h.cmake
232   ${LLVM_BINARY_DIR}/include/llvm/Support/DataTypes.h
233   )
234