1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindTIFF
6--------
7
8Find the TIFF library (``libtiff``, https://libtiff.gitlab.io/libtiff/).
9
10Optional COMPONENTS
11^^^^^^^^^^^^^^^^^^^
12
13This module supports the optional component `CXX`, for use with the COMPONENTS
14argument of the :command:`find_package` command. This component has an associated
15imported target, as described below.
16
17Imported targets
18^^^^^^^^^^^^^^^^
19
20.. versionadded:: 3.5
21
22This module defines the following :prop_tgt:`IMPORTED` targets:
23
24``TIFF::TIFF``
25  The TIFF library, if found.
26
27``TIFF::CXX``
28  .. versionadded:: 3.19
29
30  The C++ wrapper libtiffxx, if requested by the `COMPONENTS CXX` option,
31  if the compiler is not MSVC (which includes the C++ wrapper in libtiff),
32  and if found.
33
34Result variables
35^^^^^^^^^^^^^^^^
36
37This module will set the following variables in your project:
38
39``TIFF_FOUND``
40  true if the TIFF headers and libraries were found
41``TIFF_INCLUDE_DIR``
42  the directory containing the TIFF headers
43``TIFF_INCLUDE_DIRS``
44  the directory containing the TIFF headers
45``TIFF_LIBRARIES``
46  TIFF libraries to be linked
47
48Cache variables
49^^^^^^^^^^^^^^^
50
51The following cache variables may also be set:
52
53``TIFF_INCLUDE_DIR``
54  the directory containing the TIFF headers
55``TIFF_LIBRARY_RELEASE``
56  the path to the TIFF library for release configurations
57``TIFF_LIBRARY_DEBUG``
58  the path to the TIFF library for debug configurations
59``TIFFXX_LIBRARY_RELEASE``
60  the path to the TIFFXX library for release configurations
61``TIFFXX_LIBRARY_DEBUG``
62  the path to the TIFFXX library for debug configurations
63
64.. versionadded:: 3.4
65  Debug and Release variants are found separately.
66#]=======================================================================]
67
68cmake_policy(PUSH)
69cmake_policy(SET CMP0057 NEW)
70
71find_path(TIFF_INCLUDE_DIR tiff.h)
72
73set(TIFF_NAMES ${TIFF_NAMES} tiff libtiff tiff3 libtiff3)
74foreach(name ${TIFF_NAMES})
75  list(APPEND TIFF_NAMES_DEBUG "${name}d")
76endforeach()
77
78if(NOT TIFF_LIBRARY)
79  find_library(TIFF_LIBRARY_RELEASE NAMES ${TIFF_NAMES})
80  find_library(TIFF_LIBRARY_DEBUG NAMES ${TIFF_NAMES_DEBUG})
81  include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
82  select_library_configurations(TIFF)
83  mark_as_advanced(TIFF_LIBRARY_RELEASE TIFF_LIBRARY_DEBUG)
84endif()
85
86if(TIFF_INCLUDE_DIR AND EXISTS "${TIFF_INCLUDE_DIR}/tiffvers.h")
87    file(STRINGS "${TIFF_INCLUDE_DIR}/tiffvers.h" tiff_version_str
88         REGEX "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version .*")
89
90    string(REGEX REPLACE "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version +([^ \\n]*).*"
91           "\\1" TIFF_VERSION_STRING "${tiff_version_str}")
92    unset(tiff_version_str)
93endif()
94
95foreach(_comp IN LISTS TIFF_FIND_COMPONENTS)
96  if(_comp STREQUAL "CXX")
97    if(MSVC)
98      # C++ bindings are built into the main tiff library.
99      set(TIFF_CXX_FOUND 1)
100    else()
101      foreach(name ${TIFF_NAMES})
102        list(APPEND TIFFXX_NAMES "${name}xx")
103        list(APPEND TIFFXX_NAMES_DEBUG "${name}xxd")
104      endforeach()
105      find_library(TIFFXX_LIBRARY_RELEASE NAMES ${TIFFXX_NAMES})
106      find_library(TIFFXX_LIBRARY_DEBUG NAMES ${TIFFXX_NAMES_DEBUG})
107      include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
108      select_library_configurations(TIFFXX)
109      mark_as_advanced(TIFFXX_LIBRARY_RELEASE TIFFXX_LIBRARY_DEBUG)
110      unset(TIFFXX_NAMES)
111      unset(TIFFXX_NAMES_DEBUG)
112      if(TIFFXX_LIBRARY)
113        set(TIFF_CXX_FOUND 1)
114      endif()
115    endif()
116  endif()
117endforeach()
118unset(_comp)
119
120unset(TIFF_NAMES)
121unset(TIFF_NAMES_DEBUG)
122
123include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
124FIND_PACKAGE_HANDLE_STANDARD_ARGS(TIFF
125                                  HANDLE_COMPONENTS
126                                  REQUIRED_VARS TIFF_LIBRARY TIFF_INCLUDE_DIR
127                                  VERSION_VAR TIFF_VERSION_STRING)
128
129if(TIFF_FOUND)
130  set(TIFF_LIBRARIES ${TIFF_LIBRARY})
131  if("CXX" IN_LIST TIFF_FIND_COMPONENTS AND NOT MSVC)
132    list(APPEND TIFF_LIBRARIES ${TIFFXX_LIBRARY})
133  endif()
134
135  set(TIFF_INCLUDE_DIRS "${TIFF_INCLUDE_DIR}")
136
137  if(NOT TARGET TIFF::TIFF)
138    add_library(TIFF::TIFF UNKNOWN IMPORTED)
139    if(TIFF_INCLUDE_DIRS)
140      set_target_properties(TIFF::TIFF PROPERTIES
141        INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIRS}")
142    endif()
143    if(EXISTS "${TIFF_LIBRARY}")
144      set_target_properties(TIFF::TIFF PROPERTIES
145        IMPORTED_LINK_INTERFACE_LANGUAGES "C"
146        IMPORTED_LOCATION "${TIFF_LIBRARY}")
147    endif()
148    if(EXISTS "${TIFF_LIBRARY_RELEASE}")
149      set_property(TARGET TIFF::TIFF APPEND PROPERTY
150        IMPORTED_CONFIGURATIONS RELEASE)
151      set_target_properties(TIFF::TIFF PROPERTIES
152        IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
153        IMPORTED_LOCATION_RELEASE "${TIFF_LIBRARY_RELEASE}")
154    endif()
155    if(EXISTS "${TIFF_LIBRARY_DEBUG}")
156      set_property(TARGET TIFF::TIFF APPEND PROPERTY
157        IMPORTED_CONFIGURATIONS DEBUG)
158      set_target_properties(TIFF::TIFF PROPERTIES
159        IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
160        IMPORTED_LOCATION_DEBUG "${TIFF_LIBRARY_DEBUG}")
161    endif()
162  endif()
163
164  if(NOT TARGET TIFF::CXX)
165    if(MSVC)
166      add_library(TIFF::CXX INTERFACE IMPORTED)
167      set_property(TARGET TIFF::CXX PROPERTY INTERFACE_LINK_LIBRARIES TIFF::TIFF)
168    else()
169      add_library(TIFF::CXX UNKNOWN IMPORTED)
170      set_property(TARGET TIFF::CXX PROPERTY INTERFACE_LINK_LIBRARIES TIFF::TIFF)
171      if(TIFF_INCLUDE_DIRS)
172        set_target_properties(TIFF::CXX PROPERTIES
173          INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIRS}")
174      endif()
175      if(EXISTS "${TIFFXX_LIBRARY}")
176        set_target_properties(TIFF::CXX PROPERTIES
177          IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
178          IMPORTED_LOCATION "${TIFFXX_LIBRARY}")
179      endif()
180      if(EXISTS "${TIFFXX_LIBRARY_RELEASE}")
181        set_property(TARGET TIFF::CXX APPEND PROPERTY
182          IMPORTED_CONFIGURATIONS RELEASE)
183        set_target_properties(TIFF::CXX PROPERTIES
184          IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
185          IMPORTED_LOCATION_RELEASE "${TIFFXX_LIBRARY_RELEASE}")
186      endif()
187      if(EXISTS "${TIFFXX_LIBRARY_DEBUG}")
188        set_property(TARGET TIFF::CXX APPEND PROPERTY
189          IMPORTED_CONFIGURATIONS DEBUG)
190        set_target_properties(TIFF::CXX PROPERTIES
191          IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
192          IMPORTED_LOCATION_DEBUG "${TIFFXX_LIBRARY_DEBUG}")
193      endif()
194    endif()
195  endif()
196
197endif()
198
199mark_as_advanced(TIFF_INCLUDE_DIR)
200cmake_policy(POP)
201