From 49ae62fe9a9fa68837e951d1aab886c5310ee119 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Mon, 22 Jun 2020 23:12:25 +0200
Subject: [PATCH] using FindTIFF as submitted to CMake upstream

Our patch, https://gitlab.kitware.com/cmake/cmake/-/merge_requests/4934,
is expected to be released with CMake 3.19. Therefore we shall removed
FindTIFF.cmake from the BornAgain sources, once our minimum required
CMake version has been incremented accordingly.
---
 .../modules/SearchInstalledSoftware.cmake     |   2 +-
 cmake/generic/modules/FindTIFF.cmake          | 236 ++++++++++++++----
 2 files changed, 182 insertions(+), 56 deletions(-)

diff --git a/cmake/bornagain/modules/SearchInstalledSoftware.cmake b/cmake/bornagain/modules/SearchInstalledSoftware.cmake
index 9cba47767ba..6bfb1393a97 100644
--- a/cmake/bornagain/modules/SearchInstalledSoftware.cmake
+++ b/cmake/bornagain/modules/SearchInstalledSoftware.cmake
@@ -43,7 +43,7 @@ endif()
 # --- Tiff ---
 if(BORNAGAIN_TIFF_SUPPORT)
     message(STATUS "Looking for libtiff (use -DBORNAGAIN_TIFF_SUPPORT=OFF to disable)")
-    find_package(TIFF 4.0.2 REQUIRED)
+    find_package(TIFF 4.0.2 REQUIRED COMPONENTS CXX)
 endif()
 
 # --- Python ---
diff --git a/cmake/generic/modules/FindTIFF.cmake b/cmake/generic/modules/FindTIFF.cmake
index fed5d769fe6..64593422797 100644
--- a/cmake/generic/modules/FindTIFF.cmake
+++ b/cmake/generic/modules/FindTIFF.cmake
@@ -1,36 +1,88 @@
-#.rst:
-# FindTIFF
-# --------
-#
-# Find TIFF and TIFF/C++libraries
-#
-# This module defines
-#
-# ::
-#
-#   TIFF_INCLUDE_DIR, where to find tiff.h, etc.
-#   TIFF_LIBRARIES, libraries to link against to use TIFF.
-#   TIFF_FOUND, If false, do not try to use TIFF.
-#
-# also defined, but not for general use are
-#
-# ::
-#
-#   TIFF_LIBRARY, where to find the TIFF library.
-
-# This is a heavily patched version of the CMake module FindTIFF.cmake
-# Copyright 2002-2009 Kitware, Inc.
-# Copyright 2013- BornAgain team
-# Distributed under the OSI-approved BSD License
-
-if(WIN32)
-    find_path(TIFF_INCLUDE_DIR tiff.h PATHS ${CMAKE_INCLUDE_PATH}/libtiff NO_SYSTEM_ENVIRONMENT_PATH)
-else()
-    find_path(TIFF_INCLUDE_DIR tiff.h)
-endif()
+# This FindTIFF.cmake has been contributed by us to upstream CMake.
+# It is expected to be released with CMake 3.19.
+# See https://gitlab.kitware.com/cmake/cmake/-/merge_requests/4934.
+
+# Therefore we shall delete this file from BornAgain in a couple of years
+# when our minimum required CMake version has been incremented to 3.19.
+# - JWu, jun2020.
+
+# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+# file Copyright.txt or https://cmake.org/licensing for details.
+
+#[=======================================================================[.rst:
+FindTIFF
+--------
+
+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
+^^^^^^^^^^^^^^^^
+
+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
+^^^^^^^^^^^^^^^^
+
+This module will set the following variables in your project:
+
+``TIFF_FOUND``
+  true if the TIFF headers and libraries were found
+``TIFF_INCLUDE_DIR``
+  the directory containing the TIFF headers
+``TIFF_INCLUDE_DIRS``
+  the directory containing the TIFF headers
+``TIFF_LIBRARIES``
+  TIFF libraries to be linked
+
+Cache variables
+^^^^^^^^^^^^^^^
+
+The following cache variables may also be set:
+
+``TIFF_INCLUDE_DIR``
+  the directory containing the TIFF headers
+``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
+#]=======================================================================]
 
-set(TIFF_NAMES ${TIFF_NAMES} libtiff tiff libtiff3 tiff3)
-find_library(TIFF_LIBRARY NAMES ${TIFF_NAMES})
+cmake_policy(PUSH)
+cmake_policy(SET CMP0057 NEW)
+
+find_path(TIFF_INCLUDE_DIR tiff.h)
+
+set(TIFF_NAMES ${TIFF_NAMES} tiff libtiff tiff3 libtiff3)
+foreach(name ${TIFF_NAMES})
+  list(APPEND TIFF_NAMES_DEBUG "${name}d")
+endforeach()
+
+if(NOT TIFF_LIBRARY)
+  find_library(TIFF_LIBRARY_RELEASE NAMES ${TIFF_NAMES})
+  find_library(TIFF_LIBRARY_DEBUG NAMES ${TIFF_NAMES_DEBUG})
+  include(SelectLibraryConfigurations) # patched because we are outside CMake
+  select_library_configurations(TIFF)
+  mark_as_advanced(TIFF_LIBRARY_RELEASE TIFF_LIBRARY_DEBUG)
+endif()
 
 if(TIFF_INCLUDE_DIR AND EXISTS "${TIFF_INCLUDE_DIR}/tiffvers.h")
     file(STRINGS "${TIFF_INCLUDE_DIR}/tiffvers.h" tiff_version_str
@@ -41,35 +93,109 @@ if(TIFF_INCLUDE_DIR AND EXISTS "${TIFF_INCLUDE_DIR}/tiffvers.h")
     unset(tiff_version_str)
 endif()
 
-# handle the QUIETLY and REQUIRED arguments and set TIFF_FOUND to TRUE if
-# all listed variables are TRUE
-include(FindPackageHandleStandardArgs)
+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(SelectLibraryConfigurations)  # patched because we are outside 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(FindPackageHandleStandardArgs) # patched because we are outside 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} )
-endif()
+  set(TIFF_LIBRARIES ${TIFF_LIBRARY})
+  if("CXX" IN_LIST TIFF_FIND_COMPONENTS AND NOT MSVC)
+    list(APPEND TIFF_LIBRARIES ${TIFFXX_LIBRARY})
+  endif()
 
-if(TIFF_FOUND)
-    message(STATUS "Found TIFF version ${TIFF_VERSION_STRING}, includes at ${TIFF_INCLUDE_DIR}, libraries at ${TIFF_LIBRARIES}")
-
-    if(NOT WIN32)
-        # looking for C++ version of library libtiffxx.so
-        list(LENGTH TIFF_LIBRARIES len)
-        if(len EQUAL 1)
-            get_filename_component(tiff_library_name ${TIFF_LIBRARIES} NAME_WE )
-            get_filename_component(tiff_path ${TIFF_LIBRARIES} DIRECTORY )
-            get_filename_component(tiff_ext ${TIFF_LIBRARIES} EXT )
-            set(cpp_tiff_library "${tiff_path}/${tiff_library_name}xx${tiff_ext}")
-            if(NOT EXISTS ${cpp_tiff_library})
-                message(FATAL_ERROR "Could NOT find TIFF/C++ library ${cpp_tiff_library}.")
-            endif()
-            message(STATUS "Found TIFF C++ library ${cpp_tiff_library}")
-            set(TIFF_LIBRARIES ${TIFF_LIBRARIES};${cpp_tiff_library})
-        endif()
+  set(TIFF_INCLUDE_DIRS "${TIFF_INCLUDE_DIR}")
+
+  if(NOT TARGET TIFF::TIFF)
+    add_library(TIFF::TIFF UNKNOWN IMPORTED)
+    if(TIFF_INCLUDE_DIRS)
+      set_target_properties(TIFF::TIFF PROPERTIES
+        INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIRS}")
+    endif()
+    if(EXISTS "${TIFF_LIBRARY}")
+      set_target_properties(TIFF::TIFF PROPERTIES
+        IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+        IMPORTED_LOCATION "${TIFF_LIBRARY}")
+    endif()
+    if(EXISTS "${TIFF_LIBRARY_RELEASE}")
+      set_property(TARGET TIFF::TIFF APPEND PROPERTY
+        IMPORTED_CONFIGURATIONS RELEASE)
+      set_target_properties(TIFF::TIFF PROPERTIES
+        IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
+        IMPORTED_LOCATION_RELEASE "${TIFF_LIBRARY_RELEASE}")
     endif()
+    if(EXISTS "${TIFF_LIBRARY_DEBUG}")
+      set_property(TARGET TIFF::TIFF APPEND PROPERTY
+        IMPORTED_CONFIGURATIONS DEBUG)
+      set_target_properties(TIFF::TIFF PROPERTIES
+        IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
+        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 TIFF_LIBRARY_DLL)
+mark_as_advanced(TIFF_INCLUDE_DIR)
+cmake_policy(POP)
-- 
GitLab