]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
embedded: Add a skeleton libcephd library
authorBassam Tabbara <bassam.tabbara@quantum.com>
Sat, 5 Nov 2016 01:10:08 +0000 (18:10 -0700)
committerBassam Tabbara <bassam.tabbara@quantum.com>
Tue, 29 Nov 2016 07:48:02 +0000 (23:48 -0800)
libcephd is a library that contains ceph daemon code
that can be statically linked in other applications.

Added MergeStaticLibraries.cmake that can merge static libraries
to form a bigger one. This approach avoids the need to mess with
STATIC libraries all over the code base.

Signed-off-by: Bassam Tabbara <bassam.tabbara@quantum.com>
12 files changed:
CMakeLists.txt
ceph.spec.in
cmake/modules/MergeStaticLibraries.cmake [new file with mode: 0644]
debian/rules
src/CMakeLists.txt
src/include/cephd/libcephd.h [new file with mode: 0644]
src/libcephd/CMakeLists.txt [new file with mode: 0644]
src/libcephd/libcephd.cc [new file with mode: 0644]
src/test/CMakeLists.txt
src/test/libcephd/CMakeLists.txt [new file with mode: 0644]
src/test/libcephd/misc.cc [new file with mode: 0644]
src/test/libcephd/test.cc [new file with mode: 0644]

index 69a6a371701b2d57a3986723b3e3250f304668da..a31877790948b5ec73caee3dfa43c9d13e907fd3 100644 (file)
@@ -234,6 +234,13 @@ option(WITH_KVS "Key value store is here" ON)
 # remote block storage
 option(WITH_RBD "Remote block storage is here" ON)
 
+# embedded ceph daemon static library
+# NOTE: Ceph is mostly LGPL (see COPYING), which means that
+# static linking brings with it restrictions. Please be sure
+# to look at the LGPL license carefully before linking this library to
+# your code. See http://www.gnu.org/licenses/gpl-faq.html#LGPLStaticVsDynamic.
+option(WITH_EMBEDDED "build the embedded ceph daemon library" ON)
+
 option(WITH_LEVELDB "LevelDB is here" ON)
 if(WITH_LEVELDB)
   if(LEVELDB_PREFIX)
index 8d57610ce25ae6a7092100da6baf020644d65ee4..03021690eded2bd7c29a7bdd894138919096d848 100644 (file)
@@ -711,6 +711,7 @@ cmake .. \
     -DCMAKE_INSTALL_SYSCONFDIR=%{_sysconfdir} \
     -DCMAKE_INSTALL_MANDIR=%{_mandir} \
     -DCMAKE_INSTALL_DOCDIR=%{_docdir}/ceph \
+    -DWITH_EMBEDDED=OFF \
     -DWITH_MANPAGE=ON \
     -DWITH_PYTHON3=ON \
     -DWITH_SYSTEMD=ON \
diff --git a/cmake/modules/MergeStaticLibraries.cmake b/cmake/modules/MergeStaticLibraries.cmake
new file mode 100644 (file)
index 0000000..92d4156
--- /dev/null
@@ -0,0 +1,85 @@
+# This function is a helper that will merge static libraries.
+# For example,
+#
+#    merge_static_libraries(mylib staticlibX staticlibY)
+#
+# mylib.a will generate a new static library mylib that is
+# a combination of staticlibX and staticlibY
+#
+function(merge_static_libraries target)
+
+    set(dummy_source ${CMAKE_CURRENT_BINARY_DIR}/${target}_dummy.c)
+    add_library(${target} STATIC ${dummy_source})
+
+    # remove duplicates
+    set(libs ${ARGN})
+    list(REMOVE_DUPLICATES libs)
+
+    # validate that all libs are static
+    foreach(lib ${libs})
+        if (NOT TARGET ${lib})
+            message(FATAL_ERROR "${lib} not a valid target")
+        endif()
+
+        get_target_property(libtype ${lib} TYPE)
+        if(NOT libtype STREQUAL "STATIC_LIBRARY")
+            message(FATAL_ERROR "${lib} not a static library")
+        endif()
+
+        # add a dependency on the lib
+        add_dependencies(${target} ${lib})
+    endforeach()
+
+    # Force the merged Make the generated dummy source file depended on all static input
+    # libs. If input lib changes,the source file is touched
+    # which causes the desired effect (relink).
+    add_custom_command(
+        OUTPUT  ${dummy_source}
+        COMMAND ${CMAKE_COMMAND} -E touch ${dummy_source}
+        DEPENDS ${libs})
+
+    # only LINUX is currently supported. OSX's libtool and windows lib.exe
+    # have native support for merging static libraries, and support for them
+    # can be easily added if required.
+    if(LINUX)
+        # generate a script to merge the static libraries in to the target
+        # library. see https://sourceware.org/binutils/docs/binutils/ar-scripts.html
+        set(mri_script "open $<TARGET_FILE:${target}>=")
+        foreach(lib ${libs})
+            # we use the generator expression TARGET_FILE to get the location
+            # of the library. this will not be expanded until the script file
+            # is written below
+            set(mri_script "${mri_script} addlib $<TARGET_FILE:${lib}>=")
+        endforeach()
+        set(mri_script "${mri_script} save=end")
+
+        add_custom_command(
+            TARGET ${target} POST_BUILD
+            COMMAND echo ${mri_script} | tr = \\\\n | ${CMAKE_AR} -M)
+    endif(LINUX)
+
+    message("-- MergeStaticLibraries: ${target}: merged ${libs}")
+
+    # we want to set the target_link_libraries correctly for the new merged
+    # static library. First we get the list of link libraries for each
+    # of the libs we are merging
+    set(link_libs)
+    foreach(lib ${libs})
+      get_property(trans TARGET ${lib} PROPERTY LINK_LIBRARIES)
+      list(APPEND link_libs ${trans})
+    endforeach()
+
+    if (link_libs)
+        # now remove the duplicates and any of the libraries we already merged
+        list(REMOVE_DUPLICATES link_libs)
+        foreach(lib ${libs})
+            list(REMOVE_ITEM link_libs ${lib})
+        endforeach()
+
+        # set the target link libraries
+        target_link_libraries(${target} ${link_libs})
+
+        message("-- MergeStaticLibraries: ${target}: remaining ${link_libs}")
+    endif()
+
+endfunction()
index 496bc827f5bfcc91907f52593d160ecb09d9ea99..a3d189353fa740a31e0031e756eb15f0317345d8 100755 (executable)
@@ -5,7 +5,7 @@ export DESTDIR=$(CURDIR)/debian/tmp
 
 export DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
 
-extraopts += -DUSE_CRYPTOPP=OFF -DWITH_OCF=ON -DWITH_LTTNG=ON -DWITH_PYTHON3=ON
+extraopts += -DUSE_CRYPTOPP=OFF -DWITH_OCF=ON -DWITH_LTTNG=ON -DWITH_PYTHON3=ON -DWITH_EMBEDDED=OFF
 extraopts += -DWITH_CEPHFS_JAVA=ON
 # assumes that ceph is exmpt from multiarch support, so we override the libdir.
 extraopts += -DCMAKE_INSTALL_LIBDIR=/usr/lib
index 117a7e880b5f9c6b2f88972d2f934199779e8b34..e49d2a2bd371676a3ef6bd603ee5d4e84e963a83 100644 (file)
@@ -966,3 +966,7 @@ if (IS_DIRECTORY "${PROJECT_SOURCE_DIR}/.git")
 endif()
 
 add_subdirectory(script)
+
+if(WITH_EMBEDDED)
+  add_subdirectory(libcephd)
+endif()
diff --git a/src/include/cephd/libcephd.h b/src/include/cephd/libcephd.h
new file mode 100644 (file)
index 0000000..6bc7871
--- /dev/null
@@ -0,0 +1,41 @@
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define LIBCEPHD_VER_MAJOR 0
+#define LIBCEPHD_VER_MINOR 1
+#define LIBCEPHD_VER_PATCH 0
+
+#define LIBCEPHFD_VERSION(maj, min, extra) ((maj << 16) + (min << 8) + extra)
+#define LIBCEPHFD_VERSION_CODE LIBCEPHD_VERSION(LIBCEPHD_VER_MAJOR, LIBCEPHD_VER_MINOR, LIBCEPHD_VER_PATCH)
+
+#define CEPH_LIBCEPHD_API __attribute__ ((visibility ("default")))
+
+/**
+ * Get the API version of libcephd. We use semantic versioning
+ * for the API:
+ *
+ * - incrementing major is for backwards-incompatible changes
+ * - incrementing minor is for backwards-compatible changes
+ * - incrementing extra is for bug fixes
+ *
+ * @param pmajor where to store the major version number
+ * @param pminor where to store the minor version number
+ * @param ppatch where to store the patch version number
+ */
+CEPH_LIBCEPHD_API void cephd_version(int *pmajor, int *pminor, int *ppatch);
+
+/**
+ * Gets the runtime version of ceph.
+ *
+ * @param pmajor where to store the major version number
+ * @param pminor where to store the minor version number
+ * @param ppatch where to store the patch version number
+ */
+CEPH_LIBCEPHD_API const char *ceph_version(int *pmajor, int *pminor, int *ppatch);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/libcephd/CMakeLists.txt b/src/libcephd/CMakeLists.txt
new file mode 100644 (file)
index 0000000..11c7398
--- /dev/null
@@ -0,0 +1,26 @@
+include(MergeStaticLibraries)
+
+add_library(cephd_base STATIC
+  libcephd.cc)
+
+set(merge_libs
+  cephd_base
+  mon
+  osd
+  common
+  common_utf8
+  os
+  kv
+  global
+  rocksdb
+  json_spirit)
+
+if(HAVE_ARMV8_CRC)
+  list(APPEND merge_libs common_crc_aarch64)
+endif(HAVE_ARMV8_CRC)
+
+merge_static_libraries(cephd ${merge_libs})
+
+# TODO: install these libraries and add them to rpm and deb packages
+#install(TARGETS cephd DESTINATION ${CMAKE_INSTALL_LIBDIR})
+#install(FILES ../include/cephd/libcephd.h DESTINATION include/cephd)
diff --git a/src/libcephd/libcephd.cc b/src/libcephd/libcephd.cc
new file mode 100644 (file)
index 0000000..09a87c7
--- /dev/null
@@ -0,0 +1,27 @@
+#include "common/version.h"
+#include "include/cephd/libcephd.h"
+
+extern "C" void cephd_version(int *pmajor, int *pminor, int *ppatch)
+{
+  if (pmajor)
+    *pmajor = LIBCEPHD_VER_MAJOR;
+  if (pminor)
+    *pminor = LIBCEPHD_VER_MINOR;
+  if (ppatch)
+    *ppatch = LIBCEPHD_VER_PATCH;
+}
+
+extern "C" const char *ceph_version(int *pmajor, int *pminor, int *ppatch)
+{
+  int major, minor, patch;
+  const char *v = ceph_version_to_str();
+
+  int n = sscanf(v, "%d.%d.%d", &major, &minor, &patch);
+  if (pmajor)
+    *pmajor = (n >= 1) ? major : 0;
+  if (pminor)
+    *pminor = (n >= 2) ? minor : 0;
+  if (ppatch)
+    *ppatch = (n >= 3) ? patch : 0;
+  return v;
+}
index 42229f652ad5bd898a2bfc8370b04ee686a70492..257e8a23282185813c3ccafa1016c0533e5ca57d 100644 (file)
@@ -25,6 +25,9 @@ add_subdirectory(erasure-code)
 add_subdirectory(filestore)
 add_subdirectory(fs)
 add_subdirectory(journal)
+if(WITH_EMBEDDED)
+  add_subdirectory(libcephd)
+endif(WITH_EMBEDDED)
 add_subdirectory(libcephfs)
 add_subdirectory(librados)
 add_subdirectory(librados_test_stub)
diff --git a/src/test/libcephd/CMakeLists.txt b/src/test/libcephd/CMakeLists.txt
new file mode 100644 (file)
index 0000000..cb711ec
--- /dev/null
@@ -0,0 +1,18 @@
+# cephdtest
+set(cephdtest_srcs
+  test.cc)
+add_library(cephdtest STATIC ${cephdtest_srcs})
+set_target_properties(cephdtest PROPERTIES COMPILE_FLAGS ${UNITTEST_CXX_FLAGS})
+
+# ceph_test_cephd_api_misc
+add_executable(ceph_test_cephd_api_misc
+  misc.cc
+  )
+set_target_properties(ceph_test_cephd_api_misc PROPERTIES COMPILE_FLAGS
+  ${UNITTEST_CXX_FLAGS})
+target_link_libraries(ceph_test_cephd_api_misc
+  cephd global ${UNITTEST_LIBS} cephdtest z snappy)
+
+install(TARGETS
+  ceph_test_cephd_api_misc
+  DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/src/test/libcephd/misc.cc b/src/test/libcephd/misc.cc
new file mode 100644 (file)
index 0000000..274699d
--- /dev/null
@@ -0,0 +1,7 @@
+#include "gtest/gtest.h"
+#include "include/cephd/libcephd.h"
+
+TEST(LibCephdMiscVersion, Version) {
+  int major, minor, extra;
+  cephd_version(&major, &minor, &extra);
+}
diff --git a/src/test/libcephd/test.cc b/src/test/libcephd/test.cc
new file mode 100644 (file)
index 0000000..aa90d10
--- /dev/null
@@ -0,0 +1,2 @@
+void doNothing() {
+}