Merge topic 'cuda_add_12.8_new_sm_support'
[kiteware-cmake.git] / Modules / FindTIFF.cmake
blob77056d6dee00ffc5f3884757cafbe8d5b5220082
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
5 FindTIFF
6 --------
8 Find the TIFF library (``libtiff``, https://libtiff.gitlab.io/libtiff/).
10 Optional COMPONENTS
11 ^^^^^^^^^^^^^^^^^^^
13 This module supports the optional component `CXX`, for use with the COMPONENTS
14 argument of the :command:`find_package` command. This component has an associated
15 imported target, as described below.
17 Imported Targets
18 ^^^^^^^^^^^^^^^^
20 .. versionadded:: 3.5
22 This module defines the following :prop_tgt:`IMPORTED` targets:
24 ``TIFF::TIFF``
25   The TIFF library, if found.
27 ``TIFF::CXX``
28   .. versionadded:: 3.19
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.
34 Result variables
35 ^^^^^^^^^^^^^^^^
37 This module will set the following variables in your project:
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
48 Cache variables
49 ^^^^^^^^^^^^^^^
51 The following cache variables may also be set:
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
64 .. versionadded:: 3.4
65   Debug and Release variants are found separately.
66 #]=======================================================================]
68 cmake_policy(PUSH)
69 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
71 set(_TIFF_args)
72 if (TIFF_FIND_VERSION)
73   list(APPEND _TIFF_args
74     "${TIFF_FIND_VERSION}")
75   if (TIFF_FIND_VERSION_EXACT)
76     list(APPEND _TIFF_args
77       EXACT)
78   endif ()
79 endif ()
80 set(_TIFF_component_req)
81 set(_TIFF_component_opt)
82 foreach (_TIFF_component IN LISTS TIFF_FIND_COMPONENTS)
83   if (TIFF_FIND_REQUIRE_${_TIFF_component})
84     list(APPEND _TIFF_component_req
85       "${_TIFF_component}")
86   else ()
87     list(APPEND _TIFF_component_opt
88       "${_TIFF_component}")
89   endif ()
90 endforeach ()
91 unset(_TIFF_component)
92 if (_TIFF_component_req)
93   list(APPEND _TIFF_args
94     COMPONENTS "${_TIFF_component_req}")
95 endif ()
96 unset(_TIFF_component_req)
97 if (_TIFF_component_opt)
98   list(APPEND _TIFF_args
99     OPTIONAL_COMPONENTS "${_TIFF_component_opt}")
100 endif ()
101 unset(_TIFF_component_opt)
102 # Always find with QUIET to avoid noise when it is not found.
103 find_package(Tiff CONFIG QUIET ${_TIFF_args})
104 unset(_TIFF_args)
105 if (Tiff_FOUND)
106   if (NOT TARGET TIFF::TIFF)
107     add_library(TIFF::TIFF IMPORTED INTERFACE)
108     set_target_properties(TIFF::TIFF PROPERTIES
109       INTERFACE_LINK_LIBRARIES TIFF::tiff)
110   endif ()
111   get_property(TIFF_INCLUDE_DIRS TARGET TIFF::tiff PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
112   get_property(TIFF_LIBRARIES TARGET TIFF::tiff PROPERTY INTERFACE_LINK_LIBRARIES)
113   get_property(_TIFF_location TARGET TIFF::tiff PROPERTY IMPORTED_IMPLIB)
114   if (NOT _TIFF_location)
115     get_property(_TIFF_location_release TARGET TIFF::tiff PROPERTY IMPORTED_IMPLIB_RELEASE)
116     if (NOT _TIFF_location_release)
117       get_property(_TIFF_location_release TARGET TIFF::tiff PROPERTY IMPORTED_IMPLIB_RELWITHDEBINFO)
118     endif ()
119     get_property(_TIFF_location_debug TARGET TIFF::tiff PROPERTY IMPORTED_IMPLIB_DEBUG)
120     if (_TIFF_location_release AND _TIFF_location_debug)
121       set(_TIFF_location
122         optimized "${_TIFF_location_release}"
123         debug "${_TIFF_location_debug}")
124     elseif (_TIFF_location_release)
125       set(_TIFF_location "${_TIFF_location_release}")
126     elseif (_TIFF_location_debug)
127       set(_TIFF_location "${_TIFF_location_debug}")
128     else ()
129       get_property(_TIFF_location_release TARGET TIFF::tiff PROPERTY LOCATION_RELEASE)
130       if (NOT _TIFF_location_release)
131         get_property(_TIFF_location_release TARGET TIFF::tiff PROPERTY LOCATION_RELWITHDEBINFO)
132       endif ()
133       get_property(_TIFF_location_debug TARGET TIFF::tiff PROPERTY LOCATION_DEBUG)
134       if (_TIFF_location_release AND _TIFF_location_debug)
135         set(_TIFF_location
136           optimized "${_TIFF_location_release}"
137           debug "${_TIFF_location_debug}")
138       elseif (_TIFF_location_release)
139         set(_TIFF_location "${_TIFF_location_release}")
140       elseif (_TIFF_location_debug)
141         set(_TIFF_location "${_TIFF_location_debug}")
142       else ()
143         get_property(_TIFF_location TARGET TIFF::tiff PROPERTY LOCATION)
144       endif ()
145     endif ()
146     unset(_TIFF_location_release)
147     unset(_TIFF_location_debug)
148   endif ()
149   list(INSERT TIFF_LIBRARIES 0
150     "${_TIFF_location}")
151   unset(_TIFF_location)
152   set(TIFF_FOUND 1)
153   if("CXX" IN_LIST TIFF_FIND_COMPONENTS)
154     if (TARGET TIFF::CXX)
155       get_property(_TIFF_CXX_location TARGET TIFF::CXX PROPERTY IMPORTED_IMPLIB)
156       if (NOT _TIFF_CXX_location)
157         get_property(_TIFF_CXX_location_release TARGET TIFF::CXX PROPERTY IMPORTED_IMPLIB_RELEASE)
158         if (NOT _TIFF_CXX_location_release)
159           get_property(_TIFF_CXX_location_release TARGET TIFF::CXX PROPERTY IMPORTED_IMPLIB_RELWITHDEBINFO)
160         endif ()
161         get_property(_TIFF_CXX_location_debug TARGET TIFF::CXX PROPERTY IMPORTED_IMPLIB_DEBUG)
162         if (_TIFF_CXX_location_release AND _TIFF_CXX_location_debug)
163           set(_TIFF_CXX_location
164             optimized "${_TIFF_CXX_location_release}"
165             debug "${_TIFF_CXX_location_debug}")
166         elseif (_TIFF_CXX_location_release)
167           set(_TIFF_CXX_location "${_TIFF_CXX_location_release}")
168         elseif (_TIFF_CXX_location_debug)
169           set(_TIFF_CXX_location "${_TIFF_CXX_location_debug}")
170         else ()
171           get_property(_TIFF_CXX_location_release TARGET TIFF::CXX PROPERTY LOCATION_RELEASE)
172           if (NOT _TIFF_CXX_location_release)
173             get_property(_TIFF_CXX_location_release TARGET TIFF::CXX PROPERTY LOCATION_RELWITHDEBINFO)
174           endif ()
175           get_property(_TIFF_CXX_location_debug TARGET TIFF::CXX PROPERTY LOCATION_DEBUG)
176           if (_TIFF_CXX_location_release AND _TIFF_CXX_location_debug)
177             set(_TIFF_CXX_location
178               optimized "${_TIFF_CXX_location_release}"
179               debug "${_TIFF_CXX_location_debug}")
180           elseif (_TIFF_CXX_location_release)
181             set(_TIFF_CXX_location "${_TIFF_CXX_location_release}")
182           elseif (_TIFF_CXX_location_debug)
183             set(_TIFF_CXX_location "${_TIFF_CXX_location_debug}")
184           else ()
185             get_property(_TIFF_CXX_location TARGET TIFF::CXX PROPERTY LOCATION)
186           endif ()
187         endif ()
188         unset(_TIFF_CXX_location_release)
189         unset(_TIFF_CXX_location_debug)
190       endif ()
191       list(INSERT TIFF_LIBRARIES 0
192         "${_TIFF_CXX_location}")
193       unset(_TIFF_CXX_location)
194       set(TIFF_CXX_FOUND 1)
195     else ()
196       set(TIFF_CXX_FOUND 0)
197       if (TIFF_FIND_REQUIRED_CXX)
198         set(TIFF_FOUND 0)
199         list(APPEND TIFF_NOT_FOUND_REASON
200           "No C++ bindings target found")
201       endif ()
202     endif ()
203   endif ()
204   set(TIFF_VERSION_STRING "${Tiff_VERSION}")
205   foreach (_TIFF_component IN LISTS TIFF_FIND_COMPONENTS)
206     set(TIFF_${_TIFF_component}_FOUND "${Tiff_${_TIFF_component}_FOUND}")
207   endforeach ()
208   unset(_TIFF_component)
210   include(FindPackageHandleStandardArgs)
211   find_package_handle_standard_args(TIFF
212                                     HANDLE_COMPONENTS
213                                     REQUIRED_VARS Tiff_DIR
214                                     VERSION_VAR TIFF_VERSION_STRING)
216   cmake_policy(POP)
217   return ()
218 endif ()
220 find_path(TIFF_INCLUDE_DIR tiff.h)
222 set(TIFF_NAMES ${TIFF_NAMES} tiff libtiff tiff3 libtiff3)
223 foreach(name ${TIFF_NAMES})
224   list(APPEND TIFF_NAMES_DEBUG "${name}d")
225 endforeach()
227 if(NOT TIFF_LIBRARY)
228   find_library(TIFF_LIBRARY_RELEASE NAMES ${TIFF_NAMES})
229   find_library(TIFF_LIBRARY_DEBUG NAMES ${TIFF_NAMES_DEBUG})
230   include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
231   select_library_configurations(TIFF)
232   mark_as_advanced(TIFF_LIBRARY_RELEASE TIFF_LIBRARY_DEBUG)
233 endif()
235 if(TIFF_INCLUDE_DIR AND EXISTS "${TIFF_INCLUDE_DIR}/tiffvers.h")
236     file(STRINGS "${TIFF_INCLUDE_DIR}/tiffvers.h" tiff_version_str
237          REGEX "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version .*")
239     string(REGEX REPLACE "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version +([^ \\n]*).*"
240            "\\1" TIFF_VERSION_STRING "${tiff_version_str}")
241     unset(tiff_version_str)
242 endif()
244 foreach(_comp IN LISTS TIFF_FIND_COMPONENTS)
245   if(_comp STREQUAL "CXX")
246     if(MSVC)
247       # C++ bindings are built into the main tiff library.
248       set(TIFF_CXX_FOUND 1)
249     else()
250       foreach(name ${TIFF_NAMES})
251         list(APPEND TIFFXX_NAMES "${name}xx")
252         list(APPEND TIFFXX_NAMES_DEBUG "${name}xxd")
253       endforeach()
254       find_library(TIFFXX_LIBRARY_RELEASE NAMES ${TIFFXX_NAMES})
255       find_library(TIFFXX_LIBRARY_DEBUG NAMES ${TIFFXX_NAMES_DEBUG})
256       include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
257       select_library_configurations(TIFFXX)
258       mark_as_advanced(TIFFXX_LIBRARY_RELEASE TIFFXX_LIBRARY_DEBUG)
259       unset(TIFFXX_NAMES)
260       unset(TIFFXX_NAMES_DEBUG)
261       if(TIFFXX_LIBRARY)
262         set(TIFF_CXX_FOUND 1)
263       endif()
264     endif()
265   endif()
266 endforeach()
267 unset(_comp)
269 unset(TIFF_NAMES)
270 unset(TIFF_NAMES_DEBUG)
272 include(FindPackageHandleStandardArgs)
273 find_package_handle_standard_args(TIFF
274                                   HANDLE_COMPONENTS
275                                   REQUIRED_VARS TIFF_LIBRARY TIFF_INCLUDE_DIR
276                                   VERSION_VAR TIFF_VERSION_STRING)
278 if(TIFF_FOUND)
279   set(TIFF_LIBRARIES ${TIFF_LIBRARY})
280   if("CXX" IN_LIST TIFF_FIND_COMPONENTS AND NOT MSVC)
281     list(APPEND TIFF_LIBRARIES ${TIFFXX_LIBRARY})
282   endif()
284   set(TIFF_INCLUDE_DIRS "${TIFF_INCLUDE_DIR}")
286   if(NOT TARGET TIFF::TIFF)
287     add_library(TIFF::TIFF UNKNOWN IMPORTED)
288     if(TIFF_INCLUDE_DIRS)
289       set_target_properties(TIFF::TIFF PROPERTIES
290         INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIRS}")
291     endif()
292     if(EXISTS "${TIFF_LIBRARY}")
293       set_target_properties(TIFF::TIFF PROPERTIES
294         IMPORTED_LINK_INTERFACE_LANGUAGES "C"
295         IMPORTED_LOCATION "${TIFF_LIBRARY}")
296     endif()
297     if(EXISTS "${TIFF_LIBRARY_RELEASE}")
298       set_property(TARGET TIFF::TIFF APPEND PROPERTY
299         IMPORTED_CONFIGURATIONS RELEASE)
300       set_target_properties(TIFF::TIFF PROPERTIES
301         IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
302         IMPORTED_LOCATION_RELEASE "${TIFF_LIBRARY_RELEASE}")
303     endif()
304     if(EXISTS "${TIFF_LIBRARY_DEBUG}")
305       set_property(TARGET TIFF::TIFF APPEND PROPERTY
306         IMPORTED_CONFIGURATIONS DEBUG)
307       set_target_properties(TIFF::TIFF PROPERTIES
308         IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
309         IMPORTED_LOCATION_DEBUG "${TIFF_LIBRARY_DEBUG}")
310     endif()
311   endif()
313   if(NOT TARGET TIFF::CXX)
314     if(MSVC)
315       add_library(TIFF::CXX INTERFACE IMPORTED)
316       set_property(TARGET TIFF::CXX PROPERTY INTERFACE_LINK_LIBRARIES TIFF::TIFF)
317     else()
318       add_library(TIFF::CXX UNKNOWN IMPORTED)
319       set_property(TARGET TIFF::CXX PROPERTY INTERFACE_LINK_LIBRARIES TIFF::TIFF)
320       if(TIFF_INCLUDE_DIRS)
321         set_target_properties(TIFF::CXX PROPERTIES
322           INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIRS}")
323       endif()
324       if(EXISTS "${TIFFXX_LIBRARY}")
325         set_target_properties(TIFF::CXX PROPERTIES
326           IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
327           IMPORTED_LOCATION "${TIFFXX_LIBRARY}")
328       endif()
329       if(EXISTS "${TIFFXX_LIBRARY_RELEASE}")
330         set_property(TARGET TIFF::CXX APPEND PROPERTY
331           IMPORTED_CONFIGURATIONS RELEASE)
332         set_target_properties(TIFF::CXX PROPERTIES
333           IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
334           IMPORTED_LOCATION_RELEASE "${TIFFXX_LIBRARY_RELEASE}")
335       endif()
336       if(EXISTS "${TIFFXX_LIBRARY_DEBUG}")
337         set_property(TARGET TIFF::CXX APPEND PROPERTY
338           IMPORTED_CONFIGURATIONS DEBUG)
339         set_target_properties(TIFF::CXX PROPERTIES
340           IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
341           IMPORTED_LOCATION_DEBUG "${TIFFXX_LIBRARY_DEBUG}")
342       endif()
343     endif()
344   endif()
346 endif()
348 mark_as_advanced(TIFF_INCLUDE_DIR)
349 cmake_policy(POP)