]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cmake: find_package(cap) before linking against it 55529/head
authorKefu Chai <tchaikov@gmail.com>
Sun, 11 Feb 2024 08:53:26 +0000 (16:53 +0800)
committerKefu Chai <tchaikov@gmail.com>
Sun, 11 Feb 2024 08:57:38 +0000 (16:57 +0800)
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 <tchaikov@gmail.com>
cmake/modules/Findcap.cmake [new file with mode: 0644]
src/extblkdev/CMakeLists.txt

diff --git a/cmake/modules/Findcap.cmake b/cmake/modules/Findcap.cmake
new file mode 100644 (file)
index 0000000..f33b22d
--- /dev/null
@@ -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()
index 64010f31cf3eb1fad0dedc36e79268a57c1e9547..27e7c23e4942ff29135e95e5f75e79869c7b02b1 100644 (file)
@@ -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()