Skip to content

Commit

Permalink
cmake: find_package(cap) before linking against it
Browse files Browse the repository at this point in the history
before this change, we link against libcap without finding it. this
works fine as long as libcap-devel or libcap-dev is installed in the
system. but if it is not, the source would fail to build due to missing
`sys/capability.h`. this is not a great developer experience.

in this change, a `Findcap.cmake` is added to find the capability
library. which would fail the build at the configure phase.

Signed-off-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov committed Feb 11, 2024
1 parent 8bbeeb8 commit 0de5755
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmake/modules/Findcap.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Try to find libcap
#
find_package(PkgConfig QUIET REQUIRED)

pkg_check_modules(PC_cap QUIET cap)

find_library(cap_LIBRARY
NAMES cap
HINTS
${PC_cap_LIBDIR}
${PC_cap_LIBRARY_DIRS})

find_path(cap_INCLUDE_DIR
NAMES sys/capability.h
HINTS
${PC_cap_INCLUDEDIR}
${PC_cap_INCLUDE_DIRS})

mark_as_advanced(
cap_LIBRARY
cap_INCLUDE_DIR)

include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (cap
REQUIRED_VARS
cap_LIBRARY
cap_INCLUDE_DIR)

if(cap_FOUND AND NOT TARGET cap::cap)
add_library(cap::cap UNKNOWN IMPORTED)
set_target_properties(cap::cap
PROPERTIES
IMPORTED_LOCATION ${cap_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${cap_INCLUDE_DIR})
endif()
1 change: 1 addition & 0 deletions src/extblkdev/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_subdirectory(vdo)
add_library(extblkdev STATIC ExtBlkDevPlugin.cc)

if(NOT WIN32)
find_package(cap)
target_link_libraries(extblkdev cap)
endif()

Expand Down

0 comments on commit 0de5755

Please sign in to comment.