1010# zstd::libzstd_shared
1111# zstd::libzstd_static
1212
13+ # Function to get zstd version.
14+ function (get_zstd_version_string zstd_INCLUDE_DIR OUT_VAR)
15+
16+ # Check if zstd.h file is present. Expectation is that this function will only
17+ # be called if zstd is found and the include directory is valid.
18+ if (NOT EXISTS "${zstd_INCLUDE_DIR} /zstd.h" )
19+ message (FATAL_ERROR "zstd.h not found in ${zstd_INCLUDE_DIR} . "
20+ "Please set zstd_INCLUDE_DIR to the directory containing zstd.h." )
21+ endif ()
22+
23+ # Read the version string from zstd.h.
24+ # The version is defined as macros ZSTD_VERSION_MAJOR, ZSTD_VERSION_MINOR,
25+ # and ZSTD_VERSION_RELEASE in zstd.h.
26+ file (READ "${zstd_INCLUDE_DIR} /zstd.h" content )
27+ string (REGEX MATCH "#define[ \t ]+ZSTD_VERSION_MAJOR[ \t ]+([0-9]+)" _major_match "${content} " )
28+ string (REGEX MATCH "#define[ \t ]+ZSTD_VERSION_MINOR[ \t ]+([0-9]+)" _minor_match "${content} " )
29+ string (REGEX MATCH "#define[ \t ]+ZSTD_VERSION_RELEASE[ \t ]+([0-9]+)" _patch_match "${content} " )
30+
31+ if (_major_match AND _minor_match AND _patch_match)
32+ string (REGEX REPLACE ".*#define[ \t ]+ZSTD_VERSION_MAJOR[ \t ]+([0-9]+).*" "\\ 1" _major "${_major_match} " )
33+ string (REGEX REPLACE ".*#define[ \t ]+ZSTD_VERSION_MINOR[ \t ]+([0-9]+).*" "\\ 1" _minor "${_minor_match} " )
34+ string (REGEX REPLACE ".*#define[ \t ]+ZSTD_VERSION_RELEASE[ \t ]+([0-9]+).*" "\\ 1" _patch "${_patch_match} " )
35+ set (_version "${_major} .${_minor} .${_patch} " )
36+ set (${OUT_VAR} "${_version} " PARENT_SCOPE)
37+ else ()
38+ set (${OUT_VAR} "" PARENT_SCOPE)
39+ endif ()
40+ endfunction ()
41+
1342if (MSVC OR "${CMAKE_CXX_SIMULATE_ID} " STREQUAL "MSVC" )
1443 set (zstd_STATIC_LIBRARY_SUFFIX "_static\\ ${CMAKE_STATIC_LIBRARY_SUFFIX} $" )
1544else ()
@@ -28,6 +57,12 @@ find_package_handle_standard_args(
2857 zstd_LIBRARY zstd_INCLUDE_DIR
2958)
3059
60+ # Get zstd version.
61+ if (zstd_FOUND AND zstd_INCLUDE_DIR)
62+ get_zstd_version_string("${zstd_INCLUDE_DIR} " zstd_VERSION_STRING)
63+ message (STATUS "Found zstd version ${zstd_VERSION_STRING} ." )
64+ endif ()
65+
3166if (zstd_FOUND)
3267 if (zstd_LIBRARY MATCHES "${zstd_STATIC_LIBRARY_SUFFIX} $" )
3368 set (zstd_STATIC_LIBRARY "${zstd_LIBRARY} " )
0 commit comments