Skip to content

Commit 0de5755

Browse files
committed
cmake: find_package(cap) before linking against it
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]>
1 parent 8bbeeb8 commit 0de5755

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

cmake/modules/Findcap.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Try to find libcap
2+
#
3+
find_package(PkgConfig QUIET REQUIRED)
4+
5+
pkg_check_modules(PC_cap QUIET cap)
6+
7+
find_library(cap_LIBRARY
8+
NAMES cap
9+
HINTS
10+
${PC_cap_LIBDIR}
11+
${PC_cap_LIBRARY_DIRS})
12+
13+
find_path(cap_INCLUDE_DIR
14+
NAMES sys/capability.h
15+
HINTS
16+
${PC_cap_INCLUDEDIR}
17+
${PC_cap_INCLUDE_DIRS})
18+
19+
mark_as_advanced(
20+
cap_LIBRARY
21+
cap_INCLUDE_DIR)
22+
23+
include (FindPackageHandleStandardArgs)
24+
find_package_handle_standard_args (cap
25+
REQUIRED_VARS
26+
cap_LIBRARY
27+
cap_INCLUDE_DIR)
28+
29+
if(cap_FOUND AND NOT TARGET cap::cap)
30+
add_library(cap::cap UNKNOWN IMPORTED)
31+
set_target_properties(cap::cap
32+
PROPERTIES
33+
IMPORTED_LOCATION ${cap_LIBRARY}
34+
INTERFACE_INCLUDE_DIRECTORIES ${cap_INCLUDE_DIR})
35+
endif()

src/extblkdev/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_subdirectory(vdo)
77
add_library(extblkdev STATIC ExtBlkDevPlugin.cc)
88

99
if(NOT WIN32)
10+
find_package(cap)
1011
target_link_libraries(extblkdev cap)
1112
endif()
1213

0 commit comments

Comments
 (0)