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)
15 # By default, build universal with the appropriate set of architectures
16 # for the OS on which we're doing the build.
18 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
20 # Get the major version of Darwin.
22 string(REGEX MATCH "^([0-9]+)" SYSTEM_VERSION_MAJOR "${CMAKE_SYSTEM_VERSION}")
24 if(SYSTEM_VERSION_MAJOR EQUAL 9)
26 # Leopard. Build for x86 and 32-bit PowerPC, with
27 # x86 first. (That's what Apple does.)
29 set(CMAKE_OSX_ARCHITECTURES "i386;ppc")
30 elseif(SYSTEM_VERSION_MAJOR EQUAL 10)
32 # Snow Leopard. Build for x86-64 and x86, with
33 # x86-64 first. (That's what Apple does.)
35 set(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
39 ###################################################################
41 ###################################################################
43 # Get, parse, format and set tcpdump's version string from
44 # [tcpdump_root]/VERSION for later use.
46 # Get MAJOR, MINOR, PATCH & SUFFIX
47 file(STRINGS ${tcpdump_SOURCE_DIR}/VERSION
49 LIMIT_COUNT 1 # Read only the first line
52 ######################################
54 ######################################
56 add_definitions(-DHAVE_CONFIG_H)
59 ${CMAKE_CURRENT_BINARY_DIR}
64 add_definitions(-D__STDC__)
65 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
66 add_definitions("-D_U_=")
67 elseif(CMAKE_COMPILER_IS_GNUCXX)
68 add_definitions("-D_U_=__attribute__((unused))")
70 add_definitions("-D_U_=")
75 MESSAGE(STATUS "Use STATIC runtime")
77 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
78 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
79 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
80 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
82 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
83 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
84 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
85 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
87 MESSAGE(STATUS "Use DYNAMIC runtime")
89 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD")
90 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD")
91 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
92 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
94 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD")
95 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD")
96 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD")
97 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
101 ###################################################################
102 # Detect available platform features
103 ###################################################################
105 include(CMakePushCheckState)
106 include(CheckIncludeFile)
107 include(CheckIncludeFiles)
108 include(CheckFunctionExists)
109 include(CheckSymbolExists)
110 include(CheckStructHasMember)
111 include(CheckTypeSize)
116 check_include_file(fcntl.h HAVE_FCNTL_H)
117 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
118 check_include_file(rpc/rpcent.h HAVE_RPC_RPCENT_H)
119 check_include_file(netdnet/dnetdb.h HAVE_NETDNET_DNETDB_H)
120 check_include_files("sys/types.h sys/socket.h net/if.h net/pfvar.h"
123 check_include_files("sys/types.h sys/socket.h net/if.h net/pfvar.h net/if_pflog.h"
125 if(HAVE_NET_IF_PFLOG_H)
126 set(LOCALSRC print-pflog.c ${LOCALSRC})
127 endif(HAVE_NET_IF_PFLOG_H)
128 endif(HAVE_NET_PFVAR_H)
129 check_include_file(netinet/if_ether.h HAVE_NETINET_IF_ETHER_H)
132 # You are in a twisty little maze of UN*Xes, all different.
133 # Some might not have ether_ntohost().
134 # Some might have it and declare it in <net/ethernet.h>.
135 # Some might have it and declare it in <netinet/ether.h>
136 # Some might have it and declare it in <sys/ethernet.h>.
137 # Some might have it and declare it in <arpa/inet.h>.
138 # Some might have it and declare it in <netinet/if_ether.h>.
139 # Some might have it and not declare it in any header file.
141 # Before you is a C compiler.
143 check_function_exists(ether_ntohost HAVE_ETHER_NTOHOST)
144 if(HAVE_ETHER_NTOHOST)
146 # OK, we have ether_ntohost(). Is it declared in <net/ethernet.h>?
148 # This test fails if we don't have <net/ethernet.h> or if we do
149 # but it doesn't declare ether_ntohost().
151 check_symbol_exists(ether_ntohost net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
152 if(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
154 # Yes - we have it declared.
156 set(HAVE_DECL_ETHER_NTOHOST TRUE)
161 if(NOT HAVE_DECL_ETHER_NTOHOST)
163 # No - how about <netinet/ether.h>, as on Linux?
165 # This test fails if we don't have <netinet/ether.h>
166 # or if we do but it doesn't declare ether_ntohost().
168 check_symbol_exists(ether_ntohost netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
169 if(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
171 # Yes - we have it declared.
173 set(HAVE_DECL_ETHER_NTOHOST TRUE)
179 if(NOT HAVE_DECL_ETHER_NTOHOST)
181 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
183 # This test fails if we don't have <sys/ethernet.h>
184 # or if we do but it doesn't declare ether_ntohost().
186 check_symbol_exists(ether_ntohost sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
187 if(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
189 # Yes - we have it declared.
191 set(HAVE_DECL_ETHER_NTOHOST TRUE)
197 if(NOT HAVE_DECL_ETHER_NTOHOST)
199 # No, how about <arpa/inet.h>, as on AIX?
201 # This test fails if we don't have <arpa/inet.h>
202 # or if we do but it doesn't declare ether_ntohost().
204 check_symbol_exists(ether_ntohost arpa/inet.h ARPA_INET_H_DECLARES_ETHER_NTOHOST)
205 if(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
207 # Yes - we have it declared.
209 set(HAVE_DECL_ETHER_NTOHOST TRUE)
215 if(NOT HAVE_DECL_ETHER_NTOHOST)
217 # No, how about <netinet/if_ether.h>?
218 # On some platforms, it requires <net/if.h> and
219 # <netinet/in.h>, and we always include it with
220 # both of them, so test it with both of them.
222 # This test fails if we don't have <netinet/if_ether.h>
223 # and the headers we include before it, or if we do but
224 # <netinet/if_ether.h> doesn't declare ether_ntohost().
226 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)
227 if(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
229 # Yes - we have it declared.
231 set(HAVE_DECL_ETHER_NTOHOST TRUE)
235 # After all that, is ether_ntohost() declared?
237 if(NOT HAVE_DECL_ETHER_NTOHOST)
239 # No, we'll have to declare it ourselves.
240 # Do we have "struct ether_addr" if we include<netinet/if_ether.h>?
242 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)
247 # As the autoconf manual says, AC_HEADER_TIME is obsolete, as all UN*Xes
248 # let you include both time.h and sys/time.h, so just say we can.
250 check_include_file(bitypes.h HAVE_SYS_BITYPES_H)
251 check_include_file(limits.h HAVE_LIMITS_H)
255 # XXX TODO - all from vfprintf to getopt_long have replacements
257 check_function_exists(vfprintf HAVE_VFPRINTF)
258 check_function_exists(strlcat HAVE_STRLCAT)
259 check_function_exists(strlcpy HAVE_STRLCPY)
260 check_function_exists(strdup HAVE_STRDUP)
261 check_function_exists(strsep HAVE_STRSEP)
262 check_function_exists(getservent HAVE_GETSERVENT)
263 check_function_exists(getopt_long HAVE_GETOPT_LONG)
264 check_function_exists(vsnprintf HAVE_VSNPRINTF)
265 check_function_exists(snprintf HAVE_SNPRINTF)
266 check_function_exists(fork HAVE_FORK)
267 check_function_exists(vfork HAVE_VFORK)
268 check_function_exists(strftime HAVE_STRFTIME)
269 check_function_exists(setlinebuf HAVE_SETLINEBUF)
274 # XXX - there's no check_struct() macro that's like check_struct_has_member()
275 # except that it only checks for the existence of the structure type,
276 # so we use check_struct_has_member() and look for ss_family.
278 check_struct_has_member("struct sockaddr_storage" ss_family sys/socket.h HAVE_SOCKADDR_STORAGE)
279 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h sys/socket.h)
280 check_type_size("socklen_t" SOCKLEN_T)
281 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h)
286 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_SOCKADDR_SA_LEN)
288 ######################################
289 # External dependencies
290 ######################################
304 # First find it, then check for functions in it.
306 find_package(PCAP REQUIRED)
309 # Check for various functions in libpcap/WinPcap.
311 include_directories(${PCAP_INCLUDE_DIR})
312 cmake_push_check_state()
313 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
315 check_function_exists(pcap_list_datalinks HAVE_PCAP_LIST_DATALINKS)
316 if(HAVE_PCAP_LIST_DATALINKS)
317 check_function_exists(pcap_free_datalinks HAVE_PCAP_FREE_DATALINKS)
318 else(HAVE_PCAP_LIST_DATALINKS)
319 # XXX TODO - get our version of pcap_list_datalinks() and pcap_free_datalinks
320 endif(HAVE_PCAP_LIST_DATALINKS)
321 check_function_exists(pcap_set_datalink HAVE_PCAP_SET_DATALINK)
322 check_function_exists(pcap_datalink_name_to_val HAVE_PCAP_DATALINK_NAME_TO_VAL)
323 if(HAVE_PCAP_DATALINK_NAME_TO_VAL)
324 check_function_exists(pcap_datalink_val_to_description HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
325 if(NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
326 # XXX TODO - get our version of pcap_datalink_val_to_description()
327 endif(NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
328 else(HAVE_PCAP_DATALINK_NAME_TO_VAL)
329 # XXX TODO - get our version of pcap_datalink_name_to_val() etc.
330 endif(HAVE_PCAP_DATALINK_NAME_TO_VAL)
333 # Check for "pcap_breakloop()"; you can't substitute for it if
334 # it's absent (it has hooks into the live capture routines),
335 # so just define the HAVE_ value if it's there.
337 check_function_exists(pcap_breakloop HAVE_PCAP_BREAKLOOP)
340 # Check for "pcap_dump_ftell()" and use a substitute version
341 # if it's not present.
343 check_function_exists(pcap_dump_ftell HAVE_PCAP_DUMPFTELL)
344 if(NOT HAVE_PCAP_DUMPFTELL)
345 # XXX TODO - get our version of pcap_dump_ftell()
346 endif(NOT HAVE_PCAP_DUMPFTELL)
349 # Do we have the new open API? Check for pcap_create, and assume that,
350 # if we do, we also have pcap_activate() and the other new routines
351 # introduced in libpcap 1.0.0.
353 check_function_exists(pcap_create HAVE_PCAP_CREATE)
356 # OK, do we have pcap_set_tstamp_type? If so, assume we have
357 # pcap_list_tstamp_types and pcap_free_tstamp_types as well.
359 check_function_exists(pcap_set_tstamp_type HAVE_PCAP_SET_TSTAMP_TYPE)
362 # And do we have pcap_set_tstamp_precision? If so, we assume
363 # we also have pcap_open_offline_with_tstamp_precision.
365 check_function_exists(pcap_set_tstamp_precision HAVE_PCAP_SET_TSTAMP_PRECISION)
366 endif(HAVE_PCAP_CREATE)
369 # Check for a miscellaneous collection of functions which we use
372 check_function_exists(pcap_findalldevs HAVE_PCAP_FINDALLDEVS)
373 if(HAVE_PCAP_FINDALLDEVS)
375 # Check for libpcap having pcap_findalldevs() but the pcap.h header
376 # not having pcap_if_t; some versions of Mac OS X shipped with pcap.h
377 # from 0.6 and libpcap 0.8, so that libpcap had pcap_findalldevs but
378 # pcap.h didn't have pcap_if_t.
380 cmake_push_check_state()
381 set(CMAKE_EXTRA_INCLUDE_FILES pcap.h)
382 check_type_size(pcap_if_t PCAP_IF_T)
383 cmake_pop_check_state()
384 endif(HAVE_PCAP_FINDALLDEVS)
385 check_function_exists(pcap_dump_flush HAVE_PCAP_DUMP_FLUSH)
386 check_function_exists(pcap_lib_version HAVE_PCAP_LIB_VERSION)
387 if(NOT HAVE_PCAP_LIB_VERSION)
388 # Check for the pcap_version string variable and set HAVE_PCAP_VERSION
389 endif(NOT HAVE_PCAP_LIB_VERSION)
390 check_function_exists(pcap_setdirection HAVE_PCAP_SETDIRECTION)
391 check_function_exists(pcap_set_immediate_mode HAVE_PCAP_SET_IMMEDIATE_MODE)
392 check_function_exists(pcap_dump_ftell64 HAVE_PCAP_DUMP_FTELL64)
393 check_function_exists(pcap_open HAVE_PCAP_OPEN)
394 check_function_exists(pcap_findalldevs_ex HAVE_PCAP_FINDALLDEVS_EX)
397 # Check for special debugging functions
399 check_function_exists(pcap_set_parser_debug HAVE_PCAP_SET_PARSER_DEBUG)
400 if(NOT HAVE_PCAP_SET_PARSER_DEBUG)
401 # Check whether libpcap defines pcap_debug or yydebug
402 endif(NOT HAVE_PCAP_SET_PARSER_DEBUG)
404 check_function_exists(pcap_set_optimizer_debug HAVE_PCAP_SET_OPTIMIZER_DEBUG)
406 cmake_pop_check_state()
408 ######################################
410 ######################################
412 set(NETDISSECT_SOURCE_LIST_C
579 # Replace missing functions
581 foreach(FUNC vfprintf strlcat strlcpy strdup strsep getopt_long)
582 string(TOUPPER ${FUNC} FUNC_UPPERCASE)
583 set(HAVE_FUNC_UPPERCASE HAVE_${FUNC_UPPERCASE})
584 if(NOT ${HAVE_FUNC_UPPERCASE})
585 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/${FUNC}.c)
588 if(NOT HAVE_VSNPRINTF OR NOT HAVE_SNPRINTF)
589 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/snprintf.c)
590 endif(NOT HAVE_VSNPRINTF OR NOT HAVE_SNPRINTF)
592 add_library(netdissect STATIC
593 ${NETDISSECT_SOURCE_LIST_C}
596 set(TCPDUMP_SOURCE_LIST_C tcpdump.c)
598 set(PROJECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} ${TCPDUMP_SOURCE_LIST_C})
600 file(GLOB PROJECT_SOURCE_LIST_H
604 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
605 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
607 ######################################
609 ######################################
611 add_executable(tcpdump ${TCPDUMP_SOURCE_LIST_C})
612 target_link_libraries(tcpdump netdissect ${PCAP_LIBRARY} ${SMI_LIBRARY})
614 ######################################
615 # Write out the config.h file
616 ######################################
618 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)