summaryrefslogtreecommitdiffstats
path: root/cmake/modules/FindSinsp.cmake
blob: 9729b028f481dc235b41e00b29eb0609438ac02d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#
# - Find libsinsp and libscap
# Find libsinsp and libscap includes and libraries.
# Adapted from FindZSTD.cmake.
#
# This module will look for libsinsp and libscap using pkg-config. If that
# fails, it will search ${SINSP_INCLUDEDIR} and ${SINSP_HINTS}/include
# for the libsinsp and libscap include directory and ${SINSP_LIBDIR} and
# ${SINSP_HINTS}/lib for the libsinsp and libscap libraries.
#
# It will set the following variables:
#
#  SINSP_FOUND          - True if libsinsp found.
#  SINSP_INCLUDE_DIRS   - Where to find sinsp.h, scap.h, etc.
#  SINSP_LINK_LIBRARIES - List of libraries when using libsinsp.

# You must manually set the following variables:
#  FALCO_PLUGINS        - Paths to plugins built from https://github.com/falcosecurity/plugins/.

# To do:
#  SINSP_DLL_DIR        - (Windows) Path to the libsinsp and libscap DLLs
#  SINSP_DLL            - (Windows) Name of the libsinsp and libscap DLLs

include( FindWSWinLibs )
FindWSWinLibs( "libsinsp-.*" "SINSP_HINTS" )

include(CMakeDependentOption)

if( NOT USE_REPOSITORY)
  find_package(PkgConfig)
  pkg_check_modules(SINSP libsinsp)
endif()

if(NOT SINSP_FOUND)
  # pkg_check_modules didn't work, so look for ourselves.
  find_path(SINSP_INCLUDE_DIRS
    NAMES sinsp.h
    HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
    PATH_SUFFIXES falcosecurity/userspace/libsinsp
    /usr/include
    /usr/local/include
  )

  find_path(_scap_include_dir
    NAMES scap.h
    HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
    PATH_SUFFIXES falcosecurity/userspace/libscap
    /usr/include
    /usr/local/include
  )
  if(_scap_include_dir)
    list(APPEND SINSP_INCLUDE_DIRS _scap_include_dir)
  endif()
  unset(_scap_include_dir)

  find_library(SINSP_LINK_LIBRARIES
    NAMES sinsp
    HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib"
    PATHS falcosecurity
    /usr/lib
    /usr/local/lib
  )

  set(_scap_libs
    scap
    scap_engine_util
    scap_event_schema
    driver_event_schema
    scap_engine_bpf
    scap_engine_gvisor
    scap_engine_kmod
    scap_engine_nodriver
    scap_engine_noop
    scap_engine_savefile
    scap_engine_source_plugin
    scap_engine_udig
  )

  foreach(_scap_lib ${_scap_libs})
    find_library(_lib
      NAMES ${_scap_lib}
      HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib"
      PATHS falcosecurity
      /usr/lib
      /usr/local/lib
    )
    if (_lib)
      list(APPEND SINSP_LINK_LIBRARIES ${_lib})
    endif()
  endforeach()
  unset(_scap_libs)
  unset(_scap_lib)
  unset(_lib)
  if(SINSP_INCLUDE_DIRS AND JSONCPP_LIBRARY)
    set(SINSP_FOUND 1)
  endif()

  find_path(JSONCPP_INCLUDE_DIR
    NAMES json/json.h
    HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
    PATH_SUFFIXES jsoncpp
    /usr/include
    /usr/local/include
  )
  if (JSON_INCLUDE_DIR)
    list(APPEND SINSP_INCLUDE_DIRS ${JSONCPP_INCLUDE_DIR})
  endif()

  find_library(JSONCPP_LIBRARY
    NAMES jsoncpp
    HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib"
    PATHS
    /usr/lib
    /usr/local/lib
  )
  if (JSONCPP_LIBRARY)
    list(APPEND JSONCPP_LIBRARY ${JSONCPP_LIBRARY})
  endif()

  find_path(TBB_INCLUDE_DIR
    NAMES tbb/tbb.h
    HINTS "${SINSP_INCLUDEDIR}" "${SINSP_HINTS}/include"
    /usr/include
    /usr/local/include
  )
  if (TBB_INCLUDE_DIR)
    list(APPEND SINSP_INCLUDE_DIRS ${TBB_INCLUDE_DIR})
  endif()

  find_library(TBB_LIBRARY
    NAMES tbb
    HINTS "${SINSP_LIBDIR}" "${SINSP_HINTS}/lib"
    PATHS
    /usr/lib
    /usr/local/lib
  )
  if (TBB_LIBRARY)
    list(APPEND JSONCPP_LIBRARY ${TBB_LIBRARY})
  endif()
endif()

# As https://cmake.org/cmake/help/latest/command/link_directories.html
# says, "Prefer to pass full absolute paths to libraries where possible,
# since this ensures the correct library will always be linked," so use
# SINSP_LINK_LIBRARIES instead of SINSP_LIBRARIES
# XXX SINSP_VERSION will require peeking for a #define or something similar.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Sinsp
  REQUIRED_VARS
    SINSP_INCLUDE_DIRS
    SINSP_LINK_LIBRARIES
  # VERSION_VAR SINSP_VERSION
)

if(SINSP_FOUND)
#   if (WIN32)
#     set ( SINSP_DLL_DIR "${SINSP_HINTS}/bin"
#       CACHE PATH "Path to sinsp DLL"
#     )
#     file( GLOB _SINSP_dll RELATIVE "${SINSP_DLL_DIR}"
#       "${SINSP_DLL_DIR}/sinsp*.dll"
#     )
#     set ( SINSP_DLL ${_SINSP_dll}
#       # We're storing filenames only. Should we use STRING instead?
#       CACHE FILEPATH "sinsp DLL file name"
#     )
#     mark_as_advanced( SINSP_DLL_DIR SINSP_DLL )
#   endif()
else()
  set(SINSP_INCLUDE_DIRS)
  set(SINSP_LINK_LIBRARIES)
endif()

cmake_dependent_option(FALCO_PLUGINS "Paths to Falco plugins. Semicolon-separated" "" SINSP_FOUND "")

mark_as_advanced(SINSP_INCLUDE_DIRS SINSP_LINK_LIBRARIES)