Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmake): improve shared libs and pkg config files #1842

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 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,26 @@ 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})
# For CMake imported targets, keep only the suffix, e.g. gRPC::grpc -> grpc.
string(REGEX REPLACE "[^:]*::" "" dep_name ${dep_base})
message(DEBUG "[add_pkgconfig_library] processing dep ${dep}")
string(FIND "${ignored}" "${dep_name}" find_result)
if(NOT ${find_result} EQUAL -1)
message(DEBUG "[add_pkgconfig_library] \"${dep}\" ignored")
continue()
endif()

Expand Down
10 changes: 0 additions & 10 deletions cmake/modules/libscap.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ if(NOT HAVE_LIBSCAP)
endif()
endforeach()

set(libscap_link_flags)
set(libscap_link_libdirs "")
add_pkgconfig_dependency(libscap_link_libdirs libscap_link_flags scap "")

string(REPLACE ";" " " LIBSCAP_LINK_LIBRARIES_FLAGS "${libscap_link_flags}")
string(REPLACE ";" " " LIBSCAP_LINK_LIBDIRS_FLAGS "${libscap_link_libdirs}")
configure_file(
${LIBS_DIR}/userspace/libscap/libscap.pc.in ${PROJECT_BINARY_DIR}/libscap/libscap.pc @ONLY
)

install(
TARGETS ${LIBSCAP_INSTALL_LIBS}
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
Expand Down
5 changes: 5 additions & 0 deletions driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ set(DRIVER_SOURCES
ppm_consumer.h
capture_macro.h
socketcall_to_syscall.h
syscall_compat_loongarch64.h
syscall_compat_ppc64le.h
syscall_compat_riscv64.h
syscall_compat_s390x.h
syscall_compat_x86_64.h
syscall_ia32_64_map.c
)

Expand Down
5 changes: 5 additions & 0 deletions userspace/libpman/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ install(
COMPONENT "scap"
OPTIONAL
)

install(FILES include/libpman.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpman.pc.in ${PROJECT_BINARY_DIR}/libpman.pc @ONLY)

install(FILES ${PROJECT_BINARY_DIR}/libpman.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
11 changes: 11 additions & 0 deletions userspace/libpman/libpman.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prefix=@CMAKE_INSTALL_PREFIX@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

Name: libpman
Description: Utility library for BPF probes
Version: @FALCOSECURITY_LIBS_VERSION@

Requires: libbpf zlib
Libs: -L${libdir} -lpman -lscap_event_schema -lscap_platform
Cflags: -I${includedir}
26 changes: 25 additions & 1 deletion userspace/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ target_include_directories(
)

target_link_libraries(scap PRIVATE scap_error "${ZLIB_LIB}")
set(SCAP_PKGCONFIG_REQUIRES "")
set(SCAP_PKGCONFIG_REQUIRES_PRIVATE zlib)

add_library(
scap_event_schema STATIC
scap_event_schema
scap_event.c
ppm_sc_names.c
scap_print_event.c
Expand Down Expand Up @@ -174,17 +176,20 @@ if(HAS_ENGINE_BPF)
add_subdirectory(engine/bpf)
target_link_libraries(scap PUBLIC scap_engine_bpf)
target_include_directories(scap_engine_bpf PRIVATE ${PROJECT_BINARY_DIR}/driver/src)
list(APPEND SCAP_PKGCONFIG_REQUIRES libelf)
endif()

if(HAS_ENGINE_MODERN_BPF)
add_subdirectory(engine/modern_bpf)
target_link_libraries(scap PUBLIC scap_engine_modern_bpf)
target_include_directories(scap_engine_modern_bpf PRIVATE ${PROJECT_BINARY_DIR}/driver/src)
list(APPEND SCAP_PKGCONFIG_REQUIRES libpman)
endif()

if(HAS_ENGINE_GVISOR)
add_subdirectory(engine/gvisor)
target_link_libraries(scap PUBLIC scap_engine_gvisor)
list(APPEND SCAP_PKGCONFIG_REQUIRES protobuf jsoncpp)
endif()

# ##################################################################################################
Expand All @@ -193,3 +198,22 @@ if(BUILD_LIBSCAP_EXAMPLES)
add_subdirectory(examples/01-open)
add_subdirectory(examples/02-validatebuffer)
endif()

set(libscap_link_flags)
set(libscap_link_libdirs "")
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.
"${SCAP_PKGCONFIG_REQUIRES};${SCAP_PKGCONFIG_REQUIRES_PRIVATE}"
)

string(REPLACE ";" " " LIBSCAP_REQUIRES "${SCAP_PKGCONFIG_REQUIRES}")
string(REPLACE ";" " " LIBSCAP_REQUIRES_PRIVATE "${SCAP_PKGCONFIG_REQUIRES_PRIVATE}")
string(REPLACE ";" " " LIBSCAP_LINK_LIBRARIES_FLAGS "${libscap_link_flags}")
string(REPLACE ";" " " LIBSCAP_LINK_LIBDIRS_FLAGS "${libscap_link_libdirs}")
configure_file(
${LIBS_DIR}/userspace/libscap/libscap.pc.in ${PROJECT_BINARY_DIR}/libscap/libscap.pc @ONLY
)
6 changes: 4 additions & 2 deletions userspace/libscap/libscap.pc.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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@

Requires: @LIBSCAP_PKGCONFIG_REQUIRES@
Requires.private: @LIBSCAP_PKGCONFIG_REQUIRES_PRIVATE@
Libs: -L${libdir} @LIBSCAP_LINK_LIBDIRS_FLAGS@ @LIBSCAP_LINK_LIBRARIES_FLAGS@
Cflags: -I${includedir}
Cflags: -I${includedir} -I${includedir}/libscap -I${includedir}/driver -I@UTHASH_INCLUDE@
2 changes: 1 addition & 1 deletion userspace/libscap/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# the License.
#
add_library(
scap_platform STATIC
scap_platform
scap_linux_platform.c
scap_linux_hostinfo_platform.c
scap_procs.c
Expand Down
23 changes: 22 additions & 1 deletion userspace/libsinsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,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 @@ -276,6 +281,15 @@ if(NOT WIN32)
)

target_link_libraries(sinsp PUBLIC 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 @@ -291,6 +305,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 @@ -330,14 +346,19 @@ 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
9 changes: 5 additions & 4 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@
Cflags: -I${includedir} @SINSP_PKG_CONFIG_INCLUDES@
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@
8 changes: 7 additions & 1 deletion userspace/libsinsp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,13 @@ target_include_directories(
)

target_link_libraries(
unit-test-libsinsp sinsp "${GTEST_LIB}" "${GTEST_MAIN_LIB}" "${TBB_LIB}" "${JSONCPP_LIB}"
unit-test-libsinsp
sinsp
"${GRPCPP_LIB}"
"${GTEST_LIB}"
"${GTEST_MAIN_LIB}"
"${TBB_LIB}"
"${JSONCPP_LIB}"
)

# Add some additional include directories associated with `ADDITIONAL_SINSP_TESTS_SUITE`
Expand Down
Loading