1 cmake_minimum_required( VERSION 2.8.8 )
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.
9 cmake_policy(SET CMP0042 OLD)
14 # Call the library "wpcap" on Windows, for backwards compatibility.
17 set( LIBRARY_NAME wpcap )
19 set( LIBRARY_NAME pcap )
22 ###################################################################
24 ###################################################################
26 option (INET6 "Enable IPv6" ON)
28 option (USE_STATIC_RT "Use static Runtime" ON)
30 option (BUILD_SHARED_LIBS "Build shared libraries" ON)
32 set(PACKET_DLL_DIR "" CACHE PATH "Path to directory with include and lib subdirectories for packet.dll")
36 # Default to having remote capture support on Windows and, for now, to
37 # not having it on UN*X.
40 option (HAVE_REMOTE "Enable remote capture" ON)
42 option (HAVE_REMOTE "Enable remote capture" OFF)
45 ######################################
47 ######################################
49 add_definitions( -DHAVE_CONFIG_H )
52 ${CMAKE_CURRENT_BINARY_DIR}
58 # The 'Win32/WpdPack/' directory branch is for an AppVeyor build.
60 if( NOT "${PACKET_DLL_DIR}" STREQUAL "" )
61 include_directories("${PACKET_DLL_DIR}/Include")
63 link_directories("${PACKET_DLL_DIR}/Lib/x64")
65 link_directories("${PACKET_DLL_DIR}/Lib")
75 add_definitions( -DBUILDING_PCAP )
78 add_definitions( -D__STDC__ )
79 add_definitions( -D_CRT_SECURE_NO_WARNINGS )
80 add_definitions( "-D_U_=" )
81 elseif( CMAKE_COMPILER_IS_GNUCXX )
82 add_definitions( "-D_U_=__attribute__((unused))" )
84 add_definitions( "-D_U_=" )
89 MESSAGE( STATUS "Use STATIC runtime" )
91 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
92 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
93 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
94 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
96 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
97 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
98 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
99 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
101 MESSAGE( STATUS "Use DYNAMIC runtime" )
103 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD")
104 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD")
105 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
106 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
108 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD")
109 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD")
110 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD")
111 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
112 endif (USE_STATIC_RT)
115 ###################################################################
116 # Detect available platform features
117 ###################################################################
119 include(CheckIncludeFile)
120 include(CheckFunctionExists)
121 include(CheckStructHasMember)
122 include(CheckTypeSize)
127 check_include_file( inttypes.h HAVE_INTTYPES_H )
128 check_include_file( stdint.h HAVE_STDINT_H )
129 check_include_file( unistd.h HAVE_UNISTD_H )
130 if( NOT HAVE_UNISTD_H )
131 add_definitions( -DYY_NO_UNISTD_H )
132 endif( NOT HAVE_UNISTD_H )
133 check_include_file( bitypes.h HAVE_SYS_BITYPES_H )
134 check_include_file( limits.h HAVE_LIMITS_H )
139 check_function_exists( strerror HAVE_STRERROR )
140 check_function_exists( strlcpy HAVE_STRLCPY )
141 check_function_exists( snprintf HAVE_SNPRINTF )
142 check_function_exists( vsnprintf HAVE_VSNPRINTF )
143 check_function_exists( strtok_r HAVE_STRTOK_R )
147 # Check for Windows-only functions, such as packet.dll functions.
149 check_function_exists( PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER )
155 # XXX - there's no check_struct() macro that's like check_struct_has_member()
156 # except that it only checks for the existence of the structure type,
157 # so we use check_struct_has_member() and look for ss_family.
159 check_struct_has_member("struct sockaddr_storage" ss_family sys/socket.h HAVE_SOCKADDR_STORAGE)
160 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h sys/socket.h)
161 check_type_size("socklen_t" SOCKLEN_T)
162 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h)
167 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_SOCKADDR_SA_LEN )
170 MESSAGE( STATUS "Use IPv6" )
174 add_definitions( -DHAVE_ADDRINFO )
177 ######################################
178 # External dependencies
179 ######################################
181 ######################################
183 ######################################
185 set(PROJECT_SOURCE_LIST_C
202 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/win_snprintf.c )
204 if( NOT HAVE_SNPRINTF )
205 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/snprintf.c )
206 endif( NOT HAVE_SNPRINTF )
207 if( NOT HAVE_STRTOK_R )
208 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/strtok_r.c )
209 endif( NOT HAVE_STRTOK_R )
213 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C}
214 pcap-new.c pcap-rpcap.c sockutils.c)
218 # Determine the main pcap-XXX.c file to use.
224 set( PCAP_TYPE win32 )
227 # UN*X - figure out what type of packet capture mechanism we
230 if( EXISTS /dev/bpf )
232 # Cloning BPF device.
235 AC_DEFINE(HAVE_CLONING_BPF,1,[define if you have a cloning BPF device])
236 elseif( EXISTS /dev/bpf0 )
240 # XXX - many more BPF checks.
242 elseif( EXISTS /usr/include/net/pfilt.h )
244 # DEC OSF/1, Digital UNIX, Tru64 UNIX
247 elseif( EXISTS /dev/enet )
248 set( PCAP_TYPE enet )
249 elseif( EXISTS /dev/nit )
250 set( PCAP_TYPE snit )
251 elseif( EXISTS /usr/include/sys/net/nit.h )
253 elseif( EXISTS /usr/include/linux/socket.h )
254 set( PCAP_TYPE linux )
257 # Do we have the wireless extensions?
259 check_include_file( linux/wireless.h HAVE_LINUX_WIRELESS_H )
262 # XXX - many more Linux checks.
264 elseif( EXISTS /usr/include/net/raw.h )
265 set( PCAP_TYPE snoop )
266 elseif( EXISTS /usr/include/odmi.h )
268 # On AIX, the BPF devices might not yet be present - they're
269 # created the first time libpcap runs after booting.
270 # We check for odmi.h instead.
273 elseif( /usr/include/sys/dlpi.h )
274 set( PCAP_TYPE dlpi )
277 # XXX - many more DLPI checks.
280 set( PCAP_TYPE null )
283 message(STATUS "Packet capture mechanism type: ${PCAP_TYPE}")
284 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-${PCAP_TYPE}.c)
287 # Now figure out how we get a list of interfaces and addresses,
288 # if we support capturing. Don't bother if we don't support
293 # UN*X - figure out what type of interface list mechanism we
296 if( ${PCAP_TYPE} STREQUAL "null" )
298 # We can't capture, so we can't open any capture
299 # devices, so we won't return any interfaces.
301 set( FINDALLDEVS_TYPE null )
303 check_function_exists( getifaddrs HAVE_GETIFADDRS )
304 if( ${HAVE_GETIFADDRS} )
306 # We have "getifaddrs()"; make sure we have <ifaddrs.h>
307 # as well, just in case some platform is really weird.
309 check_include_file( ifaddrs.h HAVE_IFADDRS_H )
310 if( ${HAVE_IFADDRS_H} )
312 # We have the header, so we use "getifaddrs()" to
313 # get the list of interfaces.
315 set( FINDALLDEVS_TYPE getad )
318 # We don't have the header - give up.
319 # XXX - we could also fall back on some other
320 # mechanism, but, for now, this'll catch this
321 # problem so that we can at least try to figure
322 # out something to do on systems with "getifaddrs()"
323 # but without "ifaddrs.h", if there is something
324 # we can do on those systems.
326 message(FATAL_ERROR "Your system has getifaddrs() but doesn't have a usable <ifaddrs.h>." )
330 # Well, we don't have "getifaddrs()", so we have to use
331 # some other mechanism; determine what that mechanism is.
333 # The first thing we use is the type of capture mechanism,
334 # which is somewhat of a proxy for the OS we're using.
336 if( ${PCAP_TYPE} STREQUAL "dlpi" OR ${PCAP_TYPE} STREQUAL "libdlpi" )
338 # This might be Solaris 8 or later, with
339 # SIOCGLIFCONF, or it might be some other OS
340 # or some older version of Solaris, with
343 try_compile( HAVE_SIOCGLIFCONF ${CMAKE_CURRENT_BINARY_DIR} "${pcap_SOURCE_DIR}/config/have_siocglifconf.c" )
344 message( STATUS "HAVE_SIOCGLIFCONF = ${HAVE_SIOCGLIFCONF}" )
345 if( HAVE_SIOCGLIFCONF )
346 set( FINDALLDEVS_TYPE glifc )
348 set( FINDALLDEVS_TYPE gifc )
352 # Assume we just have SIOCGIFCONF.
353 # (XXX - on at least later Linux kernels, there's
354 # another mechanism, and we should be using that
357 set( FINDALLDEVS_TYPE gifc )
361 message(STATUS "Find-interfaces mechanism type: ${FINDALLDEVS_TYPE}")
362 set( PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} fad-${FINDALLDEVS_TYPE}.c )
365 file(GLOB PROJECT_SOURCE_LIST_CORE_H
369 set( PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_CORE_H} )
372 file(GLOB PROJECT_SOURCE_LIST_WIN32_H
375 set( PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_WIN32_H} )
379 # {Flex} and YACC/Berkeley YACC/Bison.
380 # From a mail message to the CMake mailing list by Andy Cedilnik of
385 # Try to find Flex, a Windows version of Flex, or Lex.
387 find_program(LEX_EXECUTABLE NAMES flex win_flex lex)
388 if( ${LEX_EXECUTABLE} STREQUAL "LEX_EXECUTABLE-NOTFOUND" )
389 message(FATAL_ERROR "Neither flex nor win_flex nor lex was found." )
391 message(STATUS "Lexical analyzer generator: ${LEX_EXECUTABLE}")
394 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/scanner.c ${CMAKE_CURRENT_BINARY_DIR}/scanner.h
395 SOURCE ${pcap_SOURCE_DIR}/scanner.l
396 COMMAND ${LEX_EXECUTABLE} -P pcap_ --header-file=scanner.h --nounput -o${CMAKE_CURRENT_BINARY_DIR}/scanner.c ${pcap_SOURCE_DIR}/scanner.l
397 DEPENDS ${pcap_SOURCE_DIR}/scanner.l
401 # Since scanner.c does not exist yet when cmake is run, mark
404 # Since scanner.c includes grammar.h, mark that as a dependency.
406 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/scanner.c PROPERTIES
408 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/scanner.h
412 # Add scanner.c to the list of sources.
414 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/scanner.c)
417 # Try to find YACC or Bison.
419 find_program(YACC_EXECUTABLE NAMES bison win_bison byacc yacc)
420 if( ${YACC_EXECUTABLE} STREQUAL "YACC_EXECUTABLE-NOTFOUND" )
421 message(FATAL_ERROR "Neither bison nor win_bison nor byacc nor yacc was found." )
423 message(STATUS "Parser generator: ${YACC_EXECUTABLE}")
426 # Create custom command for the scanner.
427 # Find out whether it's Bison or notby looking at the last component
428 # of the path (without a .exe extension, if this is Windows).
430 get_filename_component(YACC_NAME ${YACC_EXECUTABLE} NAME_WE)
431 if( "${YACC_NAME}" STREQUAL "bison" OR "${YACC_NAME}" STREQUAL "win_bison" )
432 set( YACC_COMPATIBILITY_FLAG "-y" )
435 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/grammar.c ${CMAKE_CURRENT_BINARY_DIR}/grammar.h
436 SOURCE ${pcap_SOURCE_DIR}/grammar.y
437 COMMAND ${YACC_EXECUTABLE} ${YACC_COMPATIBILITY_FLAG} -p pcap_ -o ${CMAKE_CURRENT_BINARY_DIR}/grammar.c -d ${pcap_SOURCE_DIR}/grammar.y
438 DEPENDS ${pcap_SOURCE_DIR}/grammar.y
442 # Since grammar.c does not exists yet when cmake is run, mark
445 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/grammar.c PROPERTIES
450 # Add grammar.c to the list of sources.
452 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/grammar.c)
456 # CMake does not love Windows.
458 file(TO_NATIVE_PATH "${pcap_SOURCE_DIR}/GenVersion.bat" GenVersion_path)
459 file(TO_NATIVE_PATH "${pcap_SOURCE_DIR}/VERSION" VERSION_path)
460 file(TO_NATIVE_PATH "${pcap_SOURCE_DIR}/pcap_version.h.in" version_h_in_path)
461 file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h" version_h_path)
463 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h
464 SOURCE ${pcap_SOURCE_DIR}/VERSION ${pcap_SOURCE_DIR}/pcap_version.h.in
465 COMMAND ${GenVersion_path} ${VERSION_path} ${version_h_in_path} ${version_h_path}
466 DEPENDS ${pcap_SOURCE_DIR}/VERSION ${pcap_SOURCE_DIR}/pcap_version.h.in
470 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/version.c
471 SOURCE ${pcap_SOURCE_DIR}/VERSION
472 COMMAND ${pcap_SOURCE_DIR}/gen_version_c.sh ${pcap_SOURCE_DIR}/VERSION ${CMAKE_CURRENT_BINARY_DIR}/version.c
473 DEPENDS ${pcap_SOURCE_DIR}/VERSION
476 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h
477 SOURCE ${pcap_SOURCE_DIR}/VERSION
478 COMMAND ${pcap_SOURCE_DIR}/gen_version_header.sh ${pcap_SOURCE_DIR}/VERSION ${pcap_SOURCE_DIR}/pcap_version.h.in ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h
479 DEPENDS ${pcap_SOURCE_DIR}/VERSION
483 # Since version.c does not exists yet when cmake is run, mark
486 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/version.c PROPERTIES
491 # Add version.c to the list of sources.
493 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/version.c)
497 # Since pcap_version.h does not exists yet when cmake is run, mark
500 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h PROPERTIES
505 # Add pcap_version.h to the list of headers.
507 set(PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h)
509 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
510 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
512 ######################################
514 ######################################
516 add_library(${LIBRARY_NAME}
517 ${PROJECT_SOURCE_LIST_C}
518 ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
519 ${CMAKE_CURRENT_BINARY_DIR}/scanner.c
520 ${PROJECT_SOURCE_LIST_H}
524 target_link_libraries ( ${LIBRARY_NAME}
530 ######################################
531 # Write out the config.h file
532 ######################################
534 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)