TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / cmake / modules / FindSinsp.cmake
blob9b1d71c0cb113ed2dc93706aa9133d53f4aa89d3
2 # - Find libsinsp and libscap
3 # Find libsinsp and libscap includes and libraries.
4 # Adapted from FindZSTD.cmake.
6 # This module will look for libsinsp and libscap using pkg-config. If that
7 # fails, it will search ${SINSP_INCLUDEDIR} and ${SINSP_HINTS}/include
8 # for the libsinsp and libscap include directory and ${SINSP_LIBDIR} and
9 # ${SINSP_HINTS}/lib for the libsinsp and libscap libraries.
11 # It will set the following variables:
13 #  SINSP_FOUND          - True if libsinsp found.
14 #  SINSP_INCLUDE_DIRS   - Where to find sinsp.h, scap.h, etc.
15 #  SINSP_LINK_LIBRARIES - List of libraries when using libsinsp.
17 # You must manually set the following variables:
18 #  FALCO_PLUGINS        - Paths to plugins built from https://github.com/falcosecurity/plugins/.
20 # To do:
21 #  SINSP_DLL_DIR        - (Windows) Path to the libsinsp and libscap DLLs
22 #  SINSP_DLL            - (Windows) Name of the libsinsp and libscap DLLs
24 include( FindWSWinLibs )
25 FindWSWinLibs( "falcosecurity-libs-.*" SINSP_HINTS )
27 include(CMakeDependentOption)
29 if( NOT USE_REPOSITORY)
30   find_package(PkgConfig)
31   pkg_check_modules(SINSP libsinsp)
32 endif()
34 # Include both legacy (#include <sinsp.h>) and current (#include <libsinsp/sinsp.h>) paths for now.
35 if(NOT SINSP_FOUND)
36   # We're likely building on Windows since libsinsp and libscap ship with .pc modules.
37   # We might want to dispense with this and use pkg_check_modules exclusively, but that
38   # would require adding pkg-config to the Windows build and ensuring that the .pc files
39   # we ship are discoverable and valid.
40   find_path(_sinsp_include_dirs NO_CACHE
41     NAMES libsinsp/sinsp.h
42     HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
43     PATH_SUFFIXES falcosecurity
44     /usr/include
45     /usr/local/include
46   )
47   if(_sinsp_include_dirs)
48     list(APPEND _sinsp_include_dirs ${_sinsp_include_dirs}/libsinsp)
49   endif()
51   find_path(_scap_include_dir NO_CACHE
52     NAMES scap.h
53     HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
54     PATH_SUFFIXES falcosecurity/libscap
55     /usr/include
56     /usr/local/include
57   )
58   if(_scap_include_dir)
59     list(APPEND _sinsp_include_dirs ${_scap_include_dir})
60   endif()
61   unset(_scap_include_dir)
63   find_library(_sinsp_link_libs NO_CACHE
64     NAMES sinsp
65     HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib"
66     PATHS falcosecurity
67     /usr/lib
68     /usr/local/lib
69   )
71   if (NOT SINSP_VERSION)
72     find_file(_sinsp_pkgconfig NO_CACHE
73       libsinsp.pc
74       PATHS "${SINSP_LIBDIR}/pkgconfig" "${SINSP_HINTS}/lib/pkgconfig"
75     )
76     if (_sinsp_pkgconfig)
77       file(READ ${_sinsp_pkgconfig} _sinsp_pkgconfig_contents)
78       string(REGEX MATCH "Version: *([0-9.]+)" _ ${_sinsp_pkgconfig_contents})
79       set(SINSP_VERSION ${CMAKE_MATCH_1})
80       unset(_sinsp_pkgconfig_contents)
81     endif()
82     unset(_sinsp_pkgconfig)
83   endif()
85   set(_scap_libs
86     scap
87     scap_engine_nodriver
88     scap_engine_noop
89     scap_engine_savefile
90     scap_engine_source_plugin
91     scap_engine_test_input
92     scap_error
93     scap_event_schema
94     scap_platform_util
95   )
97   foreach(_scap_lib ${_scap_libs})
98     find_library(_lib NO_CACHE
99       NAMES ${_scap_lib}
100       HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib"
101       PATHS falcosecurity
102       /usr/lib
103       /usr/local/lib
104     )
105     if (_lib)
106       list(APPEND _sinsp_link_libs ${_lib})
107       unset(_lib)
108     endif()
109   endforeach()
110   unset(_scap_libs)
111   unset(_scap_lib)
113   find_path(_jsoncpp_include_dir NO_CACHE
114     NAMES json/json.h
115     HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
116     PATH_SUFFIXES falcosecurity jsoncpp
117     PATHS
118     /usr/include
119     /usr/local/include
120   )
121   if (_jsoncpp_include_dir)
122     list(APPEND _sinsp_include_dirs ${_jsoncpp_include_dir})
123     unset(_jsoncpp_include_dir)
124   endif()
126   find_library(_jsoncpp_lib NO_CACHE
127     NAMES jsoncpp
128     HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib" "${SINSP_HINTS}/lib/falcosecurity"
129     PATHS
130     /usr/lib
131     /usr/local/lib
132   )
133   if (_jsoncpp_lib)
134     list(APPEND _sinsp_link_libs ${_jsoncpp_lib})
135     unset(_jsoncpp_lib)
136   endif()
138   find_library(_re2_lib NO_CACHE
139     NAMES re2
140     HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib" "${SINSP_HINTS}/lib/falcosecurity"
141     PATHS
142     /usr/lib
143     /usr/local/lib
144   )
145   if (_re2_lib)
146     list(APPEND _sinsp_link_libs ${_re2_lib})
147     unset(_re2_lib)
148   endif()
150   find_path(_tbb_include_dir NO_CACHE
151     NAMES tbb/tbb.h
152     HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
153     PATHS
154     /usr/include
155     /usr/local/include
156   )
157   if (_tbb_include_dir)
158     list(APPEND _sinsp_include_dirs ${_tbb_include_dir})
159     unset(_tbb_include_dir)
160   endif()
162   find_library(_tbb_lib NO_CACHE
163     NAMES tbb tbb12
164     HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib" "${SINSP_HINTS}/lib/falcosecurity"
165     PATHS
166     /usr/lib
167     /usr/local/lib
168   )
169   if (_tbb_lib)
170     list(APPEND _sinsp_link_libs ${_tbb_lib})
171     unset(_tbb_lib)
172   endif()
174   # This is terrible, but libsinsp/libscap doesn't support dynamic linking on Windows (yet).
175   find_path(_zlib_include_dir NO_CACHE
176     NAMES zlib/zlib.h
177     HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
178     PATHS
179     /usr/include
180     /usr/local/include
181   )
182   if (_zlib_include_dir)
183     list(APPEND _sinsp_include_dirs ${_zlib_include_dir})
184     unset(_zlib_include_dir)
185   endif()
187   find_library(_zlib_lib NO_CACHE
188     NAMES zlibstatic
189     HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib" "${SINSP_HINTS}/lib/falcosecurity"
190     PATHS
191     /usr/lib
192     /usr/local/lib
193   )
194   if (_zlib_lib)
195     list(APPEND _sinsp_link_libs ${_zlib_lib})
196     unset(_zlib_lib)
197   endif()
199   if(_sinsp_include_dirs AND _sinsp_link_libs)
200     list(REMOVE_DUPLICATES _sinsp_include_dirs)
201     set(SINSP_INCLUDE_DIRS ${_sinsp_include_dirs} CACHE PATH "Paths to libsinsp and libscap headers")
202     set(SINSP_LINK_LIBRARIES ${_sinsp_link_libs} CACHE PATH "Paths to libsinsp, libscap, etc.")
203     set(SINSP_FOUND 1)
204     unset(_sinsp_include_dirs)
205     unset(_sinsp_link_libs)
206   endif()
208 endif()
210 # As https://cmake.org/cmake/help/latest/command/link_directories.html
211 # says, "Prefer to pass full absolute paths to libraries where possible,
212 # since this ensures the correct library will always be linked," so use
213 # SINSP_LINK_LIBRARIES instead of SINSP_LIBRARIES
214 # XXX SINSP_VERSION will require peeking for a #define or something similar.
215 include(FindPackageHandleStandardArgs)
216 find_package_handle_standard_args(Sinsp
217   REQUIRED_VARS
218     SINSP_INCLUDE_DIRS
219     SINSP_LINK_LIBRARIES
220     VERSION_VAR SINSP_VERSION
223 if(SINSP_FOUND)
224   if (SINSP_VERSION VERSION_EQUAL 0.0.0)
225     # https://github.com/falcosecurity/libs/blob/master/README.md#versioning
226     message(WARNING "libsinsp version is 0.0.0. If you built it outside of git, did you set FALCOSECURITY_LIBS_VERSION?")
227   endif()
229   string(REGEX MATCH "([0-9]+)\.([0-9]+)\.([0-9]+)" _ ${SINSP_VERSION})
230   # Should we cache these?
231   set(SINSP_VERSION_MAJOR ${CMAKE_MATCH_1})
232   set(SINSP_VERSION_MINOR ${CMAKE_MATCH_2})
233   set(SINSP_VERSION_MICRO ${CMAKE_MATCH_3})
234 #   if (WIN32)
235 #     set ( SINSP_DLL_DIR "${SINSP_HINTS}/bin"
236 #       CACHE PATH "Path to sinsp DLL"
237 #     )
238 #     file( GLOB _SINSP_dll RELATIVE "${SINSP_DLL_DIR}"
239 #       "${SINSP_DLL_DIR}/sinsp*.dll"
240 #     )
241 #     set ( SINSP_DLL ${_SINSP_dll}
242 #       # We're storing filenames only. Should we use STRING instead?
243 #       CACHE FILEPATH "sinsp DLL file name"
244 #     )
245 #     mark_as_advanced( SINSP_DLL_DIR SINSP_DLL )
246 #   endif()
247 else()
248   set(SINSP_INCLUDE_DIRS)
249   set(SINSP_LINK_LIBRARIES)
250   set(SINSP_VERSION)
251   set(SINSP_VERSION_MAJOR)
252   set(SINSP_VERSION_MINOR)
253   set(SINSP_VERSION_MICRO)
254 endif()
256 mark_as_advanced(SINSP_INCLUDE_DIRS SINSP_LINK_LIBRARIES)
258 # Windows plugins
260 set(_falco_plugins)
261 if(WIN32 AND SINSP_FOUND AND NOT FALCO_PLUGINS)
262   FindWSWinLibs( "falcosecurity-plugins-.*" _falco_plugin_dir)
263   if(_falco_plugin_dir)
264     file( GLOB _falco_plugins LIST_DIRECTORIES false "${_falco_plugin_dir}/*.dll" )
265     unset(_falco_plugin_dir)
266   endif()
267 endif()
269 # XXX It looks like we can either autodiscover this value or provide an option but not both.
270 if(_falco_plugins)
271   set(FALCO_PLUGINS ${_falco_plugins} CACHE FILEPATH "Paths to Falco plugins. Semicolon-separated")
272   unset(_falco_plugins)
273 else()
274   cmake_dependent_option(FALCO_PLUGINS "Paths to Falco plugins. Semicolon-separated" "" "SINSP_FOUND" "")
275 endif()