Przeglądaj źródła

FindTIFF: add component CXX to include the C++ wrapper libtiffxx

Fixes: #20860
Joachim Wuttke (h) 5 lat temu
rodzic
commit
a468cc431c

+ 5 - 0
Help/release/dev/FindTIFF-tiffxx.rst

@@ -0,0 +1,5 @@
+FindTIFF-tiffxx
+---------------
+
+* The :module:`FindTIFF` module gained a ``CXX`` component to
+  find the ``tiffxx`` library containing C++ bindings.

+ 93 - 6
Modules/FindTIFF.cmake

@@ -5,7 +5,14 @@
 FindTIFF
 --------
 
-Find the TIFF library (``libtiff``).
+Find the TIFF library (``libtiff``, https://libtiff.gitlab.io/libtiff/).
+
+Optional COMPONENTS
+^^^^^^^^^^^^^^^^^^^
+
+This module supports the optional component `CXX`, for use with the COMPONENTS
+argument of the :command:`find_package` command. This component has an associated
+imported target, as described below.
 
 Imported targets
 ^^^^^^^^^^^^^^^^
@@ -15,6 +22,11 @@ This module defines the following :prop_tgt:`IMPORTED` targets:
 ``TIFF::TIFF``
   The TIFF library, if found.
 
+``TIFF::CXX``
+  The C++ wrapper libtiffxx, if requested by the `COMPONENTS CXX` option,
+  if the compiler is not MSVC (which includes the C++ wrapper in libtiff),
+  and if found.
+
 Result variables
 ^^^^^^^^^^^^^^^^
 
@@ -36,10 +48,19 @@ The following cache variables may also be set:
 
 ``TIFF_INCLUDE_DIR``
   the directory containing the TIFF headers
-``TIFF_LIBRARY``
-  the path to the TIFF library
+``TIFF_LIBRARY_RELEASE``
+  the path to the TIFF library for release configurations
+``TIFF_LIBRARY_DEBUG``
+  the path to the TIFF library for debug configurations
+``TIFFXX_LIBRARY_RELEASE``
+  the path to the TIFFXX library for release configurations
+``TIFFXX_LIBRARY_DEBUG``
+  the path to the TIFFXX library for debug configurations
 #]=======================================================================]
 
+cmake_policy(PUSH)
+cmake_policy(SET CMP0057 NEW)
+
 find_path(TIFF_INCLUDE_DIR tiff.h)
 
 set(TIFF_NAMES ${TIFF_NAMES} tiff libtiff tiff3 libtiff3)
@@ -54,8 +75,6 @@ if(NOT TIFF_LIBRARY)
   select_library_configurations(TIFF)
   mark_as_advanced(TIFF_LIBRARY_RELEASE TIFF_LIBRARY_DEBUG)
 endif()
-unset(TIFF_NAMES)
-unset(TIFF_NAMES_DEBUG)
 
 if(TIFF_INCLUDE_DIR AND EXISTS "${TIFF_INCLUDE_DIR}/tiffvers.h")
     file(STRINGS "${TIFF_INCLUDE_DIR}/tiffvers.h" tiff_version_str
@@ -66,13 +85,46 @@ if(TIFF_INCLUDE_DIR AND EXISTS "${TIFF_INCLUDE_DIR}/tiffvers.h")
     unset(tiff_version_str)
 endif()
 
+foreach(_comp IN LISTS TIFF_FIND_COMPONENTS)
+  if(_comp STREQUAL "CXX")
+    if(MSVC)
+      # C++ bindings are built into the main tiff library.
+      set(TIFF_CXX_FOUND 1)
+    else()
+      foreach(name ${TIFF_NAMES})
+        list(APPEND TIFFXX_NAMES "${name}xx")
+        list(APPEND TIFFXX_NAMES_DEBUG "${name}xxd")
+      endforeach()
+      find_library(TIFFXX_LIBRARY_RELEASE NAMES ${TIFFXX_NAMES})
+      find_library(TIFFXX_LIBRARY_DEBUG NAMES ${TIFFXX_NAMES_DEBUG})
+      include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
+      select_library_configurations(TIFFXX)
+      mark_as_advanced(TIFFXX_LIBRARY_RELEASE TIFFXX_LIBRARY_DEBUG)
+      unset(TIFFXX_NAMES)
+      unset(TIFFXX_NAMES_DEBUG)
+      if(TIFFXX_LIBRARY)
+        set(TIFF_CXX_FOUND 1)
+      endif()
+    endif()
+  endif()
+endforeach()
+unset(_comp)
+
+unset(TIFF_NAMES)
+unset(TIFF_NAMES_DEBUG)
+
 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
 FIND_PACKAGE_HANDLE_STANDARD_ARGS(TIFF
+                                  HANDLE_COMPONENTS
                                   REQUIRED_VARS TIFF_LIBRARY TIFF_INCLUDE_DIR
                                   VERSION_VAR TIFF_VERSION_STRING)
 
 if(TIFF_FOUND)
   set(TIFF_LIBRARIES ${TIFF_LIBRARY})
+  if("CXX" IN_LIST TIFF_FIND_COMPONENTS AND NOT MSVC)
+    list(APPEND TIFF_LIBRARIES ${TIFFXX_LIBRARY})
+  endif()
+
   set(TIFF_INCLUDE_DIRS "${TIFF_INCLUDE_DIR}")
 
   if(NOT TARGET TIFF::TIFF)
@@ -101,6 +153,41 @@ if(TIFF_FOUND)
         IMPORTED_LOCATION_DEBUG "${TIFF_LIBRARY_DEBUG}")
     endif()
   endif()
+
+  if(NOT TARGET TIFF::CXX)
+    if(MSVC)
+      add_library(TIFF::CXX INTERFACE IMPORTED)
+      set_property(TARGET TIFF::CXX PROPERTY INTERFACE_LINK_LIBRARIES TIFF::TIFF)
+    else()
+      add_library(TIFF::CXX UNKNOWN IMPORTED)
+      set_property(TARGET TIFF::CXX PROPERTY INTERFACE_LINK_LIBRARIES TIFF::TIFF)
+      if(TIFF_INCLUDE_DIRS)
+        set_target_properties(TIFF::CXX PROPERTIES
+          INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIRS}")
+      endif()
+      if(EXISTS "${TIFFXX_LIBRARY}")
+        set_target_properties(TIFF::CXX PROPERTIES
+          IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
+          IMPORTED_LOCATION "${TIFFXX_LIBRARY}")
+      endif()
+      if(EXISTS "${TIFFXX_LIBRARY_RELEASE}")
+        set_property(TARGET TIFF::CXX APPEND PROPERTY
+          IMPORTED_CONFIGURATIONS RELEASE)
+        set_target_properties(TIFF::CXX PROPERTIES
+          IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
+          IMPORTED_LOCATION_RELEASE "${TIFFXX_LIBRARY_RELEASE}")
+      endif()
+      if(EXISTS "${TIFFXX_LIBRARY_DEBUG}")
+        set_property(TARGET TIFF::CXX APPEND PROPERTY
+          IMPORTED_CONFIGURATIONS DEBUG)
+        set_target_properties(TIFF::CXX PROPERTIES
+          IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
+          IMPORTED_LOCATION_DEBUG "${TIFFXX_LIBRARY_DEBUG}")
+      endif()
+    endif()
+  endif()
+
 endif()
 
-mark_as_advanced(TIFF_INCLUDE_DIR TIFF_LIBRARY)
+mark_as_advanced(TIFF_INCLUDE_DIR)
+cmake_policy(POP)

+ 11 - 2
Tests/FindTIFF/Test/CMakeLists.txt

@@ -1,14 +1,23 @@
 cmake_minimum_required(VERSION 3.1)
-project(TestFindTIFF C)
+project(TestFindTIFF)
 include(CTest)
 
-find_package(TIFF REQUIRED)
+find_package(TIFF REQUIRED COMPONENTS CXX)
 
 add_executable(test_tiff_tgt main.c)
 target_link_libraries(test_tiff_tgt TIFF::TIFF)
 add_test(NAME test_tiff_tgt COMMAND test_tiff_tgt)
 
+add_executable(test_tiffxx_tgt main.cxx)
+target_link_libraries(test_tiffxx_tgt TIFF::CXX)
+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})
 add_test(NAME test_tiff_var COMMAND test_tiff_var)
+
+add_executable(test_tiffxx_var main.cxx)
+target_include_directories(test_tiffxx_var PRIVATE ${TIFF_INCLUDE_DIRS})
+target_link_libraries(test_tiffxx_var PRIVATE ${TIFF_LIBRARIES})
+add_test(NAME test_tiffxx_var COMMAND test_tiffxx_var)

+ 16 - 0
Tests/FindTIFF/Test/main.cxx

@@ -0,0 +1,16 @@
+#include <fstream>
+
+#include <assert.h>
+#include <tiffio.hxx>
+
+int main()
+{
+  /* Without any TIFF file to open, test that the call fails as
+     expected.  This tests that linking worked. */
+  TIFF* tiff = TIFFOpen("invalid.tiff", "r");
+  assert(!tiff);
+
+  std::ifstream s;
+  TIFF* tiffxx = TIFFStreamOpen("invalid.tiff", &s);
+  return 0;
+}