]> The Tcpdump Group git mirrors - libpcap/blob - CMakeLists.txt
Explain what valgrindtest does.
[libpcap] / CMakeLists.txt
1 cmake_minimum_required( VERSION 2.8.8 )
2
3 set( PROJECT_NAME pcap )
4 project( ${PROJECT_NAME} )
5
6 ###################################################################
7 # Parameters
8 ###################################################################
9
10 option (INET6 "Enable IPv6" ON)
11 if( MSVC )
12 option (USE_STATIC_RT "Use static Runtime" ON)
13 endif( MSVC )
14 option (BUILD_SHARED_LIBS "Build shared libraries" ON)
15 if( WIN32 )
16 set(PACKET_DLL_DIR "" CACHE PATH "Path to directory with include and lib subdirectories for packet.dll")
17 endif( WIN32 )
18
19 ######################################
20 # Project setings
21 ######################################
22
23 add_definitions( -DHAVE_CONFIG_H )
24
25 include_directories(
26 ${CMAKE_BINARY_DIR}
27 ${pcap_SOURCE_DIR}
28 )
29
30 if( WIN32 )
31 if( NOT "${PACKET_DLL_DIR}" STREQUAL "" )
32 include_directories("${PACKET_DLL_DIR}/Include")
33 if( CMAKE_CL_64 )
34 link_directories("${PACKET_DLL_DIR}/Lib/x64")
35 else( CMAKE_CL_64 )
36 link_directories("${PACKET_DLL_DIR}/Lib")
37 endif( CMAKE_CL_64 )
38 endif()
39 include_directories(
40 ../Common/
41 Win32/Include
42 )
43 endif( WIN32)
44
45 add_definitions( -DLIBPCAP_EXPORTS )
46
47 if( MSVC )
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))" )
53 else(MSVC)
54 add_definitions( "-D_U_=" )
55 endif( MSVC )
56
57 if( MSVC )
58 if (USE_STATIC_RT)
59 MESSAGE( STATUS "Use STATIC runtime" )
60 set(NAME_RT MT)
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")
65
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")
70 else (USE_STATIC_RT)
71 MESSAGE( STATUS "Use DYNAMIC runtime" )
72 set(NAME_RT MD)
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")
77
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")
82 endif (USE_STATIC_RT)
83 endif( MSVC )
84
85 ###################################################################
86 # Detect available platform features
87 ###################################################################
88
89 include(CheckIncludeFile)
90 include(CheckFunctionExists)
91 include(CheckStructHasMember)
92 include(CheckTypeSize)
93
94 #
95 # Header files.
96 #
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 )
105
106 #
107 # Functions.
108 #
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 )
113
114 #
115 # Data types.
116 #
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.
120 #
121 check_struct_has_member("struct sockaddr_storage" ss_family sys/socket.h HAVE_SOCKADDR_STORAGE)
122
123 #
124 # Structure fields.
125 #
126 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_SOCKADDR_SA_LEN )
127
128 if( INET6 )
129 MESSAGE( STATUS "Use IPv6" )
130 endif( INET6 )
131
132 if( WIN32 )
133 add_definitions( -DHAVE_ADDRINFO )
134 endif( WIN32 )
135
136 ######################################
137 # External dependencies
138 ######################################
139
140 ######################################
141 # Input files
142 ######################################
143
144 set(PROJECT_SOURCE_LIST_C
145 bpf_dump.c
146 bpf_image.c
147 etherent.c
148 gencode.c
149 inet.c
150 nametoaddr.c
151 optimize.c
152 pcap-common.c
153 pcap.c
154 savefile.c
155 sf-pcap-ng.c
156 sf-pcap.c
157 bpf/net/bpf_filter.c
158 )
159
160 if( WIN32 )
161 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/win_snprintf.c )
162 else()
163 if( NOT HAVE_SNPRINTF )
164 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/snprintf.c )
165 endif( NOT HAVE_SNPRINTF )
166 endif( WIN32 )
167
168 #
169 # Determine the main pcap-XXX.c file to use.
170 #
171 if( WIN32 )
172 #
173 # WinPcap.
174 #
175 set( PCAP_TYPE win32 )
176 else()
177 #
178 # UN*X - figure out what type of packet capture mechanism we
179 # have.
180 #
181 if( EXISTS /dev/bpf )
182 #
183 # Cloning BPF device.
184 #
185 set( PCAP_TYPE bpf )
186 AC_DEFINE(HAVE_CLONING_BPF,1,[define if you have a cloning BPF device])
187 elseif( EXISTS /dev/bpf0 )
188 set( PCAP_TYPE bpf )
189
190 #
191 # XXX - many more BPF checks.
192 #
193 elseif( EXISTS /usr/include/net/pfilt.h )
194 #
195 # DEC OSF/1, Digital UNIX, Tru64 UNIX
196 #
197 set( PCAP_TYPE pf )
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 )
203 set( PCAP_TYPE nit )
204 elseif( EXISTS /usr/include/linux/socket.h )
205 set( PCAP_TYPE linux )
206
207 #
208 # Do we have the wireless extensions?
209 #
210 check_include_file( linux/wireless.h HAVE_LINUX_WIRELESS_H )
211
212 #
213 # XXX - many more Linux checks.
214 #
215 elseif( EXISTS /usr/include/net/raw.h )
216 set( PCAP_TYPE snoop )
217 elseif( EXISTS /usr/include/odmi.h )
218 #
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.
222 #
223 set( PCAP_TYPE bpf )
224 elseif( /usr/include/sys/dlpi.h )
225 set( PCAP_TYPE dlpi )
226
227 #
228 # XXX - many more DLPI checks.
229 #
230 else()
231 set( PCAP_TYPE null )
232 endif()
233 endif( WIN32 )
234 message(STATUS "Packet capture mechanism type: ${PCAP_TYPE}")
235 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-${PCAP_TYPE}.c)
236
237 #
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
240 # capturing.
241 #
242 if( WIN32 )
243 #
244 # WinPcap.
245 #
246 set( FINDALLDEVS_TYPE win32 )
247 else()
248 #
249 # UN*X - figure out what type of interface list mechanism we
250 # have.
251 #
252 if( ${PCAP_TYPE} STREQUAL "null" )
253 #
254 # We can't capture, so we can't open any capture
255 # devices, so we won't return any interfaces.
256 #
257 set( FINDALLDEVS_TYPE null )
258 else()
259 check_function_exists( getifaddrs HAVE_GETIFADDRS )
260 if( ${HAVE_GETIFADDRS} )
261 #
262 # We have "getifaddrs()"; make sure we have <ifaddrs.h>
263 # as well, just in case some platform is really weird.
264 #
265 check_include_file( ifaddrs.h HAVE_IFADDRS_H )
266 if( ${HAVE_IFADDRS_H} )
267 #
268 # We have the header, so we use "getifaddrs()" to
269 # get the list of interfaces.
270 #
271 set( FINDALLDEVS_TYPE getad )
272 else()
273 #
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.
281 #
282 message(FATAL_ERROR "Your system has getifaddrs() but doesn't have a usable <ifaddrs.h>." )
283 endif()
284 else()
285 #
286 # Well, we don't have "getifaddrs()", so we have to use
287 # some other mechanism; determine what that mechanism is.
288 #
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.
291 #
292 if( ${PCAP_TYPE} STREQUAL "dlpi" OR ${PCAP_TYPE} STREQUAL "libdlpi" )
293 #
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
297 # just SIOCGIFCONF.
298 #
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 )
303 else()
304 set( FINDALLDEVS_TYPE gifc )
305 endif()
306 else()
307 #
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
311 # instead.)
312 #
313 set( FINDALLDEVS_TYPE gifc )
314 endif()
315 endif()
316 endif()
317 endif()
318 message(STATUS "Find-interfaces mechanism type: ${FINDALLDEVS_TYPE}")
319 set( PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} fad-${FINDALLDEVS_TYPE}.c )
320
321 file(GLOB PROJECT_SOURCE_LIST_CORE_H
322 *.h
323 pcap/*.h
324 )
325 set( PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_CORE_H} )
326
327 if( WIN32 )
328 file(GLOB PROJECT_SOURCE_LIST_WIN32_H
329 Win32/Include/*.h
330 )
331 set( PROJECT_SOURCE_LIST_H ${PROJECT_SOURCE_LIST_H} ${PROJECT_SOURCE_LIST_WIN32_H} )
332 endif( WIN32 )
333
334 #
335 # {Flex} and YACC/Berkeley YACC/Bison.
336 # From a mail message to the CMake mailing list by Andy Cedilnik of
337 # Kitware.
338 #
339
340 #
341 # Try to find Flex, a Windows version of Flex, or Lex.
342 #
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." )
346 endif()
347 message(STATUS "Lexical analyzer generator: ${LEX_EXECUTABLE}")
348
349 add_custom_command(
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
354 )
355
356 #
357 # Since scanner.c does not exist yet when cmake is run, mark
358 # it as generated.
359 #
360 # Since scanner.c includes grammar.h, mark that as a dependency.
361 #
362 set_source_files_properties(${CMAKE_BINARY_DIR}/scanner.c PROPERTIES
363 GENERATED TRUE
364 OBJECT_DEPENDS ${CMAKE_BINARY_DIR}/scanner.h
365 )
366
367 #
368 # Add scanner.c to the list of sources.
369 #
370 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_BINARY_DIR}/scanner.c)
371
372 #
373 # Try to find YACC or Bison.
374 #
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." )
378 endif()
379 message(STATUS "Parser generator: ${YACC_EXECUTABLE}")
380
381 #
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).
385 #
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" )
389 endif()
390 add_custom_command(
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
395 )
396
397 #
398 # Since grammar.c does not exists yet when cmake is run, mark
399 # it as generated.
400 #
401 set_source_files_properties(${CMAKE_BINARY_DIR}/grammar.c PROPERTIES
402 GENERATED TRUE
403 )
404
405 #
406 # Add grammar.c to the list of sources.
407 #
408 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_BINARY_DIR}/grammar.c)
409
410 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
411 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
412
413 ######################################
414 # Register targets
415 ######################################
416
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}
422 )
423
424 if( WIN32 )
425 target_link_libraries ( ${PROJECT_NAME}
426 packet
427 ws2_32
428 )
429 endif( WIN32 )
430
431 ######################################
432 # Write out the config.h file
433 ######################################
434
435 configure_file(${CMAKE_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_BINARY_DIR}/config.h)