Skip to content

Commit

Permalink
libsinsp: Refine pkg-config file generation.
Browse files Browse the repository at this point in the history
The generated pkg-config file now makes use of pkg-config Requires and
Requires.static fields, which should reduce over-linking when linking
to shared libraries.

* cmake/modules/BuildPkgConfigDependencies.cmake
(add_pkgconfig_library): Add debug messages and fix an issue where
IN_LIST had no effect.
* cmake/modules/libscap.cmake: Add json, pman, protobuf and zlib to
ignored link dependencies, since these are now recorded as Requires
in the pkg-config file.
* userspace/libscap/libscap.pc.in (prefix): Set directly to
CMAKE_INSTALL_PREFIX.
(Requires, Requires.private): New fields.
* userspace/libsinsp/CMakeLists.txt: Separate libraries into
pkg-config Requires and Requires.private lists.  Add the pkg-config
requirements to the ignored link dependencies, since these are now
recorded as Requires in the pkg-config file.
* userspace/libsinsp/libsinsp.pc.in (Requires): Add
@LIBSINSP_REQUIRES@.
(Requires.private): New field.
(Libs): Remove -lsinsp, automatically computed in SINSP_PKG_CONFIG_LIBS.

Signed-off-by: Maxim Cournoyer <[email protected]>
  • Loading branch information
Apteryks committed Jan 23, 2025
1 parent 15d9ff9 commit 2d7ad7a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
16 changes: 14 additions & 2 deletions cmake/modules/BuildPkgConfigDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# libsinsp.pc (which requires libscap.pc and pulls them in that way)
function(add_pkgconfig_library LIBDIRS_VAR LIBS_VAR lib ignored)

message(DEBUG "[add_pkgconfig_library] processing lib \"${lib}\"")
# if it's not a target, it doesn't have dependencies we know or care about
if(NOT TARGET ${lib})
return()
Expand All @@ -14,14 +15,25 @@ function(add_pkgconfig_library LIBDIRS_VAR LIBS_VAR lib ignored)
return()
endif()

message(DEBUG "[add_pkgconfig_library] LINK_LIBRARIES property: \"${PKGCONFIG_LIBRARIES}\"")

get_property(
target_type
TARGET ${lib}
PROPERTY TYPE
)
message(DEBUG "[add_pkgconfig_library] ignored list: \"${ignored}\"")
foreach(dep ${PKGCONFIG_LIBRARIES})
# ignore dependencies in the list ${ignored}
if(${dep} IN_LIST "${ignored}")
# XXX: We use a (very) loose match as we are potentially
# comparing absolute library file names (dep) to pkg-config
# library names to be ignored. The only alternative I can
# think of would be to maintain a map associating pkg-config
# names to their library file name.
get_filename_component(dep_base ${dep} NAME_WE)
string(REGEX REPLACE "^lib" "" dep_name ${dep_base})
message(DEBUG "[add_pkgconfig_library] processing dep ${dep}")
if("${ignored}" MATCHES "${dep_name}")
message(DEBUG "[add_pkgconfig_library] \"${dep}\" ignored")
continue()
endif()

Expand Down
6 changes: 5 additions & 1 deletion cmake/modules/libscap.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ if(NOT HAVE_LIBSCAP)

set(libscap_link_flags)
set(libscap_link_libdirs "")
add_pkgconfig_dependency(libscap_link_libdirs libscap_link_flags scap "")
add_pkgconfig_dependency(libscap_link_libdirs libscap_link_flags scap
# Avoid using these in libscap.pc Libs field, as they are
# already listed in Requires. lbpf is transitively required
# via libpman.pc.
"jsoncpp;libelf;libpman;protobuf;zlib")

string(REPLACE ";" " " LIBSCAP_LINK_LIBRARIES_FLAGS "${libscap_link_flags}")
string(REPLACE ";" " " LIBSCAP_LINK_LIBDIRS_FLAGS "${libscap_link_libdirs}")
Expand Down
7 changes: 6 additions & 1 deletion userspace/libscap/libscap.pc.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
prefix=${pcfiledir}/../..
prefix=@CMAKE_INSTALL_PREFIX@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/@LIBS_PACKAGE_NAME@

Name: libscap
Description: lib for System CAPture
Version: @FALCOSECURITY_LIBS_VERSION@

# Note: jsoncpp and protobuf are required by scap_engine_gvisor, which
# currently lacks its own pkg-config file. Similarly, libelf is
# required by scap_engine_bpf.
Requires: jsoncpp libelf libpman protobuf
Requires.private: zlib
Libs: -L${libdir} @LIBSCAP_LINK_LIBDIRS_FLAGS@ @LIBSCAP_LINK_LIBRARIES_FLAGS@
Cflags: -I${includedir} -I${includedir}/libscap -I${includedir}/driver -I@UTHASH_INCLUDE@
18 changes: 17 additions & 1 deletion userspace/libsinsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,17 @@ target_link_libraries(
PRIVATE "${CURL_LIBRARIES}" "${JSONCPP_LIB}" "${RE2_LIB}"
)

set(SINSP_PKGCONFIG_REQUIRES jsoncpp)
set(SINSP_PKGCONFIG_REQUIRES_PRIVATE libcurl re2)

if(NOT EMSCRIPTEN)
target_link_libraries(
sinsp
INTERFACE "${CARES_LIB}"
PRIVATE "${TBB_LIB}"
)
list(APPEND SINSP_PKGCONFIG_REQUIRES libcares)
list(APPEND SINSP_PKGCONFIG_REQUIRES_PRIVATE tbb)
endif()

if(USE_BUNDLED_VALIJSON)
Expand Down Expand Up @@ -275,6 +280,12 @@ if(NOT WIN32)
)

target_link_libraries(sinsp PRIVATE cri_v1alpha2 cri_v1 containerd_interface)
list(APPEND SINSP_PKGCONFIG_REQUIRES
gpr
grpc
grpc++
protobuf
libcares)

if(NOT MUSL_OPTIMIZED_BUILD)
find_library(LIB_ANL anl)
Expand All @@ -290,6 +301,8 @@ if(NOT WIN32)
endif() # NOT MINIMAL_BUILD
endif() # NOT APPLE

list(APPEND SINSP_PKGCONFIG_REQUIRES libcrypto libssl)

target_link_libraries(sinsp INTERFACE dl pthread)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Expand Down Expand Up @@ -329,14 +342,17 @@ add_definitions(-DSINSP_AGENT_CGROUP_MEM_PATH_ENV_VAR="${SINSP_AGENT_CGROUP_MEM_
# https://github.com/curl/curl/blob/curl-7_84_0/CMakeLists.txt#L1539
set(SINSP_PKG_CONFIG_LIBS)
set(SINSP_PKG_CONFIG_LIBDIRS "")
add_pkgconfig_dependency(SINSP_PKG_CONFIG_LIBDIRS SINSP_PKG_CONFIG_LIBS sinsp scap)
add_pkgconfig_dependency(SINSP_PKG_CONFIG_LIBDIRS SINSP_PKG_CONFIG_LIBS sinsp
"scap;${SINSP_PKGCONFIG_REQUIRES};${SINSP_PKGCONFIG_REQUIRES_PRIVATE}")

# Build our pkg-config "Cflags:" flags.
set(SINSP_PKG_CONFIG_INCLUDES "")
foreach(sinsp_include_directory ${LIBSINSP_INCLUDE_DIRS})
list(APPEND SINSP_PKG_CONFIG_INCLUDES -I${sinsp_include_directory})
endforeach()

string(REPLACE ";" " " LIBSINSP_REQUIRES "${SINSP_PKGCONFIG_REQUIRES}")
string(REPLACE ";" " " LIBSINSP_REQUIRES_PRIVATE "${SINSP_PKGCONFIG_REQUIRES_PRIVATE}")
string(REPLACE ";" " " SINSP_PKG_CONFIG_LIBS "${SINSP_PKG_CONFIG_LIBS}")
list(REMOVE_DUPLICATES SINSP_PKG_CONFIG_LIBDIRS)
string(REPLACE ";" " " SINSP_PKG_CONFIG_LIBDIRS "${SINSP_PKG_CONFIG_LIBDIRS}")
Expand Down
7 changes: 4 additions & 3 deletions userspace/libsinsp/libsinsp.pc.in
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
prefix=${pcfiledir}/../..
prefix=@CMAKE_INSTALL_PREFIX@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/@LIBS_PACKAGE_NAME@

Name: libsinsp
Description: lib for System INSPection
Version: @FALCOSECURITY_LIBS_VERSION@

Requires: libscap
Libs: -L${libdir} -lsinsp @SINSP_PKG_CONFIG_LIBDIRS@ @SINSP_PKG_CONFIG_LIBS@
Requires: libscap @LIBSINSP_REQUIRES@
Requires.private: @LIBSINSP_REQUIRES_PRIVATE@
Libs: -L${libdir} @SINSP_PKG_CONFIG_LIBDIRS@ @SINSP_PKG_CONFIG_LIBS@
Cflags: -I${includedir} -I${includedir}/libsinsp -I${includedir}/driver @SINSP_PKG_CONFIG_INCLUDES@

0 comments on commit 2d7ad7a

Please sign in to comment.