libtiff: Merge branch 'cmake_FindDeflate_bugs_see_526' into 'master'

From b2cdf4292ac4cb978f32e18b85f0d8e1630cbd94 Mon Sep 17 00:00:00 2001
From: Su_Laus <[EMAIL REDACTED]>
Date: Sun, 21 May 2023 18:13:50 +0200
Subject: [PATCH] CMake: FindDeflate several errors (see #526)

There are CMake issues if the library is not included in the environment path and only set with CMake -D option.
- For FindDeflate.cmake, FindJBIG.cmake, FindLERC.cmake, FindWebP.cmake, FindZSTD.cmake:
  Set IMPORTED_LOCATION (without debug or release) if neither <library>_LIBRARY_RELEASE nor <library>_LIBRARY_DEBUG were set.
- FindDeflate.cmake: Correct code to retrieve library version information from libdeflate.h
- FindLERC.cmake version string return added.
---
 cmake/FindDeflate.cmake | 15 +++++++--------
 cmake/FindJBIG.cmake    |  2 +-
 cmake/FindLERC.cmake    | 18 ++++++++++++++++--
 cmake/FindWebP.cmake    |  2 +-
 cmake/FindZSTD.cmake    |  2 +-
 5 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/cmake/FindDeflate.cmake b/cmake/FindDeflate.cmake
index ef6a5765..3d606aa0 100644
--- a/cmake/FindDeflate.cmake
+++ b/cmake/FindDeflate.cmake
@@ -66,15 +66,14 @@ unset(Deflate_OLD_FIND_LIBRARY_PREFIXES)
 
 mark_as_advanced(Deflate_INCLUDE_DIR)
 
-if(Deflate_INCLUDE_DIR AND EXISTS "${Deflate_INCLUDE_DIR}/deflate.h")
-    file(STRINGS "${Deflate_INCLUDE_DIR}/libdeflate.h" Deflate_H REGEX "^#define LIBDEFLATE_VERSION_STRING\s+\"[^\"]*\"$")
-
-    string(REGEX REPLACE "^.*Deflate_VERSION \"([0-9]+).*$" "\\1" Deflate_MAJOR_VERSION "${Deflate_H}")
-    string(REGEX REPLACE "^.*Deflate_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" Deflate_MINOR_VERSION  "${Deflate_H}")
+if(Deflate_INCLUDE_DIR AND EXISTS "${Deflate_INCLUDE_DIR}/libdeflate.h")
+    file(STRINGS "${Deflate_INCLUDE_DIR}/libdeflate.h" Deflate_H REGEX "^#define[\t ]+LIBDEFLATE_VERSION_STRING[\t ]+.*")
+    string(REGEX REPLACE "^.*LIBDEFLATE_VERSION_STRING[\t ]+\"([0-9]+).*$" "\\1" Deflate_MAJOR_VERSION "${Deflate_H}")
+    string(REGEX REPLACE "^.*LIBDEFLATE_VERSION_STRING[\t ]\"[0-9]+\\.([0-9]+).*$" "\\1" Deflate_MINOR_VERSION  "${Deflate_H}")
     set(Deflate_VERSION_STRING "${Deflate_MAJOR_VERSION}.${Deflate_MINOR_VERSION}")
 
-    set(Deflate_MAJOR_VERSION "${Deflate_VERSION_MAJOR}")
-    set(Deflate_MINOR_VERSION "${Deflate_VERSION_MINOR}")
+    set(Deflate_VERSION_MAJOR "${Deflate_MAJOR_VERSION}")
+    set(Deflate_VERSION_MINOR "${Deflate_MINOR_VERSION}")
 endif()
 
 include(FindPackageHandleStandardArgs)
@@ -110,7 +109,7 @@ if(Deflate_FOUND)
 
         if(NOT Deflate_LIBRARY_RELEASE AND NOT Deflate_LIBRARY_DEBUG)
             set_target_properties(Deflate::Deflate PROPERTIES
-                    IMPORTED_LOCATION_RELEASE "${Deflate_LIBRARY}")
+                    IMPORTED_LOCATION "${Deflate_LIBRARY}")
         endif()
     endif()
 endif()
diff --git a/cmake/FindJBIG.cmake b/cmake/FindJBIG.cmake
index f0c232c3..4d8e106e 100644
--- a/cmake/FindJBIG.cmake
+++ b/cmake/FindJBIG.cmake
@@ -110,7 +110,7 @@ if(JBIG_FOUND)
 
         if(NOT JBIG_LIBRARY_RELEASE AND NOT JBIG_LIBRARY_DEBUG)
             set_target_properties(JBIG::JBIG PROPERTIES
-                    IMPORTED_LOCATION_RELEASE "${JBIG_LIBRARY}")
+                    IMPORTED_LOCATION "${JBIG_LIBRARY}")
         endif()
     endif()
 endif()
diff --git a/cmake/FindLERC.cmake b/cmake/FindLERC.cmake
index ade742a1..aff7ea66 100644
--- a/cmake/FindLERC.cmake
+++ b/cmake/FindLERC.cmake
@@ -11,7 +11,7 @@ IMPORTED Targets
 ^^^^^^^^^^^^^^^^
 
 This module defines :prop_tgt:`IMPORTED` target ``LERC::LERC``, if
-CMath has been found.
+LERC has been found.
 
 Result Variables
 ^^^^^^^^^^^^^^^^
@@ -23,6 +23,7 @@ This module defines the following variables:
   LERC_INCLUDE_DIRS   - where to find Lerc_c_api.h, etc.
   LERC_LIBRARIES      - List of libraries when using LERC.
   LERC_FOUND          - True if LERC found.
+  LERC_VERSION_STRING - version number as a string (ex: "4.0.1")
 
 #]=======================================================================]
 
@@ -47,6 +48,19 @@ unset(LERC_NAMES)
 
 mark_as_advanced(LERC_INCLUDE_DIR)
 
+if(LERC_INCLUDE_DIR AND EXISTS "${LERC_INCLUDE_DIR}/Lerc_c_api.h")
+    file(STRINGS "${LERC_INCLUDE_DIR}/Lerc_c_api.h" LERC_H  REGEX "^#define[\t ]+LERC_VERSION_[A-Z]+[\t ]+[0-9]+")
+    string(REGEX REPLACE "^.*#define[\t ]+LERC_VERSION_MAJOR[\t ]+([0-9]+).*$" "\\1" LERC_VERSION_MAJOR "${LERC_H}")
+    string(REGEX REPLACE "^.*#define[\t ]+LERC_VERSION_MINOR[\t ]+([0-9]+).*$" "\\1" LERC_VERSION_MINOR "${LERC_H}")
+    string(REGEX REPLACE "^.*#define[\t ]+LERC_VERSION_PATCH[\t ]+([0-9]+).*$" "\\1" LERC_VERSION_PATCH "${LERC_H}")
+    set(LERC_VERSION_STRING "${LERC_VERSION_MAJOR}.${LERC_VERSION_MINOR}.${LERC_VERSION_PATCH}")
+    unset(LERC_H)
+    unset(LERC_VERSION_MAJOR)
+    unset(LERC_VERSION_MINOR)
+    unset(LERC_VERSION_PATCH)
+endif()
+
+
 include(FindPackageHandleStandardArgs)
 FIND_PACKAGE_HANDLE_STANDARD_ARGS(LERC
         REQUIRED_VARS LERC_LIBRARY LERC_INCLUDE_DIR
@@ -80,7 +94,7 @@ if(LERC_FOUND)
 
         if(NOT LERC_LIBRARY_RELEASE AND NOT LERC_LIBRARY_DEBUG)
             set_target_properties(LERC::LERC PROPERTIES
-                    IMPORTED_LOCATION_RELEASE "${LERC_LIBRARY}")
+                    IMPORTED_LOCATION "${LERC_LIBRARY}")
         endif()
     endif()
 endif()
diff --git a/cmake/FindWebP.cmake b/cmake/FindWebP.cmake
index aa3ed798..675bf8c3 100644
--- a/cmake/FindWebP.cmake
+++ b/cmake/FindWebP.cmake
@@ -85,7 +85,7 @@ if(WebP_FOUND)
 
         if(NOT WebP_LIBRARY_RELEASE AND NOT WebP_LIBRARY_DEBUG)
             set_target_properties(WebP::webp PROPERTIES
-                    IMPORTED_LOCATION_RELEASE "${WebP_LIBRARY}")
+                    IMPORTED_LOCATION "${WebP_LIBRARY}")
         endif()
     endif()
 endif()
diff --git a/cmake/FindZSTD.cmake b/cmake/FindZSTD.cmake
index a4066300..2e217f11 100644
--- a/cmake/FindZSTD.cmake
+++ b/cmake/FindZSTD.cmake
@@ -101,7 +101,7 @@ if(ZSTD_FOUND)
 
         if(NOT ZSTD_LIBRARY_RELEASE AND NOT ZSTD_LIBRARY_DEBUG)
             set_target_properties(ZSTD::ZSTD PROPERTIES
-                    IMPORTED_LOCATION_RELEASE "${ZSTD_LIBRARY}")
+                    IMPORTED_LOCATION "${ZSTD_LIBRARY}")
         endif()
     endif()
 endif()