Skip to content

Commit

Permalink
Fix/zstd vendor does not find system zstd (#1111)
Browse files Browse the repository at this point in the history
cmake did not find the Findzstd.cmake in cmake/Modules

Since we cannot use pkg-config for some Windows issues, the parsing of
the version is done by looking for the string in zstd.h.

Signed-off-by: Matthias Schoepfer <m.schoepfer@rethinkrobotics.com>
  • Loading branch information
DasRoteSkelett authored Oct 6, 2022
1 parent 16cb47f commit e7e7269
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 5 additions & 2 deletions zstd_vendor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ project(zstd_vendor)

find_package(ament_cmake REQUIRED)

list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")

option(FORCE_BUILD_VENDOR_PKG
"Build zstd from source, even if system-installed package is available"
OFF)

if(NOT FORCE_BUILD_VENDOR_PKG)
find_package(zstd QUIET)
# We need at least VERSION 1.4.4, version check is done in Findzstd.cmake
find_package(zstd 1.4.4 QUIET)
endif()

macro(build_zstd)
Expand Down Expand Up @@ -59,7 +62,7 @@ macro(build_zstd)
USE_SOURCE_PERMISSIONS)
endmacro()

if (NOT zstd_FOUND OR "${zstd_VERSION}" VERSION_LESS 1.4.4)
if (NOT zstd_FOUND)
build_zstd()
else()
message(STATUS "Found Zstd, skipping build.")
Expand Down
15 changes: 14 additions & 1 deletion zstd_vendor/cmake/Modules/Findzstd.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ endif()
find_path(zstd_INCLUDE_DIR NAMES zstd.h PATH_SUFFIXES "zstd" ${zstd_INCLUDE_PATH})
find_library(zstd_LIBRARY NAMES zstd PATH_SUFFIXES "zstd" ${zstd_LIBRARY_PATH})

if (zstd_INCLUDE_DIR)
file(STRINGS "${zstd_INCLUDE_DIR}/zstd.h" version-file
REGEX "#define[ \t]+ZSTD_VERSION_(MAJOR|MINOR|RELEASE).*")
list(GET version-file 0 major-line)
list(GET version-file 1 minor-line)
list(GET version-file 2 release-line)
string(REGEX REPLACE "^#define[ \t]+ZSTD_VERSION_MAJOR[ \t]+([0-9]+)$" "\\1" ZSTD_VERSION_MAJOR ${major-line})
string(REGEX REPLACE "^#define[ \t]+ZSTD_VERSION_MINOR[ \t]+([0-9]+)$" "\\1" ZSTD_VERSION_MINOR ${minor-line})
string(REGEX REPLACE "^#define[ \t]+ZSTD_VERSION_RELEASE[ \t]+([0-9]+)$" "\\1" ZSTD_VERSION_RELEASE ${release-line})
set(zstd_VERSION ${ZSTD_VERSION_MAJOR}.${ZSTD_VERSION_MINOR}.${ZSTD_VERSION_RELEASE})
endif()

mark_as_advanced(zstd_INCLUDE_DIR zstd_LIBRARY)

# Output variables generation
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(zstd DEFAULT_MSG zstd_LIBRARY zstd_INCLUDE_DIR)
find_package_handle_standard_args(zstd REQUIRED_VARS zstd_LIBRARY zstd_INCLUDE_DIR
VERSION_VAR zstd_VERSION)

set(zstd_FOUND ${ZSTD_FOUND}) # Enforce case-correctness: Set appropriately cased variable...
unset(ZSTD_FOUND) # ...and unset uppercase variable generated by find_package_handle_standard_args
Expand Down

0 comments on commit e7e7269

Please sign in to comment.