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("sys/types.h sys/socket.h net/if.h net/pfvar.h"
119 check_include_files("sys/types.h sys/socket.h net/if.h net/pfvar.h net/if_pflog.h"
121 if(HAVE_NET_IF_PFLOG_H)
122 set(LOCALSRC print-pflog.c ${LOCALSRC})
123 endif(HAVE_NET_IF_PFLOG_H)
124 endif(HAVE_NET_PFVAR_H)
125 check_include_file(netinet/if_ether.h HAVE_NETINET_IF_ETHER_H)
128 # You are in a twisty little maze of UN*Xes, all different.
129 # Some might not have ether_ntohost().
130 # Some might have it and declare it in <net/ethernet.h>.
131 # Some might have it and declare it in <netinet/ether.h>
132 # Some might have it and declare it in <sys/ethernet.h>.
133 # Some might have it and declare it in <arpa/inet.h>.
134 # Some might have it and declare it in <netinet/if_ether.h>.
135 # Some might have it and not declare it in any header file.
137 # Before you is a C compiler.
139 check_function_exists(ether_ntohost HAVE_ETHER_NTOHOST)
140 if(HAVE_ETHER_NTOHOST)
142 # OK, we have ether_ntohost(). Is it declared in <net/ethernet.h>?
144 # This test fails if we don't have <net/ethernet.h> or if we do
145 # but it doesn't declare ether_ntohost().
147 check_symbol_exists(ether_ntohost net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
148 if(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
150 # Yes - we have it declared.
152 set(HAVE_DECL_ETHER_NTOHOST TRUE)
157 if(NOT HAVE_DECL_ETHER_NTOHOST)
159 # No - how about <netinet/ether.h>, as on Linux?
161 # This test fails if we don't have <netinet/ether.h>
162 # or if we do but it doesn't declare ether_ntohost().
164 check_symbol_exists(ether_ntohost netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
165 if(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
167 # Yes - we have it declared.
169 set(HAVE_DECL_ETHER_NTOHOST TRUE)
175 if(NOT HAVE_DECL_ETHER_NTOHOST)
177 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
179 # This test fails if we don't have <sys/ethernet.h>
180 # or if we do but it doesn't declare ether_ntohost().
182 check_symbol_exists(ether_ntohost sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
183 if(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
185 # Yes - we have it declared.
187 set(HAVE_DECL_ETHER_NTOHOST TRUE)
193 if(NOT HAVE_DECL_ETHER_NTOHOST)
195 # No, how about <arpa/inet.h>, as on AIX?
197 # This test fails if we don't have <arpa/inet.h>
198 # or if we do but it doesn't declare ether_ntohost().
200 check_symbol_exists(ether_ntohost arpa/inet.h ARPA_INET_H_DECLARES_ETHER_NTOHOST)
201 if(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
203 # Yes - we have it declared.
205 set(HAVE_DECL_ETHER_NTOHOST TRUE)
211 if(NOT HAVE_DECL_ETHER_NTOHOST)
213 # No, how about <netinet/if_ether.h>?
214 # On some platforms, it requires <net/if.h> and
215 # <netinet/in.h>, and we always include it with
216 # both of them, so test it with both of them.
218 # This test fails if we don't have <netinet/if_ether.h>
219 # and the headers we include before it, or if we do but
220 # <netinet/if_ether.h> doesn't declare ether_ntohost().
222 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)
223 if(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
225 # Yes - we have it declared.
227 set(HAVE_DECL_ETHER_NTOHOST TRUE)
231 # After all that, is ether_ntohost() declared?
233 if(NOT HAVE_DECL_ETHER_NTOHOST)
235 # No, we'll have to declare it ourselves.
236 # Do we have "struct ether_addr" if we include<netinet/if_ether.h>?
238 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)
243 # As the autoconf manual says, AC_HEADER_TIME is obsolete, as all UN*Xes
244 # let you include both time.h and sys/time.h, so just say we can.
246 check_include_file(bitypes.h HAVE_SYS_BITYPES_H)
247 check_include_file(limits.h HAVE_LIMITS_H)
252 check_function_exists(vfprintf HAVE_VFPRINTF)
253 check_function_exists(strlcat HAVE_STRLCAT)
254 check_function_exists(strlcpy HAVE_STRLCPY)
255 check_function_exists(strdup HAVE_STRDUP)
256 check_function_exists(strsep HAVE_STRSEP)
257 check_function_exists(getservent HAVE_GETSERVENT)
258 check_function_exists(getopt_long HAVE_GETOPT_LONG)
259 check_function_exists(vsnprintf HAVE_VSNPRINTF)
260 check_function_exists(snprintf HAVE_SNPRINTF)
261 check_function_exists(fork HAVE_FORK)
262 check_function_exists(vfork HAVE_VFORK)
263 check_function_exists(strftime HAVE_STRFTIME)
264 check_function_exists(setlinebuf HAVE_SETLINEBUF)
269 # XXX - there's no check_struct() macro that's like check_struct_has_member()
270 # except that it only checks for the existence of the structure type,
271 # so we use check_struct_has_member() and look for ss_family.
273 check_struct_has_member("struct sockaddr_storage" ss_family sys/socket.h HAVE_SOCKADDR_STORAGE)
274 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h sys/socket.h)
275 check_type_size("socklen_t" SOCKLEN_T)
276 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h)
281 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_SOCKADDR_SA_LEN)
283 ######################################
284 # External dependencies
285 ######################################
299 # First find it, then check for functions in it.
301 find_package(PCAP REQUIRED)
304 # Check for various functions in libpcap/WinPcap.
306 include_directories(${PCAP_INCLUDE_DIR})
307 cmake_push_check_state()
308 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
310 check_function_exists(pcap_list_datalinks HAVE_PCAP_LIST_DATALINKS)
311 if(HAVE_PCAP_LIST_DATALINKS)
312 check_function_exists(pcap_free_datalinks HAVE_PCAP_FREE_DATALINKS)
313 else(HAVE_PCAP_LIST_DATALINKS)
314 # XXX TODO - get our version of pcap_list_datalinks() and pcap_free_datalinks
315 endif(HAVE_PCAP_LIST_DATALINKS)
316 check_function_exists(pcap_set_datalink HAVE_PCAP_SET_DATALINK)
317 check_function_exists(pcap_datalink_name_to_val HAVE_PCAP_DATALINK_NAME_TO_VAL)
318 if(HAVE_PCAP_DATALINK_NAME_TO_VAL)
319 check_function_exists(pcap_datalink_val_to_description HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
320 if(NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
321 # XXX TODO - get our version of pcap_datalink_val_to_description()
322 endif(NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
323 else(HAVE_PCAP_DATALINK_NAME_TO_VAL)
324 # XXX TODO - get our version of pcap_datalink_name_to_val() etc.
325 endif(HAVE_PCAP_DATALINK_NAME_TO_VAL)
328 # Check for "pcap_breakloop()"; you can't substitute for it if
329 # it's absent (it has hooks into the live capture routines),
330 # so just define the HAVE_ value if it's there.
332 check_function_exists(pcap_breakloop HAVE_PCAP_BREAKLOOP)
335 # Check for "pcap_dump_ftell()" and use a substitute version
336 # if it's not present.
338 check_function_exists(pcap_dump_ftell HAVE_PCAP_DUMP_FTELL)
339 if(NOT HAVE_PCAP_DUMP_FTELL)
340 # XXX TODO - get our version of pcap_dump_ftell()
341 endif(NOT HAVE_PCAP_DUMP_FTELL)
344 # Do we have the new open API? Check for pcap_create, and assume that,
345 # if we do, we also have pcap_activate() and the other new routines
346 # introduced in libpcap 1.0.0.
348 check_function_exists(pcap_create HAVE_PCAP_CREATE)
351 # OK, do we have pcap_set_tstamp_type? If so, assume we have
352 # pcap_list_tstamp_types and pcap_free_tstamp_types as well.
354 check_function_exists(pcap_set_tstamp_type HAVE_PCAP_SET_TSTAMP_TYPE)
357 # And do we have pcap_set_tstamp_precision? If so, we assume
358 # we also have pcap_open_offline_with_tstamp_precision.
360 check_function_exists(pcap_set_tstamp_precision HAVE_PCAP_SET_TSTAMP_PRECISION)
361 endif(HAVE_PCAP_CREATE)
364 # Check for a miscellaneous collection of functions which we use
367 check_function_exists(pcap_findalldevs HAVE_PCAP_FINDALLDEVS)
368 if(HAVE_PCAP_FINDALLDEVS)
370 # Check for libpcap having pcap_findalldevs() but the pcap.h header
371 # not having pcap_if_t; some versions of Mac OS X shipped with pcap.h
372 # from 0.6 and libpcap 0.8, so that libpcap had pcap_findalldevs but
373 # pcap.h didn't have pcap_if_t.
375 cmake_push_check_state()
376 set(CMAKE_EXTRA_INCLUDE_FILES pcap.h)
377 check_type_size(pcap_if_t PCAP_IF_T)
378 cmake_pop_check_state()
379 endif(HAVE_PCAP_FINDALLDEVS)
380 check_function_exists(pcap_dump_flush HAVE_PCAP_DUMP_FLUSH)
381 check_function_exists(pcap_lib_version HAVE_PCAP_LIB_VERSION)
382 if(NOT HAVE_PCAP_LIB_VERSION)
383 # Check for the pcap_version string variable and set HAVE_PCAP_VERSION
384 endif(NOT HAVE_PCAP_LIB_VERSION)
385 check_function_exists(pcap_setdirection HAVE_PCAP_SETDIRECTION)
386 check_function_exists(pcap_set_immediate_mode HAVE_PCAP_SET_IMMEDIATE_MODE)
387 check_function_exists(pcap_dump_ftell64 HAVE_PCAP_DUMP_FTELL64)
388 check_function_exists(pcap_open HAVE_PCAP_OPEN)
389 check_function_exists(pcap_findalldevs_ex HAVE_PCAP_FINDALLDEVS_EX)
392 # Check for special debugging functions
394 check_function_exists(pcap_set_parser_debug HAVE_PCAP_SET_PARSER_DEBUG)
395 if(NOT HAVE_PCAP_SET_PARSER_DEBUG)
396 # Check whether libpcap defines pcap_debug or yydebug
397 endif(NOT HAVE_PCAP_SET_PARSER_DEBUG)
399 check_function_exists(pcap_set_optimizer_debug HAVE_PCAP_SET_OPTIMIZER_DEBUG)
401 cmake_pop_check_state()
403 ######################################
405 ######################################
407 set(NETDISSECT_SOURCE_LIST_C
575 # We allow the SMB dissector to be omitted.
577 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C}
583 # Replace missing functions
585 foreach(FUNC vfprintf strlcat strlcpy strdup strsep getservent getopt_long)
586 string(TOUPPER ${FUNC} FUNC_UPPERCASE)
587 set(HAVE_FUNC_UPPERCASE HAVE_${FUNC_UPPERCASE})
588 if(NOT ${HAVE_FUNC_UPPERCASE})
589 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/${FUNC}.c)
592 if(NOT HAVE_VSNPRINTF OR NOT HAVE_SNPRINTF)
593 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/snprintf.c)
594 endif(NOT HAVE_VSNPRINTF OR NOT HAVE_SNPRINTF)
596 add_library(netdissect STATIC
597 ${NETDISSECT_SOURCE_LIST_C}
600 set(TCPDUMP_SOURCE_LIST_C tcpdump.c)
602 set(PROJECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} ${TCPDUMP_SOURCE_LIST_C})
604 file(GLOB PROJECT_SOURCE_LIST_H
608 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
609 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
611 ######################################
613 ######################################
615 add_executable(tcpdump ${TCPDUMP_SOURCE_LIST_C})
616 target_link_libraries(tcpdump netdissect ${PCAP_LIBRARY} ${SMI_LIBRARY})
618 ######################################
619 # Write out the config.h file
620 ######################################
622 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)