-
Notifications
You must be signed in to change notification settings - Fork 853
/
CMakeLists.txt
3601 lines (3379 loc) · 128 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if(WIN32)
#
# We need 3.12 or later, so that we can set policy CMP0074; see
# below.
#
cmake_minimum_required(VERSION 3.12)
else(WIN32)
#
# For now:
#
# if this is a version of CMake less than 3.5, require only
# 2.8.12, just in case somebody is configuring with CMake
# on a "long-term support" version # of some OS and that
# version supplies an older version of CMake;
#
# otherwise, if it's a version less than 3.10, require only
# 3.5, just in case somebody is configuring with CMake
# on a "long-term support" version # of some OS and that
# version supplies an older version of CMake;
#
# otherwise, require 3.10, so we don't get messages warning
# that support for versions of CMake lower than 3.10 is
# deprecated.
#
if(CMAKE_VERSION VERSION_LESS "3.5")
cmake_minimum_required(VERSION 2.8.12)
elseif(CMAKE_VERSION VERSION_LESS "3.10")
cmake_minimum_required(VERSION 3.5)
else()
cmake_minimum_required(VERSION 3.10)
endif()
endif(WIN32)
#
# Apple doesn't build with an install_name starting with @rpath, and
# neither do we with autotools; don't do so with CMake, either, and
# suppress warnings about that.
#
# Setting CMAKE_MACOSX_RPATH to FALSE uses the old behavior,
# but removes the POLICY CMP0042 OLD deprecated warning.
# See https://cmake.org/cmake/help/latest/policy/CMP0042.html
#
if (NOT DEFINED CMAKE_MACOSX_RPATH)
set(CMAKE_MACOSX_RPATH FALSE)
endif()
if(POLICY CMP0042)
cmake_policy(SET CMP0042 NEW)
endif()
#
# Squelch noise about quoted strings in if() statements.
# WE KNOW WHAT WE'RE DOING, WE'RE DOING EVERYTHING THE WAY THAT NEWER
# VERSIONS OF CMAKE EXPECT BY DEFAULT, DON'T WASTE OUR TIME WITH NOISE.
#
if(POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()
#
# We want find_file() and find_library() to honor {packagename}_ROOT,
# as that appears to be the only way, with the Visual Studio 2019 IDE
# and its CMake support, to tell CMake where to look for the Npcap
# or WinPcap SDK.
#
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()
#
# We want check_include_file() to honor CMAKE_REQUIRED_LIBRARIES; see
# the big comment before the check_include_file() test for
# infiniband/verbs.h for the reason.
#
if(POLICY CMP0075)
cmake_policy(SET CMP0075 NEW)
endif()
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
#
# We explicitly indicate what languages are used in libpcap to avoid
# checking for a C++ compiler.
#
# One reason to avoid that check is that there's no need to waste
# configuration time performing it.
#
# Another reason is that:
#
# CMake will try to determine the sizes of some data types, including
# void *, early in the process of configuration; apparently, it's done
# as part of processing the project() command.
#
# At least as of CMake 2.8.6, it does so by checking the size of
# "void *" in C, setting CMAKE_C_SIZEOF_DATA_PTR based on that,
# setting CMAKE_SIZEOF_VOID_P to that, and then checking the size
# of "void *" in C++, setting CMAKE_CXX_SIZEOF_DATA_PTR based on
# that, and then setting CMAKE_SIZEOF_VOID_P to *that*.
#
# The compile tests include whatever C flags may have been provided
# to CMake in the CFLAGS and CXXFLAGS environment variables.
#
# If you set an architecture flag such as -m32 or -m64 in CFLAGS
# but *not* in CXXFLAGS, the size for C++ will win, and hilarity
# will ensue.
#
# Or if, at least on Solaris, you have a newer version of GCC
# installed, but *not* a newer version of G++, and you have Oracle
# Studio installed, it will find GCC, which will default to building
# 64-bit, and Oracle Studio's C++ compiler, which will default to
# building 32-bit, the size for C++ will win, and, again, hilarity
# will ensue.
#
project(pcap C)
#
# Export the size of void * as SIZEOF_VOID_P so that it can be
# tested with #if.
#
set(SIZEOF_VOID_P "${CMAKE_SIZEOF_VOID_P}")
#
# Setting CMAKE_MACOSX_RPATH to FALSE causes the installed
# libpcap.A.dylib to have just libpcap.A.dylib as the install
# name; Apple built libpcap with an install_name of /usr/lib/libpcap.A.dylib
# (back when they still shipped individual system dylibs rather than
# shipping a pre-built shared library cache, at least), and we do the
# same with autotools; do the same with CMake.
#
if (NOT DEFINED CMAKE_INSTALL_NAME_DIR)
set(CMAKE_INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib)
endif()
#
# For getting raw lists of --libs and --libs --static information from a
# pkg-config module.
#
# In CMake up to 2.8.12, pkg_check_modules() sets:
#
# <XPREFIX>_LIBRARIES, which is a list of library names to which, on
# a UN*X, -l can be prefixed - i.e., names, without extensions,
# rather than full paths to the file.
# <XPREFIX>_LIBRARY_DIRS, which is a list of paths to directories
# containing the libraries, to which, on a UN*X, -L can be
# prefixed.
# <XPREFIX>_LDFLAGS, which is a list of *all* required linker flags
# <XPREFIX>_LDFLAGS_OTHER, which is a list of all linker flags other
# than -l and -L flags
#
# In 3.0 (at least as of 3.0.2), it also sets:
#
# <XPREFIX>_LINK_LIBRARIES, which is a list of full paths to the
# library files.
#
# but if <XPREFIX> is <PREFIX>_STATIC, <XPREFIX>_LINK_LIBRARIES is
# currently not set by CMake.
#
# Unfortunately, pkg_check_modules() sets the
# PKG_CONFIG_ALLOW_SYSTEM_LIBS environment variable when running
# pkg-config, so the output of --libs, etc. may include a -L for the
# system library, which we do *NOT* want to put in our libpcap.pc and
# pcap-config files.
#
# So we just run pkg-config ourselves, so that we get its output
# directly without any processing by CMake.
#
macro(pkg_get_link_info _prefix _package)
if (PKG_CONFIG_EXECUTABLE)
#
# Get the --libs information.
#
# We force PKG_CONFIG_ALLOW_SYSTEM_LIBS to be undefined, as
# at least some versions of CMake appear to define it in
# pkg_check_modules() before running pkg-config and *not* undefine
# it after running it.
#
unset(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS})
set(_pkg_config_result "")
execute_process(
COMMAND ${PKG_CONFIG_EXECUTABLE} "--libs" ${_package}
OUTPUT_VARIABLE _pkg_config_result
RESULT_VARIABLE _pkg_config_failed
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (_pkg_config_failed)
#
# pkg-config failed; assume that means that there is no such
# package for it to find. XXX - what do we do here?
#
set(${_prefix}_FOUND_WITH_PKG_CONFIG FALSE)
else()
#
# pkg-config succeeded; replace CR and LF with spaces.
#
string(REGEX REPLACE "[\r\n]" " " ${_prefix}_LIBS "${_pkg_config_result}")
#
# Now get the --libs --static information.
#
set(_pkg_config_result "")
execute_process(
COMMAND ${PKG_CONFIG_EXECUTABLE} "--libs" "--static" ${_package}
OUTPUT_VARIABLE _pkg_config_result
RESULT_VARIABLE _pkg_config_failed
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (_pkg_config_failed)
#
# pkg-config failed; assume that means that there is no such
# package for it to find. XXX - what do we do here?
#
set(${_prefix}_FOUND_WITH_PKG_CONFIG FALSE)
else()
#
# pkg-config succeeded; replace CR and LF with spaces.
#
string(REGEX REPLACE "[\r\n]" " " ${_prefix}_LIBS_STATIC "${_pkg_config_result}")
#
# List this package in its PACKAGE_NAME variable.
#
set(${_prefix}_PACKAGE_NAME "${_package}")
#
# It worked.
#
set(${_prefix}_FOUND_WITH_PKG_CONFIG TRUE)
endif()
endif()
endif()
endmacro()
macro(get_link_info_from_library_path _library_prefix _library_name)
if(NOT ${_library_prefix}_LIBRARY STREQUAL "${_library_prefix}_LIBRARY-NOTFOUND")
get_filename_component(_lib_directory "${${_library_prefix}_LIBRARY}}" DIRECTORY)
#
# The closest thing to a list of "system library directories" in
# which the linker will, by default, search for libraries appears to
# be CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES, so that's what we use
# when we're trying to construct a -L argument, for insertion into
# pcap-config and libpcap.pc, for a library upon which we depend.
#
# In some versions of CMake it appears to have duplicate entries,
# but that shouldn't affect a search for a directory in that list.
#
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${_lib_directory}" _lib_index)
if(_lib_index EQUAL -1)
#
# No, so add a -L flag to get the linker to search in that
# directory.
#
set(${_library_prefix}_LIBS "-L${_lib_directory}")
set(${_library_prefix}_LIBS_STATIC "-L${_lib_directory}")
set(${_libraryprefix}_LIBS_PRIVATE "-L${_lib_directory}")
endif()
set(${_library_prefix}_LIBS "${${_library_prefix}_LIBS} -l${_library_name}")
set(${_library_prefix}_LIBS_STATIC "${${_library_prefix}_LIBS} -l${_library_name}")
set(${_library_prefix}_LIBS_PRIVATE "${${_library_prefix}_LIBS} -l${_library_name}")
endif()
endmacro()
#
# Show the bit width for which we're compiling.
# This can help debug problems if you're dealing with a compiler that
# defaults to generating 32-bit code even when running on a 64-bit
# platform, and where that platform may provide only 64-bit versions of
# libraries that we might use (looking at *you*, Oracle Studio!).
#
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
message(STATUS "Building 32-bit")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
message(STATUS "Building 64-bit")
endif()
#
# Solaris pkg-config is annoying. For at least one package (D-Bus, I'm
# looking at *you*!), there are separate include files for 32-bit and
# 64-bit builds (I guess using "unsigned long long" as a 64-bit integer
# type on a 64-bit build is like crossing the beams or something), and
# there are two separate .pc files, so if we're doing a 32-bit build we
# should make sure we look in /usr/lib/pkgconfig for .pc files and if
# we're doing a 64-bit build we should make sure we look in
# /usr/lib/amd64/pkgconfig for .pc files.
#
if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*")
#
# Note: string(REPLACE) does not appear to support using ENV{...}
# as an argument, so we set a variable and then use set() to set
# the environment variable.
#
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
#
# 64-bit build. If /usr/lib/pkgconfig appears in the path,
# prepend /usr/lib/amd64/pkgconfig to it; otherwise,
# put /usr/lib/amd64 at the end.
#
if((NOT DEFINED ENV{PKG_CONFIG_PATH}) OR "$ENV{PKG_CONFIG_PATH}" EQUAL "")
#
# Not set, or empty. Set it to /usr/lib/amd64/pkgconfig.
#
set(fixed_path "/usr/lib/amd64/pkgconfig")
elseif("$ENV{PKG_CONFIG_PATH}" MATCHES "/usr/lib/pkgconfig")
#
# It contains /usr/lib/pkgconfig. Prepend
# /usr/lib/amd64/pkgconfig to /usr/lib/pkgconfig.
#
string(REPLACE "/usr/lib/pkgconfig"
"/usr/lib/amd64/pkgconfig:/usr/lib/pkgconfig"
fixed_path "$ENV{PKG_CONFIG_PATH}")
else()
#
# Not empty, but doesn't contain /usr/lib/pkgconfig.
# Append /usr/lib/amd64/pkgconfig to it.
#
set(fixed_path "$ENV{PKG_CONFIG_PATH}:/usr/lib/amd64/pkgconfig")
endif()
set(ENV{PKG_CONFIG_PATH} "${fixed_path}")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
#
# 32-bit build. If /usr/amd64/lib/pkgconfig appears in the path,
# prepend /usr/lib/pkgconfig to it.
#
if("$ENV{PKG_CONFIG_PATH}" MATCHES "/usr/lib/amd64/pkgconfig")
#
# It contains /usr/lib/amd64/pkgconfig. Prepend
# /usr/lib/pkgconfig to /usr/lib/amd64/pkgconfig.
#
string(REPLACE "/usr/lib/amd64/pkgconfig"
"/usr/lib/pkgconfig:/usr/lib/amd64/pkgconfig"
fixed_path "$ENV{PKG_CONFIG_PATH}")
set(ENV{PKG_CONFIG_PATH} "${fixed_path}")
endif()
endif()
endif()
include(CheckCCompilerFlag)
#
# For checking if a compiler flag works and adding it if it does.
#
macro(check_and_add_compiler_option _option)
message(STATUS "Checking C compiler flag ${_option}")
string(REPLACE "=" "-" _temp_option_variable ${_option})
string(REGEX REPLACE "^-" "" _option_variable ${_temp_option_variable})
check_c_compiler_flag("${_option}" ${_option_variable})
if(${${_option_variable}})
set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} ${_option}")
endif()
endmacro()
#
# If we're building with Visual Studio, we require Visual Studio 2015,
# in order to get sufficient C99 compatibility. Check for that.
#
# If not, try the appropriate flag for the compiler to enable C99
# features.
#
set(C_ADDITIONAL_FLAGS "")
if(MSVC)
if(MSVC_VERSION LESS 1900)
message(FATAL_ERROR "Visual Studio 2015 or later is required")
endif()
#
# Treat source files as being in UTF-8 with MSVC if it's not using
# the Clang front end.
# We assume that UTF-8 source is OK with other compilers and with
# MSVC if it's using the Clang front end.
#
if(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} /utf-8")
endif(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
else(MSVC)
#
# For checking if a compiler flag works, failing if it doesn't,
# and adding it otherwise.
#
macro(require_and_add_compiler_option _option)
message(STATUS "Checking C compiler flag ${_option}")
string(REPLACE "=" "-" _temp_option_variable ${_option})
string(REGEX REPLACE "^-" "" _option_variable ${_temp_option_variable})
check_c_compiler_flag("${_option}" ${_option_variable})
if(${${_option_variable}})
set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} ${_option}")
else()
message(FATAL_ERROR "C99 support is required, but the compiler doesn't support a compiler flag to enable it")
endif()
endmacro()
#
# Try to enable as many C99 features as we can.
# At minimum, we want C++/C99-style // comments.
#
# Newer versions of compilers might default to supporting C99, but
# older versions may require a special flag.
#
# Prior to CMake 3.1, setting CMAKE_C_STANDARD will not have any effect,
# so, unless and until we require CMake 3.1 or later, we have to do it
# ourselves on pre-3.1 CMake, so we just do it ourselves on all versions
# of CMake.
#
# Note: with CMake 3.1 through 3.5, the only compilers for which CMake
# handles CMAKE_C_STANDARD are GCC and Clang. 3.6 adds support only
# for Intel C; 3.9 adds support for PGI C, Sun C, and IBM XL C, and
# 3.10 adds support for Cray C and IAR C, but no version of CMake has
# support for HP C. Therefore, even if we use CMAKE_C_STANDARD with
# compilers for which CMake supports it, we may still have to do it
# ourselves on other compilers.
#
# See the CMake documentation for the CMAKE_<LANG>_COMPILER_ID variables
# for a list of compiler IDs.
#
# XXX - this just tests whether the option works, fails if it doesn't,
# and adds it if it does. We don't test whether it's necessary in order
# to get the C99 features that we use, or whether, if it's used, it
# enables all the features that we require.
#
if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
CMAKE_C_COMPILER_ID MATCHES "Clang")
require_and_add_compiler_option("-std=gnu99")
elseif(CMAKE_C_COMPILER_ID MATCHES "XL")
#
# We want support for extensions picked up for GNU C compatibility,
# so we use -qlanglvl=extc99.
#
require_and_add_compiler_option("-qlanglvl=extc99")
elseif(CMAKE_C_COMPILER_ID MATCHES "HP")
require_and_add_compiler_option("-AC99")
elseif(CMAKE_C_COMPILER_ID MATCHES "Sun")
require_and_add_compiler_option("-xc99")
elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
require_and_add_compiler_option("-c99")
endif()
endif(MSVC)
#
# If we're building with MinGW, we need to specify _WIN32_WINNT as
# 0x0600 ("NT 6.0", a/k/a Vista/Windows Server 2008) or higher
# in order to get the full IPv6 API, including inet_ntop(), and we
# need to specify it as 0x0601 ("NT 6.1", a/k/a Windows 7) or higher
# in order to get NdisMediumIP.
#
# NOTE: pcap does *NOT* work with msvcrt.dll; it must link with
# a newer version of the C library, i.e. Visual Studio 2015 or
# later, as it depends on C99 features introduced in VS 2015.
#
if(MINGW)
add_definitions(-D_WIN32_WINNT=0x0601)
endif(MINGW)
#
# Build all runtimes in the top-level binary directory; that way,
# on Windows, the executables will be in the same directory as
# the DLLs, so the system will find pcap.dll when any of the
# executables are run.
#
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/run)
###################################################################
# Parameters
###################################################################
if(WIN32)
#
# On Windows, allow the library name to be overridden, for the
# benefit of projects that combine libpcap with their own
# kernel-mode code to support capturing.
#
set(LIBRARY_NAME pcap CACHE STRING "Library name")
else()
#
# On UN*X, it's always been libpcap.
#
set(LIBRARY_NAME pcap)
endif()
option(INET6 "Enable IPv6" ON)
if(WIN32)
option(USE_STATIC_RT "Use static Runtime" ON)
endif(WIN32)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
set(dpdk_ROOT "" CACHE PATH "Path to directory with include and lib subdirectories for DPDK")
if(WIN32)
set(Packet_ROOT "" CACHE PATH "Path to directory with include and lib subdirectories for packet.dll")
set(AirPcap_ROOT "" CACHE PATH "Path to directory with include and lib subdirectories for airpcap.dll")
endif(WIN32)
option(ENABLE_PROFILING "Enable code profiling" OFF)
# To pacify those who hate the protochain instruction
option(NO_PROTOCHAIN "Disable protochain instruction" OFF)
#
# Start out with the capture mechanism type unspecified; the user
# can explicitly specify it and, if they don't, we'll pick an
# appropriate one.
#
set(PCAP_TYPE "" CACHE STRING "Packet capture type")
#
# Default to having remote capture support on Windows and, for now, to
# not having it on UN*X.
#
if(WIN32)
option(ENABLE_REMOTE "Enable remote capture" ON)
else()
option(ENABLE_REMOTE "Enable remote capture" OFF)
endif(WIN32)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
option(BUILD_WITH_LIBNL "Build with libnl" ON)
endif()
#
# Additional capture modules.
#
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
option(DISABLE_LINUX_USBMON "Disable Linux usbmon USB sniffing support" OFF)
endif()
option(DISABLE_BLUETOOTH "Disable Bluetooth sniffing support" OFF)
option(DISABLE_NETMAP "Disable netmap support" OFF)
option(DISABLE_DPDK "Disable DPDK support" OFF)
#
# We don't support D-Bus sniffing on macOS; see
#
# https://bugs.freedesktop.org/show_bug.cgi?id=74029
#
if(APPLE)
option(DISABLE_DBUS "Disable D-Bus sniffing support" ON)
else(APPLE)
option(DISABLE_DBUS "Disable D-Bus sniffing support" OFF)
endif(APPLE)
option(DISABLE_RDMA "Disable RDMA sniffing support" OFF)
option(DISABLE_DAG "Disable Endace DAG card support" OFF)
option(DISABLE_SEPTEL "Disable Septel card support" OFF)
set(SEPTEL_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../septel" CACHE PATH "Path to directory with include and lib subdirectories for Septel API")
option(DISABLE_SNF "Disable Myricom SNF support" OFF)
#
# Debugging options.
#
option(BDEBUG "Build optimizer debugging code" OFF)
option(YYDEBUG "Build parser debugging code" OFF)
###################################################################
# Versioning
###################################################################
# Get, parse, format and set pcap's version string from [pcap_root]/VERSION
# for later use.
# Get MAJOR, MINOR, PATCH & SUFFIX
file(STRINGS ${pcap_SOURCE_DIR}/VERSION
PACKAGE_VERSION
LIMIT_COUNT 1 # Read only the first line
)
# Get "just" MAJOR
string(REGEX MATCH "^([0-9]+)" PACKAGE_VERSION_MAJOR "${PACKAGE_VERSION}")
# Get MAJOR, MINOR & PATCH
string(REGEX MATCH "^([0-9]+.)?([0-9]+.)?([0-9]+)" PACKAGE_VERSION_NOSUFFIX "${PACKAGE_VERSION}")
if(WIN32)
# Convert PCAP_VERSION_NOSUFFIX to Windows preferred version format
string(REPLACE "." "," PACKAGE_VERSION_PREDLL ${PACKAGE_VERSION_NOSUFFIX})
# Append NANO (used for Windows internal versioning) to PCAP_VERSION_PREDLL
# 0 means unused.
set(PACKAGE_VERSION_DLL ${PACKAGE_VERSION_PREDLL},0)
endif(WIN32)
set(PACKAGE_NAME "${LIBRARY_NAME}")
set(PACKAGE_STRING "${LIBRARY_NAME} ${PACKAGE_VERSION}")
######################################
# Project settings
######################################
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${pcap_SOURCE_DIR}
)
include(CheckFunctionExists)
include(CMakePushCheckState)
include(CheckSymbolExists)
if(WIN32)
if(IS_DIRECTORY ${CMAKE_HOME_DIRECTORY}/../../Common)
include_directories(${CMAKE_HOME_DIRECTORY}/../../Common)
endif(IS_DIRECTORY ${CMAKE_HOME_DIRECTORY}/../../Common)
find_package(Packet)
if(Packet_FOUND)
set(HAVE_PACKET32 TRUE)
include_directories(${Packet_INCLUDE_DIRS})
#
# Check whether we have the NPcap PacketIsLoopbackAdapter()
# function.
#
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES ${Packet_LIBRARIES})
check_function_exists(PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER)
check_function_exists(PacketGetTimestampModes HAVE_PACKET_GET_TIMESTAMP_MODES)
cmake_pop_check_state()
endif(Packet_FOUND)
message(STATUS "checking for Npcap's version.h")
check_symbol_exists(WINPCAP_PRODUCT_NAME "${CMAKE_SOURCE_DIR}/../../version.h" HAVE_VERSION_H)
if(HAVE_VERSION_H)
message(STATUS "HAVE version.h")
else(HAVE_VERSION_H)
message(STATUS "MISSING version.h")
endif(HAVE_VERSION_H)
endif(WIN32)
if(MSVC)
add_definitions(-D__STDC__)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif(MSVC)
if(USE_STATIC_RT)
message(STATUS "Use STATIC runtime")
if(MSVC)
foreach(RT_FLAG
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
string(REGEX REPLACE "/MD" "/MT" ${RT_FLAG} "${${RT_FLAG}}")
endforeach(RT_FLAG)
elseif(MINGW)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc")
endif()
else (USE_STATIC_RT)
message(STATUS "Use DYNAMIC runtime")
endif(USE_STATIC_RT)
#
# CMake's definition of "cross-compiling" appears to be "compiling
# for an *operating system* other than the one on which the build
# is being done*.
#
# This is an inadequate definition, as people build for the same
# operating system but a different instruction set, e.g. building
# on an IA-32 or x86-64 Linux box for an Arm embedded Linux box,
# or building Arm code on an IA-32 or x86-64 Windows box.
#
# At least for the Windows case, people may do those builds by
# setting the target with th -A flag to CMake; that causes
# CMAKE_GENERATOR_PLATFORM to be set to the target. If
# CMAKE_GENERATOR_PLATFORM is set, compare it with
# CMAKE_HOST_SYSTEM_PROCESSOR and, if they're not equal, set
# CMAKE_CROSSCOMPILING to TRUE.
#
if (CMAKE_GENERATOR_PLATFORM AND
NOT CMAKE_GENERATOR_PLATFORM STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR)
set(CMAKE_CROSSCOMPILING TRUE)
endif()
###################################################################
# Detect available platform features
###################################################################
include(CheckIncludeFile)
include(CheckIncludeFiles)
include(CheckStructHasMember)
include(CheckTypeSize)
#
# Tests are a bit expensive with Visual Studio on Windows, so, on
# Windows, we skip tests for UN*X-only headers and functions.
#
#
# Header files.
#
check_include_file(unistd.h HAVE_UNISTD_H)
if(NOT HAVE_UNISTD_H)
add_definitions(-DYY_NO_UNISTD_H)
endif(NOT HAVE_UNISTD_H)
if(NOT WIN32)
check_include_file(sys/ioccom.h HAVE_SYS_IOCCOM_H)
check_include_file(sys/sockio.h HAVE_SYS_SOCKIO_H)
check_include_file(sys/select.h HAVE_SYS_SELECT_H)
check_include_file(netpacket/packet.h HAVE_NETPACKET_PACKET_H)
check_include_file(netinet/if_ether.h HAVE_NETINET_IF_ETHER_H)
endif(NOT WIN32)
#
# Functions.
#
# First, check for the __atomic_load_n() and __atomic_store_n()
# builtins.
#
# We can't use check_function_exists(), as it tries to declare
# the function, and attempting to declare a compiler builtin
# can produce an error.
#
# We don't use check_symbol_exists(), as it expects a header
# file to be specified to declare the function, but there isn't
# such a header file.
#
# So we use check_c_source_compiles().
#
check_c_source_compiles(
"int
main(void)
{
int i = 17;
return __atomic_load_n(&i, __ATOMIC_RELAXED);
}
"
HAVE___ATOMIC_LOAD_N)
check_c_source_compiles(
"int
main(void)
{
int i;
__atomic_store_n(&i, 17, __ATOMIC_RELAXED);
return 0;
}
"
HAVE___ATOMIC_STORE_N)
#
# Now check for various system functions.
#
check_function_exists(strerror_r HAVE_STRERROR_R)
if(HAVE_STRERROR_R)
#
# We have strerror_r; if we define _GNU_SOURCE, is it a
# POSIX-compliant strerror_r() or a GNU strerror_r()?
#
check_c_source_compiles(
"#define _GNU_SOURCE
#include <string.h>
/* Define it GNU-style; that will cause an error if it's not GNU-style */
extern char *strerror_r(int, char *, size_t);
int
main(void)
{
return 0;
}
"
HAVE_GNU_STRERROR_R)
if(NOT HAVE_GNU_STRERROR_R)
set(HAVE_POSIX_STRERROR_R YES)
endif(NOT HAVE_GNU_STRERROR_R)
else(HAVE_STRERROR_R)
#
# We don't have strerror_r; do we have _wcserror_s?
#
check_function_exists(_wcserror_s HAVE__WCSERROR_S)
endif(HAVE_STRERROR_R)
if (NOT CMAKE_CROSSCOMPILING)
#
# Require a proof of suitable snprintf(3), same as in Autoconf.
#
include(CheckCSourceRuns)
check_c_source_runs("
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
int main()
{
char buf[100];
uint64_t t = (uint64_t)1 << 32;
snprintf(buf, sizeof(buf), \"%zu\", sizeof(buf));
if (strncmp(buf, \"100\", sizeof(buf)))
return 1;
snprintf(buf, sizeof(buf), \"%zd\", -sizeof(buf));
if (strncmp(buf, \"-100\", sizeof(buf)))
return 2;
snprintf(buf, sizeof(buf), \"%\" PRId64, -t);
if (strncmp(buf, \"-4294967296\", sizeof(buf)))
return 3;
snprintf(buf, sizeof(buf), \"0o%\" PRIo64, t);
if (strncmp(buf, \"0o40000000000\", sizeof(buf)))
return 4;
snprintf(buf, sizeof(buf), \"0x%\" PRIx64, t);
if (strncmp(buf, \"0x100000000\", sizeof(buf)))
return 5;
snprintf(buf, sizeof(buf), \"%\" PRIu64, t);
if (strncmp(buf, \"4294967296\", sizeof(buf)))
return 6;
return 0;
}
"
SUITABLE_SNPRINTF
)
if(NOT SUITABLE_SNPRINTF)
message(FATAL_ERROR
"The snprintf(3) implementation in this libc is not suitable,
libpcap would not work correctly even if it managed to compile."
)
endif()
else()
message(STATUS "Skipped SUITABLE_SNPRINTF because cross-compiling.")
endif()
check_function_exists(strlcpy HAVE_STRLCPY)
check_function_exists(strlcat HAVE_STRLCAT)
check_function_exists(asprintf HAVE_ASPRINTF)
check_function_exists(vasprintf HAVE_VASPRINTF)
check_function_exists(strtok_r HAVE_STRTOK_R)
if(NOT WIN32)
check_function_exists(vsyslog HAVE_VSYSLOG)
endif()
#
# Look for various networking-related libraries that we may need.
#
# We need getaddrinfo() to translate host names in filters to IP
# addresses. We use getaddrinfo() because we want a portable
# thread-safe way of getting information for a host name or port;
# there exist _r versions of gethostbyname() and getservbyname() on
# some platforms, but not on all platforms.
#
# We may also need socket() and other socket functions to support:
#
# Local packet capture with capture mechanisms that use sockets.
#
# Local capture device enumeration if a socket call is needed to
# enumerate devices or get device attributes.
#
# Packet capture from services that put captured packets on the
# network, such as rpcap servers.
#
# We may also need getnameinfo() for packet capture from services
# that put packets on the network.
#
set(PCAP_LINK_LIBRARIES "")
set(LIBS "")
set(LIBS_STATIC "")
set(REQUIRES_PRIVATE "")
set(LIBS_PRIVATE "")
include(CheckLibraryExists)
if(WIN32)
#
# Windows.
#
# We need winsock2.h and ws2tcpip.h.
#
# On Windows, getaddrinfo() is in the ws2_32 library.
#
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES ws2_32)
check_symbol_exists(getaddrinfo "winsock2.h;ws2tcpip.h" LIBWS2_32_HAS_GETADDRINFO)
cmake_pop_check_state()
if(LIBWS2_32_HAS_GETADDRINFO)
set(PCAP_LINK_LIBRARIES ws2_32 ${PCAP_LINK_LIBRARIES})
else(LIBWS2_32_HAS_GETADDRINFO)
message(FATAL_ERROR "getaddrinfo is required, but wasn't found")
endif(LIBWS2_32_HAS_GETADDRINFO)
else(WIN32)
#
# UN*X.
#
# Most UN*Xes have getaddrinfo(), and the other routines we may
# need, in the default searched libraries (e.g., libc).
# Check there first.
#
# NOTE: if you hand check_library_exists as its last argument a
# variable that's been set, it skips the test, so we need different
# variables for different libraries.
#
check_function_exists(getaddrinfo STDLIBS_HAVE_GETADDRINFO)
if(NOT STDLIBS_HAVE_GETADDRINFO)
#
# Not found in the standard system libraries.
#
# In some versions of Solaris, we need to link with libsocket
# and libnsl, so check in libsocket and also link with liblnsl
# when doing this test.
#
# Linking with libsocket and libnsl will find all the routines
# we need.
#
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES nsl)
check_library_exists(socket getaddrinfo "" LIBSOCKET_HAS_GETADDRINFO)
cmake_pop_check_state()
if(LIBSOCKET_HAS_GETADDRINFO)
#
# OK, we found it in libsocket.
#
set(PCAP_LINK_LIBRARIES socket nsl ${PCAP_LINK_LIBRARIES})
set(LIBS "-lsocket -lnsl ${LIBS}")
set(LIBS_STATIC "-lsocket -lnsl ${LIBS_STATIC}")
set(LIBS_PRIVATE "-lsocket -lnsl ${LIBS_PRIVATE}")
else(LIBSOCKET_HAS_GETADDRINFO)
#
# Not found in libsocket; test for it in libnetwork, which
# is where it is in Haiku.
#
# Linking with libnetwork will find all the routines we
# need.
#
check_library_exists(network getaddrinfo "" LIBNETWORK_HAS_GETADDRINFO)
if(LIBNETWORK_HAS_GETADDRINFO)
#
# OK, we found it in libnetwork.
#
set(PCAP_LINK_LIBRARIES network ${PCAP_LINK_LIBRARIES})
set(LIBS "-lnetwork ${LIBS}")
set(LIBS_STATIC "-lnetwork ${LIBS_STATIC}")
set(LIBS_PRIVATE "-lnetwork ${LIBS_PRIVATE}")
else(LIBNETWORK_HAS_GETADDRINFO)
#
# We didn't find it.
#
message(FATAL_ERROR "getaddrinfo is required, but wasn't found")
endif(LIBNETWORK_HAS_GETADDRINFO)
endif(LIBSOCKET_HAS_GETADDRINFO)
#
# We require a version of recvmsg() that conforms to the Single
# UNIX Specification, so that we can check whether a datagram
# received with recvmsg() was truncated when received due to the
# buffer being too small.
#
# On most systems, the version of recvmsg() in the libraries
# found above conforms to the SUS.
#
# On at least some versions of Solaris, it does not conform to
# the SUS, and we need the version in libxnet, which does
# conform.
#
# Check whether libxnet exists and has a version of recvmsg();
# if it does, link with libxnet before we link with libsocket,
# to get that version.
#
# This test also links with libsocket and libnsl.
#
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES socket nsl)
check_library_exists(xnet recvmsg "" LIBXNET_HAS_RECVMSG)
cmake_pop_check_state()
if(LIBXNET_HAS_RECVMSG)
#
# libxnet has recvmsg(); link with it as well.
#
set(PCAP_LINK_LIBRARIES xnet ${PCAP_LINK_LIBRARIES})
set(LIBSC "-lxnet ${LIBS_LIBS}")
set(LIBS_STATIC "-lxnet ${LIBS_STATIC}")
set(LIBS_PRIVATE "-lxnet ${LIBS_PRIVATE}")
endif(LIBXNET_HAS_RECVMSG)
endif(NOT STDLIBS_HAVE_GETADDRINFO)
#
# DLPI needs putmsg under HP-UX, so test for -lstr while we're at it.
#
check_function_exists(putmsg STDLIBS_HAVE_PUTMSG)
if(NOT STDLIBS_HAVE_PUTMSG)
check_library_exists(str putmsg "" LIBSTR_HAS_PUTMSG)
if(LIBSTR_HAS_PUTMSG)
set(PCAP_LINK_LIBRARIES str ${PCAP_LINK_LIBRARIES})
set(LIBS "-lstr ${LIBS}")
set(LIBS_STATIC "-lstr ${LIBS_STATIC}")
set(LIBS_PRIVATE "-lstr ${LIBS_PRIVATE}")
endif(LIBSTR_HAS_PUTMSG)
endif(NOT STDLIBS_HAVE_PUTMSG)
# Haiku has getpass in libbsd
check_function_exists(getpass STDLIBS_HAVE_GETPASS)
if(NOT STDLIBS_HAVE_GETPASS)
check_library_exists(bsd getpass "" LIBBSD_HAS_GETPASS)
if(LIBBSD_HAS_GETPASS)
set(PCAP_LINK_LIBRARIES bsd ${PCAP_LINK_LIBRARIES})
endif(LIBBSD_HAS_GETPASS)
endif(NOT STDLIBS_HAVE_GETPASS)
endif(WIN32)
#
# Check for reentrant versions of getnetbyname_r(), as provided by
# Linux (glibc), Solaris, and AIX (with three different APIs!).
# If we don't find one, we just use getnetbyname(), which uses
# thread-specific data on many platforms, but doesn't use it on
# NetBSD or OpenBSD, and may not use it on older versions of other
# platforms.