ISIS(-lsp): Fix Dead Store (Dead assignement/Dead increment) Warning found by Clang
[metze/wireshark/wip.git] / CMakeLists.txt
1 # CMakeLists.txt
2 #
3 # Wireshark - Network traffic analyzer
4 # By Gerald Combs <gerald@wireshark.org>
5 # Copyright 1998 Gerald Combs
6 #
7 # SPDX-License-Identifier: GPL-2.0-or-later
8 #
9
10 if(WIN32)
11         # Needed for GREATER_EQUAL operator
12         cmake_minimum_required(VERSION 3.7)
13 else()
14         cmake_minimum_required(VERSION 3.5)
15 endif()
16 project(Wireshark C CXX)
17
18 # Updated by make-version.pl
19 set(GIT_REVISION 0)
20 set(PROJECT_MAJOR_VERSION 2)
21 set(PROJECT_MINOR_VERSION 9)
22 set(PROJECT_PATCH_VERSION 1)
23 set(PROJECT_BUILD_VERSION ${GIT_REVISION})
24 set(PROJECT_VERSION_EXTENSION "")
25 set(PROJECT_RELEASE_VERSION "${PROJECT_MAJOR_VERSION}.${PROJECT_MINOR_VERSION}")
26
27 if(DEFINED ENV{WIRESHARK_VERSION_EXTRA})
28         set(PROJECT_VERSION_EXTENSION "$ENV{WIRESHARK_VERSION_EXTRA}")
29 endif()
30
31 set(PROJECT_VERSION "${PROJECT_MAJOR_VERSION}.${PROJECT_MINOR_VERSION}.${PROJECT_PATCH_VERSION}${PROJECT_VERSION_EXTENSION}")
32
33 # packaging information
34 if(WIN32)
35         set(CPACK_PACKAGE_NAME Wireshark)
36 else()
37         set(CPACK_PACKAGE_NAME wireshark)
38 endif()
39
40 set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
41
42 message(STATUS "Generating build using CMake ${CMAKE_VERSION}")
43
44 #Where to find local cmake scripts
45 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
46
47 # If our target platform is enforced by our generator, set
48 # WIRESHARK_TARGET_PLATFORM accordingly. Otherwise use
49 # %WIRESHARK_TARGET_PLATFORM%.
50
51 if(WIN32)
52         find_package(PowerShell REQUIRED)
53
54         if(CMAKE_CL_64 OR CMAKE_GENERATOR MATCHES "Win64")
55                 set(WIRESHARK_TARGET_PLATFORM win64)
56         elseif(CMAKE_GENERATOR MATCHES "Visual Studio")
57                 set(WIRESHARK_TARGET_PLATFORM win32)
58         else()
59                 set(WIRESHARK_TARGET_PLATFORM $ENV{WIRESHARK_TARGET_PLATFORM})
60         endif()
61
62         if(WIRESHARK_TARGET_PLATFORM MATCHES "win64")
63                 set(WIRESHARK_TARGET_PROCESSOR_ARCHITECTURE amd64)
64         else()
65                 set(WIRESHARK_TARGET_PROCESSOR_ARCHITECTURE x86)
66         endif()
67
68         # Sanity check
69         if(DEFINED ENV{PLATFORM})
70                 string(TOLOWER $ENV{PLATFORM} _vs_platform)
71         else()
72                 set(_vs_platform "[undefined]") # x86
73         endif()
74         if(
75                 (_vs_platform STREQUAL "x64" AND NOT WIRESHARK_TARGET_PLATFORM STREQUAL "win64")
76                 OR
77                 (_vs_platform STREQUAL "[undefined]" AND NOT WIRESHARK_TARGET_PLATFORM STREQUAL "win32")
78         )
79                 message(FATAL_ERROR "The PLATFORM environment variable (${_vs_platform})"
80                         " doesn't match the generator platform (${WIRESHARK_TARGET_PLATFORM})")
81         endif()
82         message(STATUS "Building for ${WIRESHARK_TARGET_PLATFORM} using ${CMAKE_GENERATOR}")
83
84         # Determine where the 3rd party libraries will be
85         if( DEFINED ENV{WIRESHARK_LIB_DIR} )
86                 # The buildbots set WIRESHARK_LIB_DIR but not WIRESHARK_BASE_DIR.
87                 file( TO_CMAKE_PATH "$ENV{WIRESHARK_LIB_DIR}" _PROJECT_LIB_DIR )
88         elseif( DEFINED ENV{WIRESHARK_BASE_DIR} )
89                 file( TO_CMAKE_PATH "$ENV{WIRESHARK_BASE_DIR}" _WS_BASE_DIR )
90                 set( _PROJECT_LIB_DIR "${_WS_BASE_DIR}/wireshark-${WIRESHARK_TARGET_PLATFORM}-libs" )
91         else()
92                 # Don't know what to do
93                 message(FATAL_ERROR "Neither WIRESHARK_BASE_DIR or WIRESHARK_LIB_DIR are defined")
94         endif()
95
96         # Download third-party libraries
97         file (TO_NATIVE_PATH ${CMAKE_SOURCE_DIR}/tools/win-setup.ps1 _win_setup)
98         file (TO_NATIVE_PATH ${_PROJECT_LIB_DIR} _ws_lib_dir)
99
100         # Is it possible to have a one-time, non-cached option in CMake? If
101         # so, we could add a "-DFORCE_WIN_SETUP" which passes -Force to
102         # win-setup.ps1.
103         execute_process(
104                 COMMAND ${POWERSHELL_COMMAND} "\"${_win_setup}\"" -Destination "${_ws_lib_dir}" -Platform ${WIRESHARK_TARGET_PLATFORM}
105                 RESULT_VARIABLE _win_setup_failed
106         )
107         if(_win_setup_failed)
108                 message(FATAL_ERROR "Windows setup (win-setup.ps1) failed.")
109         endif()
110
111         # XXX Add a dependency on ${_ws_lib_dir}/current_tag.txt?
112
113         # Head off any attempts to use Cygwin's Python.
114         include(LocatePythonExecutable)
115
116         # Prepopulate some ConfigureChecks values. Compilation checks
117         # on Windows can be slow.
118         set(HAVE_FCNTL_H TRUE)
119         set(HAVE_FLOORL TRUE)
120         set(HAVE_LRINT TRUE)
121
122         # It looks like we call check_type_size somewhere, which checks
123         # for these.
124         set(HAVE_SYS_TYPES_H TRUE)
125         set(HAVE_STDINT_H TRUE)
126         set(HAVE_STDDEF_H TRUE)
127 endif(WIN32)
128
129 include(UseCustomIncludes)
130 ADD_CUSTOM_CMAKE_INCLUDE()
131
132 # This cannot be implemented via option(...)
133 if( NOT CMAKE_BUILD_TYPE )
134         set( CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
135                 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
136                 FORCE)
137 endif()
138 message(STATUS "Configuration types: ${CMAKE_CONFIGURATION_TYPES}")
139 string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
140 message(STATUS "CMAKE_C_FLAGS_${_build_type}: ${CMAKE_C_FLAGS_${_build_type}}")
141 message(STATUS "CMAKE_CXX_FLAGS_${_build_type}: ${CMAKE_CXX_FLAGS_${_build_type}}")
142
143 # Ensure that all executables and libraries end up in the same directory. Actual
144 # files might end up in a configuration subdirectory, e.g. run/Debug or
145 # run/Release. We try to set DATAFILE_DIR to actual location below.
146 if(NOT ARCHIVE_OUTPUT_PATH)
147         set(ARCHIVE_OUTPUT_PATH ${Wireshark_BINARY_DIR}/run CACHE INTERNAL
148                    "Single output directory for building all archives.")
149 endif()
150 if(NOT EXECUTABLE_OUTPUT_PATH)
151         set(EXECUTABLE_OUTPUT_PATH ${Wireshark_BINARY_DIR}/run CACHE INTERNAL
152                    "Single output directory for building all executables.")
153 endif()
154 if(NOT LIBRARY_OUTPUT_PATH)
155         set(LIBRARY_OUTPUT_PATH ${Wireshark_BINARY_DIR}/run CACHE INTERNAL
156                    "Single output directory for building all libraries.")
157 endif()
158
159 #
160 # The release mode (CMAKE_BUILD_TYPE=release) defines NDEBUG for
161 # the Unix Makefile generator.
162 #
163
164 # Defines CMAKE_INSTALL_BINDIR, CMAKE_INSTALL_DATADIR, etc ...
165 if(WIN32)
166         # Override some values on Windows, to match the existing
167         # convention of installing everything to a single root folder.
168         set(CMAKE_INSTALL_BINDIR ".")
169         set(CMAKE_INSTALL_LIBDIR ".")
170         set(CMAKE_INSTALL_INCLUDEDIR "include")
171         set(CMAKE_INSTALL_DATADIR ".")
172 else()
173         # By default INSTALL_DATADIR is set to INSTALL_DATAROOTDIR, set the
174         # proper value here.
175         set(CMAKE_INSTALL_DATADIR "share/${CPACK_PACKAGE_NAME}"
176                 CACHE PATH "Read-only architecture-independent data"
177         )
178 endif()
179 include(GNUInstallDirs)
180
181 # Make sure our executables can can load our libraries if we install into
182 # a non-default directory on Unix-like systems other than macOS.
183 # https://cmake.org/Wiki/CMake_RPATH_handling
184 if(NOT CMAKE_INSTALL_RPATH AND NOT (WIN32 OR APPLE))
185         # Some systems may have limited or non-existent support for $ORIGIN.
186         # https://www.lekensteyn.nl/rpath.html
187         set(_enable_rpath_origin 0)
188         if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|SunOS|FreeBSD)$")
189                 set(_enable_rpath_origin 1)
190         endif()
191         set(ENABLE_RPATH_ORIGIN ${_enable_rpath_origin} CACHE BOOL "Use \$ORIGIN with RPATH")
192         mark_as_advanced(ENABLE_RPATH_ORIGIN)
193
194         if(ENABLE_RPATH_ORIGIN)
195                 # Set an install RPATH relative to the location of the binary, to
196                 # provide a fully relocatable package.
197                 set(CMAKE_INSTALL_RPATH "\$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
198                 # Add the automatically determined parts of the RPATH
199                 # which point to directories outside the build tree to the install RPATH.
200                 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
201         else()
202                 # Add an absolute RPATH if it is not already included in the
203                 # default search list.
204                 list(FIND CMAKE_C_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_FULL_LIBDIR}" IS_SYSTEM_DIR)
205                 if(IS_SYSTEM_DIR STREQUAL "-1")
206                         set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
207                         set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
208                 endif()
209         endif()
210 endif()
211
212 if(WIN32)
213     # Linking with wsetargv.obj enables "wildcard expansion" of
214     # command-line arguments.
215     set(WILDCARD_OBJ wsetargv.obj)
216 endif(WIN32)
217
218 # Banner shown at top right of Qt welcome screen.
219 if(DEFINED ENV{WIRESHARK_VERSION_FLAVOR})
220         set(VERSION_FLAVOR "$ENV{WIRESHARK_VERSION_FLAVOR}")
221 else()
222         set(VERSION_FLAVOR "Development Build")
223 endif()
224
225 # These are required in .rc files and manifests
226 set(VERSION_MAJOR ${PROJECT_MAJOR_VERSION})
227 set(VERSION_MINOR ${PROJECT_MINOR_VERSION})
228 set(VERSION_MICRO ${PROJECT_PATCH_VERSION})
229 set(VERSION_BUILD ${PROJECT_BUILD_VERSION})
230 set(RC_VERSION ${PROJECT_MAJOR_VERSION},${PROJECT_MINOR_VERSION},${PROJECT_PATCH_VERSION},${PROJECT_BUILD_VERSION})
231
232 message(STATUS "V: ${PROJECT_VERSION}, MaV: ${PROJECT_MAJOR_VERSION}, MiV: ${PROJECT_MINOR_VERSION}, PL: ${PROJECT_PATCH_VERSION}, EV: ${PROJECT_VERSION_EXTENSION}.")
233
234 include(UseLemon)
235 include(UseMakePluginReg)
236 include(UseMakeTaps)
237 include(UseExecutableResources)
238 include(UseAsn2Wrs)
239
240 # The following snippet has been taken from
241 # https://github.com/USESystemEngineeringBV/cmake-eclipse-helper/wiki/HowToWorkaroundIndexer
242 # The eclipse indexer otherwise assumes __cplusplus=199711L which will lead to broken
243 # lookup tables for the epan libraries
244 # Check if CXX flags have been set to c++11 -> Setup Eclipse Indexer correctly!
245 # Also setup the project slightly different
246 if(CMAKE_EXTRA_GENERATOR MATCHES "Eclipse CDT4")
247         SET(CXX_ENABLED 0)
248         LIST(LENGTH CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS LIST_LEN)
249         if(LIST_LEN GREATER 0)
250                 SET(CXX_ENABLED 1)
251         endif()
252         SET(C_ENABLED 0)
253         LIST(LENGTH CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS LIST_LEN)
254         if(LIST_LEN GREATER 0)
255                 SET(C_ENABLED 1)
256         endif()
257         if(C_ENABLED EQUAL 1 AND CXX_ENABLED EQUAL 1)
258                 # Combined project (C and CXX). This will confuse the indexer. For that reason
259                 # we unsert set the __cplusplus variable for the indexer
260                 list(FIND CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS "__cplusplus" GEN_MACRO_INDEX)
261                 if(GEN_MACRO_INDEX GREATER -1)
262                         list(REMOVE_AT CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS ${GEN_MACRO_INDEX})
263                         list(REMOVE_AT CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS ${GEN_MACRO_INDEX})
264                 endif()
265                 SET(CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS ${CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS} CACHE INTERNAL "")
266         elseif((CXX_ENABLED EQUAL 1) AND (CMAKE_CXX_FLAGS MATCHES ".*-std=c\\+\\+11.*"))
267                 #add_definitions (-D__cplusplus=201103L)
268                 # CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS
269                 list(FIND CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS "199711L" GEN_MACRO_INDEX)
270                 if(GEN_MACRO_INDEX GREATER -1)
271                         list(REMOVE_AT CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS ${GEN_MACRO_INDEX})
272                         list(INSERT CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS ${GEN_MACRO_INDEX} "201103L")
273                         SET(CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS ${CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS} CACHE INTERNAL "")
274                 endif()
275         endif()
276 endif()
277
278 include_directories(
279         ${CMAKE_BINARY_DIR}
280         ${CMAKE_SOURCE_DIR}
281 )
282
283 include( CMakeOptions.txt )
284 if( DUMPCAP_INSTALL_OPTION STREQUAL "suid" )
285         set( DUMPCAP_SETUID "SETUID" )
286 else()
287         set( DUMPCAP_SETUID )
288 endif()
289 if( NOT CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
290         DUMPCAP_INSTALL_OPTION STREQUAL "capabilities" )
291         message( WARNING "Capabilities are only supported on Linux" )
292         set( DUMPCAP_INSTALL_OPTION )
293 endif()
294
295 if(APPLE AND EXISTS /usr/local/opt/qt5)
296         # Homebrew installs Qt5 (up to at least 5.11.0) in
297         # /usr/local/qt5. Ensure that it can be found by CMake
298         # since it is not in the default /usr/local prefix.
299         # Add it to PATHS so that it doesn't override the
300         # CMAKE_PREFIX_PATH environment variable.
301         # QT_FIND_PACKAGE_OPTIONS should be passed to find_package,
302         # e.g. find_package(Qt5Core ${QT_FIND_PACKAGE_OPTIONS})
303         set (QT_FIND_PACKAGE_OPTIONS PATHS /usr/local/opt/qt5)
304 endif()
305
306 set(OSS_FUZZ OFF CACHE BOOL "Whether building for oss-fuzz")
307 mark_as_advanced(OSS_FUZZ)
308 if(OSS_FUZZ)
309         if(ENABLE_FUZZER)
310                 # In oss-fuzz mode, the fuzzing engine can be afl or libFuzzer.
311                 message(FATAL_ERROR "Cannot force libFuzzer when using oss-fuzz")
312         endif()
313         # Must not depend on external dependencies so statically link all libs.
314         set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
315 endif()
316
317 #
318 # Linking can consume a lot of memory, especially when built with ASAN and
319 # static libraries (like oss-fuzz) or Debug mode. With Ninja, the number of
320 # parallel linker processes is constrained by job parallelism (-j), but this can
321 # be reduced further by setting "job pools" to a lower number.
322 #
323 if(CMAKE_MAKE_PROGRAM MATCHES "ninja" AND OSS_FUZZ)
324         # Assume oss-fuzz linker jobs do not require more than 1.2G per task
325         set(per_job_memory_mb 1200)
326         cmake_host_system_information(RESULT total_memory_mb QUERY TOTAL_PHYSICAL_MEMORY)
327         math(EXPR parallel_link_jobs "${total_memory_mb} / ${per_job_memory_mb}")
328         if(parallel_link_jobs LESS 1)
329                 set(parallel_link_jobs 1)
330         endif()
331         set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=${parallel_link_jobs})
332         set(CMAKE_JOB_POOL_LINK link_job_pool)
333         message(STATUS "Ninja job pool size: ${parallel_link_jobs}")
334 endif()
335
336 # Always enable position-independent code when compiling, even for
337 # executables, so you can build position-independent executables.
338 # -pie is added below for non-MSVC.
339 # Needed when either:
340 # - Qt5_POSITION_INDEPENDENT_CODE is set and CMake < 2.8.11
341 # - PIE is wanted (-pie) and you want to add -fPIC/-fPIE automatically.
342 # This option only has an effect on CMake >= 2.8.9
343 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
344
345 # Path to our generated executables (or wrapper scripts)
346 if(WIN32)
347         set(WS_PROGRAM_PATH ./run/$<CONFIG>)
348 else()
349         set(WS_PROGRAM_PATH ./run)
350 endif()
351
352 if( CMAKE_C_COMPILER_ID MATCHES "MSVC")
353         if ((MSVC_VERSION LESS "1900") OR (MSVC_VERSION GREATER_EQUAL "2000"))
354                 message(FATAL_ERROR "You are using an unsupported version of MSVC")
355         endif()
356
357         add_definitions(
358                 /DWIN32_LEAN_AND_MEAN
359                 /D_CRT_SECURE_NO_DEPRECATE
360                 # NOMINMAX keeps windows.h from defining "min" and "max" via windef.h.
361                 # This avoids conflicts with the C++ standard library.
362                 /DNOMINMAX
363                 # -DPSAPI_VERSION=1                 Programs that must run on earlier versions of Windows as well as Windows 7 and later
364                 #                                   versions should always call this function as GetProcessMemoryInfo. To ensure correct
365                 #                                   resolution of symbols, add Psapi.lib to the TARGETLIBS macro and compile the program
366                 #                                   with -DPSAPI_VERSION=1.To use run-time dynamic linking, load Psapi.dll.
367                 #                                   http://msdn.microsoft.com/en-us/library/windows/desktop/ms683219(v=vs.85).aspx
368                 # -D_ALLOW_KEYWORD_MACROS           For VS2012 onwards the, C++ STL does not permit macro redefinitions of keywords
369                 #                                   (see http://msdn.microsoft.com/en-us/library/bb531344(v=vs.110).aspx)
370                 #                                   This definition prevents the complaint about the redefinition of inline by WinPCap
371                 #                                   in pcap-stdinc.h when compiling C++ files, e.g. the Qt UI
372                 /DPSAPI_VERSION=1
373                 /D_ALLOW_KEYWORD_MACROS
374         )
375
376         if(NOT WIRESHARK_TARGET_PLATFORM STREQUAL "win64")
377                 add_definitions("/D_BIND_TO_CURRENT_CRT_VERSION=1")
378         endif()
379
380         set(LOCAL_CFLAGS
381                 /MP
382         )
383
384         set(WS_LINK_FLAGS "/LARGEADDRESSAWARE /MANIFEST:NO /INCREMENTAL:NO /RELEASE")
385
386         # To do: Add /external:... See https://blogs.msdn.microsoft.com/vcblog/2017/12/13/broken-warnings-theory/
387         #
388         # /Zo                               Enhanced debugging of optimised code
389         # /utf-8                            Set Source and Executable character sets to UTF-8
390         #                                   VS2015(MSVC14): On by default when /Zi or /Z7 used.
391         # /guard:cf                         Control Flow Guard (compile and link).
392         #                                   See https://msdn.microsoft.com/en-us/library/windows/desktop/mt637065.aspx
393         #                                   Note: This requires CMake 3.9.0 or newer.
394         #                                   https://gitlab.kitware.com/cmake/cmake/commit/f973d49ab9d4c59b93f6dac812a94bb130200836
395         # /Qspectre                         Speculative execution attack mitigation
396         #                                   See https://blogs.msdn.microsoft.com/vcblog/2018/01/15/spectre-mitigations-in-msvc/
397         list(APPEND LOCAL_CFLAGS /Zo /utf-8 /guard:cf)
398         set(WS_LINK_FLAGS "${WS_LINK_FLAGS} /guard:cf")
399         # /Qspectre is not available for VS2015 or older VS2017. Test for its availability.
400         set(WIRESHARK_COMMON_FLAGS /Qspectre)
401
402         if(ENABLE_CODE_ANALYSIS)
403                 list(APPEND LOCAL_CFLAGS /analyze:WX-)
404         endif()
405
406         # Additional compiler warnings to be treated as "Level 3"
407         # when compiling Wireshark sources. (Selected from "level 4" warnings).
408         ## 4295: array is too small to include a terminating null character
409         ## 4189: local variable is initialized but not referenced
410         # Disable warnings about about use of flexible array members:
411         ## 4200: nonstandard extension used : zero-sized array in struct/union
412         list(APPEND LOCAL_CFLAGS /w34295 /w34189 /wd4200)
413
414         # We've matched these to specific compiler versions using the
415         # checks above. There's no need to pass them to check_c_compiler_flag
416         # or check_cxx_compiler_flag, which can be slow.
417         string(REPLACE ";" " " _flags "${LOCAL_CFLAGS}")
418         set(CMAKE_C_FLAGS "${_flags} ${CMAKE_C_FLAGS}")
419         set(CMAKE_CXX_FLAGS "${_flags} ${CMAKE_CXX_FLAGS}")
420
421 else() # ! MSVC
422         if(APPLE)
423                 set(MIN_MACOS_VERSION 10.6) # Arbitrary.
424                 if(CMAKE_OSX_DEPLOYMENT_TARGET)
425                         if(CMAKE_OSX_DEPLOYMENT_TARGET VERSION_LESS MIN_MACOS_VERSION)
426                                 message(FATAL_ERROR "We don't support building for macOS < ${MIN_MACOS_VERSION}")
427                         endif()
428                         set(MIN_MACOS_VERSION ${CMAKE_OSX_DEPLOYMENT_TARGET})
429                         message(STATUS "Building for Mac OS X/OS X/macOS ${CMAKE_OSX_DEPLOYMENT_TARGET}")
430                 endif()
431         elseif(CMAKE_OSX_DEPLOYMENT_TARGET)
432                 message(FATAL_ERROR "CMAKE_OSX_DEPLOYMENT_TARGET only applies when building for macOS")
433         endif()
434
435         #
436         # Do whatever is necessary to enable as much C99 support as
437         # possible in the C compiler.  Newer versions of compilers
438         # might default to supporting C99, but older versions may
439         # require a special flag.
440         #
441         # We do not want strict C99 support, as we may also want to
442         # use compiler extensions.
443         #
444         # Prior to CMake 3.1, setting CMAKE_C_STANDARD will not have
445         # any effect, so, unless and until we require CMake 3.1 or
446         # later, we have to do it ourselves on pre-3.1 CMake, so we
447         # just do it ourselves on all versions of CMake.
448         #
449         # Note: with CMake 3.1 through 3.5, the only compilers for
450         # which CMake handles CMAKE_C_STANDARD are GCC and Clang.
451         # 3.6 adds support only for Intel C; 3.9 adds support for
452         # PGI C, Sun C, and IBM XL C, and 3.10 adds support for
453         # Cray C and IAR C, but no version of CMake has support for
454         # HP C.  Therefore, even if we use CMAKE_C_STANDARD with
455         # compilers for which CMake supports it, we may still have
456         # to do it ourselves on other compilers.
457         #
458         # In addition, CMake 3.5.2 seems to think that GCC versions
459         # less than 4.4 don't support -std=gnu99, which we need in
460         # order to get support for "for (int i = 0; i < n; i++) ;",
461         # which is another reason not to rely on CMake's CMAKE_C_STANDARD
462         # support.
463         #
464         # See the CMake documentation for the CMAKE_<LANG>_COMPILER_ID
465         # variables for a list of compiler IDs.
466         #
467         # We don't worry about MSVC; it doesn't have such a flag -
468         # either it doesn't support the C99 features we need at all,
469         # or it supports them regardless of the compiler flag.
470         #
471         # XXX - we add the flag for a given compiler to CMAKE_C_FLAGS,
472         # so we test whether it works and add it if we do.  We don't
473         # test whether it's necessary in order to get the C99 features
474         # that we use; if we ever have a user who tries to compile with
475         # a compiler that can't be made to support those features, we
476         # can add a test to make sure we actually *have* C99 support.
477         #
478         if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
479            CMAKE_C_COMPILER_ID MATCHES "Clang")
480                 #
481                 # We use -std=gnu99 rather than -std=c99 because, for
482                 # some older compilers such as GCC 4.4.7, -std=gnu99
483                 # is required to avoid errors about C99 constructs
484                 # such as "for (int i = 0; i < n; i++) ;".
485                 #
486                 set(CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}")
487         elseif(CMAKE_C_COMPILER_ID MATCHES "XL")
488                 #
489                 # We want support for extensions picked up for
490                 # GNU C compatibility, so we use -qlanglvl=extc99.
491                 #
492                 set(CMAKE_C_FLAGS "-qlanglvl=extc99 ${CMAKE_C_FLAGS}")
493         elseif(CMAKE_C_COMPILER_ID MATCHES "HP")
494                 #
495                 # We also need to add -Wp,-H200000 to handle some large
496                 # #defines we have; that flag is not necessary for the
497                 # C++ compiler unless the "legacy" C++ preprocessor is
498                 # being used (+legacy_cpp).  We don't want the legacy
499                 # preprocessor if it's not the default, so we just add
500                 # -Wp,-H200000 to the C flags.  (If there are older
501                 # versions of aC++ that only support the legacy
502                 # preprocessor, and require that we boost the table
503                 # size, we'd have to check whether -Wp,-H200000 is
504                 # supported by the C++ compiler and add it only if it is.)
505                 #
506                 set(CMAKE_C_FLAGS "-AC99 -Wp,-H200000 $WS_CFLAGS ${CMAKE_C_FLAGS}")
507         elseif(CMAKE_C_COMPILER_ID MATCHES "Sun")
508                 #
509                 # We also crank up the warning level.
510                 #
511                 set(CMAKE_C_FLAGS "-xc99 -v ${CMAKE_C_FLAGS}")
512         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
513                 set(CMAKE_C_FLAGS "-c99 ${CMAKE_C_FLAGS}")
514         endif()
515
516         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
517                 set(WIRESHARK_COMMON_FLAGS ${WIRESHARK_COMMON_FLAGS}
518                         # avoid "argument unused during compilation" warnings
519                         # (for example, when getting the -gsplit-dwarf option or
520                         # when combining -fwrapv with -fno-strict-overflow)
521                         -Qunused-arguments
522                 )
523         else()
524                 set(WIRESHARK_COMMON_FLAGS ${WIRESHARK_COMMON_FLAGS}
525                         -fexcess-precision=fast
526                 )
527         endif()
528
529         list(APPEND WIRESHARK_COMMON_FLAGS
530                 # The following are for C and C++
531                 # -O<X> and -g get set by the CMAKE_BUILD_TYPE
532                 -Wall
533                 -Wextra
534                 -Wendif-labels
535                 -Wpointer-arith
536                 -Wformat-security
537                 -fwrapv
538                 -fno-strict-overflow
539                 -Wvla
540                 -Waddress
541                 -Wattributes
542                 -Wdiv-by-zero
543                 -Wignored-qualifiers
544                 -Wpragmas
545                 -Wno-overlength-strings
546                 -Wno-long-long
547                 -Wheader-guard
548                 -Wcomma
549                 -Wshorten-64-to-32
550         )
551
552         #
553         # Code that may be worth looking into (coding practices)
554         #
555         if((NOT ENABLE_ASAN) AND (NOT ENABLE_TSAN) AND (NOT ENABLE_UBSAN) AND (NOT DISABLE_FRAME_LARGER_THAN_WARNING))
556                 #
557                 # Only do this if none of ASan, TSan, and UBSan are
558                 # enabled; the instrumentation they add increases
559                 # the stack usage - we only care about stack
560                 # usage in normal operation.
561                 #
562                 set(WIRESHARK_COMMON_FLAGS ${WIRESHARK_COMMON_FLAGS}
563                         -Wframe-larger-than=32768
564                 )
565         endif()
566
567         list(APPEND WIRESHARK_C_ONLY_FLAGS
568                 # The following are C only, not C++
569                 -Wc++-compat
570                 -Wunused-const-variable
571                 #
572                 # XXX - some versions of GCC, including the one in at
573                 # least some Xcode versions that come with Mac OS X
574                 # 10.5, complain about variables in function and
575                 # function pointer *declarations* shadowing other
576                 # variables.  The autoconf script checked for that; we
577                 # don't.
578                 -Wshadow
579                 -Wno-pointer-sign
580                 -Wold-style-definition
581                 -Wstrict-prototypes
582                 #
583                 # Some versions of GCC, such as 4.3.2 and 4.4.5,
584                 # generate logical-op warnings when strchr() is given a
585                 # constant string.  The autoconf script checked for that;
586                 # we don't.
587                 #
588                 -Wlogical-op
589                 -Wjump-misses-init
590                 #
591                 # Implicit function declarations are an error in C++ and most
592                 # likely a programming error in C. Turn -Wimplicit-int and
593                 # -Wimplicit-function-declaration into an error by default.
594                 #
595                 -Werror=implicit
596         )
597
598         #
599         # The universal zero initializer (in C: struct s x = { 0 };) for
600         # structures with multiple members is perfectly legal, but some older
601         # compilers warn about it. Silence those older compilers.
602         #
603         if((CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS "5.1") OR
604            (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0") OR
605            (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_C_COMPILER_VERSION VERSION_LESS "10.0"))
606                 if(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_VERSION VERSION_LESS "4.7")
607                         list(APPEND WIRESHARK_C_ONLY_FLAGS -Wno-missing-field-initializers)
608                 endif()
609                 # Silence warnings for initialization of nested structs like
610                 # struct { struct { int a, b; } s; int c; } v = { 0 };
611                 list(APPEND WIRESHARK_C_ONLY_FLAGS -Wno-missing-braces)
612         endif()
613
614
615         list(APPEND WIRESHARK_CXX_ONLY_FLAGS
616                 -Wextra-semi
617         )
618
619         #
620         # These are not enabled by default, because the warnings they
621         # produce are very hard or impossible to eliminate.
622         #
623         if(ENABLE_EXTRA_COMPILER_WARNINGS)   # This overrides -Werror
624                 list(APPEND WIRESHARK_COMMON_FLAGS
625                         # The following are for C and C++
626                         -Wpedantic
627                         #
628                         # As we use variadic macros, we don't want warnings
629                         # about them, even with -Wpedantic.
630                         #
631                         -Wno-variadic-macros
632                         #
633                         # Various code blocks this one.
634                         #
635                         -Woverflow
636                         -fstrict-overflow -Wstrict-overflow=4
637                         #
638                         # Due to various places where APIs we don't control
639                         # require us to cast away constness, we can probably
640                         # never enable this one with -Werror.
641                         #
642                         -Wcast-qual
643                         #
644                         # Some generated ASN.1 dissectors block this one;
645                         # multiple function declarations for the same
646                         # function are being generated.
647                         #
648                         -Wredundant-decls
649                         #
650                         # Some loops are safe, but it's hard to convince the
651                         # compiler of that.
652                         #
653                         -Wunsafe-loop-optimizations
654                         #
655                         # All the registration functions block these for now.
656                         #
657                         -Wmissing-prototypes
658                         -Wmissing-declarations
659                         #
660                         # A bunch of "that might not work on SPARC" code blocks
661                         # this one for now; some of it is code that *will* work
662                         # on SPARC, such as casts of "struct sockaddr *" to
663                         # "struct sockaddr_in *", which are required by some
664                         # APIs such as getifaddrs().
665                         #
666                         -Wcast-align
667                         #
668                         # Works only with Clang
669                         #
670                         -Wunreachable-code
671                         #
672                         # Works only with Clang but generates a lot of warnings
673                         # (about glib library not using Doxygen)
674                         #
675                         -Wdocumentation
676                         #
677                         # Works only with GCC 7
678                         #
679                         -Wduplicated-branches
680                         #
681                         # No longer supported by El Capitan clang on C++
682                         # XXX - is this one of those where CMake's check
683                         # doesn't fail, so it won't reject this?
684                         #
685                         -fno-delete-null-pointer-checks
686                 )
687
688                 list(APPEND WIRESHARK_C_ONLY_FLAGS
689                         # The following are C only, not C++
690                         #
691                         # Due to various places where APIs we don't control
692                         # require us to cast away constness, we can probably
693                         # never enable this one with -Werror.
694                         #
695                         -Wbad-function-cast
696                 )
697
698                 list(APPEND WIRESHARK_CXX_ONLY_FLAGS
699                 )
700         endif()
701
702         add_definitions(
703                 -DG_DISABLE_DEPRECATED
704                 -DG_DISABLE_SINGLE_INCLUDES
705         )
706
707         set(WIRESHARK_LD_FLAGS
708                 # See also CheckCLinkerFlag.cmake
709                 -Wl,--as-needed
710                 # -flto
711                 # -fwhopr
712                 # -fwhole-program
713         )
714 endif() # ! MSVC
715
716 # Counterhack to work around some cache magic in CHECK_C_SOURCE_COMPILES
717 include(CheckCCompilerFlag)
718 include(CheckCXXCompilerFlag)
719
720 if(ENABLE_STATIC)
721         set(BUILD_SHARED_LIBS 0)
722 else()
723         set(BUILD_SHARED_LIBS 1)
724 endif()
725
726 function(test_compiler_flag _lang _this_flag _valid_flags_var)
727         string(MAKE_C_IDENTIFIER "${_lang}${_this_flag}_VALID" _flag_var)
728         set(_test_flags "${${_valid_flags_var}} ${_this_flag}")
729         if(_lang STREQUAL "C")
730                 check_c_compiler_flag("${_test_flags}" ${_flag_var})
731         elseif(_lang STREQUAL "CXX")
732                 check_cxx_compiler_flag("${_test_flags}" ${_flag_var})
733         else()
734                 message(FATAL_ERROR "Language must be C or CXX")
735         endif()
736         if (${_flag_var})
737                 set(${_valid_flags_var} "${_test_flags}" PARENT_SCOPE)
738         endif()
739 endfunction()
740
741 foreach(THIS_FLAG ${WIRESHARK_COMMON_FLAGS} ${WIRESHARK_C_ONLY_FLAGS})
742         test_compiler_flag(C ${THIS_FLAG} ADDED_CMAKE_C_FLAGS)
743 endforeach()
744 set(CMAKE_C_FLAGS "${ADDED_CMAKE_C_FLAGS} ${CMAKE_C_FLAGS}")
745
746 foreach(THIS_FLAG ${WIRESHARK_COMMON_FLAGS} ${WIRESHARK_CXX_ONLY_FLAGS})
747         test_compiler_flag(CXX ${THIS_FLAG} ADDED_CMAKE_CXX_FLAGS)
748 endforeach()
749 set(CMAKE_CXX_FLAGS "${ADDED_CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
750
751 include(CMakePushCheckState)
752
753 if(ENABLE_ASAN)
754         cmake_push_check_state()
755         set(CMAKE_REQUIRED_LIBRARIES "-fsanitize=address")
756         check_c_compiler_flag(-fsanitize=address C__fsanitize_address_VALID)
757         check_cxx_compiler_flag(-fsanitize=address CXX__fsanitize_address_VALID)
758         cmake_pop_check_state()
759         if(NOT C__fsanitize_address_VALID OR NOT CXX__fsanitize_address_VALID)
760                 message(FATAL_ERROR "ENABLE_ASAN was requested, but not supported!")
761         endif()
762         set(CMAKE_C_FLAGS "-fsanitize=address ${CMAKE_C_FLAGS}")
763         set(CMAKE_CXX_FLAGS "-fsanitize=address ${CMAKE_CXX_FLAGS}")
764         # Disable ASAN for build-time tools, e.g. lemon
765         check_c_compiler_flag(-fno-sanitize=all C__fno_sanitize_all_VALID)
766         if(C__fno_sanitize_all_VALID)
767                 set(NO_SANITIZE_CFLAGS "-fno-sanitize=all")
768                 set(NO_SANITIZE_LDFLAGS "-fno-sanitize=all")
769         endif()
770 endif()
771
772 if(ENABLE_TSAN)
773         # Available since Clang >= 3.2 and GCC >= 4.8
774         cmake_push_check_state()
775         set(CMAKE_REQUIRED_LIBRARIES "-fsanitize=thread")
776         check_c_compiler_flag(-fsanitize=thread C__fsanitize_thread_VALID)
777         check_cxx_compiler_flag(-fsanitize=thread CXX__fsanitize_thread_VALID)
778         cmake_pop_check_state()
779         if(NOT C__fsanitize_thread_VALID OR NOT CXX__fsanitize_thread_VALID)
780                 message(FATAL_ERROR "ENABLE_TSAN was requested, but not supported!")
781         endif()
782         set(CMAKE_C_FLAGS "-fsanitize=thread ${CMAKE_C_FLAGS}")
783         set(CMAKE_CXX_FLAGS "-fsanitize=thread ${CMAKE_CXX_FLAGS}")
784         set(WS_LINK_FLAGS "-fsanitize=thread ${WS_LINK_FLAGS}")
785 endif()
786
787 if(ENABLE_UBSAN)
788         # Available since Clang >= 3.3 and GCC >= 4.9
789         cmake_push_check_state()
790         set(CMAKE_REQUIRED_LIBRARIES "-fsanitize=undefined")
791         check_c_compiler_flag(-fsanitize=undefined C__fsanitize_undefined_VALID)
792         check_cxx_compiler_flag(-fsanitize=undefined CXX__fsanitize_undefined_VALID)
793         cmake_pop_check_state()
794         if(NOT C__fsanitize_undefined_VALID OR NOT CXX__fsanitize_undefined_VALID)
795                 message(FATAL_ERROR "ENABLE_UBSAN was requested, but not supported!")
796         endif()
797         set(CMAKE_C_FLAGS "-fsanitize=undefined ${CMAKE_C_FLAGS}")
798         set(CMAKE_CXX_FLAGS "-fsanitize=undefined ${CMAKE_CXX_FLAGS}")
799 endif()
800
801 if(ENABLE_FUZZER)
802         # Available since Clang >= 6
803         # Will enable coverage flags which can be used by the fuzzshark target.
804         cmake_push_check_state()
805         set(CMAKE_REQUIRED_LIBRARIES "-fsanitize=fuzzer-no-link")
806         check_c_compiler_flag(-fsanitize=fuzzer C__fsanitize_fuzzer_no_link_VALID)
807         check_cxx_compiler_flag(-fsanitize=fuzzer CXX__fsanitize_fuzzer_no_link_VALID)
808         cmake_pop_check_state()
809         if(NOT C__fsanitize_fuzzer_no_link_VALID OR NOT CXX__fsanitize_fuzzer_no_link_VALID)
810                 message(FATAL_ERROR "ENABLE_FUZZER was requested, but not supported!")
811         endif()
812         set(CMAKE_C_FLAGS "-fsanitize=fuzzer-no-link ${CMAKE_C_FLAGS}")
813         set(CMAKE_CXX_FLAGS "-fsanitize=fuzzer-no-link ${CMAKE_CXX_FLAGS}")
814 endif()
815
816 set(WERROR_COMMON_FLAGS "")
817 if(NOT DISABLE_WERROR AND NOT ENABLE_EXTRA_COMPILER_WARNINGS)
818         if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
819                 set(WERROR_COMMON_FLAGS "/WX")
820         else()
821                 #
822                 # If a warning has been enabled by -Wall or -W,
823                 # and have specified -Werror, there appears to be
824                 # no way, in Apple's llvm-gcc, to prevent that
825                 # particular warning from giving an error - not
826                 # with a pragma, not with -Wno-{warning}, and not
827                 # with -Wno-error={warning}.
828                 #
829                 # Therefore, with that compiler, we just disable
830                 # -Werror.
831                 #
832                 if ((NOT APPLE) OR CMAKE_C_COMPILER_ID MATCHES "Clang")
833                         check_c_compiler_flag(-Werror WERROR)
834                         if (WERROR)
835                                 set(WERROR_COMMON_FLAGS "-Werror")
836                         endif()
837                 endif()
838         endif()
839 endif()
840
841 #
842 # Try to have the compiler default to hiding symbols, so that only
843 # symbols explicitly exported with WS_DLL_PUBLIC will be visible
844 # outside (shared) libraries; that way, more UN*X builds will catch
845 # failures to export symbols, rather than having that fail only on
846 # Windows.
847 #
848 # We don't need that with MSVC, as that's the default.
849 #
850 if( NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
851         #
852         # Try the GCC-and-compatible -fvisibility-hidden first.
853         #
854         check_c_compiler_flag(-fvisibility=hidden FVHIDDEN)
855         if(FVHIDDEN)
856                 set(CMAKE_C_FLAGS "-fvisibility=hidden ${CMAKE_C_FLAGS}")
857         else()
858                 #
859                 # OK, try the Sun^WOracle C -xldscope=hidden
860                 #
861                 check_c_compiler_flag(-xldscope=hidden XLDSCOPEHIDDEN)
862                 if(XLDSCOPEHIDDEN)
863                         set(CMAKE_C_FLAGS "-xldscope=hidden ${CMAKE_C_FLAGS}")
864                 else()
865                         #
866                         # Anything else?
867                         # If there is anything else, we might want to
868                         # make a list of options to try, and try them
869                         # in a loop.
870                         #
871                         message(WARNING "Hiding shared library symbols is not supported by the compiler."
872                                 " All shared library symbols will be exported.")
873                 endif()
874         endif()
875 endif()
876
877 include(CheckCLinkerFlag)
878
879 if(NOT CMAKE_C_COMPILER_ID MATCHES "MSVC" AND NOT OSS_FUZZ)
880         #
881         # The -pie linker option produces a position-independent executable.
882         # Some Linux distributions have this enabled by default in the compiler,
883         # so setting it here will be superfluous though.
884         #
885         # Note that linking with static libraries that are not position
886         # independent may fail, the user can set CMAKE_EXE_LINKER_FLAGS=-no-pie
887         # as a workaround.
888         #
889         check_c_linker_flag(-pie LINK_pie_VALID)
890         if(LINK_pie_VALID)
891                 set(CMAKE_EXE_LINKER_FLAGS "-pie ${CMAKE_EXE_LINKER_FLAGS}")
892         endif()
893 endif()
894
895 foreach(THIS_FLAG ${WIRESHARK_LD_FLAGS})
896         string(MAKE_C_IDENTIFIER "LINK${THIS_FLAG}_VALID" _flag_var)
897         check_c_linker_flag(${THIS_FLAG} ${_flag_var})
898         if (${_flag_var})
899                 set(WS_LINK_FLAGS "${WS_LINK_FLAGS} ${THIS_FLAG}")
900         endif()
901 endforeach()
902 message(STATUS "Linker flags: ${WS_LINK_FLAGS}")
903
904 if(APPLE AND EXISTS /usr/local/opt/gettext)
905         # GLib on macOS requires libintl. Homebrew installs gettext (and
906         # libintl) in /usr/local/opt/gettext
907         include_directories(/usr/local/opt/gettext/include)
908         link_directories(/usr/local/opt/gettext/lib)
909 endif()
910
911 # The packagelist is doing some magic: If we add XXX to the packagelist, we
912 # - may optionally set XXX_OPTIONS to pass to the find_package command
913 # - will call FindXXX.cmake or find_package
914 # - return found libraries in XXX_LIBRARIES
915 # - return found include in XXX_INCLUDE_DIRS
916 # - set HAVE_XXX
917
918 # The minimum package list
919 set(PACKAGELIST Git GLIB2 GMODULE2 GTHREAD2 GCRYPT LEX YACC Perl PythonInterp)
920 set(LEX_OPTIONS REQUIRED)
921 set(GLIB2_OPTIONS REQUIRED)
922 set(GLIB2_FIND_OPTIONS REQUIRED)
923 set(GLIB2_MIN_VERSION 2.32.0)
924 set(GTHREAD2_OPTIONS REQUIRED)
925 set(GCRYPT_OPTIONS "1.4.2" REQUIRED)
926 set(Perl_OPTIONS REQUIRED)
927 set(PythonInterp_OPTIONS "3.4" REQUIRED)
928 set(YACC_OPTIONS REQUIRED)
929
930 if (NOT WIN32)
931         set(PACKAGELIST ${PACKAGELIST} Gettext M)
932         set(M_OPTIONS REQUIRED)
933 endif()
934
935 set(PACKAGELIST ${PACKAGELIST} LIBSSH)
936 set(LIBSSH_OPTIONS "0.6")
937
938 if(ENABLE_PCAP)
939         set(PACKAGELIST ${PACKAGELIST} PCAP)
940 endif()
941
942 if(ENABLE_AIRPCAP)
943         set(PACKAGELIST ${PACKAGELIST} AIRPCAP)
944 endif()
945
946 if(BUILD_sdjournal)
947         set(PACKAGELIST ${PACKAGELIST} Systemd)
948 endif()
949
950 # Build the Qt GUI?
951 if(BUILD_wireshark)
952         # Untested, may not work if CMAKE_PREFIX_PATH gets overwritten
953         # somewhere. The if WIN32 in this place is annoying as well.
954         if( WIN32 )
955                 set( QT5_BASE_PATH "$ENV{QT5_BASE_DIR}" )
956                 set( CMAKE_PREFIX_PATH "${QT5_BASE_PATH}" )
957         endif()
958         list (INSERT QT_FIND_PACKAGE_OPTIONS 0 REQUIRED)
959         set(PACKAGELIST ${PACKAGELIST}
960                 Qt5Core
961                 Qt5LinguistTools
962                 Qt5Multimedia
963                 Qt5PrintSupport
964                 Qt5Svg
965                 Qt5Widgets
966         )
967         set(Qt5Core_OPTIONS ${QT_FIND_PACKAGE_OPTIONS})
968         set(Qt5LinguistTools_OPTIONS ${QT_FIND_PACKAGE_OPTIONS})
969         set(Qt5Multimedia_OPTIONS ${QT_FIND_PACKAGE_OPTIONS})
970         set(Qt5PrintSupport_OPTIONS ${QT_FIND_PACKAGE_OPTIONS})
971         set(Qt5Svg_OPTIONS ${QT_FIND_PACKAGE_OPTIONS})
972         set(Qt5Widgets_OPTIONS ${QT_FIND_PACKAGE_OPTIONS})
973         if (APPLE)
974                 set(PACKAGELIST ${PACKAGELIST} Qt5MacExtras)
975                 set(Qt5MacExtras_OPTIONS ${QT_FIND_PACKAGE_OPTIONS})
976         endif()
977         if( WIN32 )
978                 set(PACKAGELIST ${PACKAGELIST} Qt5WinExtras)
979                 set(Qt5WinExtras_OPTIONS ${QT_FIND_PACKAGE_OPTIONS})
980         endif()
981 endif()
982
983 # MaxMind DB address resolution
984 if(BUILD_mmdbresolve)
985         set(PACKAGELIST ${PACKAGELIST} MaxMindDB)
986 endif()
987
988 # SMI SNMP
989 if(ENABLE_SMI)
990         set(PACKAGELIST ${PACKAGELIST} SMI)
991 endif()
992
993 # Support for TLS decryption using RSA private keys.
994 if(ENABLE_GNUTLS)
995         set(PACKAGELIST ${PACKAGELIST} GNUTLS)
996         # Minimum version needed.
997         # 3.0.2 due to GNUTLS_PRIVKEY_IMPORT_COPY
998         # 3.2.0 to remain GPLv2-compatible.
999         set(GNUTLS_OPTIONS "3.2.0")
1000 endif()
1001
1002 # Kerberos
1003 if(ENABLE_KERBEROS)
1004         set(PACKAGELIST ${PACKAGELIST} KERBEROS)
1005 endif()
1006
1007 # C Asynchronous resolver
1008 if(ENABLE_CARES)
1009         set(PACKAGELIST ${PACKAGELIST} CARES)
1010         # Minimum version needed.
1011         set(CARES_OPTIONS "1.5.0")
1012 endif()
1013
1014 # Zlib compression
1015 if(ENABLE_ZLIB)
1016         if (WIN32)
1017                 # On Windows we build our own version of zlib, so add the paths
1018                 set(ZLIB_SRC_DIR "${_PROJECT_LIB_DIR}/zlib-1.2.11-2-ws")
1019                 set(SKIP_INSTALL_STATIC True)
1020                 set(SKIP_INSTALL_HEADERS True)
1021                 set(SKIP_INSTALL_FILES True)
1022                 set(ZLIB_EXPORTS "WiresharkTargets")
1023                 add_subdirectory("${ZLIB_SRC_DIR}" "${CMAKE_BINARY_DIR}/zlib")
1024                 unset(SKIP_INSTALL_STATIC)
1025                 unset(SKIP_INSTALL_HEADERS)
1026                 unset(SKIP_INSTALL_FILES)
1027                 set(ZLIB_INCLUDE_DIR  "${ZLIB_SRC_DIR}" "${CMAKE_BINARY_DIR}/zlib")
1028                 set(ZLIB_LIBRARY zlib)
1029                 set(ZLIB_DLL "zlib1.dll")
1030                 set_target_properties(zlib PROPERTIES FOLDER "Libs/zlib")
1031                 # Annoyingly zlib also builds some other stuff we aren't interested in
1032                 set_target_properties(zlibstatic PROPERTIES
1033                         FOLDER "Libs/zlib"
1034                         EXCLUDE_FROM_ALL True
1035                         EXCLUDE_FROM_DEFAULT_BUILD True
1036                 )
1037         endif()
1038         set(PACKAGELIST ${PACKAGELIST} ZLIB)
1039 endif()
1040
1041 # LZ4 compression
1042 if(ENABLE_LZ4)
1043         set(PACKAGELIST ${PACKAGELIST} LZ4)
1044 endif()
1045
1046 # Snappy compression
1047 if(ENABLE_SNAPPY)
1048         set(PACKAGELIST ${PACKAGELIST} SNAPPY)
1049 endif()
1050
1051 # Enhanced HTTP/2 dissection
1052 if(ENABLE_NGHTTP2)
1053         set(PACKAGELIST ${PACKAGELIST} NGHTTP2)
1054 endif()
1055
1056 # Embedded Lua interpreter
1057 if(ENABLE_LUA)
1058         set(PACKAGELIST ${PACKAGELIST} LUA)
1059         set(LUA_OPTIONS "5.1")
1060 endif()
1061
1062 if(ENABLE_NETLINK)
1063         set(PACKAGELIST ${PACKAGELIST} NL)
1064 endif()
1065
1066 if(ENABLE_SBC)
1067         set(PACKAGELIST ${PACKAGELIST} SBC)
1068 endif()
1069
1070 if(ENABLE_SPANDSP)
1071         set(PACKAGELIST ${PACKAGELIST} SPANDSP)
1072 endif()
1073
1074 if(ENABLE_BCG729)
1075         set(PACKAGELIST ${PACKAGELIST} BCG729)
1076 endif()
1077
1078 if(ENABLE_LIBXML2)
1079         set(PACKAGELIST ${PACKAGELIST} LibXml2)
1080 endif()
1081
1082 # Capabilities
1083 if(ENABLE_CAP)
1084         set(PACKAGELIST ${PACKAGELIST} CAP SETCAP)
1085 endif()
1086
1087 # Windows version updates
1088 if(ENABLE_WINSPARKLE)
1089         set(PACKAGELIST ${PACKAGELIST} WINSPARKLE)
1090 endif()
1091
1092 set(PACKAGELIST ${PACKAGELIST} POD)
1093
1094 set(PACKAGELIST ${PACKAGELIST} DOXYGEN)
1095
1096 set(PROGLIST)
1097
1098 # Sort the package list
1099 list(SORT PACKAGELIST)
1100 string(REPLACE ";" " " _package_list "${PACKAGELIST}")
1101 message(STATUS "Package List: ${_package_list}")
1102 # Let's loop the package list
1103 foreach(PACKAGE ${PACKAGELIST})
1104         # Most packages export uppercase variables, but there are exceptions.
1105         if(PACKAGE MATCHES "^(Qt5)")
1106                 set(PACKAGE_VAR "${PACKAGE}")
1107         else()
1108                 string(TOUPPER "${PACKAGE}" PACKAGE_VAR)
1109         endif()
1110         if(${PACKAGE}_OPTIONS)
1111                 find_package(${PACKAGE} ${${PACKAGE}_OPTIONS})
1112         else()
1113                 find_package(${PACKAGE})
1114         endif()
1115         if (${PACKAGE_VAR}_FOUND)
1116                 message(STATUS "${PACKAGE_VAR} FOUND")
1117                 set(HAVE_LIB${PACKAGE_VAR} 1)
1118                 if (NOT DEFINED ${PACKAGE_VAR}_INCLUDE_DIRS AND ${PACKAGE_VAR}_INCLUDE_DIR)
1119                         set(${PACKAGE_VAR}_INCLUDE_DIRS ${${PACKAGE_VAR}_INCLUDE_DIR})
1120                 endif()
1121                 if (${PACKAGE_VAR}_INCLUDE_DIRS)
1122                         include_directories(SYSTEM ${${PACKAGE_VAR}_INCLUDE_DIRS})
1123                         message(STATUS "${PACKAGE} includes: ${${PACKAGE_VAR}_INCLUDE_DIRS}")
1124                 endif()
1125                 if (${PACKAGE_VAR}_LIBRARIES)
1126                         message(STATUS "${PACKAGE} libs: ${${PACKAGE_VAR}_LIBRARIES}")
1127                 endif()
1128                 if (${PACKAGE_VAR}_DEFINITIONS)
1129                         message(STATUS "${PACKAGE} definitions: ${${PACKAGE_VAR}_DEFINITIONS}")
1130                         string(REPLACE ";" " " _definitions "${${PACKAGE_VAR}_DEFINITIONS}")
1131                         set(CMAKE_C_FLAGS "${_definitions} ${CMAKE_C_FLAGS}")
1132                         set(CMAKE_CXX_FLAGS "${_definitions} ${CMAKE_CXX_FLAGS}")
1133                 endif()
1134                 if (${PACKAGE_VAR}_LINK_FLAGS)
1135                         string(REPLACE ";" " " _link_flags "${${PACKAGE_VAR}_LINK_FLAGS}")
1136                         set(WS_LINK_FLAGS "${_link_flags} ${WS_LINK_FLAGS}")
1137                         message(STATUS "${PACKAGE} other link flags: ${_link_flags}")
1138                 endif()
1139                 if (${PACKAGE_VAR}_EXECUTABLE)
1140                         message(STATUS "${PACKAGE} executable: ${${PACKAGE_VAR}_EXECUTABLE}")
1141                 endif()
1142         else()
1143                 #
1144                 # Not finding a package is only a fatal error if the
1145                 # package is required; if it's required, then its
1146                 # XXX_OPTIONS variable contains REQUIRED, and the above
1147                 # code will pass REQUIRED to find_package, and the
1148                 # configure will fail if the package isn't found.
1149                 #
1150                 # Do *NOT* report this as an error!
1151                 #
1152                 message(STATUS "${PACKAGE_VAR} NOT FOUND")
1153         endif()
1154 endforeach()
1155
1156 # dist target that prepares source dir
1157 # XXX Duplicated in the RPM section below.
1158 add_custom_target(dist
1159         COMMAND ./tools/git-export-release.sh -d "${CMAKE_BINARY_DIR}"
1160         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
1161 )
1162
1163 if(HAVE_LIBAIRPCAP)
1164         set(HAVE_AIRPCAP 1)
1165 endif()
1166 if(HAVE_LIBLUA)
1167         set(HAVE_LUA_H 1)
1168         set(HAVE_LUA 1)
1169 endif()
1170 if(GNUTLS_FOUND AND NOT GNUTLS_VERSION VERSION_LESS "3.4.0")
1171         # While all Linux and Windows builds have PKCS #11 support enabled,
1172         # macos-setup.sh explicitly disables it using --without-p11-kit.
1173         #
1174         # Require at least GnuTLS 3.4.0 such that public keys can be calculated
1175         # from PKCS #11 private keys.
1176         include(CheckSymbolExists)
1177         cmake_push_check_state()
1178         if(WIN32)
1179                 set(CMAKE_REQUIRED_DEFINITIONS -Dssize_t=int)
1180         endif()
1181         set(CMAKE_REQUIRED_INCLUDES ${GNUTLS_INCLUDE_DIRS})
1182         set(CMAKE_REQUIRED_LIBRARIES ${GNUTLS_LIBRARIES})
1183         check_symbol_exists(gnutls_pkcs11_obj_list_import_url4 gnutls/pkcs11.h HAVE_GNUTLS_PKCS11)
1184         cmake_pop_check_state()
1185 endif()
1186 if(HAVE_LIBKERBEROS)
1187         set(HAVE_KERBEROS 1)
1188 endif()
1189 if(MAXMINDDB_FOUND)
1190         set(HAVE_MAXMINDDB 1)
1191 endif()
1192 if(LIBSSH_FOUND)
1193         set(HAVE_LIBSSH 1)
1194 endif()
1195 if(NGHTTP2_FOUND)
1196         set(HAVE_NGHTTP2 1)
1197 endif()
1198 if(HAVE_LIBCARES)
1199         set(HAVE_C_ARES 1)
1200 endif()
1201 if(NOT HAVE_LIBCARES)
1202         message(WARNING "Not using c-ares.")
1203         message(WARNING "DNS name resolution for captures will be disabled.")
1204 endif()
1205 if(HAVE_LIBNL AND HAVE_AIRPCAP)
1206         message(ERROR "Airpcap and Libnl support are mutually exclusive")
1207 endif()
1208 if(HAVE_LIBSBC)
1209         set(HAVE_SBC 1)
1210 endif()
1211 if(SPANDSP_FOUND)
1212         set(HAVE_SPANDSP 1)
1213 endif()
1214 if(BCG729_FOUND)
1215         set(HAVE_BCG729 1)
1216 endif()
1217 if(LIBXML2_FOUND)
1218         set(HAVE_LIBXML2 1)
1219 else()
1220         # The (official) FindLibXml2.cmake file sets this cache variable to a
1221         # non-empty value, be sure to clear it when not found.
1222         set(LIBXML2_LIBRARIES "")
1223 endif()
1224
1225 if (HAVE_LIBWINSPARKLE)
1226         set(HAVE_SOFTWARE_UPDATE 1)
1227 endif()
1228
1229 if(HAVE_LIBZLIB)
1230         set(HAVE_ZLIB 1)
1231         # Always include the "true" zlib includes first. This works around a
1232         # bug in the Windows setup of GTK[23] which has a faulty zconf.h.
1233         include_directories(BEFORE ${ZLIB_INCLUDE_DIRS})
1234 endif()
1235 if(HAVE_LIBLZ4)
1236         set(HAVE_LZ4 1)
1237 endif()
1238 if(SNAPPY_FOUND)
1239         set(HAVE_SNAPPY 1)
1240 endif()
1241
1242 if (Qt5Widgets_FOUND)
1243         if (Qt5Widgets_VERSION VERSION_LESS 5.2)
1244                 message(FATAL_ERROR "Qt 5.2 or later is required.")
1245         endif()
1246         if (Qt5Widgets_VERSION VERSION_GREATER 5.6
1247             AND (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang"))
1248                 # Qt 5.7 and later require C++ 11.
1249                 set(CMAKE_CXX_STANDARD 11)
1250                 set(CMAKE_CXX_STANDARD_REQUIRED ON)
1251         endif()
1252         set (QT_FOUND ON)
1253         set (QT_LIBRARIES ${Qt5Widgets_LIBRARIES} ${Qt5PrintSupport_LIBRARIES})
1254         if(Qt5Multimedia_FOUND)
1255                 set (QT_LIBRARIES ${QT_LIBRARIES} ${Qt5Multimedia_LIBRARIES})
1256                 # That's the name autotools used
1257                 set(QT_MULTIMEDIA_LIB 1)
1258         endif()
1259         if(Qt5Svg_FOUND)
1260                 set (QT_LIBRARIES ${QT_LIBRARIES} ${Qt5Svg_LIBRARIES})
1261                 # That's the name autotools used
1262                 set(QT_SVG_LIB 1)
1263         endif()
1264         if(Qt5MacExtras_FOUND)
1265                 set (QT_LIBRARIES ${QT_LIBRARIES} ${Qt5MacExtras_LIBRARIES})
1266                 # That's the name autotools used
1267                 set(QT_MACEXTRAS_LIB 1)
1268         endif()
1269         if(Qt5WinExtras_FOUND)
1270                 set (QT_LIBRARIES ${QT_LIBRARIES} ${Qt5WinExtras_LIBRARIES})
1271                 # set(QT_WINEXTRAS_LIB 1) # Not needed?
1272         endif()
1273         if(NOT DEFINED MOC_OPTIONS)
1274                 # Squelch moc verbose "nothing to do" output
1275                 set(MOC_OPTIONS -nn)
1276         endif()
1277
1278         # CMake uses qmake to find Qt4. It relies on Qt's CMake modules
1279         # to find Qt5. This means that we can't assume that the qmake
1280         # in our PATH is the correct one. We can fetch qmake's location
1281         # from Qt5::qmake, which is is defined in Qt5CoreConfigExtras.cmake.
1282         get_target_property(QT_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
1283         get_filename_component(_qt_bin_path "${QT_QMAKE_EXECUTABLE}" DIRECTORY)
1284         set(QT_BIN_PATH "${_qt_bin_path}" CACHE INTERNAL
1285                 "Path to qmake, macdeployqt, windeployqt, and other Qt utilities."
1286         )
1287         # Use qmake to find windeployqt and macdeployqt. Ideally one of
1288         # the modules in ${QTDIR}/lib/cmake would do this for us.
1289         if(WIN32)
1290                 find_program(QT_WINDEPLOYQT_EXECUTABLE windeployqt
1291                         HINTS "${QT_BIN_PATH}"
1292                         DOC "Path to the windeployqt utility."
1293                 )
1294         elseif(APPLE)
1295                 find_program(QT_MACDEPLOYQT_EXECUTABLE macdeployqt
1296                         HINTS "${QT_BIN_PATH}"
1297                         DOC "Path to the macdeployqt utility."
1298                 )
1299                 # https://doc.qt.io/qt-5.11/supported-platforms-and-configurations.html
1300                 # CMake < 3.7 doesn't support VERSION_GREATER_EQUAL.
1301                 if(Qt5Widgets_VERSION VERSION_GREATER "5.11.999")
1302                         set(MIN_MACOS_VERSION 10.12)
1303                 elseif(Qt5Widgets_VERSION VERSION_GREATER "5.9.999")
1304                         set(MIN_MACOS_VERSION 10.11)
1305                 elseif(Qt5Widgets_VERSION VERSION_GREATER "5.8.999")
1306                         set(MIN_MACOS_VERSION 10.10)
1307                 elseif(Qt5Widgets_VERSION VERSION_GREATER "5.7.999")
1308                         set(MIN_MACOS_VERSION 10.9)
1309                 elseif(Qt5Widgets_VERSION VERSION_GREATER "5.4.999")
1310                         set(MIN_MACOS_VERSION 10.8)
1311                 endif()
1312                 if(CMAKE_OSX_DEPLOYMENT_TARGET AND CMAKE_OSX_DEPLOYMENT_TARGET VERSION_LESS MIN_MACOS_VERSION)
1313                         message(FATAL_ERROR "Qt version ${Qt5Widgets_VERSION} requires CMAKE_OSX_DEPLOYMENT_TARGET (${CMAKE_OSX_DEPLOYMENT_TARGET}) >= ${MIN_MACOS_VERSION}")
1314                 endif()
1315         endif()
1316
1317 endif()
1318
1319 if(ENABLE_CHECKHF_CONFLICT)
1320         set(ENABLE_CHECK_FILTER 1)
1321 endif()
1322
1323 #
1324 # Platform-specific additional libraries.
1325 #
1326 if(WIN32)
1327         set(WIN_PSAPI_LIBRARY    psapi.lib)
1328         set(WIN_WSOCK32_LIBRARY  wsock32.lib)
1329         set(WIN_COMCTL32_LIBRARY comctl32.lib )
1330         set(WIN_VERSION_LIBRARY  version.lib)
1331 endif()
1332
1333 if(APPLE)
1334         #
1335         # We assume that APPLE means macOS so that we have the macOS
1336         # frameworks.
1337         #
1338         set(HAVE_MACOS_FRAMEWORKS 1)
1339         FIND_LIBRARY (APPLE_APPLICATION_SERVICES_LIBRARY ApplicationServices)
1340         FIND_LIBRARY (APPLE_APPKIT_LIBRARY AppKit)
1341         FIND_LIBRARY (APPLE_CORE_FOUNDATION_LIBRARY CoreFoundation)
1342         FIND_LIBRARY (APPLE_SYSTEM_CONFIGURATION_LIBRARY SystemConfiguration)
1343 endif()
1344
1345 include(ConfigureChecks.cmake)
1346
1347 # Global properties
1348 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
1349
1350 if(ENABLE_CCACHE AND (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang"))
1351         # http://stackoverflow.com/a/24305849/82195
1352         find_program(CCACHE_EXECUTABLE ccache)
1353         if(CCACHE_EXECUTABLE)
1354                 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_EXECUTABLE}")
1355                 set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CCACHE_EXECUTABLE}")
1356         endif()
1357 endif()
1358
1359 # The top level checkAPIs target, add before subdirectory calls so it's avaiable to all
1360 add_custom_target(checkAPI)
1361 set_target_properties(checkAPI
1362         PROPERTIES
1363                 FOLDER "Auxiliary"
1364                 EXCLUDE_FROM_ALL True
1365                 EXCLUDE_FROM_DEFAULT_BUILD True
1366 )
1367
1368 include( UseCheckAPI )
1369
1370 # Target platform locations
1371 # UN*X in general, including macOS if not building an app bundle:
1372 # $DESTDIR/lib/wireshark/extcap
1373 # Windows: $DESTDIR/extcap
1374 # macOS app bundle: Wireshark.app/Contents/Resources/share/wireshark/extcap
1375 # If you change the nesting level be sure to check also the INSTALL_RPATH
1376 # target property.
1377 if (WIN32)
1378         set(EXTCAP_INSTALL_LIBDIR "extcap" CACHE INTERNAL "The extcap dir")
1379 else ()
1380         set(EXTCAP_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/${CPACK_PACKAGE_NAME}/extcap" CACHE INTERNAL "The extcap dir")
1381 endif()
1382
1383 # Directory where plugins and Lua dissectors can be found.
1384 if(WIN32)
1385         set(PLUGIN_INSTALL_LIBDIR "plugins" CACHE INTERNAL "The plugin dir")
1386 else()
1387         set(PLUGIN_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/${CPACK_PACKAGE_NAME}/plugins" CACHE INTERNAL "The plugin dir")
1388 endif()
1389 set(PLUGIN_INSTALL_VERSION_LIBDIR "${PLUGIN_INSTALL_LIBDIR}/${PROJECT_RELEASE_VERSION}")
1390 set(PLUGIN_VERSION_DIR "plugins/${PROJECT_RELEASE_VERSION}")
1391
1392 add_subdirectory( capchild )
1393 add_subdirectory( caputils )
1394 add_subdirectory( codecs )
1395 add_subdirectory( doc )
1396 add_subdirectory( docbook EXCLUDE_FROM_ALL )
1397 add_subdirectory( epan )
1398 add_subdirectory( extcap )
1399 add_subdirectory( randpkt_core )
1400 add_subdirectory( tools/lemon )
1401 add_subdirectory( ui )
1402 add_subdirectory( wiretap )
1403 add_subdirectory( writecap )
1404
1405 # Location of our data files. This should be set to a value that allows
1406 # running from the build directory on Windows, on macOS when building an
1407 # application bundle, and on UNIX in general if
1408 # WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set.
1409 if(ENABLE_APPLICATION_BUNDLE)
1410         if(CMAKE_CFG_INTDIR STREQUAL ".")
1411                 set(_datafile_dir "${CMAKE_BINARY_DIR}/run/Wireshark.app/Contents/Resources/share/wireshark")
1412         else()
1413                 # Xcode
1414                 set(_datafile_dir "${CMAKE_BINARY_DIR}/run/${CMAKE_CFG_INTDIR}/Wireshark.app/Contents/Resources/share/wireshark")
1415         endif()
1416 elseif(NOT CMAKE_CFG_INTDIR STREQUAL ".")
1417         # Visual Studio, Xcode, etc.
1418         set(_datafile_dir "${CMAKE_BINARY_DIR}/run/${CMAKE_CFG_INTDIR}")
1419 else()
1420         # Makefile, Ninja, etc.
1421         set(_datafile_dir "${CMAKE_BINARY_DIR}/run")
1422 endif()
1423
1424 set(DATAFILE_DIR ${_datafile_dir} CACHE INTERNAL "Build time data file location.")
1425
1426 # wsutil must be added after DATAFILE_DIR is set such that filesystem.c can
1427 # learn about the directory location.
1428 add_subdirectory( wsutil )
1429
1430 if(NOT WIN32)
1431         add_custom_target(dumpabi DEPENDS dumpabi-libwireshark dumpabi-libwiretap dumpabi-libwsutil)
1432 endif()
1433
1434 if(BUILD_wireshark AND QT_FOUND)
1435         add_subdirectory( ui/qt )
1436 endif()
1437
1438 # Location of our plugins. PLUGIN_DIR should allow running
1439 # from the build directory similar to DATAFILE_DIR above.
1440 if(ENABLE_PLUGINS)
1441         # Target platform locations
1442         # UN*X in general, including macOS if not building an app bundle:
1443         # $DESTDIR/lib/wireshark/plugins/$VERSION
1444         # Windows: $DESTDIR/wireshark/plugins/$VERSION
1445         # macOS app bundle: Wireshark.app/Contents/PlugIns/wireshark
1446         set(HAVE_PLUGINS 1)
1447         add_custom_target(plugins)
1448         set_target_properties(plugins PROPERTIES FOLDER "Plugins")
1449         set(PLUGIN_SRC_DIRS
1450                 plugins/epan/ethercat
1451                 plugins/epan/gryphon
1452                 plugins/epan/irda
1453                 plugins/epan/mate
1454                 plugins/epan/opcua
1455                 plugins/epan/profinet
1456                 plugins/epan/stats_tree
1457                 plugins/epan/transum
1458                 plugins/epan/unistim
1459                 plugins/epan/wimax
1460                 plugins/epan/wimaxasncp
1461                 plugins/epan/wimaxmacphy
1462                 plugins/wiretap/usbdump
1463                 plugins/codecs/l16_mono
1464                 ${CUSTOM_PLUGIN_SRC_DIR}
1465         )
1466
1467         # Build demo plugin, only if asked explicitly
1468         if(ENABLE_PLUGIN_IFDEMO)
1469                 set(PLUGIN_SRC_DIRS
1470                         ${PLUGIN_SRC_DIRS}
1471                         plugins/epan/pluginifdemo
1472                 )
1473         endif()
1474
1475 else()
1476         set(PLUGIN_SRC_DIRS )
1477 endif()
1478
1479 if(ENABLE_APPLICATION_BUNDLE)
1480         if(CMAKE_CFG_INTDIR STREQUAL ".")
1481                 set(_plugin_dir "${CMAKE_BINARY_DIR}/run/Wireshark.app/Contents/PlugIns/wireshark/${PROJECT_RELEASE_VERSION}")
1482         else()
1483           # Xcode
1484                 set(_plugin_dir "${CMAKE_BINARY_DIR}/run/$<CONFIG>/Wireshark.app/Contents/PlugIns/wireshark/${PROJECT_RELEASE_VERSION}")
1485         endif()
1486 elseif(WIN32 AND NOT CMAKE_CFG_INTDIR STREQUAL ".")
1487         set(_plugin_dir "${CMAKE_BINARY_DIR}/run/$<CONFIG>/${PLUGIN_VERSION_DIR}")
1488 else()
1489         set(_plugin_dir "${DATAFILE_DIR}/${PLUGIN_VERSION_DIR}")
1490 endif()
1491 set (PLUGIN_DIR ${_plugin_dir} CACHE INTERNAL "Build time plugin location.")
1492
1493 foreach(_plugin_src_dir ${PLUGIN_SRC_DIRS})
1494         add_subdirectory( ${_plugin_src_dir} )
1495 endforeach()
1496
1497 if(ENABLE_PCAP_NG_DEFAULT)
1498         set(PCAP_NG_DEFAULT 1)
1499 endif()
1500
1501 # Large file support (e.g. make off_t 64 bit if supported)
1502 include(gmxTestLargeFiles)
1503 gmx_test_large_files(GMX_LARGEFILES)
1504
1505 set( VERSION ${PROJECT_VERSION} )
1506 add_custom_target(version
1507         BYPRODUCTS version.h
1508         COMMAND ${PERL_EXECUTABLE}
1509                 ${CMAKE_SOURCE_DIR}/make-version.pl
1510                 ${CMAKE_SOURCE_DIR}
1511 )
1512 set_target_properties(version PROPERTIES FOLDER "Auxiliary")
1513
1514 set( configure_input "Built with CMake ${CMAKE_VERSION}" )
1515 configure_file(${CMAKE_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_BINARY_DIR}/config.h)
1516
1517 configure_file(${CMAKE_SOURCE_DIR}/ws_version.h.in ${CMAKE_BINARY_DIR}/ws_version.h)
1518
1519 set( prefix "${CMAKE_INSTALL_PREFIX}" )
1520 set( exec_prefix "\${prefix}" )
1521 set( libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}" )
1522 set( includedir  "\${prefix}/include" )
1523 set( plugindir "\${libdir}/wireshark/${PLUGIN_VERSION_DIR}" )
1524
1525 # Doxygen variables
1526 file(GLOB TOP_LEVEL_SOURCE_LIST *.c *.cpp *.h)
1527 string (REPLACE ";" " " DOXYGEN_TOP_LEVEL_SOURCES "${TOP_LEVEL_SOURCE_LIST}")
1528 set(DOXYGEN_INPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
1529 set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
1530
1531 set(CFG_OUT_FILES
1532         doxygen.cfg
1533         image/libwireshark.rc
1534         image/wireshark.rc
1535         image/dumpcap.rc
1536         image/libwscodecs.rc
1537         image/libwsutil.rc
1538         image/wiretap.rc
1539         image/wireshark.exe.manifest
1540         packaging/macosx/Info.plist
1541         packaging/macosx/osx-app.sh
1542         packaging/macosx/osx-dmg.sh
1543         packaging/macosx/Read_me_first.rtf
1544         packaging/macosx/Wireshark_package.pmdoc/index.xml
1545         wireshark.pc
1546 )
1547 foreach( _cfg_file ${CFG_OUT_FILES} )
1548         configure_file( ${CMAKE_SOURCE_DIR}/${_cfg_file}.in ${CMAKE_BINARY_DIR}/${_cfg_file} @ONLY )
1549 endforeach()
1550
1551 include(FeatureSummary)
1552 set_package_properties(SBC PROPERTIES
1553         DESCRIPTION "Bluetooth low-complexity, subband codec (SBC) decoder"
1554         URL "https://git.kernel.org/pub/scm/bluetooth/sbc.git"
1555         PURPOSE "Support for playing SBC codec in RTP player"
1556 )
1557 set_package_properties(SPANDSP PROPERTIES
1558         DESCRIPTION "a library of many DSP functions for telephony"
1559         URL "http://www.soft-switch.org/"
1560         PURPOSE "Support for G.722 and G.726 codecs in RTP player"
1561 )
1562 set_package_properties(BCG729 PROPERTIES
1563         DESCRIPTION "G.729 decoder"
1564         URL "https://www.linphone.org/technical-corner/bcg729/overview"
1565         PURPOSE "Support for G.729 codec in RTP player"
1566 )
1567 set_package_properties(LIBXML2 PROPERTIES
1568         DESCRIPTION "XML parsing library"
1569         URL "http://xmlsoft.org/"
1570         PURPOSE "Read XML configuration files in EPL dissector"
1571 )
1572 set_package_properties(LIBSSH PROPERTIES
1573         DESCRIPTION "Library for implementing SSH clients"
1574         URL "https://www.libssh.org/"
1575         PURPOSE "extcap remote SSH interfaces (sshdump, ciscodump)"
1576 )
1577 set_package_properties(LZ4 PROPERTIES
1578         DESCRIPTION "LZ4 is lossless compression algorithm used in some protocol (CQL...)"
1579         URL "http://www.lz4.org"
1580         PURPOSE "LZ4 decompression in CQL and Kafka dissectors"
1581 )
1582 set_package_properties(SNAPPY PROPERTIES
1583         DESCRIPTION "A fast compressor/decompressor from Google"
1584         URL "http://google.github.io/snappy/"
1585         PURPOSE "Snappy decompression in CQL and Kafka dissectors"
1586 )
1587 set_package_properties(NGHTTP2 PROPERTIES
1588         DESCRIPTION "HTTP/2 C library and tools"
1589         URL "https://nghttp2.org"
1590         PURPOSE "Header decompression in HTTP2"
1591 )
1592
1593 message(STATUS "C-Flags: ${CMAKE_C_FLAGS}")
1594 message(STATUS "CXX-Flags: ${CMAKE_CXX_FLAGS}")
1595 message(STATUS "Warnings as errors: ${WERROR_COMMON_FLAGS}")
1596
1597 feature_summary(WHAT ALL)
1598
1599 if(WIN32)
1600         set(PLATFORM_UI_SRC
1601                 ui/win32/console_win32.c
1602                 ui/win32/file_dlg_win32.c
1603         )
1604         set(PLATFORM_UI_RC_FILES
1605                 image/file_dlg_win32.rc
1606         )
1607 elseif(APPLE)
1608         set(PLATFORM_UI_SRC
1609                 ui/macosx/cocoa_bridge.mm
1610         )
1611 endif()
1612
1613 # sources common for wireshark, tshark, rawshark and sharkd
1614 set(SHARK_COMMON_SRC
1615         cfile.c
1616         file_packet_provider.c
1617         frame_tvbuff.c
1618         sync_pipe_write.c
1619         version_info.c
1620         extcap.c
1621         extcap_parser.c
1622 )
1623
1624 set(TSHARK_TAP_SRC
1625         ${CMAKE_SOURCE_DIR}/ui/cli/tap-camelsrt.c
1626         ${CMAKE_SOURCE_DIR}/ui/cli/tap-diameter-avp.c
1627         ${CMAKE_SOURCE_DIR}/ui/cli/tap-expert.c
1628         ${CMAKE_SOURCE_DIR}/ui/cli/tap-exportobject.c
1629         ${CMAKE_SOURCE_DIR}/ui/cli/tap-endpoints.c
1630         ${CMAKE_SOURCE_DIR}/ui/cli/tap-flow.c
1631         ${CMAKE_SOURCE_DIR}/ui/cli/tap-follow.c
1632         ${CMAKE_SOURCE_DIR}/ui/cli/tap-funnel.c
1633         ${CMAKE_SOURCE_DIR}/ui/cli/tap-gsm_astat.c
1634         ${CMAKE_SOURCE_DIR}/ui/cli/tap-hosts.c
1635         ${CMAKE_SOURCE_DIR}/ui/cli/tap-httpstat.c
1636         ${CMAKE_SOURCE_DIR}/ui/cli/tap-icmpstat.c
1637         ${CMAKE_SOURCE_DIR}/ui/cli/tap-icmpv6stat.c
1638         ${CMAKE_SOURCE_DIR}/ui/cli/tap-iostat.c
1639         ${CMAKE_SOURCE_DIR}/ui/cli/tap-iousers.c
1640         ${CMAKE_SOURCE_DIR}/ui/cli/tap-macltestat.c
1641         ${CMAKE_SOURCE_DIR}/ui/cli/tap-protocolinfo.c
1642         ${CMAKE_SOURCE_DIR}/ui/cli/tap-protohierstat.c
1643         ${CMAKE_SOURCE_DIR}/ui/cli/tap-rlcltestat.c
1644         ${CMAKE_SOURCE_DIR}/ui/cli/tap-rpcprogs.c
1645         ${CMAKE_SOURCE_DIR}/ui/cli/tap-rtd.c
1646         ${CMAKE_SOURCE_DIR}/ui/cli/tap-rtp.c
1647         ${CMAKE_SOURCE_DIR}/ui/cli/tap-rtspstat.c
1648         ${CMAKE_SOURCE_DIR}/ui/cli/tap-sctpchunkstat.c
1649         ${CMAKE_SOURCE_DIR}/ui/cli/tap-simple_stattable.c
1650         ${CMAKE_SOURCE_DIR}/ui/cli/tap-sipstat.c
1651         ${CMAKE_SOURCE_DIR}/ui/cli/tap-smbsids.c
1652         ${CMAKE_SOURCE_DIR}/ui/cli/tap-srt.c
1653         ${CMAKE_SOURCE_DIR}/ui/cli/tap-stats_tree.c
1654         ${CMAKE_SOURCE_DIR}/ui/cli/tap-sv.c
1655         ${CMAKE_SOURCE_DIR}/ui/cli/tap-wspstat.c
1656 )
1657
1658 set(INSTALL_DIRS
1659         diameter
1660         dtds
1661         profiles
1662         radius
1663         tpncp
1664         wimaxasncp
1665 )
1666
1667 # FIXME: The help files are textfified on Windows into the staging dir.
1668 # The DATAFILE_DIR depends on CMAKE_CFG_INTDIR therefore it cannot be
1669 # added to an install() command with build systems supporting multiple
1670 # configurations in the build tree (e.g.: Visual Studio).
1671 if(NOT WIN32)
1672         list(APPEND INSTALL_DIRS help)
1673 endif()
1674
1675
1676 set(INSTALL_FILES
1677         cfilters
1678         colorfilters
1679         dfilters
1680         enterprises.tsv
1681         manuf
1682         pdml2html.xsl
1683         services
1684         smi_modules
1685         wka
1686         docbook/ws.css
1687         ${CMAKE_BINARY_DIR}/doc/AUTHORS-SHORT
1688         ${CMAKE_BINARY_DIR}/doc/androiddump.html
1689         ${CMAKE_BINARY_DIR}/doc/udpdump.html
1690         ${CMAKE_BINARY_DIR}/doc/capinfos.html
1691         ${CMAKE_BINARY_DIR}/doc/captype.html
1692         ${CMAKE_BINARY_DIR}/doc/ciscodump.html
1693         ${CMAKE_BINARY_DIR}/doc/dftest.html
1694         ${CMAKE_BINARY_DIR}/doc/dumpcap.html
1695         ${CMAKE_BINARY_DIR}/doc/editcap.html
1696         ${CMAKE_BINARY_DIR}/doc/extcap.html
1697         ${CMAKE_BINARY_DIR}/doc/mergecap.html
1698         ${CMAKE_BINARY_DIR}/doc/randpkt.html
1699         ${CMAKE_BINARY_DIR}/doc/randpktdump.html
1700         ${CMAKE_BINARY_DIR}/doc/rawshark.html
1701         ${CMAKE_BINARY_DIR}/doc/reordercap.html
1702         ${CMAKE_BINARY_DIR}/doc/sshdump.html
1703         ${CMAKE_BINARY_DIR}/doc/text2pcap.html
1704         ${CMAKE_BINARY_DIR}/doc/tshark.html
1705         ${CMAKE_BINARY_DIR}/doc/wireshark.html
1706         ${CMAKE_BINARY_DIR}/doc/wireshark-filter.html
1707 )
1708
1709 if(MAXMINDDB_FOUND)
1710         list(APPEND INSTALL_FILES ${CMAKE_BINARY_DIR}/doc/mmdbresolve.html)
1711 endif()
1712
1713 if (BUILD_corbaidl2wrs)
1714         list(APPEND INSTALL_FILES ${CMAKE_BINARY_DIR}/doc/idl2wrs.html)
1715 endif()
1716 if (BUILD_xxx2deb)
1717         list(APPEND INSTALL_FILES
1718                 ${CMAKE_BINARY_DIR}/doc/asn2deb.html
1719                 ${CMAKE_BINARY_DIR}/doc/idl2deb.html
1720         )
1721 endif()
1722
1723 if(WIN32)
1724         set(TEXTIFY_FILES COPYING NEWS README.windows)
1725         set(TEXTIFY_MD_FILES README.md)
1726         foreach(_text_file ${TEXTIFY_FILES} ${TEXTIFY_MD_FILES})
1727                 string(REGEX REPLACE ".md$" "" _out_file ${_text_file})
1728                 set(INSTALL_FILES ${CMAKE_BINARY_DIR}/${_out_file}.txt ${INSTALL_FILES})
1729         endforeach()
1730 else()
1731         set(INSTALL_FILES COPYING ${INSTALL_FILES})
1732 endif()
1733
1734 set(VERSION_INFO_LIBS
1735                 ${ZLIB_LIBRARIES}
1736 )
1737
1738 if(WIN32)
1739         set(_dll_output_dir "${DATAFILE_DIR}")
1740         add_custom_target(copy_cli_dlls)
1741         set_target_properties(copy_cli_dlls PROPERTIES FOLDER "Copy Tasks")
1742         add_custom_command(TARGET copy_cli_dlls PRE_BUILD
1743                 COMMAND ${CMAKE_COMMAND} -E make_directory "${_dll_output_dir}"
1744         )
1745
1746         # XXX Can (and should) we iterate over these similar to the way
1747         # the top-level CMakeLists.txt iterates over the package list?
1748
1749         # Required DLLs.
1750         # The gio, gnutls, png, and other OBS-generated DLLs depend on
1751         # zlib1.dll. We compile zlib locally but the Debug configuration
1752         # (the default) creates zlibd1.dll.
1753         add_custom_command(TARGET copy_cli_dlls PRE_BUILD
1754                 COMMAND ${CMAKE_COMMAND} -E copy_if_different
1755                         ${GLIB2_DLLS} $<$<CONFIG:Debug>:zlib1.dll>
1756                         "${_dll_output_dir}"
1757                 WORKING_DIRECTORY "${GLIB2_DLL_DIR}"
1758         )
1759
1760         # Optional DLLs.
1761         set (OPTIONAL_DLLS)
1762         if (AIRPCAP_FOUND)
1763                 list (APPEND OPTIONAL_DLLS "${AIRPCAP_DLL_DIR}/${AIRPCAP_DLL}")
1764         endif(AIRPCAP_FOUND)
1765         if (CARES_FOUND)
1766                 list (APPEND OPTIONAL_DLLS "${CARES_DLL_DIR}/${CARES_DLL}")
1767         endif(CARES_FOUND)
1768         if (MAXMINDDB_FOUND)
1769                 list (APPEND OPTIONAL_DLLS "${MAXMINDDB_DLL_DIR}/${MAXMINDDB_DLL}")
1770         endif(MAXMINDDB_FOUND)
1771         if (LIBSSH_FOUND)
1772                 list (APPEND OPTIONAL_DLLS "${LIBSSH_DLL_DIR}/${LIBSSH_DLL}")
1773         endif(LIBSSH_FOUND)
1774         foreach( _dll ${GCRYPT_DLLS} )
1775                 list (APPEND OPTIONAL_DLLS "${GCRYPT_DLL_DIR}/${_dll}")
1776         endforeach(_dll)
1777         foreach( _dll ${GNUTLS_DLLS} )
1778                 list (APPEND OPTIONAL_DLLS "${GNUTLS_DLL_DIR}/${_dll}")
1779         endforeach(_dll)
1780         foreach( _dll ${KERBEROS_DLLS} )
1781                 list (APPEND OPTIONAL_DLLS "${KERBEROS_DLL_DIR}/${_dll}")
1782         endforeach(_dll)
1783         if (LUA_FOUND)
1784                 list (APPEND OPTIONAL_DLLS "${LUA_DLL_DIR}/${LUA_DLL}")
1785         endif(LUA_FOUND)
1786         if (LZ4_FOUND)
1787                 list (APPEND OPTIONAL_DLLS "${LZ4_DLL_DIR}/${LZ4_DLL}")
1788         endif(LZ4_FOUND)
1789         if (NGHTTP2_FOUND)
1790                 list (APPEND OPTIONAL_DLLS "${NGHTTP2_DLL_DIR}/${NGHTTP2_DLL}")
1791         endif(NGHTTP2_FOUND)
1792         if (SBC_FOUND)
1793                 list (APPEND OPTIONAL_DLLS "${SBC_DLL_DIR}/${SBC_DLL}")
1794         endif(SBC_FOUND)
1795         if (SPANDSP_FOUND)
1796                 list (APPEND OPTIONAL_DLLS "${SPANDSP_DLL_DIR}/${SPANDSP_DLL}")
1797         endif(SPANDSP_FOUND)
1798         if (BCG729_FOUND)
1799                 list (APPEND OPTIONAL_DLLS "${BCG729_DLL_DIR}/${BCG729_DLL}")
1800         endif(BCG729_FOUND)
1801         if (LIBXML2_FOUND)
1802                 list (APPEND OPTIONAL_DLLS "${LIBXML2_DLL_DIR}/${LIBXML2_DLL}")
1803         endif(LIBXML2_FOUND)
1804         if (SMI_FOUND)
1805                 list (APPEND OPTIONAL_DLLS "${SMI_DLL_DIR}/${SMI_DLL}")
1806                 # Wireshark.nsi wants SMI_DIR which is the base SMI directory
1807                 get_filename_component(SMI_DIR ${SMI_DLL_DIR} DIRECTORY)
1808                 add_custom_command(TARGET copy_cli_dlls PRE_BUILD
1809                         COMMAND ${CMAKE_COMMAND} -E make_directory
1810                                 "${_dll_output_dir}/snmp"
1811                         COMMAND ${CMAKE_COMMAND} -E make_directory
1812                                 "${_dll_output_dir}/snmp/mibs"
1813                         COMMAND ${CMAKE_COMMAND} -E copy_directory
1814                                 "${SMI_SHARE_DIR}/mibs/iana"
1815                                 "${_dll_output_dir}/snmp/mibs"
1816                         COMMAND ${CMAKE_COMMAND} -E copy_directory
1817                                 "${SMI_SHARE_DIR}/mibs/ietf"
1818                                 "${_dll_output_dir}/snmp/mibs"
1819                         COMMAND ${CMAKE_COMMAND} -E copy_directory
1820                                 "${SMI_SHARE_DIR}/mibs/irtf"
1821                                 "${_dll_output_dir}/snmp/mibs"
1822                         COMMAND ${CMAKE_COMMAND} -E copy_directory
1823                                 "${SMI_SHARE_DIR}/mibs/site"
1824                                 "${_dll_output_dir}/snmp/mibs"
1825                         COMMAND ${CMAKE_COMMAND} -E copy_directory
1826                                 "${SMI_SHARE_DIR}/mibs/tubs"
1827                                 "${_dll_output_dir}/snmp/mibs"
1828                         COMMAND ${CMAKE_COMMAND} -E copy_directory
1829                                 "${SMI_SHARE_DIR}/pibs"
1830                                 "${_dll_output_dir}/snmp/mibs"
1831                         COMMAND ${CMAKE_COMMAND} -E copy_directory
1832                                 "${SMI_SHARE_DIR}/yang"
1833                                 "${_dll_output_dir}/snmp/mibs"
1834                         #remove the extra directories copied (shallow copying the above would remove the need for this)
1835                         COMMAND ${CMAKE_COMMAND} -E remove_directory
1836                                 "${_dll_output_dir}/snmp/mibs/iana"
1837                         COMMAND ${CMAKE_COMMAND} -E remove_directory
1838                                 "${_dll_output_dir}/snmp/mibs/ietf"
1839                         COMMAND ${CMAKE_COMMAND} -E remove_directory
1840                                 "${_dll_output_dir}/snmp/mibs/site"
1841                         COMMAND ${CMAKE_COMMAND} -E remove_directory
1842                                 "${_dll_output_dir}/snmp/mibs/tubs"
1843                 )
1844         endif(SMI_FOUND)
1845         if (SNAPPY_FOUND)
1846                 list (APPEND OPTIONAL_DLLS "${SNAPPY_DLL_DIR}/${SNAPPY_DLL}")
1847         endif(SNAPPY_FOUND)
1848         if (WINSPARKLE_FOUND)
1849                 list (APPEND OPTIONAL_DLLS "${WINSPARKLE_DLL_DIR}/${WINSPARKLE_DLL}")
1850         endif(WINSPARKLE_FOUND)
1851
1852         # With libs downloaded to c:/wireshark-win64-libs this currently
1853         # (early 2018) expands to about 1900 characters.
1854         if (OPTIONAL_DLLS)
1855                 add_custom_command(TARGET copy_cli_dlls PRE_BUILD
1856                         COMMAND ${CMAKE_COMMAND} -E copy_if_different
1857                                 ${OPTIONAL_DLLS}
1858                                 "${_dll_output_dir}"
1859                         VERBATIM
1860                 )
1861                 install(FILES ${OPTIONAL_DLLS} DESTINATION "${CMAKE_INSTALL_BINDIR}")
1862         endif(OPTIONAL_DLLS)
1863
1864         add_dependencies(epan copy_cli_dlls)
1865
1866         # We have a lot of choices for creating zip archives:
1867         # - 7z, WinZip, etc., which require a separate download+install.
1868         # - Cygwin's zip, which requires Cygwin.
1869         # - "CMake -E tar cz", which creates a tar file.
1870         # - CPack, which requires a CPack configuration.
1871         # - PowerShell via PSCX or System.IO.Compression.FileSystem.
1872         # - Python via zipfile.
1873         # For now, just look for 7z. It's installed on the Windows builders,
1874         # which might be the only systems that use this target.
1875         find_program(ZIP_EXECUTABLE 7z
1876                 PATH "$ENV{PROGRAMFILES}/7-Zip" "$ENV{PROGRAMW6432}/7-Zip"
1877                 DOC "Path to the 7z utility."
1878         )
1879         if(ZIP_EXECUTABLE)
1880                 add_custom_target(pdb_zip_package COMMENT "This packages .PDBs but will not create them.")
1881                 set_target_properties(pdb_zip_package PROPERTIES FOLDER "Packaging")
1882                 set(_pdb_zip "${CMAKE_BINARY_DIR}/Wireshark-pdb-${WIRESHARK_TARGET_PLATFORM}-${VERSION}.zip")
1883                 file(TO_NATIVE_PATH "${_pdb_zip}" _pdb_zip_win)
1884                 add_custom_command(TARGET pdb_zip_package POST_BUILD
1885                         COMMAND ${CMAKE_COMMAND} -E remove -f "${_pdb_zip}"
1886                         COMMAND ${ZIP_EXECUTABLE} a -tzip -mmt=on "${_pdb_zip_win}"
1887                                 *.pdb *.lib
1888                                 extcap/*.pdb
1889                                 ${PLUGIN_VERSION_DIR}/epan/*.pdb
1890                                 ${PLUGIN_VERSION_DIR}/wiretap/*.pdb
1891                         WORKING_DIRECTORY "${_dll_output_dir}"
1892                 )
1893         endif()
1894 endif(WIN32)
1895
1896 # List of extra dependencies for the "copy_data_files" target
1897 set(copy_data_files_depends)
1898
1899 # glob patterns relative to the source directory that should be copied to
1900 # ${DATAFILE_DIR} (including directory prefixes)
1901 set(DATA_FILES_SRC
1902         "help/toc"
1903 )
1904
1905 if(WIN32)
1906         foreach(_text_file ${TEXTIFY_FILES})
1907                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${_text_file}.txt
1908                         COMMAND ${POWERSHELL_COMMAND} "${CMAKE_SOURCE_DIR}/tools/textify.ps1"
1909                                 -Destination ${CMAKE_BINARY_DIR}
1910                                 ${CMAKE_SOURCE_DIR}/${_text_file}
1911                         DEPENDS
1912                                 ${CMAKE_SOURCE_DIR}/${_text_file}
1913                 )
1914         endforeach()
1915         foreach(_md_file ${TEXTIFY_MD_FILES})
1916                 string(REGEX REPLACE ".md$" ".txt" _text_file ${_md_file})
1917                 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${_text_file}
1918                         COMMAND ${POWERSHELL_COMMAND} "${CMAKE_SOURCE_DIR}/tools/textify.ps1"
1919                                 -Destination ${CMAKE_BINARY_DIR}
1920                                 ${CMAKE_SOURCE_DIR}/${_md_file}
1921                         COMMAND ${CMAKE_COMMAND} -E rename
1922                                 ${CMAKE_BINARY_DIR}/${_md_file}.txt
1923                                 ${CMAKE_BINARY_DIR}/${_text_file}
1924                         DEPENDS
1925                                 ${CMAKE_SOURCE_DIR}/${_text_file}
1926                 )
1927         endforeach()
1928 endif()
1929
1930 foreach(_install_file ${INSTALL_FILES})
1931         get_filename_component(_install_file_src "${_install_file}" ABSOLUTE)
1932         get_filename_component(_install_basename "${_install_file}" NAME)
1933         set(_output_file "${DATAFILE_DIR}/${_install_basename}")
1934         add_custom_command(OUTPUT "${_output_file}"
1935                 COMMAND ${CMAKE_COMMAND} -E copy_if_different
1936                         "${_install_file_src}"
1937                         "${_output_file}"
1938                 DEPENDS
1939                         docs
1940                         "${_install_file}"
1941         )
1942         list(APPEND copy_data_files_depends "${_output_file}")
1943 endforeach()
1944
1945 # faq.txt is handled separately below.
1946 set(_help_source_files
1947         help/capture_filters.txt
1948         help/capturing.txt
1949         help/display_filters.txt
1950         help/getting_started.txt
1951         help/overview.txt
1952 )
1953
1954 if(WIN32)
1955         file(TO_NATIVE_PATH "${DATAFILE_DIR}/help" _help_dest_dir)
1956         foreach(_help_file IN LISTS _help_source_files)
1957                 add_custom_command(OUTPUT "${DATAFILE_DIR}/${_help_file}"
1958                         COMMAND ${CMAKE_COMMAND} -E make_directory "${DATAFILE_DIR}/help"
1959                         COMMAND ${POWERSHELL_COMMAND} "${CMAKE_SOURCE_DIR}/tools/textify.ps1"
1960                                 -Destination "${_help_dest_dir}"
1961                                 "${CMAKE_SOURCE_DIR}/${_help_file}"
1962                         DEPENDS
1963                                 "${CMAKE_SOURCE_DIR}/${_help_file}"
1964                 )
1965                 list(APPEND copy_data_files_depends "${DATAFILE_DIR}/${_help_file}")
1966         endforeach()
1967 else()
1968         list(APPEND DATA_FILES_SRC ${_help_source_files})
1969 endif(WIN32)
1970
1971 # Create help/faq.txt when missing
1972 add_custom_command(OUTPUT "${DATAFILE_DIR}/help/faq.txt"
1973         COMMAND ${CMAKE_COMMAND} -E make_directory "${DATAFILE_DIR}/help"
1974         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/help/faq.py -b > faq.tmp.html
1975         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/html2text.py
1976                 faq.tmp.html > "${DATAFILE_DIR}/help/faq.txt"
1977         COMMAND ${CMAKE_COMMAND} -E remove faq.tmp.html
1978         DEPENDS
1979                 "${CMAKE_SOURCE_DIR}/help/faq.py"
1980                 "${CMAKE_SOURCE_DIR}/tools/html2text.py"
1981 )
1982 list(APPEND copy_data_files_depends "${DATAFILE_DIR}/help/faq.txt")
1983
1984 # Install LUA files in staging directory such that LUA can used when Wireshark
1985 # is ran from the build directory. For install targets, see
1986 # epan/wslua/CMakeLists.txt
1987 if(LUA_FOUND AND ENABLE_LUA)
1988         set(_lua_files
1989                 "${CMAKE_BINARY_DIR}/epan/wslua/init.lua"
1990                 "${CMAKE_SOURCE_DIR}/epan/wslua/console.lua"
1991                 "${CMAKE_SOURCE_DIR}/epan/wslua/dtd_gen.lua"
1992         )
1993         foreach(_lua_file ${_lua_files})
1994                 get_filename_component(_lua_filename "${_lua_file}" NAME)
1995                 list(APPEND copy_data_files_depends
1996                         "${DATAFILE_DIR}/${_lua_filename}")
1997                 add_custom_command(OUTPUT "${DATAFILE_DIR}/${_lua_filename}"
1998                         COMMAND ${CMAKE_COMMAND} -E copy_if_different
1999                                 "${_lua_file}"
2000                                 "${DATAFILE_DIR}/${_lua_filename}"
2001                         DEPENDS
2002                                 wsluaauxiliary
2003                                 "${_lua_file}"
2004                 )
2005         endforeach()
2006 endif(LUA_FOUND AND ENABLE_LUA)
2007 # doc/*.html handled elsewhere.
2008
2009 # TODO shouldn't this use full (relative) paths instead of glob patterns?
2010 list(APPEND DATA_FILES_SRC
2011         "tpncp/tpncp.dat"
2012         "wimaxasncp/*.dtd"
2013         "wimaxasncp/*.xml"
2014 )
2015
2016 # Copy all paths from the source tree to the data directory. Directories are
2017 # automatically created if missing as the filename is given.
2018 file(GLOB _data_files RELATIVE "${CMAKE_SOURCE_DIR}" ${DATA_FILES_SRC})
2019 foreach(_data_file ${_data_files})
2020         add_custom_command(OUTPUT "${DATAFILE_DIR}/${_data_file}"
2021                 COMMAND ${CMAKE_COMMAND} -E copy_if_different
2022                         "${CMAKE_SOURCE_DIR}/${_data_file}"
2023                         "${DATAFILE_DIR}/${_data_file}"
2024                 DEPENDS
2025                         "${CMAKE_SOURCE_DIR}/${_data_file}"
2026         )
2027         list(APPEND copy_data_files_depends "${DATAFILE_DIR}/${_data_file}")
2028 endforeach()
2029
2030 file(GLOB _dtds_src_files RELATIVE "${CMAKE_SOURCE_DIR}" "dtds/*.dtd")
2031
2032 set (_dtds_data_files)
2033 foreach(_data_file ${_dtds_src_files})
2034         list(APPEND _dtds_data_files "${DATAFILE_DIR}/${_data_file}")
2035 endforeach()
2036
2037 add_custom_command(
2038         OUTPUT ${_dtds_data_files}
2039         COMMAND ${CMAKE_COMMAND} -E make_directory "${DATAFILE_DIR}/dtds"
2040         COMMAND ${CMAKE_COMMAND} -E copy_if_different
2041                 ${_dtds_src_files}
2042                 "${DATAFILE_DIR}/dtds"
2043         VERBATIM
2044         DEPENDS ${_dtds_src_files}
2045         WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
2046 )
2047
2048 file(GLOB _diameter_src_files RELATIVE "${CMAKE_SOURCE_DIR}"
2049         diameter/*.dtd
2050         diameter/*.xml
2051 )
2052
2053 set (_diameter_data_files)
2054 foreach(_data_file ${_diameter_src_files})
2055         list(APPEND _diameter_data_files "${DATAFILE_DIR}/${_data_file}")
2056 endforeach()
2057
2058 add_custom_command(
2059         OUTPUT ${_diameter_data_files}
2060         COMMAND ${CMAKE_COMMAND} -E make_directory "${DATAFILE_DIR}/diameter"
2061         COMMAND ${CMAKE_COMMAND} -E copy_if_different
2062                 ${_diameter_src_files}
2063                 "${DATAFILE_DIR}/diameter"
2064         VERBATIM
2065         DEPENDS ${_diameter_src_files}
2066         WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
2067 )
2068
2069 file(GLOB _radius_src_files RELATIVE "${CMAKE_SOURCE_DIR}"
2070         radius/README.radius_dictionary
2071         radius/custom.includes
2072         radius/dictionary
2073         radius/dictionary.*
2074 )
2075
2076 set (_radius_data_files)
2077 foreach(_data_file ${_radius_src_files})
2078         list(APPEND _radius_data_files "${DATAFILE_DIR}/${_data_file}")
2079 endforeach()
2080
2081 add_custom_command(
2082         OUTPUT ${_radius_data_files}
2083         COMMAND ${CMAKE_COMMAND} -E make_directory "${DATAFILE_DIR}/radius"
2084         COMMAND ${CMAKE_COMMAND} -E copy_if_different
2085                 ${_radius_src_files}
2086                 "${DATAFILE_DIR}/radius"
2087         VERBATIM
2088         DEPENDS ${_radius_src_files}
2089         WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
2090 )
2091
2092 file(GLOB _profiles_src_files RELATIVE "${CMAKE_SOURCE_DIR}" profiles/*/*)
2093 set (_profiles_data_files)
2094 foreach(_data_file ${_profiles_src_files})
2095         list(APPEND _profiles_data_files "${DATAFILE_DIR}/${_data_file}")
2096 endforeach()
2097
2098 add_custom_command(
2099         OUTPUT ${_profiles_data_files}
2100         COMMAND ${CMAKE_COMMAND} -E copy_directory
2101                 "${CMAKE_SOURCE_DIR}/profiles" "${DATAFILE_DIR}/profiles"
2102 )
2103
2104 list(APPEND copy_data_files_depends
2105         ${_dtds_data_files}
2106         ${_diameter_data_files}
2107         ${_radius_data_files}
2108         ${_profiles_data_files}
2109 )
2110
2111 # Copy files including ${INSTALL_FILES} and ${INSTALL_DIRS} to ${DATAFILE_DIR}
2112 add_custom_target(copy_data_files ALL DEPENDS ${copy_data_files_depends} )
2113 set_target_properties(copy_data_files PROPERTIES FOLDER "Copy Tasks")
2114
2115 if(BUILD_wireshark AND QT_FOUND)
2116         set(WIRESHARK_SRC
2117                 capture_info.c
2118                 capture_opts.c
2119                 file.c
2120                 fileset.c
2121                 ${SHARK_COMMON_SRC}
2122                 ${PLATFORM_UI_SRC}
2123         )
2124         set(wireshark_FILES
2125                 ${WIRESHARK_SRC}
2126                 ${PLATFORM_UI_RC_FILES}
2127         )
2128         set_executable_resources(wireshark "Wireshark" UNIQUE_RC)
2129 endif()
2130
2131 if(ENABLE_APPLICATION_BUNDLE)
2132         #
2133         # Add -Wl,-single_module to the LDFLAGS used with shared
2134         # libraries, to fix some error that show up in some cases;
2135         # some Apple documentation recommends it for most shared
2136         # libraries.
2137         #
2138         set( CMAKE_SHARED_LINKER_FLAGS "-Wl,-single_module ${CMAKE_SHARED_LINKER_FLAGS}" )
2139         #
2140         # Add -Wl,-headerpad_max_install_names to the LDFLAGS, as
2141         # code-signing issues is running out of padding space.
2142         #
2143         # Add -Wl,-search_paths_first to make sure that if we search
2144         # directories A and B, in that order, for a given library, a
2145         # non-shared version in directory A, rather than a shared
2146         # version in directory B, is chosen (so we can use
2147         # --with-pcap=/usr/local to force all programs to be linked
2148         # with a static version installed in /usr/local/lib rather than
2149         # the system version in /usr/lib).
2150         #
2151
2152         set(CMAKE_EXE_LINKER_FLAGS
2153         "-Wl,-headerpad_max_install_names -Wl,-search_paths_first ${CMAKE_EXE_LINKER_FLAGS}"
2154         )
2155
2156         # Create our Frameworks directory
2157         file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/run/${CMAKE_CFG_INTDIR}/Wireshark.app/Contents/Frameworks")
2158
2159         # Add files to the app bundle
2160         # Wireshark.app/Contents
2161         file(WRITE ${CMAKE_BINARY_DIR}/packaging/macosx/PkgInfo "APPLWshk\n")
2162         set(BUNDLE_CONTENTS_FILES
2163                 ${CMAKE_BINARY_DIR}/packaging/macosx/PkgInfo
2164         )
2165         set_source_files_properties(${BUNDLE_CONTENTS_FILES} PROPERTIES
2166                 MACOSX_PACKAGE_LOCATION .
2167         )
2168
2169         # Wireshark.app/Contents/Resources
2170         set(BUNDLE_RESOURCE_FILES
2171                 ${CMAKE_SOURCE_DIR}/packaging/macosx/Wireshark.icns
2172                 ${CMAKE_SOURCE_DIR}/packaging/macosx/Wiresharkdoc.icns
2173         )
2174         set_source_files_properties(${BUNDLE_RESOURCE_FILES} PROPERTIES
2175                 MACOSX_PACKAGE_LOCATION Resources
2176         )
2177
2178         # Wireshark.app/Contents/Resources/share/man/man1
2179         set_source_files_properties(${BUNDLE_RESOURCE_SHARE_MAN1_FILES} PROPERTIES
2180                 MACOSX_PACKAGE_LOCATION Resources/share/man/man1
2181                 GENERATED 1
2182         )
2183
2184         # Wireshark.app/Contents/Resources/share/man/man4
2185         set_source_files_properties(${BUNDLE_RESOURCE_SHARE_MAN4_FILES} PROPERTIES
2186                 MACOSX_PACKAGE_LOCATION Resources/share/man/man4
2187                 GENERATED 1
2188         )
2189
2190         # INSTALL_FILES and INSTALL_DIRS are handled by copy_data_files
2191
2192         set(EXTRA_BUNDLE_FILES
2193                 ${BUNDLE_CONTENTS_FILES}
2194                 ${BUNDLE_RESOURCE_FILES}
2195                 ${BUNDLE_RESOURCE_SHARE_MAN1_FILES}
2196                 ${BUNDLE_RESOURCE_SHARE_MAN4_FILES}
2197         )
2198 else()
2199         set(EXTRA_BUNDLE_FILES)
2200 endif()
2201
2202 if(BUILD_wireshark AND QT_FOUND)
2203         set(wireshark_LIBS
2204                 qtui
2205                 ui
2206                 capchild
2207                 caputils
2208                 wiretap
2209                 ${QT_LIBRARIES}
2210                 ${GTHREAD2_LIBRARIES}
2211                 wscodecs
2212                 epan
2213                 ${VERSION_INFO_LIBS}
2214                 ${APPLE_APPLICATION_SERVICES_LIBRARY}
2215                 ${APPLE_APPKIT_LIBRARY}
2216                 ${APPLE_CORE_FOUNDATION_LIBRARY}
2217                 ${APPLE_SYSTEM_CONFIGURATION_LIBRARY}
2218                 ${NL_LIBRARIES}
2219                 ${WIN_VERSION_LIBRARY}
2220                 ${WINSPARKLE_LIBRARIES}
2221         )
2222
2223         add_executable(wireshark WIN32 MACOSX_BUNDLE ${wireshark_FILES} ${EXTRA_BUNDLE_FILES})
2224         add_dependencies(wireshark version)
2225         set(PROGLIST ${PROGLIST} wireshark)
2226         set_target_properties(wireshark PROPERTIES
2227                 LINK_FLAGS "${WS_LINK_FLAGS}"
2228                 FOLDER "Executables"
2229         )
2230         if(ENABLE_APPLICATION_BUNDLE OR WIN32)
2231                 set_target_properties(wireshark PROPERTIES OUTPUT_NAME Wireshark)
2232         endif()
2233
2234         if(ENABLE_APPLICATION_BUNDLE)
2235                 add_dependencies(wireshark manpages)
2236                 set_target_properties(
2237                         wireshark PROPERTIES
2238                                 MACOSX_BUNDLE_INFO_PLIST ${CMAKE_BINARY_DIR}/packaging/macosx/Info.plist
2239                 )
2240                 if(CMAKE_CFG_INTDIR STREQUAL ".")
2241                         # Add a wrapper script which opens the bundle. This adds
2242                         # convenience but makes debugging more difficult.
2243                         # It is not created if using Xcode
2244                         file(REMOVE ${CMAKE_BINARY_DIR}/run/wireshark)
2245                         file(WRITE ${CMAKE_BINARY_DIR}/run/wireshark "#!/bin/sh\n")
2246                         file(APPEND ${CMAKE_BINARY_DIR}/run/wireshark "# Generated by ${CMAKE_CURRENT_LIST_FILE}\n")
2247                         file(APPEND ${CMAKE_BINARY_DIR}/run/wireshark "exec ${CMAKE_BINARY_DIR}/run/Wireshark.app/Contents/MacOS/Wireshark \"\$\@\"\n")
2248                         execute_process(COMMAND chmod a+x ${CMAKE_BINARY_DIR}/run/wireshark)
2249                 endif()
2250         endif()
2251
2252         target_link_libraries(wireshark ${wireshark_LIBS})
2253         install(
2254                 TARGETS wireshark
2255                 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
2256                 BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}
2257         )
2258
2259         if(QT_WINDEPLOYQT_EXECUTABLE)
2260                 add_custom_target(copy_qt_dlls ALL)
2261                 set_target_properties(copy_qt_dlls PROPERTIES FOLDER "Copy Tasks")
2262                 # Will we ever need to use --debug? Windeployqt seems to
2263                 # be smart enough to copy debug DLLs when needed.
2264                 add_custom_command(TARGET copy_qt_dlls
2265                         POST_BUILD
2266                         COMMAND set "PATH=${QT_BIN_PATH};%PATH%"
2267                         COMMAND "${QT_WINDEPLOYQT_EXECUTABLE}"
2268                                 $<$<CONFIG:Debug>:--debug>
2269                                 $<$<NOT:$<CONFIG:Debug>>:--release>
2270                                 --no-compiler-runtime
2271                                 --verbose 10
2272                                 "$<TARGET_FILE:wireshark>"
2273                 )
2274                 add_dependencies(copy_qt_dlls wireshark)
2275
2276                 install(CODE "execute_process(COMMAND
2277                         \"${QT_WINDEPLOYQT_EXECUTABLE}\"
2278                         --no-compiler-runtime
2279                         \"\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/Wireshark.exe\")"
2280                 )
2281
2282         endif(QT_WINDEPLOYQT_EXECUTABLE)
2283 endif()
2284
2285 # Common properties for CLI executables
2286 macro(set_extra_executable_properties _executable _folder)
2287         set_target_properties(${_executable} PROPERTIES
2288                 LINK_FLAGS "${WILDCARD_OBJ} ${WS_LINK_FLAGS}"
2289                 FOLDER ${_folder}
2290         )
2291
2292         set(PROGLIST ${PROGLIST} ${_executable})
2293
2294         if(ENABLE_APPLICATION_BUNDLE)
2295                 if(NOT CMAKE_CFG_INTDIR STREQUAL ".")
2296                         # Xcode
2297                         set_target_properties(${_executable} PROPERTIES
2298                                 RUNTIME_OUTPUT_DIRECTORY run/$<CONFIG>/Wireshark.app/Contents/MacOS
2299                         )
2300                 else ()
2301                         set_target_properties(${_executable} PROPERTIES
2302                                 RUNTIME_OUTPUT_DIRECTORY run/Wireshark.app/Contents/MacOS
2303                         )
2304                         # Add a wrapper script which runs each executable from the
2305                         # correct location. This adds convenience but makes debugging
2306                         # more difficult.
2307                         file(REMOVE ${CMAKE_BINARY_DIR}/run/${_executable})
2308                         file(WRITE ${CMAKE_BINARY_DIR}/run/${_executable} "#!/bin/sh\n")
2309                         file(APPEND ${CMAKE_BINARY_DIR}/run/${_executable} "exec ${CMAKE_BINARY_DIR}/run/Wireshark.app/Contents/MacOS/${_executable} \"\$\@\"\n")
2310                         execute_process(COMMAND chmod a+x ${CMAKE_BINARY_DIR}/run/${_executable})
2311                 endif()
2312         endif()
2313 endmacro()
2314
2315 register_tap_files(tshark-tap-register.c
2316         ${TSHARK_TAP_SRC}
2317 )
2318
2319 if(BUILD_tshark)
2320         set(tshark_LIBS
2321                 ui
2322                 capchild
2323                 caputils
2324                 wiretap
2325                 epan
2326                 ${VERSION_INFO_LIBS}
2327                 ${APPLE_CORE_FOUNDATION_LIBRARY}
2328                 ${APPLE_SYSTEM_CONFIGURATION_LIBRARY}
2329                 ${M_LIBRARIES}
2330         )
2331         set(tshark_FILES
2332                 cli_main.c
2333                 capture_opts.c
2334                 tshark-tap-register.c
2335                 tshark.c
2336                 ${TSHARK_TAP_SRC}
2337                 ${SHARK_COMMON_SRC}
2338         )
2339
2340         set_executable_resources(tshark "TShark" UNIQUE_RC)
2341         add_executable(tshark ${tshark_FILES})
2342         add_dependencies(tshark version)
2343         set_extra_executable_properties(tshark "Executables")
2344         target_link_libraries(tshark ${tshark_LIBS})
2345         install(TARGETS tshark RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2346 endif()
2347
2348 if(BUILD_tfshark)
2349         set(tfshark_LIBS
2350                 ui
2351                 wiretap
2352                 epan
2353                 ${VERSION_INFO_LIBS}
2354                 ${APPLE_CORE_FOUNDATION_LIBRARY}
2355                 ${APPLE_SYSTEM_CONFIGURATION_LIBRARY}
2356         )
2357         set(tfshark_FILES
2358                 cli_main.c
2359                 tfshark.c
2360                 ${TSHARK_TAP_SRC}
2361                 ${SHARK_COMMON_SRC}
2362         )
2363         set_executable_resources(tfshark "TFShark")
2364         add_executable(tfshark ${tfshark_FILES})
2365         add_dependencies(tfshark version)
2366         set_extra_executable_properties(tfshark "Executables")
2367         target_link_libraries(tfshark ${tfshark_LIBS})
2368         install(TARGETS tfshark RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2369 endif()
2370
2371 if(BUILD_rawshark AND PCAP_FOUND)
2372         set(rawshark_LIBS
2373                 caputils
2374                 ui
2375                 wiretap
2376                 epan
2377                 ${VERSION_INFO_LIBS}
2378                 ${APPLE_CORE_FOUNDATION_LIBRARY}
2379                 ${APPLE_SYSTEM_CONFIGURATION_LIBRARY}
2380         )
2381         set(rawshark_FILES
2382                 cli_main.c
2383                 ${SHARK_COMMON_SRC}
2384                 rawshark.c
2385         )
2386         set_executable_resources(rawshark "Rawshark")
2387         add_executable(rawshark ${rawshark_FILES})
2388         add_dependencies(rawshark version)
2389         set_extra_executable_properties(rawshark "Executables")
2390         target_link_libraries(rawshark ${rawshark_LIBS})
2391         install(TARGETS rawshark RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2392 endif()
2393
2394 if(BUILD_sharkd)
2395         set(sharkd_LIBS
2396                 ui
2397                 wscodecs
2398                 wiretap
2399                 epan
2400                 ${VERSION_INFO_LIBS}
2401                 ${APPLE_CORE_FOUNDATION_LIBRARY}
2402                 ${APPLE_SYSTEM_CONFIGURATION_LIBRARY}
2403         )
2404         set(sharkd_FILES
2405                 #
2406                 # XXX - currently doesn't work on Windows if it uses
2407                 # cli_main.c and has real_main().
2408                 #
2409                 sharkd.c
2410                 sharkd_daemon.c
2411                 sharkd_session.c
2412                 ${SHARK_COMMON_SRC}
2413         )
2414         set_executable_resources(sharkd "SharkD")
2415         add_executable(sharkd ${sharkd_FILES})
2416         add_dependencies(sharkd version)
2417         set_extra_executable_properties(sharkd "Executables")
2418         target_link_libraries(sharkd ${sharkd_LIBS})
2419         if(WIN32)
2420                 target_link_libraries(sharkd "ws2_32.lib")
2421         endif(WIN32)
2422
2423         install(TARGETS sharkd RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2424 endif()
2425
2426 if(BUILD_dftest)
2427         set(dftest_LIBS
2428                 ui
2429                 wiretap
2430                 epan
2431                 ${VERSION_INFO_LIBS}
2432         )
2433         set(dftest_FILES
2434                 dftest.c
2435         )
2436         add_executable(dftest ${dftest_FILES})
2437         add_dependencies(dftest version)
2438         set_extra_executable_properties(dftest "Tests")
2439         target_link_libraries(dftest ${dftest_LIBS})
2440 endif()
2441
2442 if(BUILD_randpkt)
2443         set(randpkt_LIBS
2444                 randpkt_core
2445                 ui
2446                 wiretap
2447                 wsutil
2448                 ${M_LIBRARIES}
2449                 ${PCAP_LIBRARIES}
2450                 ${CARES_LIBRARIES}
2451                 ${ZLIB_LIBRARIES}
2452         )
2453         set(randpkt_FILES
2454                 cli_main.c
2455                 randpkt.c
2456                 version_info.c
2457         )
2458         add_executable(randpkt ${randpkt_FILES})
2459         add_dependencies(randpkt version)
2460         set_extra_executable_properties(randpkt "Executables")
2461         target_link_libraries(randpkt ${randpkt_LIBS})
2462         install(TARGETS randpkt RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2463 endif()
2464
2465 if(BUILD_fuzzshark OR ENABLE_FUZZER OR OSS_FUZZ)
2466         add_subdirectory(fuzz)
2467 endif()
2468
2469 if(BUILD_text2pcap)
2470         set(text2pcap_LIBS
2471                 writecap
2472                 wsutil
2473                 ${M_LIBRARIES}
2474                 ${ZLIB_LIBRARIES}
2475         )
2476         set(text2pcap_FILES
2477                 cli_main.c
2478                 text2pcap.c
2479                 version_info.c
2480         )
2481         add_lex_files(text2pcap_LEX_FILES text2pcap_FILES
2482                 text2pcap-scanner.l
2483         )
2484         set_executable_resources(text2pcap "Text2pcap"
2485                 COPYRIGHT_INFO "2001 Ashok Narayanan <ashokn@cisco.com>")
2486         add_executable(text2pcap ${text2pcap_FILES})
2487         add_dependencies(text2pcap version)
2488         set_extra_executable_properties(text2pcap "Executables")
2489         target_link_libraries(text2pcap ${text2pcap_LIBS})
2490         install(TARGETS text2pcap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2491 endif()
2492
2493 if(BUILD_mergecap)
2494         set(mergecap_LIBS
2495                 ui
2496                 wiretap
2497                 ${ZLIB_LIBRARIES}
2498                 ${CMAKE_DL_LIBS}
2499         )
2500         set(mergecap_FILES
2501                 cli_main.c
2502                 mergecap.c
2503                 version_info.c
2504         )
2505         set_executable_resources(mergecap "Mergecap")
2506         add_executable(mergecap ${mergecap_FILES})
2507         add_dependencies(mergecap version)
2508         set_extra_executable_properties(mergecap "Executables")
2509         target_link_libraries(mergecap ${mergecap_LIBS})
2510         install(TARGETS mergecap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2511 endif()
2512
2513 if(BUILD_reordercap)
2514         set(reordercap_LIBS
2515                 ui
2516                 wiretap
2517                 ${ZLIB_LIBRARIES}
2518                 ${CMAKE_DL_LIBS}
2519         )
2520         set(reordercap_FILES
2521                 cli_main.c
2522                 reordercap.c
2523                 version_info.c
2524         )
2525         set_executable_resources(reordercap "Reordercap")
2526         add_executable(reordercap ${reordercap_FILES})
2527         add_dependencies(reordercap version)
2528         set_extra_executable_properties(reordercap "Executables")
2529         target_link_libraries(reordercap ${reordercap_LIBS})
2530         install(TARGETS reordercap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2531 endif()
2532
2533 if(BUILD_capinfos)
2534         set(capinfos_LIBS
2535                 ui
2536                 wiretap
2537                 wsutil
2538                 ${ZLIB_LIBRARIES}
2539                 ${GCRYPT_LIBRARIES}
2540                 ${CMAKE_DL_LIBS}
2541         )
2542         set(capinfos_FILES
2543                 cli_main.c
2544                 capinfos.c
2545                 version_info.c
2546         )
2547         set_executable_resources(capinfos "Capinfos")
2548         add_executable(capinfos ${capinfos_FILES})
2549         add_dependencies(capinfos version)
2550         set_extra_executable_properties(capinfos "Executables")
2551         target_link_libraries(capinfos ${capinfos_LIBS})
2552         install(TARGETS capinfos RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2553 endif()
2554
2555 if(BUILD_captype)
2556         set(captype_LIBS
2557                 ui
2558                 wiretap
2559                 wsutil
2560                 ${ZLIB_LIBRARIES}
2561                 ${CMAKE_DL_LIBS}
2562         )
2563         set(captype_FILES
2564                 cli_main.c
2565                 captype.c
2566                 version_info.c
2567         )
2568         set_executable_resources(captype "Captype")
2569         add_executable(captype ${captype_FILES})
2570         add_dependencies(captype version)
2571         set_extra_executable_properties(captype "Executables")
2572         target_link_libraries(captype ${captype_LIBS})
2573         install(TARGETS captype RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2574 endif()
2575
2576 if(BUILD_editcap)
2577         set(editcap_LIBS
2578                 ui
2579                 wiretap
2580                 ${ZLIB_LIBRARIES}
2581                 ${GCRYPT_LIBRARIES}
2582                 ${CMAKE_DL_LIBS}
2583         )
2584         set(editcap_FILES
2585                 cli_main.c
2586                 editcap.c
2587                 version_info.c
2588         )
2589         set_executable_resources(editcap "Editcap")
2590         add_executable(editcap ${editcap_FILES})
2591         add_dependencies(editcap version)
2592         set_extra_executable_properties(editcap "Executables")
2593         target_link_libraries(editcap ${editcap_LIBS})
2594         install(TARGETS editcap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2595 endif()
2596
2597 if(BUILD_dumpcap AND PCAP_FOUND)
2598         set(dumpcap_LIBS
2599                 writecap
2600                 wsutil
2601                 caputils
2602                 ui
2603                 ${PCAP_LIBRARIES}
2604                 ${CAP_LIBRARIES}
2605                 ${GLIB2_LIBRARIES}
2606                 ${GTHREAD2_LIBRARIES}
2607                 ${ZLIB_LIBRARIES}
2608                 ${APPLE_CORE_FOUNDATION_LIBRARY}
2609                 ${APPLE_SYSTEM_CONFIGURATION_LIBRARY}
2610                 ${NL_LIBRARIES}
2611         )
2612         set(dumpcap_FILES
2613                 cli_main.c
2614                 capture_opts.c
2615                 dumpcap.c
2616                 ringbuffer.c
2617                 sync_pipe_write.c
2618                 version_info.c
2619         )
2620         set_executable_resources(dumpcap "Dumpcap" UNIQUE_RC)
2621         add_executable(dumpcap ${dumpcap_FILES})
2622         add_dependencies(dumpcap version)
2623         set_extra_executable_properties(dumpcap "Executables")
2624         target_link_libraries(dumpcap ${dumpcap_LIBS})
2625         install(TARGETS dumpcap
2626                         RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
2627                         PERMISSIONS ${DUMPCAP_SETUID}
2628                                 OWNER_READ OWNER_WRITE OWNER_EXECUTE
2629                                 GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
2630         )
2631         if(ENABLE_DUMPCAP_GROUP)
2632                 install(CODE "execute_process(COMMAND chgrp ${DUMPCAP_INSTALL_GROUP} ${CMAKE_INSTALL_FULL_BINDIR}/dumpcap)")
2633                 install(CODE "execute_process(COMMAND chmod o-x ${CMAKE_INSTALL_FULL_BINDIR}/dumpcap)")
2634         endif()
2635         if(DUMPCAP_INSTALL_OPTION STREQUAL "capabilities")
2636                 install( CODE "execute_process(
2637                         COMMAND
2638                                 ${SETCAP_EXECUTABLE}
2639                                 cap_net_raw,cap_net_admin+ep
2640                                 ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/dumpcap${CMAKE_EXECUTABLE_SUFFIX}
2641                         RESULT_VARIABLE
2642                                 _SETCAP_RESULT
2643                         )
2644                         if( _SETCAP_RESULT )
2645                                 message( WARNING \"setcap failed (${_SETCAP_RESULT}).\")
2646                         endif()"
2647                 )
2648         endif()
2649 endif()
2650
2651 # We have two idl2wrs utilities: this and the CORBA version in tools.
2652 # We probably shouldn't do that.
2653 if(BUILD_dcerpcidl2wrs)
2654         set(idl2wrs_LIBS
2655                 ${GLIB2_LIBRARIES}
2656                 wsutil
2657         )
2658         set(idl2wrs_FILES
2659                 epan/dissectors/dcerpc/idl2wrs.c
2660         )
2661
2662         add_executable(idl2wrs ${idl2wrs_FILES})
2663         set_target_properties(idl2wrs PROPERTIES FOLDER "Executables")
2664         set_extra_executable_properties(idl2wrs "Executables")
2665         target_link_libraries(idl2wrs ${idl2wrs_LIBS})
2666         install(TARGETS idl2wrs RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2667 endif()
2668
2669 if (WIN32)
2670         find_package( MSVC_REDIST )
2671
2672         # Must come after executable targets are defined.
2673         find_package( NSIS )
2674
2675         if(MAKENSIS_EXECUTABLE)
2676                 add_subdirectory( packaging/nsis EXCLUDE_FROM_ALL )
2677                 ADD_NSIS_UNINSTALLER_TARGET()
2678                 ADD_NSIS_PACKAGE_TARGET()
2679         endif()
2680
2681         find_package( WiX )
2682
2683         if(WIX_CANDLE_EXECUTABLE)
2684                 add_subdirectory( packaging/wix EXCLUDE_FROM_ALL )
2685                 ADD_WIX_PACKAGE_TARGET()
2686         endif()
2687
2688         find_package( PortableApps )
2689         if(PORTABLEAPPS_LAUNCHER_GENERATOR_EXECUTABLE AND PORTABLEAPPS_INSTALLER_EXECUTABLE)
2690                 add_subdirectory( packaging/portableapps EXCLUDE_FROM_ALL )
2691                 ADD_PORTABLEAPPS_PACKAGE_TARGET()
2692         endif()
2693 endif()
2694
2695 if (MAXMINDDB_FOUND)
2696         set(mmdbresolve_LIBS
2697                 # Note: libmaxminddb is not GPL-2 compatible.
2698                 ${MAXMINDDB_LIBRARY}
2699         )
2700         set(mmdbresolve_FILES
2701                 mmdbresolve.c
2702         )
2703         add_executable(mmdbresolve ${mmdbresolve_FILES})
2704         set_extra_executable_properties(mmdbresolve "Executables")
2705         target_link_libraries(mmdbresolve ${mmdbresolve_LIBS})
2706         target_include_directories(mmdbresolve PUBLIC ${MAXMINDDB_INCLUDE_DIR})
2707         install(TARGETS mmdbresolve RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2708 endif()
2709
2710 if(ENABLE_APPLICATION_BUNDLE)
2711         add_custom_target(app_bundle)
2712         set_target_properties(app_bundle PROPERTIES FOLDER "Copy Tasks")
2713         add_custom_command(TARGET app_bundle
2714                 POST_BUILD
2715                 COMMAND "${CMAKE_BINARY_DIR}/packaging/macosx/osx-app.sh"
2716                 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/run"
2717         )
2718         add_dependencies(app_bundle ${PROGLIST})
2719
2720         add_custom_target(dmg_package_prep DEPENDS app_bundle)
2721
2722         ADD_CUSTOM_COMMAND(
2723                 OUTPUT ${CMAKE_BINARY_DIR}/packaging/macosx/PkgInfo
2724                 COMMAND ${CMAKE_COMMAND} -E echo APPLWshk > ${CMAKE_BINARY_DIR}/packaging/macosx/PkgInfo
2725         )
2726
2727         ADD_CUSTOM_TARGET( dmg_package
2728                 COMMAND ${CMAKE_COMMAND} -E copy_directory
2729                                         ${CMAKE_SOURCE_DIR}/packaging/macosx/ChmodBPF
2730                                         ${CMAKE_BINARY_DIR}/run/ChmodBPF
2731                 COMMAND ${CMAKE_COMMAND} -E copy_directory
2732                                         ${CMAKE_SOURCE_DIR}/packaging/macosx/Scripts
2733                                         ${CMAKE_BINARY_DIR}/run/Scripts
2734                 COMMAND ${CMAKE_COMMAND} -E copy_directory
2735                                         ${CMAKE_SOURCE_DIR}/packaging/macosx/utility-launcher
2736                                         ${CMAKE_BINARY_DIR}/run/utility-launcher
2737                 COMMAND ${CMAKE_COMMAND} -E copy_if_different
2738                                         ${CMAKE_SOURCE_DIR}/COPYING
2739                                         ${CMAKE_BINARY_DIR}/run/COPYING.txt
2740                 COMMAND ${CMAKE_COMMAND} -E copy_directory
2741                                         ${CMAKE_SOURCE_DIR}/packaging/macosx/Wireshark_package.pmdoc
2742                                         ${CMAKE_BINARY_DIR}/run/Wireshark_package.pmdoc
2743                 COMMAND ${CMAKE_COMMAND} -E copy_if_different
2744                                         ${CMAKE_BINARY_DIR}/packaging/macosx/Wireshark_package.pmdoc/index.xml
2745                                         ${CMAKE_BINARY_DIR}/run/Wireshark_package.pmdoc/index.xml
2746                 COMMAND ${CMAKE_COMMAND} -E copy_if_different
2747                                         ${CMAKE_SOURCE_DIR}/packaging/macosx/dmg_background.png
2748                                         ${CMAKE_BINARY_DIR}/run/dmg_background.png
2749                 COMMAND bash -x ${CMAKE_BINARY_DIR}/packaging/macosx/osx-dmg.sh
2750                         --source-directory ${CMAKE_SOURCE_DIR}/packaging/macosx
2751                 # Unlike nsis_package_prep + nsis_package, we can add a direct
2752                 # dependency here.
2753                 DEPENDS dmg_package_prep
2754                 # We create Wireshark.app in "run". Do our work there.
2755                 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/run
2756         )
2757
2758 endif()
2759
2760 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
2761         find_program(RPMBUILD_EXECUTABLE rpmbuild)
2762         find_program(DPKG_BUILDPACKAGE_EXECUTABLE dpkg-buildpackage)
2763         find_program(GIT_EXECUTABLE git)
2764         # Should we add appimaged's monitored directories
2765         # as HINTS?
2766         # https://github.com/AppImage/appimaged
2767         find_program(LINUXDEPLOYQT_EXECUTABLE linuxdeployqt)
2768         find_program(APPIMAGETOOL_EXECUTABLE appimagetool)
2769 endif()
2770
2771 function(_SET_GITVERSION_CMAKE_VARIABLE OUTPUT_VARIABLE)
2772         # Load version string and write it to a cmake variable so it can be accessed from cmake.
2773         FILE(READ "${CMAKE_CURRENT_BINARY_DIR}/version.h" VERSION_H_FILE_CONTENT)
2774         string(REPLACE "\n" "" VERSION_H_FILE_CONTENT ${VERSION_H_FILE_CONTENT})
2775         #define VCSVERSION "v2.9.0rc0-305-gb8e8aa87"
2776         string(SUBSTRING "${VERSION_H_FILE_CONTENT}" 21 -1 VERSION_STRING)
2777         STRING(REGEX REPLACE "\"" "" VERSION_STRING "${VERSION_STRING}")
2778         MESSAGE(STATUS "Version string created from version.h: ${VERSION_STRING}")
2779         SET(${OUTPUT_VARIABLE} "${VERSION_STRING}"  CACHE INTERNAL "${OUTPUT_VARIABLE}")
2780 endfunction(_SET_GITVERSION_CMAKE_VARIABLE)
2781
2782
2783 if(RPMBUILD_EXECUTABLE)
2784         foreach(_rpm_dir BUILD RPMS SOURCES SPECS SRPMS)
2785                 file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/packaging/rpm/${_rpm_dir}")
2786         endforeach()
2787
2788         set(_rpmbuild_with_args)
2789         if(CMAKE_GENERATOR STREQUAL "Ninja")
2790                 list(APPEND _rpmbuild_with_args --with ninja)
2791         endif()
2792         if(CCACHE_EXECUTABLE)
2793                 list(APPEND _rpmbuild_with_args --with ccache)
2794         endif()
2795         if (BUILD_wireshark)
2796                 list(APPEND _rpmbuild_with_args --with qt5)
2797         endif()
2798         if (MAXMINDDB_FOUND)
2799                 list(APPEND _rpmbuild_with_args --with mmdbresolve)
2800         endif()
2801         if (LUA_FOUND)
2802                 list(APPEND _rpmbuild_with_args --with lua)
2803         endif()
2804         if (LZ4_FOUND AND SNAPPY_FOUND)
2805                 list(APPEND _rpmbuild_with_args --with lz4_and_snappy)
2806         endif()
2807         if (CARES_FOUND)
2808                 list(APPEND _rpmbuild_with_args --with c_ares)
2809         endif()
2810         if (SPANDSP_FOUND)
2811                 list(APPEND _rpmbuild_with_args --with spandsp)
2812         endif()
2813         if (BCG729_FOUND)
2814                 list(APPEND _rpmbuild_with_args --with bcg729)
2815         endif()
2816         if (LIBXML2_FOUND)
2817                 list(APPEND _rpmbuild_with_args --with libxml2)
2818         endif()
2819         if (NGHTTP2_FOUND)
2820                 list(APPEND _rpmbuild_with_args --with nghttp2)
2821         endif()
2822         if (SYSTEMD_FOUND)
2823                 list(APPEND _rpmbuild_with_args --with sdjournal)
2824         endif()
2825
2826         execute_process(
2827                 COMMAND ${PERL_EXECUTABLE}
2828                         ${CMAKE_SOURCE_DIR}/make-version.pl
2829                         ${CMAKE_SOURCE_DIR}
2830         )
2831
2832         _SET_GITVERSION_CMAKE_VARIABLE(_git_description)
2833
2834         if (NOT _git_description)
2835                 # We're building the rpm outside the source. Guess the version from the dirname.
2836                 get_filename_component(CMAKE_SOURCE_DIR_NAME ${CMAKE_SOURCE_DIR} NAME)
2837                 # XXX this assumes the directory to start with "wireshark-"
2838                 string(SUBSTRING "${CMAKE_SOURCE_DIR_NAME}" 10 -1 _git_description)
2839         endif()
2840         string(REPLACE "-" "_" RPM_VERSION "${_git_description}")
2841         configure_file(packaging/rpm/wireshark.spec.in ${CMAKE_BINARY_DIR}/packaging/rpm/SPECS/wireshark.spec)
2842
2843         # XXX Replace with the "dist" target?
2844         set(_export_tarball "${CPACK_PACKAGE_NAME}-${_git_description}.tar.xz")
2845         add_custom_command(
2846                 OUTPUT "${CMAKE_BINARY_DIR}/packaging/rpm/SOURCES/${_export_tarball}"
2847                 COMMAND ./tools/git-export-release.sh
2848                         -d "${CMAKE_BINARY_DIR}/packaging/rpm/SOURCES"
2849                         "${_git_description}"
2850                 # XXX Add an option to git-export-release.sh to write to a
2851                 # specific directory so that we can get rid of `ln` below.
2852                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
2853         )
2854         add_custom_target(rpm-package
2855                 COMMAND ${RPMBUILD_EXECUTABLE}
2856                         --define "_topdir ${CMAKE_BINARY_DIR}/packaging/rpm"
2857                         --define "_prefix ${CMAKE_INSTALL_PREFIX}"
2858                         ${_rpmbuild_with_args}
2859                         --clean -ba SPECS/wireshark.spec
2860                 DEPENDS "${CMAKE_BINARY_DIR}/packaging/rpm/SOURCES/${_export_tarball}"
2861                 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/packaging/rpm"
2862                 COMMENT "Create a tarball from the current git commit."
2863         )
2864 endif()
2865
2866 if(DPKG_BUILDPACKAGE_EXECUTABLE)
2867         add_custom_target(deb-package
2868                 COMMAND ${DPKG_BUILDPACKAGE_EXECUTABLE} -us -uc
2869                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
2870         )
2871 endif()
2872
2873 if(LINUXDEPLOYQT_EXECUTABLE AND APPIMAGETOOL_EXECUTALBE)
2874         # The documentation at https://github.com/probonopd/linuxdeployqt
2875         # says that you need to set CMAKE_BUILD_TYPE=Release and
2876         # CMAKE_INSTALL_PREFIX=/usr. I (gcc) also had to set
2877         # CMAKE_INSTALL_LIBDIR=/usr/lib.
2878         if (CMAKE_BUILD_TYPE STREQUAL "Release" AND CMAKE_INSTALL_PREFIX STREQUAL "/usr" AND CMAKE_INSTALL_LIBDIR STREQUAL "/usr/lib" )
2879                 add_custom_target(appimage-prerequisites)
2880                 add_dependencies(appimage-prerequisites ${PROGLIST})
2881         else()
2882                 add_custom_target(appimage-prerequisites
2883                         COMMAND echo "CMAKE_BUILD_TYPE isn't Release or CMAKE_INSTALL_PREFIX isn't /usr or CMAKE_INSTALL_LIBDIR isn't /usr/lib."
2884                         COMMAND false
2885                 )
2886         endif()
2887         set (_ai_appdir "${CMAKE_BINARY_DIR}/packaging/appimage/appdir")
2888         add_custom_target(appimage-appdir
2889                 COMMAND ${CMAKE_COMMAND} -E make_directory "${_ai_appdir}"
2890                 COMMAND env DESTDIR=${_ai_appdir}
2891                         ${CMAKE_COMMAND} --build . --target install
2892                 DEPENDS appimage-prerequisites
2893         )
2894         set(_exe_args)
2895         foreach(_prog ${PROGLIST})
2896                 # XXX Is this the correct path?
2897                 list(APPEND _exe_args "-executable=${_ai_appdir}/usr/bin/${_prog}")
2898         endforeach()
2899         # linuxdeployqt currently clobbers AppRun:
2900         # https://github.com/probonopd/linuxdeployqt/issues/159
2901         # When that's fixed we will no longer need appimagetool. Until
2902         # then, we need to prep and package in two steps:
2903         # https://github.com/probonopd/linuxdeployqt/wiki/Custom-wrapper-script-instead-of-AppRun
2904         add_custom_target(appimage-prep
2905                 COMMAND ${LINUXDEPLOYQT_EXECUTABLE}
2906                         "${_ai_appdir}/usr/share/applications/wireshark.desktop"
2907                         ${_exe_args}
2908                 COMMAND rm -f "${_ai_appdir}/AppRun"
2909                 COMMAND install
2910                         "${CMAKE_SOURCE_DIR}/packaging/appimage/AppRun"
2911                         "${_ai_appdir}/AppRun"
2912                 DEPENDS appimage-appdir
2913         )
2914         add_custom_target(appimage
2915                 COMMAND env VERSION=${PROJECT_VERSION} ${APPIMAGETOOL_EXECUTABLE} appdir
2916                 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/packaging/appimage"
2917                 DEPENDS appimage-prep
2918         )
2919 endif()
2920
2921 set(CLEAN_C_FILES
2922         ${dumpcap_FILES}
2923         ${wireshark_FILES}
2924         ${tshark_FILES}
2925         ${rawshark_FILES}
2926         ${dftest_FILES}
2927         ${randpkt_FILES}
2928         ${randpktdump_FILES}
2929         ${udpdump_FILES}
2930         ${text2pcap_FILES}
2931         ${mergecap_FILES}
2932         ${capinfos_FILES}
2933         ${captype_FILES}
2934         ${editcap_FILES}
2935         ${idl2wrs_FILES}
2936         ${mmdbresolve_FILES}
2937 )
2938
2939 # Make sure we don't pass /WX to rc.exe. Rc doesn't have a /WX flag,
2940 # but it does have /W (warn about invalid code pages) and /X (ignore
2941 # the INCLUDE environment variable).
2942 # This should apparently be handled for us via CMAKE_RC_FLAG_REGEX
2943 # in CMakeRCInformation.cmake but that doesn't appear to work.
2944 if (WIN32)
2945         list(FILTER CLEAN_C_FILES EXCLUDE REGEX ".*\\.rc")
2946 endif (WIN32)
2947
2948 set_source_files_properties(
2949         ${CLEAN_C_FILES}
2950         PROPERTIES
2951         COMPILE_FLAGS "${WERROR_COMMON_FLAGS}"
2952 )
2953
2954 install(
2955         FILES
2956                 ${INSTALL_FILES}
2957         PERMISSIONS
2958                 OWNER_WRITE OWNER_READ
2959                 GROUP_READ
2960                 WORLD_READ
2961         DESTINATION
2962                 ${CMAKE_INSTALL_DATADIR}
2963 )
2964
2965 set(SHARK_PUBLIC_HEADERS
2966         cfile.h
2967         cli_main.h
2968         file.h
2969         globals.h
2970         log.h
2971         ws_attributes.h
2972         ws_compiler_tests.h
2973         ws_diag_control.h
2974         ws_symbol_export.h
2975         version_info.h
2976         ${CMAKE_BINARY_DIR}/ws_version.h
2977 )
2978
2979 install(FILES ${SHARK_PUBLIC_HEADERS}
2980         DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CPACK_PACKAGE_NAME}
2981 )
2982
2983 # Install icons and other desktop files for Freedesktop.org-compliant desktops.
2984 if((BUILD_wireshark AND QT_FOUND) AND NOT (WIN32 OR APPLE))
2985         install(FILES wireshark-mime-package.xml
2986                 DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/mime/packages"
2987                 RENAME wireshark.xml
2988         )
2989         install(FILES wireshark.appdata.xml
2990                 DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/appdata"
2991         )
2992         if(BUILD_wireshark AND QT_FOUND)
2993                 install(FILES wireshark.desktop
2994                         DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications")
2995         endif()
2996         foreach(size 16 24 32 48 64 128 256)
2997                 install(FILES image/wsicon${size}.png
2998                         DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/${size}x${size}/apps"
2999                         RENAME wireshark.png)
3000                 install(FILES image/WiresharkDoc-${size}.png
3001                         DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/${size}x${size}/mimetypes"
3002                         RENAME application-wireshark-doc.png)
3003         endforeach()
3004         install(FILES image/wsicon.svg
3005                 DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/scalable/apps"
3006                 RENAME wireshark.svg)
3007 endif()
3008
3009 install(
3010         FILES
3011                 "${CMAKE_BINARY_DIR}/wireshark.pc"
3012         DESTINATION
3013                 ${CMAKE_INSTALL_LIBDIR}/pkgconfig
3014 )
3015
3016 install(
3017         DIRECTORY
3018                 ${INSTALL_DIRS}
3019         DESTINATION
3020                 ${CMAKE_INSTALL_DATADIR}
3021         FILE_PERMISSIONS
3022                 OWNER_WRITE OWNER_READ
3023                 GROUP_READ
3024                 WORLD_READ
3025         DIRECTORY_PERMISSIONS
3026                 OWNER_EXECUTE OWNER_WRITE OWNER_READ
3027                 GROUP_EXECUTE GROUP_READ
3028                 WORLD_EXECUTE WORLD_READ
3029         PATTERN ".git" EXCLUDE
3030         PATTERN ".svn" EXCLUDE
3031         PATTERN "Makefile.*" EXCLUDE
3032 )
3033
3034 if(WIN32)
3035         # Note: CMake export mechanism misbehaves with a '.' in the
3036         # path (incorrect relative path computation).
3037         set(WIRESHARK_INSTALL_CMAKEDIR "cmake")
3038 else()
3039         set(WIRESHARK_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/${CPACK_PACKAGE_NAME}/cmake")
3040 endif()
3041
3042 install(
3043         FILES
3044                 ${CMAKE_MODULE_PATH}/FindGLIB2.cmake
3045                 ${CMAKE_MODULE_PATH}/FindWSWinLibs.cmake
3046                 ${CMAKE_MODULE_PATH}/UseAsn2Wrs.cmake
3047                 ${CMAKE_MODULE_PATH}/LocatePythonModule.cmake
3048                 ${CMAKE_MODULE_PATH}/UseMakePluginReg.cmake
3049         DESTINATION
3050                 ${WIRESHARK_INSTALL_CMAKEDIR}
3051 )
3052
3053 include(CMakePackageConfigHelpers)
3054
3055 configure_package_config_file(WiresharkConfig.cmake.in
3056         ${CMAKE_BINARY_DIR}/WiresharkConfig.cmake
3057         INSTALL_DESTINATION ${WIRESHARK_INSTALL_CMAKEDIR}
3058         PATH_VARS
3059                 CMAKE_INSTALL_LIBDIR
3060                 CMAKE_INSTALL_INCLUDEDIR
3061                 PLUGIN_INSTALL_VERSION_LIBDIR
3062                 EXTCAP_INSTALL_LIBDIR
3063 )
3064
3065 write_basic_package_version_file(
3066         ${CMAKE_BINARY_DIR}/WiresharkConfigVersion.cmake
3067         COMPATIBILITY AnyNewerVersion
3068 )
3069
3070 install(
3071         FILES
3072                 ${CMAKE_BINARY_DIR}/WiresharkConfig.cmake
3073                 ${CMAKE_BINARY_DIR}/WiresharkConfigVersion.cmake
3074         DESTINATION
3075                 ${WIRESHARK_INSTALL_CMAKEDIR}
3076 )
3077
3078 install(EXPORT WiresharkTargets
3079         DESTINATION ${WIRESHARK_INSTALL_CMAKEDIR}
3080 )
3081
3082 if (DOXYGEN_EXECUTABLE)
3083         # API reference
3084         # We don't have a good way of tracking dependencies, so we simply
3085         # recreate the whole thing from scratch each time.
3086         add_custom_target(wsar_html
3087                 COMMAND ${CMAKE_COMMAND} -E remove_directory wsar_html
3088                 COMMAND ${DOXYGEN_EXECUTABLE} doxygen.cfg
3089         )
3090
3091         add_custom_target(wsar_html_zip
3092                 COMMAND ${CMAKE_COMMAND} -E tar "cfv" "wsar_html.zip" --format=zip wsar_html
3093                 DEPENDS wsar_html
3094         )
3095         set_target_properties(wsar_html wsar_html_zip PROPERTIES
3096                 FOLDER "Docs"
3097                 EXCLUDE_FROM_DEFAULT_BUILD True
3098         )
3099 endif(DOXYGEN_EXECUTABLE)
3100
3101 add_custom_target(test-programs
3102         DEPENDS exntest
3103                 oids_test
3104                 reassemble_test
3105                 tvbtest
3106                 wmem_test
3107         COMMENT "Building unit test programs and wrapper"
3108 )
3109 set_target_properties(test-programs PROPERTIES
3110         FOLDER "Tests"
3111         EXCLUDE_FROM_DEFAULT_BUILD True
3112 )
3113
3114 # Test suites
3115 enable_testing()
3116 # We could try to build this list dynamically, but given that we tend to
3117 # go years between adding suites just run
3118 #     test/test.py --list-groups | sort
3119 # and paste the output here.
3120 set(_test_group_list
3121         suite_capture
3122         suite_clopts
3123         suite_decryption
3124         suite_dfilter.group_bytes_ether
3125         suite_dfilter.group_bytes_ipv6
3126         suite_dfilter.group_bytes_type
3127         suite_dfilter.group_double
3128         suite_dfilter.group_integer
3129         suite_dfilter.group_integer_1byte
3130         suite_dfilter.group_ipv4
3131         suite_dfilter.group_membership
3132         suite_dfilter.group_range_method
3133         suite_dfilter.group_scanner
3134         suite_dfilter.group_string_type
3135         suite_dfilter.group_stringz
3136         suite_dfilter.group_time_relative
3137         suite_dfilter.group_time_type
3138         suite_dfilter.group_tvb
3139         suite_dfilter.group_uint64
3140         suite_dissection
3141         suite_dissectors.group_asterix
3142         suite_fileformats
3143         suite_follow
3144         suite_io
3145         suite_mergecap
3146         suite_nameres
3147         suite_outputformats
3148         suite_text2pcap
3149         suite_sharkd
3150         suite_unittests
3151         suite_wslua
3152 )
3153
3154 # We don't currently handle spaces in arguments. On Windows this
3155 # means that you will probably have to pass in an interface index
3156 # instead of a name.
3157 set(TEST_EXTRA_ARGS "" CACHE STRING "Extra arguments to pass to test/test.py")
3158 separate_arguments(TEST_EXTRA_ARGS)
3159
3160 foreach(_group_name ${_test_group_list})
3161         add_test(
3162                 NAME ${_group_name}
3163                 COMMAND ${CMAKE_COMMAND} -E env PYTHONIOENCODING=UTF-8
3164                         ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/test/test.py
3165                         --verbose
3166                         --program-path ${WS_PROGRAM_PATH}
3167                         ${TEST_EXTRA_ARGS}
3168                         ${_group_name}
3169                 )
3170         set_tests_properties(${_group_name} PROPERTIES TIMEOUT 600)
3171 endforeach()
3172
3173 # Make it possible to run pytest without passing the full path as argument.
3174 if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
3175         file(READ "${CMAKE_CURRENT_SOURCE_DIR}/pytest.ini" pytest_ini)
3176         string(REGEX REPLACE "\naddopts = ([^\n]+)"
3177                 "\naddopts = ${CMAKE_CURRENT_SOURCE_DIR}/test \\1"
3178                 pytest_ini "${pytest_ini}")
3179         file(WRITE "${CMAKE_BINARY_DIR}/pytest.ini" "${pytest_ini}")
3180 endif()
3181
3182 if (GIT_EXECUTABLE)
3183         # Update AUTHORS file with entries from git shortlog
3184         add_custom_target(
3185                 gen-authors
3186                 COMMAND ${PERL_EXECUTABLE} tools/generate_authors.pl AUTHORS.src > AUTHORS
3187                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
3188         )
3189 else (GIT_EXECUTABLE)
3190         add_custom_target( gen-authors COMMAND ${CMAKE_COMMAND} -E echo "Git not found." )
3191 endif (GIT_EXECUTABLE)
3192 set_target_properties(gen-authors PROPERTIES FOLDER "Docs")
3193
3194 if (WIN32)
3195         file (TO_NATIVE_PATH ${CMAKE_SOURCE_DIR}/tools/Get-HardenFlags.ps1 _win_harden_flags)
3196         add_custom_target(hardening-check
3197                 COMMAND ${POWERSHELL_COMMAND} "${_win_harden_flags}" "${_dll_output_dir_win}"
3198                 DEPENDS ${PROGLIST}
3199                 COMMENT "Checking binaries for security features"
3200         )
3201         set_target_properties(hardening-check PROPERTIES FOLDER "Tests")
3202 else ()
3203         find_program(HARDENING_CHECK_EXECUTABLE hardening-check
3204                 DOC "Path to the hardening-check utility."
3205         )
3206         if(HARDENING_CHECK_EXECUTABLE)
3207                 foreach(_prog ${PROGLIST})
3208                         get_target_property(_prog_dir ${_prog} RUNTIME_OUTPUT_DIRECTORY)
3209                         if(NOT _prog_dir)
3210                                 set(_prog_dir "${CMAKE_BINARY_DIR}/run")
3211                         endif()
3212                         set(_prog_paths ${_prog_paths} "${_prog_dir}/${_prog}")
3213                 endforeach()
3214                 add_custom_target(hardening-check
3215                         COMMAND ${HARDENING_CHECK_EXECUTABLE} ${_prog_paths}
3216                         DEPENDS ${PROGLIST}
3217                         COMMENT "Checking binaries for security features"
3218                 )
3219         endif()
3220 endif()
3221
3222 CHECKAPI(
3223         NAME
3224           main
3225         SWITCHES
3226           -build
3227         SOURCES
3228           ${WIRESHARK_SRC}
3229           ${TSHARK_TAP_SRC}
3230 )
3231
3232 find_program(SHELLCHECK_EXECUTABLE shellcheck
3233         DOC "Path to the shellcheck utility."
3234 )
3235 if(SHELLCHECK_EXECUTABLE)
3236         add_custom_target(shellcheck)
3237         set_target_properties(shellcheck PROPERTIES FOLDER "Tests")
3238         # --external-sources requires 0.4.0 or later.
3239         add_custom_command(TARGET shellcheck POST_BUILD
3240                 COMMAND shellcheck --external-sources
3241                         image/stock_icons/svg-to-png.sh
3242                         packaging/appimage/AppRun
3243                         packaging/macosx/osx-app.sh.in
3244                         packaging/macosx/osx-dmg.sh.in
3245                         tools/compress-pngs.sh
3246                         tools/debian-setup.sh
3247                         tools/git-export-release.sh
3248                         tools/fuzz-test.sh
3249                         tools/gen-bugnote
3250                         tools/pre-commit
3251                         tools/randpkt-test.sh
3252                         tools/release-update-debian-soversions.sh
3253                         tools/test-captures.sh
3254                         tools/update-tx
3255                         tools/valgrind-wireshark.sh
3256                 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
3257         )
3258 endif()
3259
3260 #
3261 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
3262 #
3263 # Local variables:
3264 # c-basic-offset: 8
3265 # tab-width: 8
3266 # indent-tabs-mode: t
3267 # End:
3268 #
3269 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
3270 # :indentSize=8:tabSize=8:noTabs=false:
3271 #