]> The Tcpdump Group git mirrors - libpcap/blob - CMakeLists.txt
Merge pull request #1 from the-tcpdump-group/master
[libpcap] / CMakeLists.txt
1 cmake_minimum_required( VERSION 2.8.8 )
2
3 #
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.
7 #
8 if (POLICY CMP0042)
9 cmake_policy(SET CMP0042 OLD)
10 endif()
11
12 project( pcap )
13 #
14 # Call the library "wpcap" on Windows, for backwards compatibility.
15 #
16 if( WIN32 )
17 set( LIBRARY_NAME wpcap )
18 else()
19 set( LIBRARY_NAME pcap )
20 endif()
21
22 ###################################################################
23 # Parameters
24 ###################################################################
25
26 option (INET6 "Enable IPv6" ON)
27 if( MSVC )
28 option (USE_STATIC_RT "Use static Runtime" ON)
29 endif( MSVC )
30 option (BUILD_SHARED_LIBS "Build shared libraries" ON)
31 if( WIN32 )
32 set(PACKET_DLL_DIR "" CACHE PATH "Path to directory with include and lib subdirectories for packet.dll")
33 endif( WIN32 )
34
35 #
36 # Default to having remote capture support on Windows and, for now, to
37 # not having it on UN*X.
38 #
39 if( WIN32 )
40 option (HAVE_REMOTE "Enable remote capture" ON)
41 else()
42 option (HAVE_REMOTE "Enable remote capture" OFF)
43 endif( WIN32 )
44
45 ######################################
46 # Project settings
47 ######################################
48
49 add_definitions( -DHAVE_CONFIG_H )
50
51 include_directories(
52 ${CMAKE_CURRENT_BINARY_DIR}
53 ${pcap_SOURCE_DIR}
54 )
55
56 if( WIN32 )
57 #
58 # The 'Win32/WpdPack/' directory branch is for an AppVeyor build.
59 #
60 if( NOT "${PACKET_DLL_DIR}" STREQUAL "" )
61 include_directories("${PACKET_DLL_DIR}/Include")
62 if( CMAKE_CL_64 )
63 link_directories("${PACKET_DLL_DIR}/Lib/x64")
64 else( CMAKE_CL_64 )
65 link_directories("${PACKET_DLL_DIR}/Lib")
66 endif( CMAKE_CL_64 )
67 endif()
68 include_directories(
69 ../Common/
70 Win32/Include
71 Win32/WpdPack/Include
72 )
73 endif( WIN32)
74
75 add_definitions( -DBUILDING_PCAP )
76
77 if( MSVC )
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))" )
83 else(MSVC)
84 add_definitions( "-D_U_=" )
85 endif( MSVC )
86
87 if( MSVC )
88 if (USE_STATIC_RT)
89 MESSAGE( STATUS "Use STATIC runtime" )
90 set(NAME_RT MT)
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")
95
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")
100 else (USE_STATIC_RT)
101 MESSAGE( STATUS "Use DYNAMIC runtime" )
102 set(NAME_RT MD)
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")
107
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)
113 endif( MSVC )
114
115 ###################################################################
116 # Detect available platform features
117 ###################################################################
118
119 include(CheckIncludeFile)
120 include(CheckFunctionExists)
121 include(CheckStructHasMember)
122 include(CheckTypeSize)
123
124 #
125 # Header files.
126 #
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 )
135
136 #
137 # Functions.
138 #
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 )
144
145 if (WIN32)
146 #
147 # Check for Windows-only functions, such as packet.dll functions.
148 #
149 check_function_exists( PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER )
150 endif()
151
152 #
153 # Data types.
154 #
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.
158 #
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)
163
164 #
165 # Structure fields.
166 #
167 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_SOCKADDR_SA_LEN )
168
169 if( INET6 )
170 MESSAGE( STATUS "Use IPv6" )
171 endif( INET6 )
172
173 if( WIN32 )
174 add_definitions( -DHAVE_ADDRINFO )
175 endif( WIN32 )
176
177 ######################################
178 # External dependencies
179 ######################################
180
181 ######################################
182 # Input files
183 ######################################
184
185 set(PROJECT_SOURCE_LIST_C
186 bpf_dump.c
187 bpf_image.c
188 etherent.c
189 gencode.c
190 inet.c
191 nametoaddr.c
192 optimize.c
193 pcap-common.c
194 pcap.c
195 savefile.c
196 sf-pcap-ng.c
197 sf-pcap.c
198 bpf/net/bpf_filter.c
199 )
200
201 if( WIN32 )
202 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/win_snprintf.c )
203 else()
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 )
210 endif( WIN32 )
211
212 if( HAVE_REMOTE )
213 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C}
214 pcap-new.c pcap-rpcap.c sockutils.c)
215 endif( HAVE_REMOTE )
216
217 #
218 # Determine the main pcap-XXX.c file to use.
219 #
220 if( WIN32 )
221 #
222 # WinPcap.
223 #
224 set( PCAP_TYPE win32 )
225 else()
226 #
227 # UN*X - figure out what type of packet capture mechanism we
228 # have.
229 #
230 if( EXISTS /dev/bpf )
231 #
232 # Cloning BPF device.
233 #
234 set( PCAP_TYPE bpf )
235 AC_DEFINE(HAVE_CLONING_BPF,1,[define if you have a cloning BPF device])
236 elseif( EXISTS /dev/bpf0 )
237 set( PCAP_TYPE bpf )
238
239 #
240 # XXX - many more BPF checks.
241 #
242 elseif( EXISTS /usr/include/net/pfilt.h )
243 #
244 # DEC OSF/1, Digital UNIX, Tru64 UNIX
245 #
246 set( PCAP_TYPE pf )
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 )
252 set( PCAP_TYPE nit )
253 elseif( EXISTS /usr/include/linux/socket.h )
254 set( PCAP_TYPE linux )
255
256 #
257 # Do we have the wireless extensions?
258 #
259 check_include_file( linux/wireless.h HAVE_LINUX_WIRELESS_H )
260
261 #
262 # XXX - many more Linux checks.
263 #
264 elseif( EXISTS /usr/include/net/raw.h )
265 set( PCAP_TYPE snoop )
266 elseif( EXISTS /usr/include/odmi.h )
267 #
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.
271 #
272 set( PCAP_TYPE bpf )
273 elseif( /usr/include/sys/dlpi.h )
274 set( PCAP_TYPE dlpi )
275
276 #
277 # XXX - many more DLPI checks.
278 #
279 else()
280 set( PCAP_TYPE null )
281 endif()
282 endif( WIN32 )
283 message(STATUS "Packet capture mechanism type: ${PCAP_TYPE}")
284 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-${PCAP_TYPE}.c)
285
286 #
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
289 # capturing.
290 #
291 if( NOT WIN32 )
292 #
293 # UN*X - figure out what type of interface list mechanism we
294 # have.
295 #
296 if( ${PCAP_TYPE} STREQUAL "null" )
297 #
298 # We can't capture, so we can't open any capture
299 # devices, so we won't return any interfaces.
300 #
301 set( FINDALLDEVS_TYPE null )
302 else()
303 check_function_exists( getifaddrs HAVE_GETIFADDRS )
304 if( ${HAVE_GETIFADDRS} )
305 #
306 # We have "getifaddrs()"; make sure we have <ifaddrs.h>
307 # as well, just in case some platform is really weird.
308 #
309 check_include_file( ifaddrs.h HAVE_IFADDRS_H )
310 if( ${HAVE_IFADDRS_H} )
311 #
312 # We have the header, so we use "getifaddrs()" to
313 # get the list of interfaces.
314 #
315 set( FINDALLDEVS_TYPE getad )
316 else()
317 #
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.
325 #
326 message(FATAL_ERROR "Your system has getifaddrs() but doesn't have a usable <ifaddrs.h>." )
327 endif()
328 else()
329 #
330 # Well, we don't have "getifaddrs()", so we have to use
331 # some other mechanism; determine what that mechanism is.
332 #
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.
335 #
336 if( ${PCAP_TYPE} STREQUAL "dlpi" OR ${PCAP_TYPE} STREQUAL "libdlpi" )
337 #
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
341 # just SIOCGIFCONF.
342 #
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 )
347 else()
348 set( FINDALLDEVS_TYPE gifc )
349 endif()
350 else()
351 #
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
355 # instead.)
356 #
357 set( FINDALLDEVS_TYPE gifc )
358 endif()
359 endif()
360 endif()
361 message(STATUS "Find-interfaces mechanism type: ${FINDALLDEVS_TYPE}")
362 set( PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} fad-${FINDALLDEVS_TYPE}.c )
363 endif()
364
365 file(GLOB PROJECT_SOURCE_LIST_CORE_H
366 *.h
367 pcap/*.h
368 )
369 set( PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_CORE_H} )
370
371 if( WIN32 )
372 file(GLOB PROJECT_SOURCE_LIST_WIN32_H
373 Win32/Include/*.h
374 )
375 set( PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_WIN32_H} )
376 endif( WIN32 )
377
378 #
379 # {Flex} and YACC/Berkeley YACC/Bison.
380 # From a mail message to the CMake mailing list by Andy Cedilnik of
381 # Kitware.
382 #
383
384 #
385 # Try to find Flex, a Windows version of Flex, or Lex.
386 #
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." )
390 endif()
391 message(STATUS "Lexical analyzer generator: ${LEX_EXECUTABLE}")
392
393 add_custom_command(
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
398 )
399
400 #
401 # Since scanner.c does not exist yet when cmake is run, mark
402 # it as generated.
403 #
404 # Since scanner.c includes grammar.h, mark that as a dependency.
405 #
406 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/scanner.c PROPERTIES
407 GENERATED TRUE
408 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/scanner.h
409 )
410
411 #
412 # Add scanner.c to the list of sources.
413 #
414 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/scanner.c)
415
416 #
417 # Try to find YACC or Bison.
418 #
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." )
422 endif()
423 message(STATUS "Parser generator: ${YACC_EXECUTABLE}")
424
425 #
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).
429 #
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" )
433 endif()
434 add_custom_command(
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
439 )
440
441 #
442 # Since grammar.c does not exists yet when cmake is run, mark
443 # it as generated.
444 #
445 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/grammar.c PROPERTIES
446 GENERATED TRUE
447 )
448
449 #
450 # Add grammar.c to the list of sources.
451 #
452 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/grammar.c)
453
454 if( WIN32 )
455 #
456 # CMake does not love Windows.
457 #
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)
462 add_custom_command(
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
467 )
468 else( WIN32 )
469 add_custom_command(
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
474 )
475 add_custom_command(
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
480 )
481
482 #
483 # Since version.c does not exists yet when cmake is run, mark
484 # it as generated.
485 #
486 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/version.c PROPERTIES
487 GENERATED TRUE
488 )
489
490 #
491 # Add version.c to the list of sources.
492 #
493 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/version.c)
494 endif( WIN32 )
495
496 #
497 # Since pcap_version.h does not exists yet when cmake is run, mark
498 # it as generated.
499 #
500 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h PROPERTIES
501 GENERATED TRUE
502 )
503
504 #
505 # Add pcap_version.h to the list of headers.
506 #
507 set(PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${CMAKE_CURRENT_BINARY_DIR}/pcap_version.h)
508
509 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
510 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
511
512 ######################################
513 # Register targets
514 ######################################
515
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}
521 )
522
523 if( WIN32 )
524 target_link_libraries ( ${LIBRARY_NAME}
525 packet
526 ws2_32
527 )
528 endif( WIN32 )
529
530 ######################################
531 # Write out the config.h file
532 ######################################
533
534 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)