From: Bassam Tabbara Date: Sat, 5 Nov 2016 01:10:08 +0000 (-0700) Subject: embedded: Add a skeleton libcephd library X-Git-Tag: v11.1.0~128^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=c6067f48ec7aa927129e98bf8424d80ed03119f3;p=ceph.git embedded: Add a skeleton libcephd library 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 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 69a6a371701b..a31877790948 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/ceph.spec.in b/ceph.spec.in index 8d57610ce25a..03021690eded 100644 --- a/ceph.spec.in +++ b/ceph.spec.in @@ -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 index 000000000000..92d41567b8c8 --- /dev/null +++ b/cmake/modules/MergeStaticLibraries.cmake @@ -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 $=") + 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 $=") + 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() diff --git a/debian/rules b/debian/rules index 496bc827f5bf..a3d189353fa7 100755 --- a/debian/rules +++ b/debian/rules @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 117a7e880b5f..e49d2a2bd371 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 index 000000000000..6bc7871c852a --- /dev/null +++ b/src/include/cephd/libcephd.h @@ -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 index 000000000000..11c739836f31 --- /dev/null +++ b/src/libcephd/CMakeLists.txt @@ -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 index 000000000000..09a87c71dfc7 --- /dev/null +++ b/src/libcephd/libcephd.cc @@ -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; +} diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 42229f652ad5..257e8a232821 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -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 index 000000000000..cb711ec0ad47 --- /dev/null +++ b/src/test/libcephd/CMakeLists.txt @@ -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 index 000000000000..274699d6ecc7 --- /dev/null +++ b/src/test/libcephd/misc.cc @@ -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 index 000000000000..aa90d10e9ab5 --- /dev/null +++ b/src/test/libcephd/test.cc @@ -0,0 +1,2 @@ +void doNothing() { +}