Debian Codegraph @

cmake/dependencies.cmake

raw
1 # dynamips - check dependencies
2 # - libdl : maybe required
3 # - librt : maybe required
4 # - libsocket : maybe required
5 # - libelf : required
6 # - pthreads : required
7 # - libpcap/winpcap : optional
8 # accumulators:
9 # - DYNAMIPS_FLAGS
10 # - DYNAMIPS_DEFINITIONS
11 # - DYNAMIPS_INCLUDES
12 # - DYNAMIPS_LIBRARIES
13 # XXX assumes utils.cmake was included
14
15 message ( STATUS "dependencies - BEGIN" )
16
17 if ( "${CMAKE_C_COMPILER}" MATCHES "clang$" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" )
18 set ( CMAKE_COMPILER_IS_CLANG 1 )
19 endif()
20
21 # compiler check
22 if ( NOT CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_COMPILER_IS_CLANG AND NOT ANY_COMPILER )
23 print_variables (
24 CMAKE_COMPILER_IS_GNUCC
25 CMAKE_COMPILER_IS_CLANG
26 CMAKE_C_COMPILER
27 CMAKE_C_COMPILER_ABI
28 CMAKE_C_COMPILER_ID
29 CMAKE_C_COMPILER_VERSION
30 )
31 message ( FATAL_ERROR
32 "Not a GNU C or Clang compiler. "
33 "The source and build system assumes gcc or clang so it might not compile. "
34 "Invoke cmake with -DANY_COMPILER=1 to skip this check. "
35 )
36 endif ( NOT CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_COMPILER_IS_CLANG AND NOT ANY_COMPILER )
37
38 set ( DYNAMIPS_FLAGS -Wall -O2 -fomit-frame-pointer )
39 set ( DYNAMIPS_DEFINITIONS )
40 set ( DYNAMIPS_INCLUDES )
41 set ( DYNAMIPS_LIBRARIES ${CMAKE_DL_LIBS} )
42 if ( CYGWIN )
43 list ( APPEND DYNAMIPS_FLAGS -static -static-libgcc )
44 endif ( CYGWIN )
45 macro ( set_cmake_required )
46 set ( CMAKE_REQUIRED_FLAGS ${DYNAMIPS_FLAGS} )
47 set ( CMAKE_REQUIRED_DEFINITIONS ${DYNAMIPS_DEFINITIONS} )
48 set ( CMAKE_REQUIRED_INCLUDES ${DYNAMIPS_INCLUDES} )
49 set ( CMAKE_REQUIRED_LIBRARIES ${DYNAMIPS_LIBRARIES} )
50 endmacro ( set_cmake_required )
51
52 # Target architecture:
53 # - Use "amd64" to build for x86_64 (64-bit)
54 # - Use "x86" to build for x86 (32-bit)
55 # - Use "nojit" to build for other architectures (no recompilation)
56 # Based on https://github.com/petroules/solar-cmake/blob/master/TargetArch.cmake
57 set ( _code "
58 #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
59 int main (void) { return 0; }
60 #else
61 #error cmake_FAIL
62 #endif
63 " )
64 set_cmake_required ()
65 list ( INSERT CMAKE_REQUIRED_FLAGS 0 -m64 )
66 check_c_source_compiles ( "${_code}" ARCH_AMD64 )
67 set ( _code "
68 #if defined(__i386) || defined(__i386__) || defined(_M_IX86)
69 int main (void) { return 0; }
70 #else
71 #error cmake_FAIL
72 #endif
73 " )
74 set_cmake_required ()
75 list ( INSERT CMAKE_REQUIRED_FLAGS 0 -m32 )
76 check_c_source_compiles ( "${_code}" ARCH_X86 )
77 if ( ARCH_AMD64 )
78 set ( _default "amd64" )
79 elseif ( ARCH_X86 )
80 set ( _default "x86" )
81 else ()
82 set ( _default "nojit" )
83 endif ()
84 set ( DYNAMIPS_ARCH "${_default}" CACHE STRING "Target architecture (amd64;x86;nojit)" )
85 set_property ( CACHE DYNAMIPS_ARCH PROPERTY STRINGS "amd64" "x86" "nojit" )
86 if ( NOT DYNAMIPS_ARCH )
87 set ( DYNAMIPS_ARCH "${_default}" )
88 endif ()
89 if ( "amd64" STREQUAL "${DYNAMIPS_ARCH}" AND ARCH_AMD64 )
90 list ( INSERT DYNAMIPS_FLAGS 0 -m64 )
91 elseif ( "x86" STREQUAL "${DYNAMIPS_ARCH}" AND ARCH_X86 )
92 list ( INSERT DYNAMIPS_FLAGS 0 -m32 )
93 elseif ( NOT "nojit" STREQUAL "${DYNAMIPS_ARCH}" )
94 print_variables ( ARCH_AMD64 ARCH_X86 DYNAMIPS_ARCH )
95 message ( FATAL_ERROR "cannot build target arch DYNAMIPS_ARCH=${DYNAMIPS_ARCH}" )
96 endif ()
97 print_variables ( ARCH_AMD64 ARCH_X86 DYNAMIPS_ARCH )
98
99 # Compiler flags
100 foreach ( _flag
101 -mdynamic-no-pic # Mac OS X
102 )
103 standard_variable_name ( _var "FLAG_${_flag}" )
104 set_cmake_required ()
105 check_c_compiler_flag ( ${_flag} ${_var} )
106 if ( ${_var} )
107 list ( APPEND DYNAMIPS_FLAGS ${_flag} )
108 endif ()
109 endforeach ()
110
111 # librt (clock_gettime)
112 set_cmake_required ()
113 check_library_exists ( rt clock_gettime "time.h" USE_LIBRT )
114 if ( USE_LIBRT )
115 list ( APPEND DYNAMIPS_LIBRARIES rt )
116 print_variables ( USE_LIBRT )
117 endif ()
118
119 # libsocket (connect)
120 set_cmake_required ()
121 check_library_exists ( socket connect "sys/socket.h" USE_LIBSOCKET )
122 if ( USE_LIBSOCKET )
123 list ( APPEND DYNAMIPS_LIBRARIES socket )
124 print_variables ( USE_LIBSOCKET )
125 endif ()
126
127 # libnsl (gethostbyname)
128 set_cmake_required ()
129 check_library_exists ( nsl gethostbyname "netdb.h" USE_LIBNSL )
130 if ( USE_LIBNSL )
131 list ( APPEND DYNAMIPS_LIBRARIES nsl )
132 print_variables ( USE_LIBNSL )
133 endif ()
134
135 # libelf
136 set_cmake_required ()
137 find_package ( LibElf REQUIRED )
138 print_variables ( LIBELF_FOUND LIBELF_INCLUDE_DIRS LIBELF_LIBRARIES LIBELF_DEFINITIONS )
139 # make sure it can be used
140 set_cmake_required ()
141 list ( APPEND CMAKE_REQUIRED_DEFINITIONS ${LIBELF_DEFINITIONS} )
142 list ( APPEND CMAKE_REQUIRED_INCLUDES ${LIBELF_INCLUDE_DIRS} )
143 check_arch_library ( LIBELF_VALID elf_begin "libelf.h" LIBELF_LIBRARIES elf )
144 if ( NOT LIBELF_VALID )
145 bad_arch_library ( FATAL_ERROR "libelf" "LIBELF_INCLUDE_DIRS and LIBELF_LIBRARIES" )
146 endif ()
147 list ( APPEND DYNAMIPS_DEFINITIONS ${LIBELF_DEFINITIONS} )
148 list ( APPEND DYNAMIPS_INCLUDES ${LIBELF_INCLUDE_DIRS} )
149 list ( APPEND DYNAMIPS_LIBRARIES ${LIBELF_LIBRARIES} )
150 # XXX some old libelf's aren't large file aware with ILP32
151 set ( _code "
152 #define _FILE_OFFSET_BITS 64
153 #define _LARGEFILE_SOURCE
154 #define _LARGEFILE_SOURCE64
155 #include <libelf.h>
156 int main() { return 0; }
157 " )
158 set_cmake_required ()
159 check_c_source_compiles ( "${_code}" LIBELF_LARGEFILE )
160 print_variables ( LIBELF_LARGEFILE )
161
162 # pthreads
163 set ( CMAKE_THREAD_PREFER_PTHREAD 1 )
164 set_cmake_required ()
165 find_package ( Threads REQUIRED )
166 if ( CMAKE_USE_PTHREADS_INIT )
167 list ( APPEND DYNAMIPS_LIBRARIES ${CMAKE_THREAD_LIBS_INIT} )
168 print_variables ( CMAKE_THREAD_LIBS_INIT CMAKE_USE_PTHREADS_INIT )
169 # ok
170 else ()
171 print_variables ( CMAKE_THREAD_LIBS_INIT CMAKE_USE_SPROC_INIT
172 CMAKE_USE_WIN32_THREADS_INIT CMAKE_USE_PTHREADS_INIT
173 CMAKE_HP_PTHREADS_INIT )
174 message ( FATAL_ERROR
175 "Not using pthreads. "
176 "The source assumes pthreads is available. "
177 )
178 endif ()
179
180 # libpcap/winpcap (optional)
181 set_cmake_required ()
182 find_package ( PCAP )
183 print_variables ( PCAP_FOUND PCAP_INCLUDE_DIRS PCAP_LIBRARIES )
184 set ( HAVE_PCAP ${PCAP_FOUND} )
185 if ( HAVE_PCAP )
186 # make sure it can be used
187 set_cmake_required ()
188 list ( APPEND CMAKE_REQUIRED_INCLUDES ${PCAP_INCLUDE_DIRS} )
189 check_arch_library ( PCAP_VALID pcap_open_live "pcap.h" PCAP_LIBRARIES pcap wpcap )
190 if ( NOT PCAP_VALID )
191 bad_arch_library ( WARNING "pcap/wpcap" "PCAP_INCLUDE_DIRS and PCAP_LIBRARIES" )
192 endif ()
193 set ( HAVE_PCAP ${PCAP_VALID} )
194 endif ()
195 if ( HAVE_PCAP AND CYGWIN )
196 # cygwin requires pcap_open
197 set_cmake_required ()
198 list ( APPEND CMAKE_REQUIRED_DEFINITIONS "-DHAVE_REMOTE" )
199 list ( APPEND CMAKE_REQUIRED_INCLUDES ${PCAP_INCLUDE_DIRS} )
200 list ( APPEND CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARIES} )
201 check_function_exists ( pcap_open HAVE_PCAP_OPEN )
202 set ( HAVE_PCAP ${HAVE_PCAP_OPEN} )
203 endif ()
204 print_variables ( HAVE_PCAP )
205
206 # headers
207 # TODO minimize headers in the source
208 set ( _missing )
209 foreach ( _header #standalone
210 "arpa/inet.h"
211 "arpa/telnet.h" #dev_vtty.c
212 "assert.h"
213 "ctype.h"
214 "dlfcn.h" #plugin.c
215 "errno.h"
216 "fcntl.h"
217 "getopt.h" #dynamips.c
218 "glob.h"
219 #"libelf.h" #find_package
220 #"linux/if.h" #linux_eth.c (LINUX_ETH)
221 #"linux/if_packet.h" #linux_eth.c (LINUX_ETH)
222 "netdb.h"
223 #"netinet/if_ether.h" #linux_eth.c (LINUX_ETH)
224 #"netinet/tcp.h" #dev_vtty.c
225 #"pcap.h" #find_package (GEN_ETH)
226 #"pthread.h" #find_package
227 "setjmp.h"
228 "signal.h"
229 "stdarg.h"
230 "stddef.h"
231 "stdio.h"
232 "stdlib.h"
233 "string.h"
234 "sys/ioctl.h"
235 "sys/mman.h" #utils.c
236 "sys/select.h"
237 "sys/socket.h"
238 "sys/stat.h"
239 "sys/time.h"
240 "sys/types.h"
241 "sys/uio.h" #atm_vsar.c
242 "sys/un.h"
243 "sys/wait.h"
244 "termios.h"
245 "time.h"
246 "unistd.h"
247 )
248 standard_variable_name ( _var "HAVE_${_header}" )
249 set_cmake_required ()
250 check_include_file ( "${_header}" ${_var} )
251 if ( NOT "${${_var}}" )
252 print_variables ( ${_var} )
253 list ( APPEND _missing ${_header} )
254 endif ()
255 endforeach ()
256 if ( HAVE_SYS_TYPES_H )
257 foreach ( _header #requires sys/types.h
258 "netinet/tcp.h"
259 )
260 standard_variable_name ( _var "HAVE_${_header}" )
261 set_cmake_required ()
262 check_include_files ( "sys/types.h;${_header}" ${_var} )
263 if ( NOT "${${_var}}" )
264 print_variables ( ${_var} )
265 list ( APPEND _missing ${_header} )
266 endif ()
267 endforeach ()
268 endif ( HAVE_SYS_TYPES_H )
269 if ( _missing )
270 message ( FATAL_ERROR "missing headers: ${_missing}" )
271 endif ( _missing )
272
273 # posix_memalign
274 check_function_exists ( posix_memalign HAVE_POSIX_MEMALIGN )
275 if ( HAVE_POSIX_MEMALIGN )
276 list ( APPEND DYNAMIPS_DEFINITIONS "-DHAS_POSIX_MEMALIGN=1" )
277 else ()
278 list ( APPEND DYNAMIPS_DEFINITIONS "-DHAS_POSIX_MEMALIGN=0" )
279 endif ()
280 print_variables ( HAVE_POSIX_MEMALIGN )
281
282 # IPv6 (RFC 2553)
283 set ( _headers "sys/socket.h" "arpa/inet.h" "net/if.h" "netdb.h"
284 "netinet/in.h" )
285 check_include_files ( "${_headers}" IPV6_HEADERS )
286 check_function_exists ( getaddrinfo HAVE_GETADDRINFO )
287 check_function_exists ( freeaddrinfo HAVE_FREEADDRINFO )
288 check_function_exists ( gai_strerror HAVE_GAI_STRERROR )
289 check_function_exists ( inet_pton HAVE_INET_PTON )
290 check_function_exists ( inet_ntop HAVE_INET_NTOP )
291 # TODO AF_INET6, PF_INET6, AI_PASSIVE, IPPROTO_IPV6, IPV6_JOIN_GROUP,
292 # IPV6_MULTICAST_HOPS, INET6_ADDRSTRLEN, struct sockaddr_storage,
293 # struct sockaddr_in6, struct ipv6_mreq
294 if ( IPV6_HEADERS AND HAVE_GETADDRINFO AND HAVE_FREEADDRINFO
295 AND HAVE_GAI_STRERROR AND HAVE_INET_PTON AND HAVE_INET_NTOP )
296 set ( HAVE_IPV6 1 )
297 else ()
298 set ( HAVE_IPV6 0 )
299 endif ()
300 print_variables ( HAVE_IPV6 )
301
302 message ( STATUS "dependencies - END" )