]> The Tcpdump Group git mirrors - libpcap/blob - CMakeLists.txt
Clean up the ether_hostton() stuff.
[libpcap] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8.6)
2
3 #
4 # Apple doesn't build with an install_name starting with @rpath, and
5 # neither do we with autotools; don't do so with CMake, either, and
6 # suppress warnings about that.
7 #
8 if(POLICY CMP0042)
9 cmake_policy(SET CMP0042 OLD)
10 endif()
11
12 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
13
14 project(pcap)
15 #
16 # Call the library "wpcap" on Windows, for backwards compatibility.
17 #
18 if(WIN32)
19 set(LIBRARY_NAME wpcap)
20 else()
21 set(LIBRARY_NAME pcap)
22 endif()
23
24 ###################################################################
25 # Parameters
26 ###################################################################
27
28 option(INET6 "Enable IPv6" ON)
29 if(MSVC)
30 option(USE_STATIC_RT "Use static Runtime" ON)
31 endif(MSVC)
32 option(BUILD_SHARED_LIBS "Build shared libraries" ON)
33 if(WIN32)
34 set(PACKET_DLL_DIR "" CACHE PATH "Path to directory with include and lib subdirectories for packet.dll")
35 endif(WIN32)
36
37 #
38 # Default to having remote capture support on Windows and, for now, to
39 # not having it on UN*X.
40 #
41 if(WIN32)
42 option(HAVE_REMOTE "Enable remote capture" ON)
43 else()
44 option(HAVE_REMOTE "Enable remote capture" OFF)
45 endif(WIN32)
46
47 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
48 option(BUILD_WITH_LIBNL "Build with libnl" ON)
49 endif()
50
51 ######################################
52 # Project settings
53 ######################################
54
55 add_definitions(-DHAVE_CONFIG_H)
56
57 include_directories(
58 ${CMAKE_CURRENT_BINARY_DIR}
59 ${pcap_SOURCE_DIR}
60 )
61
62 include(CheckFunctionExists)
63
64 if(WIN32)
65
66 find_package(Packet)
67 if(PACKET_FOUND)
68 set(HAVE_PACKET32 TRUE)
69 set(CMAKE_REQUIRED_LIBRARIES ${PACKET_LIBRARY})
70 include_directories(${PACKET_INCLUDE_DIR})
71 #
72 # Check whether we have the NPcap PacketIsLoopbackAdapter()
73 # function.
74 #
75 check_function_exists(PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER)
76 endif(PACKET_FOUND)
77
78 include_directories(
79 ../Common/
80 Win32/Include
81 Win32/WpdPack/Include
82 )
83 endif(WIN32)
84
85 add_definitions(-DBUILDING_PCAP)
86
87 if(MSVC)
88 add_definitions(-D__STDC__)
89 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
90 add_definitions("-D_U_=")
91 elseif(CMAKE_COMPILER_IS_GNUCXX)
92 add_definitions("-D_U_=__attribute__((unused))")
93 else(MSVC)
94 add_definitions("-D_U_=")
95 endif(MSVC)
96
97 if(MSVC)
98 if(USE_STATIC_RT)
99 message(STATUS "Use STATIC runtime")
100 foreach(RT_FLAG
101 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
102 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
103 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
104 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
105 string(REGEX REPLACE "/MD" "/MT" ${RT_FLAG} "${${RT_FLAG}}")
106 endforeach(RT_FLAG)
107 else (USE_STATIC_RT)
108 message(STATUS "Use DYNAMIC runtime")
109 endif(USE_STATIC_RT)
110 endif(MSVC)
111
112 ###################################################################
113 # Detect available platform features
114 ###################################################################
115
116 include(CheckIncludeFile)
117 include(CheckStructHasMember)
118 include(CheckTypeSize)
119
120 #
121 # Header files.
122 #
123 check_include_file(inttypes.h HAVE_INTTYPES_H)
124 check_include_file(stdint.h HAVE_STDINT_H)
125 check_include_file(unistd.h HAVE_UNISTD_H)
126 if(NOT HAVE_UNISTD_H)
127 add_definitions(-DYY_NO_UNISTD_H)
128 endif(NOT HAVE_UNISTD_H)
129 check_include_file(bitypes.h HAVE_SYS_BITYPES_H)
130 check_include_file(sys/ioccom.h HAVE_SYS_IOCCOM_H)
131 check_include_file(sys/sockio.h HAVE_SYS_SOCKIO_H)
132 check_include_file(sys/select.h HAVE_SYS_SELECT_H)
133 check_include_file(limits.h HAVE_LIMITS_H)
134 check_include_file(netpacket/packet.h HAVE_NETPACKET_PACKET_H)
135 check_include_file(net/pfvar.h HAVE_NET_PFVAR_H)
136 if(HAVE_NET_PFVAR_H)
137 check_c_source_compiles(
138 "#include <sys/types.h>
139 #include <sys/socket.h>
140 #include <net/if.h>
141 #include <net/pfvar.h>
142
143 int
144 main(void)
145 {
146 return PF_NAT+PF_NONAT+PF_BINAT+PF_NOBINAT+PF_RDR+PF_NORDR;
147 }
148 "
149 HAVE_PF_NAT_THROUGH_PF_NORDR)
150 endif()
151 check_include_file(netinet/if_ether.h HAVE_NETINET_IF_ETHER_H)
152 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
153 check_include_file(linux/sockios.h HAVE_LINUX_SOCKIOS_H)
154 check_include_file(linux/if_bonding.h HAVE_LINUX_IF_BONDING_H)
155 endif()
156
157 #
158 # Functions.
159 #
160 check_function_exists(strerror HAVE_STRERROR)
161 check_function_exists(strlcpy HAVE_STRLCPY)
162 check_function_exists(strlcat HAVE_STRLCAT)
163 check_function_exists(snprintf HAVE_SNPRINTF)
164 check_function_exists(vsnprintf HAVE_VSNPRINTF)
165 check_function_exists(strtok_r HAVE_STRTOK_R)
166
167 #
168 # Find library needed for gethostbyname.
169 # NOTE: if you hand check_library_exists as its last argument a variable
170 # that's been set, it skips the test, so we need different variables.
171 #
172 include(CheckLibraryExists)
173 check_function_exists(gethostbyname STDLIBS_HAVE_GETHOSTBYNAME)
174 if(NOT STDLIBS_HAVE_GETHOSTBYNAME)
175 check_library_exists(nsl gethostbyname "" LIBNSL_HAS_GETHOSTBYNAME)
176 if(LIBNSL_HAS_GETHOSTBYNAME)
177 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} nsl)
178 endif()
179 endif()
180
181 #
182 # Data types.
183 #
184 # XXX - there's no check_struct() macro that's like check_struct_has_member()
185 # except that it only checks for the existence of the structure type,
186 # so we use check_struct_has_member() and look for ss_family.
187 #
188 check_struct_has_member("struct sockaddr_storage" ss_family sys/socket.h HAVE_SOCKADDR_STORAGE)
189 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h sys/socket.h)
190 check_type_size("socklen_t" SOCKLEN_T)
191 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h)
192
193 #
194 # Structure fields.
195 #
196 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_SOCKADDR_SA_LEN)
197
198 #
199 # You are in a twisty little maze of UN*Xes, all different.
200 # Some might not have ether_hostton().
201 # Some might have it and declare it in <net/ethernet.h>.
202 # Some might have it and declare it in <netinet/ether.h>
203 # Some might have it and declare it in <sys/ethernet.h>.
204 # Some might have it and declare it in <arpa/inet.h>.
205 # Some might have it and declare it in <netinet/if_ether.h>.
206 # Some might have it and not declare it in any header file.
207 #
208 # Before you is a C compiler.
209 #
210 include(CheckSymbolExists)
211 check_function_exists(ether_hostton HAVE_ETHER_HOSTTON)
212 if(HAVE_ETHER_HOSTTON)
213 #
214 # OK, we have ether_hostton(). Is it declared in <net/ethernet.h>?
215 #
216 # This test fails if we don't have <net/ethernet.h> or if we do
217 # but it doesn't declare ether_hostton().
218 #
219 check_symbol_exists(ether_hostton net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
220 if(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
221 #
222 # Yes - we have it declared.
223 #
224 set(HAVE_DECL_ETHER_HOSTTON TRUE)
225 endif()
226 #
227 # Did that succeed?
228 #
229 if(NOT HAVE_DECL_ETHER_HOSTTON)
230 #
231 # No - how about <netinet/ether.h>, as on Linux?
232 #
233 # This test fails if we don't have <netinet/ether.h>
234 # or if we do but it doesn't declare ether_hostton().
235 #
236 check_symbol_exists(ether_hostton netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
237 if(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
238 #
239 # Yes - we have it declared.
240 #
241 set(HAVE_DECL_ETHER_HOSTTON TRUE)
242 endif()
243 endif()
244 #
245 # Did that succeed?
246 #
247 if(NOT HAVE_DECL_ETHER_HOSTTON)
248 #
249 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
250 #
251 # This test fails if we don't have <sys/ethernet.h>
252 # or if we do but it doesn't declare ether_hostton().
253 #
254 check_symbol_exists(ether_hostton sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
255 if(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
256 #
257 # Yes - we have it declared.
258 #
259 set(HAVE_DECL_ETHER_HOSTTON TRUE)
260 endif()
261 endif()
262 #
263 # Did that succeed?
264 #
265 if(NOT HAVE_DECL_ETHER_HOSTTON)
266 #
267 # No, how about <arpa/inet.h>, as on AIX?
268 #
269 # This test fails if we don't have <arpa/inet.h>
270 # or if we do but it doesn't declare ether_hostton().
271 #
272 check_symbol_exists(ether_hostton arpa/inet.h ARPA_INET_H_DECLARES_ETHER_HOSTTON)
273 if(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
274 #
275 # Yes - we have it declared.
276 #
277 set(HAVE_DECL_ETHER_HOSTTON TRUE)
278 endif()
279 endif()
280 #
281 # Did that succeed?
282 #
283 if(NOT HAVE_DECL_ETHER_HOSTTON)
284 #
285 # No, how about <netinet/if_ether.h>?
286 # On some platforms, it requires <net/if.h> and
287 # <netinet/in.h>, and we always include it with
288 # both of them, so test it with both of them.
289 #
290 # This test fails if we don't have <netinet/if_ether.h>
291 # and the headers we include before it, or if we do but
292 # <netinet/if_ether.h> doesn't declare ether_hostton().
293 #
294 check_symbol_exists(ether_hostton "sys/types.h;sys/socket.h;net/if.h;netinet/in.h;netinet/if_ether.h" NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
295 if(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
296 #
297 # Yes - we have it declared.
298 #
299 set(HAVE_DECL_ETHER_HOSTTON TRUE)
300 endif()
301 endif()
302 #
303 # After all that, is ether_hostton() declared?
304 #
305 if(NOT HAVE_DECL_ETHER_HOSTTON)
306 #
307 # No, we'll have to declare it ourselves.
308 # Do we have "struct ether_addr" if we include<netinet/if_ether.h>?
309 #
310 check_struct_has_member("struct ether_addr" octet "sys/types.h;sys/socket.h;net/if.h;netinet/in.h;netinet/if_ether.h" HAVE_STRUCT_ETHER_ADDR)
311 endif()
312 endif()
313
314 #
315 # Large file support on UN*X, a/k/a LFS.
316 #
317 if(NOT WIN32)
318 include(FindLFS)
319 if(LFS_FOUND)
320 #
321 # Add the required #defines.
322 #
323 add_definitions(${LFS_DEFINITIONS})
324 endif()
325 endif()
326
327 if(INET6)
328 message(STATUS "Use IPv6")
329 endif(INET6)
330
331 if(WIN32)
332 add_definitions(-DHAVE_ADDRINFO)
333 endif(WIN32)
334
335 ######################################
336 # External dependencies
337 ######################################
338
339 ######################################
340 # Input files
341 ######################################
342
343 set(PROJECT_SOURCE_LIST_C
344 bpf_dump.c
345 bpf_image.c
346 etherent.c
347 gencode.c
348 nametoaddr.c
349 optimize.c
350 pcap-common.c
351 pcap.c
352 savefile.c
353 sf-pcap-ng.c
354 sf-pcap.c
355 bpf/net/bpf_filter.c
356 )
357
358 if(WIN32)
359 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/win_snprintf.c)
360 else()
361 if(NOT HAVE_SNPRINTF)
362 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/snprintf.c)
363 endif(NOT HAVE_SNPRINTF)
364 if(NOT HAVE_STRTOK_R)
365 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/strtok_r.c)
366 endif(NOT HAVE_STRTOK_R)
367 endif(WIN32)
368
369 if(HAVE_REMOTE)
370 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C}
371 pcap-new.c pcap-rpcap.c sockutils.c)
372 endif(HAVE_REMOTE)
373
374 #
375 # Determine the main pcap-XXX.c file to use, and the libraries with
376 # which we need to link libpcap, if any.
377 #
378 if(WIN32)
379 #
380 # Windows; we need to link with WinSock2.
381 #
382 set(PCAP_LINK_LIBRARIES ws2_32)
383
384 #
385 # Check whether we have packet.dll.
386 #
387 if(HAVE_PACKET32)
388 #
389 # We have packet.dll.
390 # Set the capture type to NPF.
391 #
392 set(PCAP_TYPE npf)
393 else()
394 #
395 # We don't have any capture type we know about, so just use
396 # the null capture type, and only support reading (and writing)
397 # capture files.
398 #
399 set(PCAP_TYPE null)
400 endif()
401 else()
402 #
403 # UN*X.
404 #
405 # Figure out what type of packet capture mechanism we have, and
406 # what libraries we'd need to link libpcap with, if any.
407 #
408 set(PCAP_LINK_LIBRARIES "")
409
410 #
411 # Check for a bunch of headers for various packet capture mechanisms.
412 #
413 include(CheckIncludeFiles)
414 check_include_files("sys/types.h;net/bpf.h" HAVE_NET_BPF_H)
415 check_include_file(net/pfilt.h HAVE_NET_PFILT_H)
416 check_include_file(net/enet.h HAVE_NET_ENET_H)
417 check_include_file(net/nit.h HAVE_NET_NIT_H)
418 check_include_file(sys/net/nit.h HAVE_SYS_NET_NIT_H)
419 check_include_file(linux/socket.h HAVE_LINUX_SOCKET_H)
420 check_include_file(net/raw.h HAVE_NET_RAW_H)
421 check_include_file(sys/dlpi.h HAVE_SYS_DLPI_H)
422
423 if(HAVE_NET_BPF_H)
424 #
425 # BPF.
426 # Check this before DLPI, so that we pick BPF on
427 # Solaris 11 and later.
428 #
429 set(PCAP_TYPE bpf)
430 elseif(HAVE_NET_PFILT_H)
431 #
432 # DEC OSF/1, Digital UNIX, Tru64 UNIX
433 #
434 set(PCAP_TYPE pf)
435 elseif(HAVE_NET_ENET_H)
436 #
437 # Stanford Enetfilter.
438 #
439 set(PCAP_TYPE enet)
440 elseif(HAVE_NET_NIT_H)
441 #
442 # SunOS 4.x STREAMS NIT.
443 #
444 set(PCAP_TYPE snit)
445 elseif(HAVE_SYS_NET_NIT_H)
446 #
447 # Pre-SunOS 4.x non-STREAMS NIT.
448 #
449 set(PCAP_TYPE nit)
450 elseif(HAVE_LINUX_SOCKET_H)
451 #
452 # No prizes for guessing this one.
453 #
454 set(PCAP_TYPE linux)
455 elseif(HAVE_NET_RAW_H)
456 #
457 # IRIX snoop.
458 #
459 set(PCAP_TYPE snoop)
460 elseif(HAVE_SYS_DLPI_H)
461 #
462 # DLPI on pre-Solaris 11 SunOS 5, HP-UX, possibly others.
463 #
464 set(PCAP_TYPE dlpi)
465 else()
466 #
467 # Nothing we support.
468 #
469 set(PCAP_TYPE null)
470 endif()
471 endif(WIN32)
472 message(STATUS "Packet capture mechanism type: ${PCAP_TYPE}")
473 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-${PCAP_TYPE}.c)
474
475
476 #
477 # Do capture-mechanism-dependent tests.
478 #
479 if(PCAP_TYPE STREQUAL "npf")
480 #
481 # Link with packet.dll before WinSock2.
482 #
483 set(PCAP_LINK_LIBRARIES ${PACKET_LIBRARY} ${PCAP_LINK_LIBRARIES})
484 elseif(PCAP_TYPE STREQUAL "dlpi")
485 #
486 # Needed for common functions used by pcap-[dlpi,libdlpi].c
487 #
488 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} dlpisubs.c)
489
490 #
491 # Checks for some header files.
492 #
493 check_include_file(sys/bufmod.h HAVE_SYS_BUFMOD_H)
494 check_include_file(sys/dlpi_ext.h HAVE_SYS_DLPI_EXT_H)
495
496 #
497 # Checks to see if Solaris has the public libdlpi(3LIB) library.
498 # Note: The existence of /usr/include/libdlpi.h does not mean it is the
499 # public libdlpi(3LIB) version. Before libdlpi was made public, a
500 # private version also existed, which did not have the same APIs.
501 # Due to a gcc bug, the default search path for 32-bit libraries does
502 # not include /lib, we add it explicitly here.
503 # [http://bugs.opensolaris.org/view_bug.do?bug_id=6619485].
504 # Also, due to the bug above applications that link to libpcap with
505 # libdlpi will have to add "-L/lib" option to "configure".
506 #
507 cmake_push_check_state()
508 set(CMAKE_REQUIRED_FLAGS "-L/lib")
509 set(CMAKE_REQUIRED_LIBRARIES dlpi)
510 check_function_exists(dlpi_walk HAVE_LIBDLPI)
511 cmake_pop_check_state()
512 if(HAVE_LIBDLPI)
513 #
514 # XXX - add -L/lib
515 #
516 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} dlpi)
517 set(PCAP_TYPE libdlpi)
518 endif()
519
520 #
521 # This check is for Solaris with DLPI support for passive modes.
522 # See dlpi(7P) for more details.
523 #
524 # XXX - there's no check_struct() macro that's like
525 # check_struct_has_member() except that it only checks for the
526 # existence of the structure type, so we use
527 # check_struct_has_member() and look for dl_primitive.
528 #
529 check_struct_has_member("dl_passive_req_t" dl_primitive "sys/types.h;sys/dlpi.h" HAVE_DLPI_PASSIVE)
530 elseif(PCAP_TYPE STREQUAL "linux")
531 #
532 # Do we have the wireless extensions?
533 #
534 check_include_file(linux/wireless.h HAVE_LINUX_WIRELESS_H)
535
536 #
537 # Do we have libnl?
538 #
539 if(BUILD_WITH_LIBNL)
540 #
541 # Try libnl 3.x first.
542 #
543 cmake_push_check_state()
544 set(CMAKE_REQUIRED_LIBRARIES nl-3)
545 check_function_exists(nl_socket_alloc HAVE_LIBNL)
546 cmake_pop_check_state()
547 if(HAVE_LIBNL)
548 #
549 # Yes, we have libnl 3.x.
550 #
551 set(PCAP_LINK_LIBRARIES nl-genl-3 nl-3 ${PCAP_LINK_LIBRARIES})
552 set(HAVE_LIBNL_3_x ON)
553 set(HAVE_LIBNL_NLE ON)
554 set(HAVE_LIBNL_SOCKETS ON)
555 include_directories("/usr/include/libnl3")
556 else()
557 #
558 # Try libnl 2.x.
559 #
560 cmake_push_check_state()
561 set(CMAKE_REQUIRED_LIBRARIES nl)
562 check_function_exists(nl_socket_alloc HAVE_LIBNL)
563 cmake_pop_check_state()
564 if(HAVE_LIBNL)
565 #
566 # Yes, we have libnl 2.x.
567 #
568 set(PCAP_LINK_LIBRARIES nl-genl nl ${PCAP_LINK_LIBRARIES})
569 set(HAVE_LIBNL_2_x ON)
570 set(HAVE_LIBNL_NLE ON)
571 set(HAVE_LIBNL_SOCKETS ON)
572 else()
573 #
574 # No, we don't; do we have libnl 1.x?
575 #
576 cmake_push_check_state()
577 set(CMAKE_REQUIRED_LIBRARIES nl)
578 check_function_exists(nl_handle_alloc HAVE_LIBNL)
579 cmake_pop_check_state()
580 if(HAVE_LIBNL)
581 set(PCAP_LINK_LIBRARIES nl ${PCAP_LINK_LIBRARIES})
582 endif()
583 endif()
584 endif()
585 endif()
586
587 check_include_file(linux/ethtool.h HAVE_LINUX_ETHTOOL_H)
588
589 #
590 # Checks to see if tpacket_stats is defined in linux/if_packet.h
591 # If so then pcap-linux.c can use this to report proper statistics.
592 #
593 check_struct_has_member("struct tpacket_stats" tp_packets linux/if_packet.h HAVE_TPACKET_STATS)
594
595 check_struct_has_member("struct tpacket_auxdata" tp_vlan_tci linux/if_packet.h HAVE_LINUX_TPACKET_AUXDATA_TP_VLAN_TCI)
596 if(HAVE_LINUX_TPACKET_AUXDATA_TP_VLAN_TCI)
597 set(HAVE_LINUX_TPACKET_AUXDATA tp_vlan_tci)
598 endif()
599 elseif(PCAP_TYPE STREQUAL "bpf")
600 #
601 # Check whether we have the *BSD-style ioctls.
602 #
603 check_include_files("sys/types.h;net/if_media.h" HAVE_NET_IF_MEDIA_H)
604
605 #
606 # Check whether we have struct BPF_TIMEVAL.
607 #
608 if(HAVE_SYS_IOCCOM_H)
609 check_struct_has_member("struct BPF_TIMEVAL" tv_sec "sys/types.h;sys/ioccom.h;net/bpf.h" HAVE_STRUCT_BPF_TIMEVAL)
610 else()
611 check_struct_has_member("struct BPF_TIMEVAL" tv_sec "sys/types.h;net/bpf.h" HAVE_STRUCT_BPF_TIMEVAL)
612 endif()
613 endif()
614
615 #
616 # Now figure out how we get a list of interfaces and addresses,
617 # if we support capturing. Don't bother if we don't support
618 # capturing.
619 #
620 if(NOT WIN32)
621 #
622 # UN*X - figure out what type of interface list mechanism we
623 # have.
624 #
625 # If the capture type is null, that means we can't capture,
626 # so we can't open any capture devices, so we won't return
627 # any interfaces.
628 #
629 if(NOT PCAP_TYPE STREQUAL "null")
630 check_function_exists(getifaddrs HAVE_GETIFADDRS)
631 if(NOT HAVE_GETIFADDRS)
632 #
633 # It's in libsocket on Solaris and possibly other OSes;
634 # check there.
635 #
636 # NOTE: if you hand check_library_exists as its last
637 # argument a variable that's been set, it skips the test,
638 # so we need different variables.
639 #
640 check_library_exists(socket getifaddrs "" SOCKET_HAS_GETIFADDRS)
641 if(SOCKET_HAS_GETIFADDRS)
642 set(PCAP_LINK_LIBRARIES socket ${PCAP_LINK_LIBRARIES})
643 set(HAVE_GETIFADDRS TRUE)
644 endif()
645 endif()
646 if(HAVE_GETIFADDRS)
647 #
648 # We have "getifaddrs()"; make sure we have <ifaddrs.h>
649 # as well, just in case some platform is really weird.
650 # It may require that sys/types.h be included first,
651 # so include it first.
652 #
653 check_include_files("sys/types.h;ifaddrs.h" HAVE_IFADDRS_H)
654 if(HAVE_IFADDRS_H)
655 #
656 # We have the header, so we use "getifaddrs()" to
657 # get the list of interfaces.
658 #
659 set(FINDALLDEVS_TYPE getad)
660 else()
661 #
662 # We don't have the header - give up.
663 # XXX - we could also fall back on some other
664 # mechanism, but, for now, this'll catch this
665 # problem so that we can at least try to figure
666 # out something to do on systems with "getifaddrs()"
667 # but without "ifaddrs.h", if there is something
668 # we can do on those systems.
669 #
670 message(FATAL_ERROR "Your system has getifaddrs() but doesn't have a usable <ifaddrs.h>.")
671 endif()
672 else()
673 #
674 # Well, we don't have "getifaddrs()", so we have to use
675 # some other mechanism; determine what that mechanism is.
676 #
677 # The first thing we use is the type of capture mechanism,
678 # which is somewhat of a proxy for the OS we're using.
679 #
680 if(PCAP_TYPE STREQUAL "dlpi" OR PCAP_TYPE STREQUAL "libdlpi")
681 #
682 # This might be Solaris 8 or later, with
683 # SIOCGLIFCONF, or it might be some other OS
684 # or some older version of Solaris, with
685 # just SIOCGIFCONF.
686 #
687 try_compile(HAVE_SIOCGLIFCONF ${CMAKE_CURRENT_BINARY_DIR} "${pcap_SOURCE_DIR}/cmake/have_siocglifconf.c" )
688 if(HAVE_SIOCGLIFCONF)
689 set(FINDALLDEVS_TYPE glifc)
690 else()
691 set(FINDALLDEVS_TYPE gifc)
692 endif()
693 else()
694 #
695 # Assume we just have SIOCGIFCONF.
696 # (XXX - on at least later Linux kernels, there's
697 # another mechanism, and we should be using that
698 # instead.)
699 #
700 set(FINDALLDEVS_TYPE gifc)
701 endif()
702 endif()
703 message(STATUS "Find-interfaces mechanism type: ${FINDALLDEVS_TYPE}")
704 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} fad-${FINDALLDEVS_TYPE}.c)
705 endif()
706 endif()
707
708 file(GLOB PROJECT_SOURCE_LIST_CORE_H
709 *.h
710 pcap/*.h
711 )
712 set(PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_CORE_H})
713
714 if(WIN32)
715 file(GLOB PROJECT_SOURCE_LIST_WIN32_H
716 Win32/Include/*.h
717 )
718 set(PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_WIN32_H})
719 endif(WIN32)
720
721 #
722 # {Flex} and YACC/Berkeley YACC/Bison.
723 # From a mail message to the CMake mailing list by Andy Cedilnik of
724 # Kitware.
725 #
726
727 #
728 # Try to find Flex, a Windows version of Flex, or Lex.
729 #
730 find_program(LEX_EXECUTABLE NAMES flex win_flex lex)
731 if(LEX_EXECUTABLE STREQUAL "LEX_EXECUTABLE-NOTFOUND")
732 message(FATAL_ERROR "Neither flex nor win_flex nor lex was found.")
733 endif()
734 message(STATUS "Lexical analyzer generator: ${LEX_EXECUTABLE}")
735
736 add_custom_command(
737 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/scanner.c ${CMAKE_CURRENT_BINARY_DIR}/scanner.h
738 SOURCE ${pcap_SOURCE_DIR}/scanner.l
739 COMMAND ${LEX_EXECUTABLE} -P pcap_ --header-file=scanner.h --nounput -o${CMAKE_CURRENT_BINARY_DIR}/scanner.c ${pcap_SOURCE_DIR}/scanner.l
740 DEPENDS ${pcap_SOURCE_DIR}/scanner.l
741 )
742
743 #
744 # Since scanner.c does not exist yet when cmake is run, mark
745 # it as generated.
746 #
747 # Since scanner.c includes grammar.h, mark that as a dependency.
748 #
749 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/scanner.c PROPERTIES
750 GENERATED TRUE
751 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/scanner.h
752 )
753
754 #
755 # Add scanner.c to the list of sources.
756 #
757 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/scanner.c)
758
759 #
760 # Try to find YACC or Bison.
761 #
762 find_program(YACC_EXECUTABLE NAMES bison win_bison byacc yacc)
763 if(YACC_EXECUTABLE STREQUAL "YACC_EXECUTABLE-NOTFOUND")
764 message(FATAL_ERROR "Neither bison nor win_bison nor byacc nor yacc was found.")
765 endif()
766 message(STATUS "Parser generator: ${YACC_EXECUTABLE}")
767
768 #
769 # Create custom command for the scanner.
770 # Find out whether it's Bison or notby looking at the last component
771 # of the path (without a .exe extension, if this is Windows).
772 #
773 get_filename_component(YACC_NAME ${YACC_EXECUTABLE} NAME_WE)
774 if("${YACC_NAME}" STREQUAL "bison" OR "${YACC_NAME}" STREQUAL "win_bison")
775 set(YACC_COMPATIBILITY_FLAG "-y")
776 endif()
777 add_custom_command(
778 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/grammar.c ${CMAKE_CURRENT_BINARY_DIR}/grammar.h
779 SOURCE ${pcap_SOURCE_DIR}/grammar.y
780 COMMAND ${YACC_EXECUTABLE} ${YACC_COMPATIBILITY_FLAG} -p pcap_ -o ${CMAKE_CURRENT_BINARY_DIR}/grammar.c -d ${pcap_SOURCE_DIR}/grammar.y
781 DEPENDS ${pcap_SOURCE_DIR}/grammar.y
782 )
783
784 #
785 # Since grammar.c does not exists yet when cmake is run, mark
786 # it as generated.
787 #
788 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/grammar.c PROPERTIES
789 GENERATED TRUE
790 )
791
792 #
793 # Add grammar.c to the list of sources.
794 #
795 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/grammar.c)
796
797 if(WIN32 AND NOT MSYS AND NOT ${CMAKE_CROSSCOMPILING})
798 #
799 # CMake does not love Windows. Convert its UN*X-style paths to
800 # Windows-style paths to hand to Windows programs.
801 #
802 file(TO_NATIVE_PATH "${pcap_SOURCE_DIR}/GenVersion.bat" GenVersion_path)
803 file(TO_NATIVE_PATH "${pcap_SOURCE_DIR}/VERSION" VERSION_path)
804 file(TO_NATIVE_PATH "${pcap_SOURCE_DIR}/version.c.in" version_c_in_path)
805 file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/version.c" version_c_path)
806 file(TO_NATIVE_PATH "${pcap_SOURCE_DIR}/pcap_version.h.in" pcap_version_h_in_path)
807 file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h" pcap_version_h_path)
808 add_custom_command(
809 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/version.c
810 SOURCE ${pcap_SOURCE_DIR}/VERSION
811 COMMAND ${GenVersion_path} ${VERSION_path} ${version_c_in_path} ${version_c_path}
812 DEPENDS ${pcap_SOURCE_DIR}/VERSION ${pcap_SOURCE_DIR}/version.c.in
813 )
814 add_custom_command(
815 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h
816 SOURCE ${pcap_SOURCE_DIR}/VERSION ${pcap_SOURCE_DIR}/pcap_version.h.in
817 COMMAND ${GenVersion_path} ${VERSION_path} ${pcap_version_h_in_path} ${pcap_version_h_path}
818 DEPENDS ${pcap_SOURCE_DIR}/VERSION ${pcap_SOURCE_DIR}/pcap_version.h.in
819 )
820 else(WIN32 AND NOT MSYS AND NOT ${CMAKE_CROSSCOMPILING})
821 add_custom_command(
822 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/version.c
823 SOURCE ${pcap_SOURCE_DIR}/VERSION
824 COMMAND ${pcap_SOURCE_DIR}/gen_version.sh ${pcap_SOURCE_DIR}/VERSION ${pcap_SOURCE_DIR}/version.c.in ${CMAKE_CURRENT_BINARY_DIR}/version.c
825 DEPENDS ${pcap_SOURCE_DIR}/VERSION
826 )
827 add_custom_command(
828 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h
829 SOURCE ${pcap_SOURCE_DIR}/VERSION
830 COMMAND ${pcap_SOURCE_DIR}/gen_version.sh ${pcap_SOURCE_DIR}/VERSION ${pcap_SOURCE_DIR}/pcap_version.h.in ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h
831 DEPENDS ${pcap_SOURCE_DIR}/VERSION
832 )
833
834 #
835 # Since version.c does not exists yet when cmake is run, mark
836 # it as generated.
837 #
838 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/version.c PROPERTIES
839 GENERATED TRUE
840 )
841
842 #
843 # Add version.c to the list of sources.
844 #
845 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/version.c)
846 endif(WIN32 AND NOT MSYS AND NOT ${CMAKE_CROSSCOMPILING})
847
848 #
849 # Since pcap_version.h does not exists yet when cmake is run, mark
850 # it as generated.
851 #
852 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h PROPERTIES
853 GENERATED TRUE
854 )
855
856 #
857 # Add pcap_version.h to the list of headers.
858 #
859 set(PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h)
860
861 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
862 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
863
864 ######################################
865 # Register targets
866 ######################################
867
868 #
869 # Special target to serialize the building of the generated source.
870 #
871 # See
872 #
873 # http://public.kitware.com/pipermail/cmake/2013-August/055510.html
874 #
875 add_custom_target(SerializeTarget
876 DEPENDS
877 ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
878 ${CMAKE_CURRENT_BINARY_DIR}/scanner.c
879 ${CMAKE_CURRENT_BINARY_DIR}/version.c
880 )
881
882 if(BUILD_SHARED_LIBS)
883 add_library(${LIBRARY_NAME} SHARED
884 ${PROJECT_SOURCE_LIST_C}
885 ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
886 ${CMAKE_CURRENT_BINARY_DIR}/scanner.c
887 ${PROJECT_SOURCE_LIST_H}
888 )
889 add_dependencies(${LIBRARY_NAME} SerializeTarget)
890 endif(BUILD_SHARED_LIBS)
891
892 add_library(${LIBRARY_NAME}_static STATIC
893 ${PROJECT_SOURCE_LIST_C}
894 ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
895 ${CMAKE_CURRENT_BINARY_DIR}/scanner.c
896 ${PROJECT_SOURCE_LIST_H}
897 )
898 add_dependencies(${LIBRARY_NAME}_static SerializeTarget)
899
900 if(MSVC)
901 foreach(DBG_FLAGS
902 CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELWITHDEBINFO
903 CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELWITHDEBINFO)
904 string(REGEX REPLACE "/Zi" "" ${DBG_FLAGS} "${${DBG_FLAGS}}")
905 endforeach(DBG_FLAGS)
906
907 set_target_properties(${LIBRARY_NAME} PROPERTIES COMPILE_OPTIONS "/Zi")
908 set_target_properties(${LIBRARY_NAME}_static PROPERTIES OUTPUT_NAME "${LIBRARY_NAME}_static" COMPILE_OPTIONS "/Z7")
909 else(MSVC)
910 set_target_properties(${LIBRARY_NAME}_static PROPERTIES OUTPUT_NAME "${LIBRARY_NAME}")
911 endif(MSVC)
912
913 if(BUILD_SHARED_LIBS)
914 target_link_libraries(${LIBRARY_NAME} ${PCAP_LINK_LIBRARIES})
915 endif(BUILD_SHARED_LIBS)
916
917 ######################################
918 # Write out the config.h file
919 ######################################
920
921 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
922
923 ######################################
924 # Install pcap library and include files
925 ######################################
926
927 set(LIBRARY_NAME_STATIC ${LIBRARY_NAME}_static)
928
929 if(NOT BUILD_SHARED_LIBS)
930 unset(LIBRARY_NAME)
931 endif(NOT BUILD_SHARED_LIBS)
932
933 if(WIN32)
934 if(CMAKE_CL_64)
935 #
936 # Install 64-bit code built with MSVC in the amd64 subdirectories,
937 # as that's where it expects it to be.
938 #
939 install(TARGETS ${LIBRARY_NAME} ${LIBRARY_NAME_STATIC}
940 RUNTIME DESTINATION bin/amd64
941 LIBRARY DESTINATION lib/amd64
942 ARCHIVE DESTINATION lib/amd64)
943 if(BUILD_SHARED_LIBS)
944 install(FILES $<TARGET_PDB_FILE:${LIBRARY_NAME}>
945 DESTINATION bin/amd64 OPTIONAL)
946 endif(BUILD_SHARED_LIBS)
947 else(CMAKE_CL_64)
948 #
949 # Install 32-bit code, and 64-bit code not built with MSVC
950 # in the top-level directories, as those are where they
951 # expect it to be.
952 #
953 install(TARGETS ${LIBRARY_NAME} ${LIBRARY_NAME_STATIC}
954 RUNTIME DESTINATION bin
955 LIBRARY DESTINATION lib
956 ARCHIVE DESTINATION lib)
957 if(BUILD_SHARED_LIBS)
958 install(FILES $<TARGET_PDB_FILE:${LIBRARY_NAME}>
959 DESTINATION bin OPTIONAL)
960 endif(BUILD_SHARED_LIBS)
961 endif(CMAKE_CL_64)
962 else(WIN32)
963 install(TARGETS ${LIBRARY_NAME} ${LIBRARY_NAME_STATIC} DESTINATION lib)
964 endif(WIN32)
965
966 install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/pcap/ DESTINATION include/pcap)
967 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/pcap.h DESTINATION include)