cmake: speed up zstd detection

Before this patch we detected the presence of a specific zstd API to
see if we can use the library. zstd published that API in its first
stable release: v1.0.0 (2016-08-31).

Replace that method by detecting the zstd library version instead and
accepting if it's v1.0.0 or newer. Also display this detected version
and display a warning if the zstd found is unfit for curl.

We use the same version detection method as zstd itself, via its public
C header.

This deviates from autotools which keeps using the slow method of
looking for the API by building a test program. The outcome is the same
as long as zstd keeps offering this API.

Ref: 5a0c8e2439 (2016-08-12, committed)
Ref: https://github.com/facebook/zstd/releases/tag/v0.8.1 (2016-08-18, first released)
Ref: https://github.com/facebook/zstd/releases/tag/v1.0.0

Reviewed-by: Daniel Stenberg
Closes #12200
This commit is contained in:
Viktor Szakats 2023-10-25 23:37:48 +00:00
parent 9ee6da65d8
commit c5d506e9bb
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
2 changed files with 10 additions and 9 deletions

View File

@ -56,11 +56,18 @@ find_library(Zstd_LIBRARY NAMES zstd
${PC_Zstd_LIBRARY_DIRS}
)
if(Zstd_INCLUDE_DIR)
file(READ "${Zstd_INCLUDE_DIR}/zstd.h" _zstd_header)
string(REGEX MATCH ".*define ZSTD_VERSION_MAJOR *([0-9]+).*define ZSTD_VERSION_MINOR *([0-9]+).*define ZSTD_VERSION_RELEASE *([0-9]+)" _zstd_ver "${_zstd_header}")
set(Zstd_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Zstd
REQUIRED_VARS
Zstd_LIBRARY
Zstd_INCLUDE_DIR
VERSION_VAR Zstd_VERSION
)
if(Zstd_FOUND)

View File

@ -54,7 +54,6 @@
# HAVE_GNUTLS_SRP: `gnutls_srp_verifier` present in GnuTLS
# HAVE_SSL_CTX_SET_QUIC_METHOD: `SSL_CTX_set_quic_method` present in OpenSSL/wolfSSL
# HAVE_QUICHE_CONN_SET_QLOG_FD: `quiche_conn_set_qlog_fd` present in QUICHE
# HAVE_ZSTD_CREATEDSTREAM: `ZSTD_createDStream` present in Zstd
#
# For each of the above variables, if the variable is DEFINED (either
# to ON or OFF), the symbol detection will be skipped. If the
@ -610,17 +609,12 @@ option(CURL_ZSTD "Set to ON to enable building curl with zstd support." OFF)
set(HAVE_ZSTD OFF)
if(CURL_ZSTD)
find_package(Zstd REQUIRED)
if(NOT DEFINED HAVE_ZSTD_CREATEDSTREAM)
cmake_push_check_state()
set(CMAKE_REQUIRED_INCLUDES ${Zstd_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES ${Zstd_LIBRARIES})
check_symbol_exists(ZSTD_createDStream "zstd.h" HAVE_ZSTD_CREATEDSTREAM)
cmake_pop_check_state()
endif()
if(Zstd_FOUND AND HAVE_ZSTD_CREATEDSTREAM)
if(Zstd_FOUND AND NOT Zstd_VERSION VERSION_LESS "1.0.0")
set(HAVE_ZSTD ON)
list(APPEND CURL_LIBS ${Zstd_LIBRARIES})
include_directories(${Zstd_INCLUDE_DIRS})
else()
message(WARNING "zstd v1.0.0 or newer is required, disabling zstd support.")
endif()
endif()