cmake: allow use libzstd in system

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>
This commit is contained in:
Kefu Chai 2021-03-31 12:15:17 +08:00
parent 41fb5ff0b2
commit 16fd07244d
3 changed files with 82 additions and 23 deletions

View File

@ -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()

View File

@ -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()

View 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