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.

* userspace/libsinsp/libsinsp.pc.in: Delete file.
* userspace/libsinsp/CMakeLists.txt: Separate libraries into
pkg-config Requires and Requires.private lists.  Generate the
libsinsp.pc file via 'file', which supports generator expressions.

Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com>
  • Loading branch information
Apteryks committed May 8, 2024
1 parent 134843c commit 816a718
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 64 deletions.
5 changes: 3 additions & 2 deletions userspace/libscap/libscap.pc.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
prefix=${pcfiledir}/../..
prefix=@CMAKE_INSTALL_PREFIX@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

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

Libs: -L${libdir} @LIBSCAP_LINK_LIBDIRS_FLAGS@ @LIBSCAP_LINK_LIBRARIES_FLAGS@
Requires: zlib
Libs: -L${libdir} -L{libdir}/@LIBS_PACKAGE_NAME@/libscap @LIBSCAP_LINK_LIBDIRS_FLAGS@ @LIBSCAP_LINK_LIBRARIES_FLAGS@
Cflags: -I${includedir}/@LIBS_PACKAGE_NAME@/libscap -I${includedir}/@LIBS_PACKAGE_NAME@ -I${includedir}/@LIBS_PACKAGE_NAME@/driver
93 changes: 42 additions & 51 deletions userspace/libsinsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,14 @@ target_link_libraries(sinsp
"${RE2_LIB}"
)

set(SINSP_PKGCONFIG_LIBRARIES
scap
"${ZLIB_LIB}"
"${CURL_LIBRARIES}"
"${JSONCPP_LIB}"
"${RE2_LIB}"
set(SINSP_PKGCONFIG_LIBRARIES)
set(SINSP_PKGCONFIG_REQUIRES
$<$<BOOL:${JSONCPP_LIB}>:jsoncpp>
)

set(SINSP_PKGCONFIG_REQUIRES_PRIVATE
$<$<BOOL:${CURL_LIBRARIES}>:libcurl>
$<$<BOOL:${RE2_LIB}>:re2>
)

if(NOT EMSCRIPTEN)
Expand All @@ -184,7 +186,12 @@ if(NOT EMSCRIPTEN)
PRIVATE
"${TBB_LIB}"
)
list(APPEND SINSP_PKGCONFIG_LIBRARIES "${CARES_LIB}")
list(APPEND SINSP_PKGCONFIG_REQUIRES
$<$<BOOL:${CARES_LIB}>:libcares>
)
list(APPEND SINSP_PKGCONFIG_REQUIRES_PRIVATE
$<$<BOOL:${TBB_LIB}>:tbb>
)
endif()

if(USE_BUNDLED_VALIJSON)
Expand Down Expand Up @@ -248,13 +255,12 @@ if(NOT WIN32)
"${PROTOBUF_LIB}"
"${CARES_LIB}"
)
list(APPEND SINSP_PKGCONFIG_LIBRARIES
"${GRPC_LIBRARIES}"
"${GRPCPP_LIB}"
"${GRPC_LIB}"
"${GPR_LIB}"
"${PROTOBUF_LIB}"
"${CARES_LIB}"
list(APPEND SINSP_PKGCONFIG_REQUIRES
$<$<BOOL:${GPR_LIB}>:gpr>
$<$<BOOL:${GRPC_LIB}>:grpc>
$<$<BOOL:${GRPCPP_LIB}>:grpc++>
$<$<BOOL:${PROTOBUF_LIB}>:protobuf>
$<$<BOOL:${CARES_LIB}>:libcares>
)

if(NOT MUSL_OPTIMIZED_BUILD)
Expand All @@ -269,7 +275,8 @@ if(NOT WIN32)
endif() # NOT APPLE

target_link_libraries(sinsp INTERFACE "${OPENSSL_LIBRARIES}")
list(APPEND SINSP_PKGCONFIG_LIBRARIES "${OPENSSL_LIBRARIES}")
list(APPEND SINSP_PKGCONFIG_REQUIRES
$<$<BOOL:${OPENSSL_LIBRARIES}>:libcrypto libssl>)

target_link_libraries(sinsp INTERFACE dl pthread)
list(APPEND SINSP_PKGCONFIG_LIBRARIES dl pthread)
Expand Down Expand Up @@ -306,41 +313,25 @@ if(NOT DEFINED SINSP_AGENT_CGROUP_MEM_PATH_ENV_VAR)
endif()
add_definitions(-DSINSP_AGENT_CGROUP_MEM_PATH_ENV_VAR="${SINSP_AGENT_CGROUP_MEM_PATH_ENV_VAR}")

# Build our pkg-config "Libs:" flags. For now, loop over SINSP_PKGCONFIG_LIBRARIES. If
# we ever start using pkg_search_module or pkg_check_modules in cmake/modules
# we could add each module to our "Requires:" line instead. We might need to
# expand this to use some of the techniques in
# https://github.com/curl/curl/blob/curl-7_84_0/CMakeLists.txt#L1539
set(SINSP_PKG_CONFIG_LIBS)
set(SINSP_PKG_CONFIG_LIBDIRS "")
foreach(sinsp_lib ${SINSP_PKGCONFIG_LIBRARIES})
if(${sinsp_lib} MATCHES "^-")
# We have a flag. Pass it through unchanged.
list(APPEND SINSP_PKG_CONFIG_LIBS ${sinsp_lib})
elseif(${sinsp_lib} MATCHES "/")
# We have a path. Convert it to -L<dir> + -l<lib>.
get_filename_component(sinsp_lib_dir ${sinsp_lib} DIRECTORY)
list(APPEND SINSP_PKG_CONFIG_LIBDIRS -L${sinsp_lib_dir})
get_filename_component(sinsp_lib_base ${sinsp_lib} NAME_WE)
string(REGEX REPLACE "^lib" "" sinsp_lib_base ${sinsp_lib_base})
list(APPEND SINSP_PKG_CONFIG_LIBS -l${sinsp_lib_base})
elseif(${sinsp_lib} STREQUAL "scap")
# We require libscap.pc, so skip it.
else()
# Assume we have a plain library name. Prefix it with "-l".
list(APPEND SINSP_PKG_CONFIG_LIBS -l${sinsp_lib})
endif()
endforeach()

# 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 ";" " " 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}")
list(REMOVE_DUPLICATES SINSP_PKG_CONFIG_INCLUDES)
string(REPLACE ";" " " SINSP_PKG_CONFIG_INCLUDES "${SINSP_PKG_CONFIG_INCLUDES}")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libsinsp.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libsinsp.pc @ONLY)

list(REMOVE_DUPLICATES SINSP_PKGCONFIG_LIBRARIES)
list(REMOVE_DUPLICATES SINSP_PKGCONFIG_REQUIRES)
list(REMOVE_DUPLICATES SINSP_PKGCONFIG_REQUIRES_PRIVATE)

file(GENERATE
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/libsinsp.pc
CONTENT "prefix=${CMAKE_INSTALL_PREFIX}
libdir=\${prefix}/${CMAKE_INSTALL_LIBDIR}
includedir=\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}
Name: libsinsp
Description: lib for System INSPection
Version: ${FALCOSECURITY_LIBS_VERSION}
Requires: libscap $<JOIN:${SINSP_PKGCONFIG_REQUIRES}, >
Requires.private: $<JOIN:${SINSP_PKGCONFIG_REQUIRES_PRIVATE}, >
Libs: -L\${libdir} -lsinsp $<JOIN:${SINSP_PKG_CONFIG_LIBS}, -l>
Cflags: -I\${includedir}/${LIBS_PACKAGE_NAME}/libsinsp -I\${includedir}/${LIBS_PACKAGE_NAME}/driver -I\${includedir}/${LIBS_PACKAGE_NAME}
")
11 changes: 0 additions & 11 deletions userspace/libsinsp/libsinsp.pc.in

This file was deleted.

0 comments on commit 816a718

Please sign in to comment.