1 cmake_minimum_required(VERSION 2.8.6)
3 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
6 set(LIBRARY_NAME netdissect)
8 ###################################################################
10 ###################################################################
12 option(WITH_SMI "Build with libsmi, if available" ON)
13 option(ENABLE_SMB "Build with the SMB dissector" ON)
16 # By default, build universal with the appropriate set of architectures
17 # for the OS on which we're doing the build.
19 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
21 # Get the major version of Darwin.
23 string(REGEX MATCH "^([0-9]+)" SYSTEM_VERSION_MAJOR "${CMAKE_SYSTEM_VERSION}")
25 if(SYSTEM_VERSION_MAJOR EQUAL 9)
27 # Leopard. Build for x86 and 32-bit PowerPC, with
28 # x86 first. (That's what Apple does.)
30 set(CMAKE_OSX_ARCHITECTURES "i386;ppc")
31 elseif(SYSTEM_VERSION_MAJOR EQUAL 10)
33 # Snow Leopard. Build for x86-64 and x86, with
34 # x86-64 first. (That's what Apple does.)
36 set(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
40 ###################################################################
42 ###################################################################
44 # Get, parse, format and set tcpdump's version string from
45 # [tcpdump_root]/VERSION for later use.
47 # Get MAJOR, MINOR, PATCH & SUFFIX
48 file(STRINGS ${tcpdump_SOURCE_DIR}/VERSION
50 LIMIT_COUNT 1 # Read only the first line
53 ######################################
55 ######################################
57 add_definitions(-DHAVE_CONFIG_H)
60 ${CMAKE_CURRENT_BINARY_DIR}
65 add_definitions(-D__STDC__)
66 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
71 MESSAGE(STATUS "Use STATIC runtime")
73 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
74 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
75 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
76 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
78 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
79 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
80 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
81 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
83 MESSAGE(STATUS "Use DYNAMIC runtime")
85 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD")
86 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD")
87 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
88 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
90 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD")
91 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD")
92 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD")
93 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
97 ###################################################################
98 # Detect available platform features
99 ###################################################################
101 include(CMakePushCheckState)
102 include(CheckIncludeFile)
103 include(CheckIncludeFiles)
104 include(CheckFunctionExists)
105 include(CheckSymbolExists)
106 include(CheckStructHasMember)
107 include(CheckTypeSize)
112 check_include_file(fcntl.h HAVE_FCNTL_H)
113 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
114 check_include_file(rpc/rpcent.h HAVE_RPC_RPCENT_H)
115 check_include_file(netdnet/dnetdb.h HAVE_NETDNET_DNETDB_H)
116 check_include_files(net/pfvar.h HAVE_NET_PFVAR_H)
118 check_include_files(net/if_pflog.h HAVE_NET_IF_PFLOG_H)
119 if(HAVE_NET_IF_PFLOG_H)
120 set(LOCALSRC print-pflog.c ${LOCALSRC})
121 endif(HAVE_NET_IF_PFLOG_H)
122 endif(HAVE_NET_PFVAR_H)
125 # You are in a twisty little maze of UN*Xes, all different.
126 # Some might not have ether_ntohost().
127 # Some might have it and declare it in <net/ethernet.h>.
128 # Some might have it and declare it in <netinet/ether.h>
129 # Some might have it and declare it in <sys/ethernet.h>.
130 # Some might have it and declare it in <arpa/inet.h>.
131 # Some might have it and declare it in <netinet/if_ether.h>.
132 # Some might have it and not declare it in any header file.
134 # Before you is a C compiler.
136 check_function_exists(ether_ntohost HAVE_ETHER_NTOHOST)
137 if(HAVE_ETHER_NTOHOST)
139 # OK, we have ether_ntohost(). We don't check whether it's buggy,
140 # as we assume any system that has CMake is likely to be new enough
141 # that, if it has ether_ntohost(), whatever bug is checked for in
142 # autotools is fixed; we just decide to use it.
144 set(USE_ETHER_NTOHOST TRUE)
147 # Is it declared in <net/ethernet.h>?
149 # This test fails if we don't have <net/ethernet.h> or if we do
150 # but it doesn't declare ether_ntohost().
152 check_symbol_exists(ether_ntohost net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
153 if(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
155 # Yes - we have it declared.
157 set(HAVE_DECL_ETHER_NTOHOST TRUE)
162 if(NOT HAVE_DECL_ETHER_NTOHOST)
164 # No - how about <netinet/ether.h>, as on Linux?
166 # This test fails if we don't have <netinet/ether.h>
167 # or if we do but it doesn't declare ether_ntohost().
169 check_symbol_exists(ether_ntohost netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
170 if(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
172 # Yes - we have it declared.
174 set(HAVE_DECL_ETHER_NTOHOST TRUE)
180 if(NOT HAVE_DECL_ETHER_NTOHOST)
182 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
184 # This test fails if we don't have <sys/ethernet.h>
185 # or if we do but it doesn't declare ether_ntohost().
187 check_symbol_exists(ether_ntohost sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
188 if(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
190 # Yes - we have it declared.
192 set(HAVE_DECL_ETHER_NTOHOST TRUE)
198 if(NOT HAVE_DECL_ETHER_NTOHOST)
200 # No, how about <arpa/inet.h>, as on AIX?
202 # This test fails if we don't have <arpa/inet.h>
203 # or if we do but it doesn't declare ether_ntohost().
205 check_symbol_exists(ether_ntohost arpa/inet.h ARPA_INET_H_DECLARES_ETHER_NTOHOST)
206 if(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
208 # Yes - we have it declared.
210 set(HAVE_DECL_ETHER_NTOHOST TRUE)
216 if(NOT HAVE_DECL_ETHER_NTOHOST)
218 # No, how about <netinet/if_ether.h>?
219 # On some platforms, it requires <net/if.h> and
220 # <netinet/in.h>, and we always include it with
221 # both of them, so test it with both of them.
223 # This test fails if we don't have <netinet/if_ether.h>
224 # and the headers we include before it, or if we do but
225 # <netinet/if_ether.h> doesn't declare ether_ntohost().
227 check_symbol_exists(ether_ntohost "sys/types.h;sys/socket.h;net/if.h;netinet/in.h;netinet/if_ether.h" NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
228 if(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
230 # Yes - we have it declared.
232 set(HAVE_DECL_ETHER_NTOHOST TRUE)
236 # After all that, is ether_ntohost() declared?
238 if(NOT HAVE_DECL_ETHER_NTOHOST)
240 # No, we'll have to declare it ourselves.
241 # Do we have "struct ether_addr" if we include<netinet/if_ether.h>?
243 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)
248 # As the autoconf manual says, AC_HEADER_TIME is obsolete, as all UN*Xes
249 # let you include both time.h and sys/time.h, so just say we can.
251 check_include_file(bitypes.h HAVE_SYS_BITYPES_H)
252 check_include_file(limits.h HAVE_LIMITS_H)
255 # Check whether we have pcap/pcap-inttypes.h.
256 # If we do, we use that to get the C99 types defined.
258 check_include_file(pcap/pcap-inttypes.h HAVE_PCAP_PCAP_INTTYPES_H)
261 # Check for some headers introduced in later versions of libpcap
262 # and used by some printers.
264 # Those headers might use the {u_}intN_t types, so we must include
265 # netdissect-stdinc.h first.
267 cmake_push_check_state()
268 set(CMAKE_EXTRA_INCLUDE_FILES netdissect-stdinc.h)
269 check_include_file(pcap/bluetooth.h HAVE_PCAP_BLUETOOTH_H)
270 check_include_file(pcap/nflog.h HAVE_PCAP_NFLOG_H)
271 cmake_pop_check_state()
276 check_function_exists(strlcat HAVE_STRLCAT)
277 check_function_exists(strlcpy HAVE_STRLCPY)
278 check_function_exists(strdup HAVE_STRDUP)
279 check_function_exists(strsep HAVE_STRSEP)
280 check_function_exists(getservent HAVE_GETSERVENT)
281 check_function_exists(getopt_long HAVE_GETOPT_LONG)
282 check_function_exists(vsnprintf HAVE_VSNPRINTF)
283 check_function_exists(snprintf HAVE_SNPRINTF)
284 check_function_exists(fork HAVE_FORK)
285 check_function_exists(vfork HAVE_VFORK)
286 check_function_exists(strftime HAVE_STRFTIME)
287 check_function_exists(setlinebuf HAVE_SETLINEBUF)
289 check_function_exists(getrpcbynumber HAVE_GETRPCBYNUMBER)
290 if(NOT HAVE_GETRPCBYNUMBER)
291 cmake_push_check_state()
292 set(CMAKE_REQUIRED_LIBRARIES nsl)
293 check_function_exists(getrpcbynumber HAVE_GETRPCBYNUMBER)
294 cmake_pop_check_state()
295 endif(NOT HAVE_GETRPCBYNUMBER)
300 # XXX - there's no check_struct() macro that's like check_struct_has_member()
301 # except that it only checks for the existence of the structure type,
302 # so we use check_struct_has_member() and look for ss_family.
304 check_struct_has_member("struct sockaddr_storage" ss_family sys/socket.h HAVE_SOCKADDR_STORAGE)
305 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h sys/socket.h)
306 check_type_size("socklen_t" SOCKLEN_T)
307 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h)
310 # Check for IPv6 support.
311 # We just check for AF_INET6 and struct in6_addr.
313 cmake_push_check_state()
315 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h ws2tcpip.h)
316 check_symbol_exists(AF_INET6 "sys/types.h;ws2tcpip.h" HAVE_AF_INET6)
318 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/socket.h netinet/in.h)
319 check_symbol_exists(AF_INET6 "sys/types.h;sys/socket.h;netinet/in.h" HAVE_AF_INET6)
321 check_type_size("struct in6_addr" HAVE_STRUCT_IN6_ADDR)
322 cmake_pop_check_state()
323 if(HAVE_AF_INET6 AND HAVE_STRUCT_IN6_ADDR)
324 set(HAVE_OS_IPV6_SUPPORT TRUE)
325 endif(HAVE_AF_INET6 AND HAVE_STRUCT_IN6_ADDR)
330 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_SOCKADDR_SA_LEN)
332 ######################################
333 # External dependencies
334 ######################################
348 # First find it, then check for functions in it.
350 find_package(PCAP REQUIRED)
353 # Check for various functions in libpcap/WinPcap.
355 include_directories(${PCAP_INCLUDE_DIR})
356 cmake_push_check_state()
357 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
360 # Check for "pcap_list_datalinks()" and use a substitute version if
361 # it's not present. If it is present, check for "pcap_free_datalinks()";
362 # if it's not present, we don't replace it for now. (We could do so
363 # on UN*X, but not on Windows, where hilarity ensues if a program
364 # built with one version of the MSVC support library tries to free
365 # something allocated by a library built with another version of
366 # the MSVC support library.)
368 check_function_exists(pcap_list_datalinks HAVE_PCAP_LIST_DATALINKS)
369 if(HAVE_PCAP_LIST_DATALINKS)
370 check_function_exists(pcap_free_datalinks HAVE_PCAP_FREE_DATALINKS)
371 endif(HAVE_PCAP_LIST_DATALINKS)
374 # Check for "pcap_datalink_name_to_val()", and use a substitute
375 # version if it's not present. If it is present, check for
376 # "pcap_datalink_val_to_description()", and if we don't have it,
377 # use a substitute version.
379 check_function_exists(pcap_datalink_name_to_val HAVE_PCAP_DATALINK_NAME_TO_VAL)
380 if(HAVE_PCAP_DATALINK_NAME_TO_VAL)
381 check_function_exists(pcap_datalink_val_to_description HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
382 endif(HAVE_PCAP_DATALINK_NAME_TO_VAL)
385 # Check for "pcap_set_datalink()"; you can't substitute for it if
386 # it's absent (it has hooks into libpcap), so just define the
387 # HAVE_ value if it's there.
389 check_function_exists(pcap_set_datalink HAVE_PCAP_SET_DATALINK)
392 # Check for "pcap_breakloop()"; you can't substitute for it if
393 # it's absent (it has hooks into the live capture routines),
394 # so just define the HAVE_ value if it's there.
396 check_function_exists(pcap_breakloop HAVE_PCAP_BREAKLOOP)
399 # Check for "pcap_dump_ftell()"; we use a substitute version
400 # if it's not present.
402 check_function_exists(pcap_dump_ftell HAVE_PCAP_DUMP_FTELL)
405 # Do we have the new open API? Check for pcap_create, and assume that,
406 # if we do, we also have pcap_activate() and the other new routines
407 # introduced in libpcap 1.0.0.
409 check_function_exists(pcap_create HAVE_PCAP_CREATE)
412 # OK, do we have pcap_set_tstamp_type? If so, assume we have
413 # pcap_list_tstamp_types and pcap_free_tstamp_types as well.
415 check_function_exists(pcap_set_tstamp_type HAVE_PCAP_SET_TSTAMP_TYPE)
418 # And do we have pcap_set_tstamp_precision? If so, we assume
419 # we also have pcap_open_offline_with_tstamp_precision.
421 check_function_exists(pcap_set_tstamp_precision HAVE_PCAP_SET_TSTAMP_PRECISION)
422 endif(HAVE_PCAP_CREATE)
425 # Check for a miscellaneous collection of functions which we use
428 check_function_exists(pcap_findalldevs HAVE_PCAP_FINDALLDEVS)
429 if(HAVE_PCAP_FINDALLDEVS)
431 # Check for libpcap having pcap_findalldevs() but the pcap.h header
432 # not having pcap_if_t; some versions of Mac OS X shipped with pcap.h
433 # from 0.6 and libpcap 0.8, so that libpcap had pcap_findalldevs but
434 # pcap.h didn't have pcap_if_t.
436 cmake_push_check_state()
437 set(CMAKE_EXTRA_INCLUDE_FILES pcap.h)
438 check_type_size(pcap_if_t PCAP_IF_T)
439 cmake_pop_check_state()
440 endif(HAVE_PCAP_FINDALLDEVS)
441 check_function_exists(pcap_dump_flush HAVE_PCAP_DUMP_FLUSH)
442 check_function_exists(pcap_lib_version HAVE_PCAP_LIB_VERSION)
443 if(NOT HAVE_PCAP_LIB_VERSION)
444 # Check for the pcap_version string variable and set HAVE_PCAP_VERSION
445 endif(NOT HAVE_PCAP_LIB_VERSION)
446 check_function_exists(pcap_setdirection HAVE_PCAP_SETDIRECTION)
447 check_function_exists(pcap_set_immediate_mode HAVE_PCAP_SET_IMMEDIATE_MODE)
448 check_function_exists(pcap_dump_ftell64 HAVE_PCAP_DUMP_FTELL64)
449 check_function_exists(pcap_open HAVE_PCAP_OPEN)
450 check_function_exists(pcap_findalldevs_ex HAVE_PCAP_FINDALLDEVS_EX)
453 # Check for special debugging functions
455 check_function_exists(pcap_set_parser_debug HAVE_PCAP_SET_PARSER_DEBUG)
456 if(NOT HAVE_PCAP_SET_PARSER_DEBUG)
457 # Check whether libpcap defines pcap_debug or yydebug
458 endif(NOT HAVE_PCAP_SET_PARSER_DEBUG)
460 check_function_exists(pcap_set_optimizer_debug HAVE_PCAP_SET_OPTIMIZER_DEBUG)
461 check_function_exists(bpf_dump HAVE_BPF_DUMP)
463 cmake_pop_check_state()
465 ######################################
467 ######################################
469 set(NETDISSECT_SOURCE_LIST_C
637 # We allow the SMB dissector to be omitted.
639 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C}
645 # Replace missing functions
647 foreach(FUNC strlcat strlcpy strdup strsep getservent getopt_long)
648 string(TOUPPER ${FUNC} FUNC_UPPERCASE)
649 set(HAVE_FUNC_UPPERCASE HAVE_${FUNC_UPPERCASE})
650 if(NOT ${HAVE_FUNC_UPPERCASE})
651 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/${FUNC}.c)
654 if(NOT HAVE_VSNPRINTF OR NOT HAVE_SNPRINTF)
655 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/snprintf.c)
656 endif(NOT HAVE_VSNPRINTF OR NOT HAVE_SNPRINTF)
658 add_library(netdissect STATIC
659 ${NETDISSECT_SOURCE_LIST_C}
662 set(TCPDUMP_SOURCE_LIST_C tcpdump.c)
664 if(NOT HAVE_BPF_DUMP)
665 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} bpf_dump.c)
666 endif(NOT HAVE_BPF_DUMP)
667 if(NOT HAVE_PCAP_DUMP_FTELL)
668 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/pcap_dump_ftell.c)
669 endif(NOT HAVE_PCAP_DUMP_FTELL)
671 if(NOT HAVE_PCAP_LIST_DATALINKS)
672 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/datalinks.c)
673 endif(NOT HAVE_PCAP_LIST_DATALINKS)
675 if((NOT HAVE_PCAP_DATALINK_NAME_TO_VAL) OR (NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION))
676 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/dlnames.c)
677 endif((NOT HAVE_PCAP_DATALINK_NAME_TO_VAL) OR (NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION))
679 set(PROJECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} ${TCPDUMP_SOURCE_LIST_C})
681 file(GLOB PROJECT_SOURCE_LIST_H
685 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
686 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
688 ######################################
690 ######################################
692 add_executable(tcpdump ${TCPDUMP_SOURCE_LIST_C})
693 target_link_libraries(tcpdump netdissect ${PCAP_LIBRARY} ${SMI_LIBRARY})
695 ######################################
696 # Write out the config.h file
697 ######################################
699 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
701 ######################################
702 # Install tcpdump and man pages
703 ######################################
706 # "Define GNU standard installation directories", which actually
707 # are also defined, to some degree, by autotools, and at least
708 # some of which are general UN*X conventions.
710 include(GNUInstallDirs)
715 # XXX TODO where to install on Windows?
717 install(TARGETS tcpdump DESTINATION sbin)
720 # On UN*X, and on Windows when not using MSVC, process man pages and
721 # arrange that they be installed.
726 install(FILES ${MAN1} DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
731 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
732 "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
735 add_custom_target(uninstall
736 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)