1 cmake_minimum_required( VERSION 2.8.8 )
3 set( PROJECT_NAME pcap )
4 project( ${PROJECT_NAME} )
6 ###################################################################
8 ###################################################################
10 option (INET6 "Enable IPv6" ON)
12 option (USE_STATIC_RT "Use static Runtime" ON)
14 option (BUILD_SHARED_LIBS "Build shared libraries" ON)
16 set(PACKET_DLL_DIR "" CACHE PATH "Path to directory with include and lib subdirectories for packet.dll")
19 ######################################
21 ######################################
23 add_definitions( -DHAVE_CONFIG_H )
31 if( NOT "${PACKET_DLL_DIR}" STREQUAL "" )
32 include_directories("${PACKET_DLL_DIR}/Include")
34 link_directories("${PACKET_DLL_DIR}/Lib/x64")
36 link_directories("${PACKET_DLL_DIR}/Lib")
45 add_definitions( -DLIBPCAP_EXPORTS )
48 add_definitions( -D__STDC__ )
49 add_definitions( -D_CRT_SECURE_NO_WARNINGS )
50 add_definitions( "-D_U_=" )
51 elseif( CMAKE_COMPILER_IS_GNUCXX )
52 add_definitions( "-D_U_=__attribute__((unused))" )
54 add_definitions( "-D_U_=" )
59 MESSAGE( STATUS "Use STATIC runtime" )
61 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
62 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
63 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
64 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
66 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
67 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
68 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
69 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
71 MESSAGE( STATUS "Use DYNAMIC runtime" )
73 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD")
74 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD")
75 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
76 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
78 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD")
79 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD")
80 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD")
81 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
85 ###################################################################
86 # Detect available platform features
87 ###################################################################
89 include(CheckIncludeFile)
90 include(CheckFunctionExists)
91 include(CheckStructHasMember)
92 include(CheckTypeSize)
97 check_include_file( inttypes.h HAVE_INTTYPES_H )
98 check_include_file( stdint.h HAVE_STDINT_H )
99 check_include_file( unistd.h HAVE_UNISTD_H )
100 if( NOT HAVE_UNISTD_H )
101 add_definitions( -DYY_NO_UNISTD_H )
102 endif( NOT HAVE_UNISTD_H )
103 check_include_file( bitypes.h HAVE_SYS_BITYPES_H )
104 check_include_file( limits.h HAVE_LIMITS_H )
109 check_function_exists( strerror HAVE_STRERROR )
110 check_function_exists( strlcpy HAVE_STRLCPY )
111 check_function_exists( snprintf HAVE_SNPRINTF )
112 check_function_exists( vsnprintf HAVE_VSNPRINTF )
117 # XXX - there's no check_struct() macro that's like check_struct_has_member()
118 # except that it only checks for the existence of the structure type,
119 # so we use check_struct_has_member() and look for ss_family.
121 check_struct_has_member("struct sockaddr_storage" ss_family sys/socket.h HAVE_SOCKADDR_STORAGE)
126 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_SOCKADDR_SA_LEN )
129 MESSAGE( STATUS "Use IPv6" )
133 add_definitions( -DHAVE_ADDRINFO )
136 ######################################
137 # External dependencies
138 ######################################
140 ######################################
142 ######################################
144 set(PROJECT_SOURCE_LIST_C
161 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/win_snprintf.c )
163 if( NOT HAVE_SNPRINTF )
164 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/snprintf.c )
165 endif( NOT HAVE_SNPRINTF )
169 # Determine the main pcap-XXX.c file to use.
175 set( PCAP_TYPE win32 )
178 # UN*X - figure out what type of packet capture mechanism we
181 if( EXISTS /dev/bpf )
183 # Cloning BPF device.
186 AC_DEFINE(HAVE_CLONING_BPF,1,[define if you have a cloning BPF device])
187 elseif( EXISTS /dev/bpf0 )
191 # XXX - many more BPF checks.
193 elseif( EXISTS /usr/include/net/pfilt.h )
195 # DEC OSF/1, Digital UNIX, Tru64 UNIX
198 elseif( EXISTS /dev/enet )
199 set( PCAP_TYPE enet )
200 elseif( EXISTS /dev/nit )
201 set( PCAP_TYPE snit )
202 elseif( EXISTS /usr/include/sys/net/nit.h )
204 elseif( EXISTS /usr/include/linux/socket.h )
205 set( PCAP_TYPE linux )
208 # Do we have the wireless extensions?
210 check_include_file( linux/wireless.h HAVE_LINUX_WIRELESS_H )
213 # XXX - many more Linux checks.
215 elseif( EXISTS /usr/include/net/raw.h )
216 set( PCAP_TYPE snoop )
217 elseif( EXISTS /usr/include/odmi.h )
219 # On AIX, the BPF devices might not yet be present - they're
220 # created the first time libpcap runs after booting.
221 # We check for odmi.h instead.
224 elseif( /usr/include/sys/dlpi.h )
225 set( PCAP_TYPE dlpi )
228 # XXX - many more DLPI checks.
231 set( PCAP_TYPE null )
234 message(STATUS "Packet capture mechanism type: ${PCAP_TYPE}")
235 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-${PCAP_TYPE}.c)
238 # Now figure out how we get a list of interfaces and addresses,
239 # if we support capturing. Don't bother if we don't support
246 set( FINDALLDEVS_TYPE win32 )
249 # UN*X - figure out what type of interface list mechanism we
252 if( ${PCAP_TYPE} STREQUAL "null" )
254 # We can't capture, so we can't open any capture
255 # devices, so we won't return any interfaces.
257 set( FINDALLDEVS_TYPE null )
259 check_function_exists( getifaddrs HAVE_GETIFADDRS )
260 if( ${HAVE_GETIFADDRS} )
262 # We have "getifaddrs()"; make sure we have <ifaddrs.h>
263 # as well, just in case some platform is really weird.
265 check_include_file( ifaddrs.h HAVE_IFADDRS_H )
266 if( ${HAVE_IFADDRS_H} )
268 # We have the header, so we use "getifaddrs()" to
269 # get the list of interfaces.
271 set( FINDALLDEVS_TYPE getad )
274 # We don't have the header - give up.
275 # XXX - we could also fall back on some other
276 # mechanism, but, for now, this'll catch this
277 # problem so that we can at least try to figure
278 # out something to do on systems with "getifaddrs()"
279 # but without "ifaddrs.h", if there is something
280 # we can do on those systems.
282 message(FATAL_ERROR "Your system has getifaddrs() but doesn't have a usable <ifaddrs.h>." )
286 # Well, we don't have "getifaddrs()", so we have to use
287 # some other mechanism; determine what that mechanism is.
289 # The first thing we use is the type of capture mechanism,
290 # which is somewhat of a proxy for the OS we're using.
292 if( ${PCAP_TYPE} STREQUAL "dlpi" OR ${PCAP_TYPE} STREQUAL "libdlpi" )
294 # This might be Solaris 8 or later, with
295 # SIOCGLIFCONF, or it might be some other OS
296 # or some older version of Solaris, with
299 try_compile( HAVE_SIOCGLIFCONF ${CMAKE_BINARY_DIR} "${pcap_SOURCE_DIR}/config/have_siocglifconf.c" )
300 message( STATUS "HAVE_SIOCGLIFCONF = ${HAVE_SIOCGLIFCONF}" )
301 if( HAVE_SIOCGLIFCONF )
302 set( FINDALLDEVS_TYPE glifc )
304 set( FINDALLDEVS_TYPE gifc )
308 # Assume we just have SIOCGIFCONF.
309 # (XXX - on at least later Linux kernels, there's
310 # another mechanism, and we should be using that
313 set( FINDALLDEVS_TYPE gifc )
318 message(STATUS "Find-interfaces mechanism type: ${FINDALLDEVS_TYPE}")
319 set( PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} fad-${FINDALLDEVS_TYPE}.c )
321 file(GLOB PROJECT_SOURCE_LIST_CORE_H
325 set( PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_CORE_H} )
328 file(GLOB PROJECT_SOURCE_LIST_WIN32_H
331 set( PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_WIN32_H} )
335 # {Flex} and YACC/Berkeley YACC/Bison.
336 # From a mail message to the CMake mailing list by Andy Cedilnik of
341 # Try to find Flex, a Windows version of Flex, or Lex.
343 find_program(LEX_EXECUTABLE NAMES flex win_flex lex)
344 if( ${LEX_EXECUTABLE} STREQUAL "LEX_EXECUTABLE-NOTFOUND" )
345 message(FATAL_ERROR "Neither flex nor win_flex nor lex was found." )
347 message(STATUS "Lexical analyzer generator: ${LEX_EXECUTABLE}")
350 OUTPUT ${CMAKE_BINARY_DIR}/scanner.c ${CMAKE_BINARY_DIR}/scanner.h
351 SOURCE ${pcap_SOURCE_DIR}/scanner.l
352 COMMAND ${LEX_EXECUTABLE} -P pcap_ --header-file=scanner.h --nounput -o${CMAKE_BINARY_DIR}/scanner.c ${pcap_SOURCE_DIR}/scanner.l
353 DEPENDS ${pcap_SOURCE_DIR}/scanner.l
357 # Since scanner.c does not exist yet when cmake is run, mark
360 # Since scanner.c includes grammar.h, mark that as a dependency.
362 set_source_files_properties(${CMAKE_BINARY_DIR}/scanner.c PROPERTIES
364 OBJECT_DEPENDS ${CMAKE_BINARY_DIR}/scanner.h
368 # Add scanner.c to the list of sources.
370 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_BINARY_DIR}/scanner.c)
373 # Try to find YACC or Bison.
375 find_program(YACC_EXECUTABLE NAMES bison win_bison byacc yacc)
376 if( ${YACC_EXECUTABLE} STREQUAL "YACC_EXECUTABLE-NOTFOUND" )
377 message(FATAL_ERROR "Neither bison nor win_bison nor byacc nor yacc was found." )
379 message(STATUS "Parser generator: ${YACC_EXECUTABLE}")
382 # Create custom command for the scanner.
383 # Find out whether it's Bison or notby looking at the last component
384 # of the path (without a .exe extension, if this is Windows).
386 get_filename_component(YACC_NAME ${YACC_EXECUTABLE} NAME_WE)
387 if( "${YACC_NAME}" STREQUAL "bison" OR "${YACC_NAME}" STREQUAL "win_bison" )
388 set( YACC_COMPATIBILITY_FLAG "-y" )
391 OUTPUT ${CMAKE_BINARY_DIR}/grammar.c ${CMAKE_BINARY_DIR}/grammar.h
392 SOURCE ${pcap_SOURCE_DIR}/grammar.y
393 COMMAND ${YACC_EXECUTABLE} ${YACC_COMPATIBILITY_FLAG} -p pcap_ -o ${CMAKE_BINARY_DIR}/grammar.c -d ${pcap_SOURCE_DIR}/grammar.y
394 DEPENDS ${pcap_SOURCE_DIR}/grammar.y
398 # Since grammar.c does not exists yet when cmake is run, mark
401 set_source_files_properties(${CMAKE_BINARY_DIR}/grammar.c PROPERTIES
406 # Add grammar.c to the list of sources.
408 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_BINARY_DIR}/grammar.c)
410 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
411 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
413 ######################################
415 ######################################
417 add_library(${PROJECT_NAME}
418 ${PROJECT_SOURCE_LIST_C}
419 ${CMAKE_BINARY_DIR}/grammar.c
420 ${CMAKE_BINARY_DIR}/scanner.c
421 ${PROJECT_SOURCE_LIST_H}
425 target_link_libraries ( ${PROJECT_NAME}
431 ######################################
432 # Write out the config.h file
433 ######################################
435 configure_file(${CMAKE_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_BINARY_DIR}/config.h)