]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cmake: allow use libzstd in system
authorKefu Chai <kchai@redhat.com>
Wed, 31 Mar 2021 04:15:17 +0000 (12:15 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 31 Mar 2021 04:53:39 +0000 (12:53 +0800)
since we are moving the test nodes from bionic to focal, we are able to
use the prebuilt libzstd libraries when running "make check". to speed
up the build and test, in this change:

* add FindZstd.cmake which allows us to use the libzstd in system
* extract BuildZstd.cmake for better readability
* add an option named "WITH_SYSTEM_ZSTD", which defaults to "OFF",
  so user can enable it on demand.

Signed-off-by: Kefu Chai <kchai@redhat.com>
cmake/modules/BuildZstd.cmake [new file with mode: 0644]
cmake/modules/FindZstd.cmake [new file with mode: 0644]
src/compressor/zstd/CMakeLists.txt

diff --git a/cmake/modules/BuildZstd.cmake b/cmake/modules/BuildZstd.cmake
new file mode 100644 (file)
index 0000000..799b14b
--- /dev/null
@@ -0,0 +1,22 @@
+# libzstd - build it statically
+function(build_Zstd)
+  set(ZSTD_C_FLAGS "-fPIC -Wno-unused-variable -O3")
+
+  include(ExternalProject)
+  ExternalProject_Add(zstd_ext
+    SOURCE_DIR ${CMAKE_SOURCE_DIR}/src/zstd/build/cmake
+    CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
+               -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
+               -DCMAKE_C_FLAGS=${ZSTD_C_FLAGS}
+               -DCMAKE_AR=${CMAKE_AR}
+               -DCMAKE_POSITION_INDEPENDENT_CODE=${ENABLE_SHARED}
+    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/libzstd
+    BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target libzstd_static
+    BUILD_BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/libzstd/lib/libzstd.a"
+    INSTALL_COMMAND "")
+  add_library(Zstd::Zstd STATIC IMPORTED)
+  set_target_properties(Zstd::Zstd PROPERTIES
+    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/src/zstd/lib"
+    IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/libzstd/lib/libzstd.a")
+  add_dependencies(Zstd::Zstd zstd_ext)
+endfunction()
diff --git a/cmake/modules/FindZstd.cmake b/cmake/modules/FindZstd.cmake
new file mode 100644 (file)
index 0000000..44d2dc3
--- /dev/null
@@ -0,0 +1,51 @@
+# Try to find liblz4
+#
+# Once done, this will define
+#
+# Zstd_FOUND
+# Zstd_INCLUDE_DIRS
+# Zstd_LIBRARIES
+# Zstd_VERSION_STRING
+# Zstd_VERSION_MAJOR
+# Zstd_VERSION_MINOR
+# Zstd_VERSION_RELEASE
+
+find_path(Zstd_INCLUDE_DIR
+  NAMES zstd.h
+  HINTS ${Zstd_ROOT_DIR}/include)
+
+if(Zstd_INCLUDE_DIR AND EXISTS "${Zstd_INCLUDE_DIR}/zstd.h")
+  foreach(ver "MAJOR" "MINOR" "RELEASE")
+    file(STRINGS "${Zstd_INCLUDE_DIR}/zstd.h" Zstd_VER_${ver}_LINE
+      REGEX "^#define[ \t]+ZSTD_VERSION_${ver}[ \t]+[0-9]+$")
+    string(REGEX REPLACE "^#define[ \t]+ZSTD_VERSION_${ver}[ \t]+([0-9]+)$"
+      "\\1" Zstd_VERSION_${ver} "${Zstd_VER_${ver}_LINE}")
+    unset(${Zstd_VER_${ver}_LINE})
+  endforeach()
+  set(Zstd_VERSION_STRING
+    "${Zstd_VERSION_MAJOR}.${Zstd_VERSION_MINOR}.${Zstd_VERSION_RELEASE}")
+endif()
+
+find_library(Zstd_LIBRARY
+  NAMES "${CMAKE_STATIC_LIBRARY_PREFIX}zstd.${CMAKE_STATIC_LIBRARY_SUFFIX}" zstd
+  HINTS ${Zstd_ROOT_DIR}/lib)
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Zstd
+  REQUIRED_VARS Zstd_LIBRARY Zstd_INCLUDE_DIR
+  VERSION_VAR Zstd_VERSION_STRING)
+
+mark_as_advanced(
+  Zstd_LIBRARY
+  Zstd_INCLUDE_DIR)
+
+if(Zstd_FOUND AND NOT (TARGET Zstd::Zstd))
+  set(Zstd_INCLUDE_DIRS ${Zstd_INCLUDE_DIR})
+  set(Zstd_LIBRARIES ${Zstd_LIBRARY})
+  add_library (Zstd::Zstd UNKNOWN IMPORTED)
+  set_target_properties(Zstd::Zstd PROPERTIES
+    INTERFACE_INCLUDE_DIRECTORIES ${Zstd_INCLUDE_DIR}
+    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+    IMPORTED_LOCATION ${Zstd_LIBRARY}
+    VERSION "${Zstd_VERSION_STRING}")
+endif()
index 5427a3317ef3b5464cd8ff052f6bf1515f61b4d9..5a522840acf95b176bb8741b49c79c6247ee6f6a 100644 (file)
@@ -1,33 +1,19 @@
 # zstd
 
-# libzstd - build it statically
-set(ZSTD_C_FLAGS "-fPIC -Wno-unused-variable -O3")
+option(WITH_SYSTEM_ZSTD "use prebuilt libzstd in system" OFF)
 
-include(ExternalProject)
-ExternalProject_Add(zstd_ext
-  SOURCE_DIR ${CMAKE_SOURCE_DIR}/src/zstd/build/cmake
-  CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-             -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-             -DCMAKE_C_FLAGS=${ZSTD_C_FLAGS}
-             -DCMAKE_AR=${CMAKE_AR}
-             -DCMAKE_POSITION_INDEPENDENT_CODE=${ENABLE_SHARED}
-  BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/libzstd
-  BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target libzstd_static
-  BUILD_BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/libzstd/lib/libzstd.a"
-  INSTALL_COMMAND "")
-
-add_library(zstd STATIC IMPORTED)
-set_target_properties(zstd PROPERTIES
-  INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/src/zstd/lib"
-  IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/libzstd/lib/libzstd.a")
-add_dependencies(zstd zstd_ext)
+if(WITH_SYSTEM_ZSTD)
+  find_package(Zstd 1.4.4 REQUIRED)
+else()
+  include(BuildZstd)
+  build_Zstd()
+endif()
 
 set(zstd_sources
-  CompressionPluginZstd.cc
-)
+  CompressionPluginZstd.cc)
 
 add_library(ceph_zstd SHARED ${zstd_sources})
-target_link_libraries(ceph_zstd PRIVATE zstd $<$<PLATFORM_ID:Windows>:ceph-common>)
+target_link_libraries(ceph_zstd PRIVATE Zstd::Zstd $<$<PLATFORM_ID:Windows>:ceph-common>)
 set_target_properties(ceph_zstd PROPERTIES
   VERSION 2.0.0
   SOVERSION 2