Browse Source

Merge topic 'patch-FindTIFF-version'

8076414d2d FindTIFF: Add TIFF_VERSION

Acked-by: Kitware Robot <[email protected]>
Merge-request: !11021
Brad King 3 months ago
parent
commit
0e48bf825d

+ 3 - 0
Help/release/dev/find-modules.rst

@@ -58,3 +58,6 @@ Find Modules
   modules now provide their respective ``<PackageName>_VERSION`` result
   variables. Previous ``<PACKAGENAME>_VERSION_STRING`` result variables
   are deprecated.
+
+* The :module:`FindTIFF` module now provides a ``TIFF_VERSION`` result
+  variable.  The ``TIFF_VERSION_STRING`` result variable is deprecated.

+ 35 - 15
Modules/FindTIFF.cmake

@@ -5,14 +5,25 @@
 FindTIFF
 --------
 
-Finds the `TIFF library <https://libtiff.gitlab.io/libtiff/>`_ (``libtiff``).
+Finds the `TIFF library <https://libtiff.gitlab.io/libtiff/>`_ (``libtiff``):
+
+.. code-block:: cmake
+
+  find_package(TIFF [<version>] [COMPONENTS <components>...] [...])
+
 This module also takes into account the upstream TIFF library's exported CMake
 package configuration, if available.
 
 Components
 ^^^^^^^^^^
 
-This module supports the following components:
+This module supports optional components which can be specified with:
+
+.. code-block:: cmake
+
+  find_package(TIFF [COMPONENTS <components>...])
+
+Supported components include:
 
 ``CXX``
   .. versionadded:: 3.19
@@ -20,12 +31,6 @@ This module supports the following components:
   Optional component that ensures that the C++ wrapper library (``libtiffxx``)
   is found.
 
-Components can be specified using the standard syntax:
-
-.. code-block:: cmake
-
-  find_package(TIFF [COMPONENTS <components>...])
-
 Imported Targets
 ^^^^^^^^^^^^^^^^
 
@@ -51,10 +56,12 @@ Result Variables
 This module defines the following variables:
 
 ``TIFF_FOUND``
-  Boolean indicating whether the TIFF is found.
+  Boolean indicating whether (the requested version of) TIFF is found.
 
-``TIFF_VERSION_STRING``
-  The version of the TIFF library found.
+``TIFF_VERSION``
+  .. versionadded:: 4.2
+
+  The version of TIFF library found.
 
 ``TIFF_INCLUDE_DIRS``
   The directory containing the TIFF headers.
@@ -90,6 +97,17 @@ The following cache variables may also be set:
 
   The path to the TIFFXX library for debug configurations.
 
+Deprecated Variables
+^^^^^^^^^^^^^^^^^^^^
+
+The following variables are provided for backward compatibility:
+
+``TIFF_VERSION_STRING``
+  .. deprecated:: 4.2
+    Superseded by the ``TIFF_VERSION``.
+
+  The version of TIFF library found.
+
 Examples
 ^^^^^^^^
 
@@ -244,7 +262,8 @@ if (Tiff_FOUND)
       endif ()
     endif ()
   endif ()
-  set(TIFF_VERSION_STRING "${Tiff_VERSION}")
+  set(TIFF_VERSION "${Tiff_VERSION}")
+  set(TIFF_VERSION_STRING "${TIFF_VERSION}")
   foreach (_TIFF_component IN LISTS TIFF_FIND_COMPONENTS)
     set(TIFF_${_TIFF_component}_FOUND "${Tiff_${_TIFF_component}_FOUND}")
   endforeach ()
@@ -254,7 +273,7 @@ if (Tiff_FOUND)
   find_package_handle_standard_args(TIFF
                                     HANDLE_COMPONENTS
                                     REQUIRED_VARS Tiff_DIR
-                                    VERSION_VAR TIFF_VERSION_STRING)
+                                    VERSION_VAR TIFF_VERSION)
 
   cmake_policy(POP)
   return ()
@@ -280,7 +299,8 @@ if(TIFF_INCLUDE_DIR AND EXISTS "${TIFF_INCLUDE_DIR}/tiffvers.h")
          REGEX "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version .*")
 
     string(REGEX REPLACE "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version +([^ \\n]*).*"
-           "\\1" TIFF_VERSION_STRING "${tiff_version_str}")
+           "\\1" TIFF_VERSION "${tiff_version_str}")
+    set(TIFF_VERSION_STRING "${TIFF_VERSION}")
     unset(tiff_version_str)
 endif()
 
@@ -316,7 +336,7 @@ include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(TIFF
                                   HANDLE_COMPONENTS
                                   REQUIRED_VARS TIFF_LIBRARY TIFF_INCLUDE_DIR
-                                  VERSION_VAR TIFF_VERSION_STRING)
+                                  VERSION_VAR TIFF_VERSION)
 
 if(TIFF_FOUND)
   set(TIFF_LIBRARIES ${TIFF_LIBRARY})

+ 1 - 0
Tests/CMakeOnly/AllFindModules/CMakeLists.txt

@@ -126,6 +126,7 @@ foreach(
     PNG PostgreSQL Protobuf
     Ruby RUBY
     SDL SWIG
+    TIFF
     ZLIB
 )
   check_version_string(${VTEST} ${VTEST}_VERSION)

+ 8 - 0
Tests/FindTIFF/Test/CMakeLists.txt

@@ -6,6 +6,10 @@ find_package(TIFF REQUIRED COMPONENTS CXX)
 
 add_executable(test_tiff_tgt main.c)
 target_link_libraries(test_tiff_tgt TIFF::TIFF)
+target_compile_definitions(
+  test_tiff_tgt
+  PRIVATE CMAKE_EXPECTED_TIFF_VERSION="${TIFF_VERSION}"
+)
 add_test(NAME test_tiff_tgt COMMAND test_tiff_tgt)
 
 add_executable(test_tiffxx_tgt main.cxx)
@@ -15,6 +19,10 @@ add_test(NAME test_tiffxx_tgt COMMAND test_tiffxx_tgt)
 add_executable(test_tiff_var main.c)
 target_include_directories(test_tiff_var PRIVATE ${TIFF_INCLUDE_DIRS})
 target_link_libraries(test_tiff_var PRIVATE ${TIFF_LIBRARIES})
+target_compile_definitions(
+  test_tiff_var
+  PRIVATE CMAKE_EXPECTED_TIFF_VERSION="${TIFF_VERSION}"
+)
 add_test(NAME test_tiff_var COMMAND test_tiff_var)
 
 add_executable(test_tiffxx_var main.cxx)

+ 24 - 1
Tests/FindTIFF/Test/main.c

@@ -1,4 +1,6 @@
 #include <assert.h>
+#include <stdio.h>
+#include <string.h>
 #include <tiffio.h>
 
 int main(void)
@@ -8,5 +10,26 @@ int main(void)
   TIFF* tiff = TIFFOpen("invalid.tiff", "r");
   assert(!tiff);
 
-  return 0;
+  char const* info = TIFFGetVersion();
+  char const* version_prefix = "Version ";
+  char const* start = strstr(info, version_prefix);
+  char version_str[16];
+
+  if (start) {
+    start += strlen(version_prefix);
+    int major, minor, patch;
+
+    if (sscanf(start, "%d.%d.%d", &major, &minor, &patch) == 3) {
+      snprintf(version_str, sizeof(version_str), "%d.%d.%d", major, minor,
+               patch);
+      printf("Found TIFF version %s, expected version %s\n", version_str,
+             CMAKE_EXPECTED_TIFF_VERSION);
+
+      return strcmp(version_str, CMAKE_EXPECTED_TIFF_VERSION);
+    }
+  }
+
+  fprintf(stderr,
+          "TIFF version not found or TIFF version could not be parsed\n");
+  return 1;
 }