FindTIFF.cmake 12 KB

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