FindTIFF.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  52. set(_TIFF_args)
  53. if (TIFF_FIND_VERSION)
  54. list(APPEND _TIFF_args
  55. "${TIFF_FIND_VERSION}")
  56. if (TIFF_FIND_VERSION_EXACT)
  57. list(APPEND _TIFF_args
  58. EXACT)
  59. endif ()
  60. endif ()
  61. set(_TIFF_component_req)
  62. set(_TIFF_component_opt)
  63. foreach (_TIFF_component IN LISTS TIFF_FIND_COMPONENTS)
  64. if (TIFF_FIND_REQUIRE_${_TIFF_component})
  65. list(APPEND _TIFF_component_req
  66. "${_TIFF_component}")
  67. else ()
  68. list(APPEND _TIFF_component_opt
  69. "${_TIFF_component}")
  70. endif ()
  71. endforeach ()
  72. unset(_TIFF_component)
  73. if (_TIFF_component_req)
  74. list(APPEND _TIFF_args
  75. COMPONENTS "${_TIFF_component_req}")
  76. endif ()
  77. unset(_TIFF_component_req)
  78. if (_TIFF_component_opt)
  79. list(APPEND _TIFF_args
  80. OPTIONAL_COMPONENTS "${_TIFF_component_opt}")
  81. endif ()
  82. unset(_TIFF_component_opt)
  83. # Always find with QUIET to avoid noise when it is not found.
  84. find_package(Tiff CONFIG QUIET ${_TIFF_args})
  85. unset(_TIFF_args)
  86. if (Tiff_FOUND)
  87. if (NOT TARGET TIFF::TIFF)
  88. add_library(TIFF::TIFF IMPORTED INTERFACE)
  89. set_target_properties(TIFF::TIFF PROPERTIES
  90. INTERFACE_LINK_LIBRARIES TIFF::tiff)
  91. endif ()
  92. get_property(TIFF_INCLUDE_DIRS TARGET TIFF::tiff PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
  93. get_property(TIFF_LIBRARIES TARGET TIFF::tiff PROPERTY INTERFACE_LINK_LIBRARIES)
  94. get_property(_TIFF_location TARGET TIFF::tiff PROPERTY IMPORTED_IMPLIB)
  95. if (NOT _TIFF_location)
  96. get_property(_TIFF_location_release TARGET TIFF::tiff PROPERTY IMPORTED_IMPLIB_RELEASE)
  97. if (NOT _TIFF_location_release)
  98. get_property(_TIFF_location_release TARGET TIFF::tiff PROPERTY IMPORTED_IMPLIB_RELWITHDEBINFO)
  99. endif ()
  100. get_property(_TIFF_location_debug TARGET TIFF::tiff PROPERTY IMPORTED_IMPLIB_DEBUG)
  101. if (_TIFF_location_release AND _TIFF_location_debug)
  102. set(_TIFF_location
  103. optimized "${_TIFF_location_release}"
  104. debug "${_TIFF_location_debug}")
  105. elseif (_TIFF_location_release)
  106. set(_TIFF_location "${_TIFF_location_release}")
  107. elseif (_TIFF_location_debug)
  108. set(_TIFF_location "${_TIFF_location_debug}")
  109. else ()
  110. get_property(_TIFF_location_release TARGET TIFF::tiff PROPERTY LOCATION_RELEASE)
  111. if (NOT _TIFF_location_release)
  112. get_property(_TIFF_location_release TARGET TIFF::tiff PROPERTY LOCATION_RELWITHDEBINFO)
  113. endif ()
  114. get_property(_TIFF_location_debug TARGET TIFF::tiff PROPERTY LOCATION_DEBUG)
  115. if (_TIFF_location_release AND _TIFF_location_debug)
  116. set(_TIFF_location
  117. optimized "${_TIFF_location_release}"
  118. debug "${_TIFF_location_debug}")
  119. elseif (_TIFF_location_release)
  120. set(_TIFF_location "${_TIFF_location_release}")
  121. elseif (_TIFF_location_debug)
  122. set(_TIFF_location "${_TIFF_location_debug}")
  123. else ()
  124. get_property(_TIFF_location TARGET TIFF::tiff PROPERTY LOCATION)
  125. endif ()
  126. endif ()
  127. unset(_TIFF_location_release)
  128. unset(_TIFF_location_debug)
  129. endif ()
  130. list(INSERT TIFF_LIBRARIES 0
  131. "${_TIFF_location}")
  132. unset(_TIFF_location)
  133. set(TIFF_FOUND 1)
  134. if("CXX" IN_LIST TIFF_FIND_COMPONENTS)
  135. if (TARGET TIFF::CXX)
  136. get_property(_TIFF_CXX_location TARGET TIFF::CXX PROPERTY IMPORTED_IMPLIB)
  137. if (NOT _TIFF_CXX_location)
  138. get_property(_TIFF_CXX_location_release TARGET TIFF::CXX PROPERTY IMPORTED_IMPLIB_RELEASE)
  139. if (NOT _TIFF_CXX_location_release)
  140. get_property(_TIFF_CXX_location_release TARGET TIFF::CXX PROPERTY IMPORTED_IMPLIB_RELWITHDEBINFO)
  141. endif ()
  142. get_property(_TIFF_CXX_location_debug TARGET TIFF::CXX PROPERTY IMPORTED_IMPLIB_DEBUG)
  143. if (_TIFF_CXX_location_release AND _TIFF_CXX_location_debug)
  144. set(_TIFF_CXX_location
  145. optimized "${_TIFF_CXX_location_release}"
  146. debug "${_TIFF_CXX_location_debug}")
  147. elseif (_TIFF_CXX_location_release)
  148. set(_TIFF_CXX_location "${_TIFF_CXX_location_release}")
  149. elseif (_TIFF_CXX_location_debug)
  150. set(_TIFF_CXX_location "${_TIFF_CXX_location_debug}")
  151. else ()
  152. get_property(_TIFF_CXX_location_release TARGET TIFF::CXX PROPERTY LOCATION_RELEASE)
  153. if (NOT _TIFF_CXX_location_release)
  154. get_property(_TIFF_CXX_location_release TARGET TIFF::CXX PROPERTY LOCATION_RELWITHDEBINFO)
  155. endif ()
  156. get_property(_TIFF_CXX_location_debug TARGET TIFF::CXX PROPERTY LOCATION_DEBUG)
  157. if (_TIFF_CXX_location_release AND _TIFF_CXX_location_debug)
  158. set(_TIFF_CXX_location
  159. optimized "${_TIFF_CXX_location_release}"
  160. debug "${_TIFF_CXX_location_debug}")
  161. elseif (_TIFF_CXX_location_release)
  162. set(_TIFF_CXX_location "${_TIFF_CXX_location_release}")
  163. elseif (_TIFF_CXX_location_debug)
  164. set(_TIFF_CXX_location "${_TIFF_CXX_location_debug}")
  165. else ()
  166. get_property(_TIFF_CXX_location TARGET TIFF::CXX PROPERTY LOCATION)
  167. endif ()
  168. endif ()
  169. unset(_TIFF_CXX_location_release)
  170. unset(_TIFF_CXX_location_debug)
  171. endif ()
  172. list(INSERT TIFF_LIBRARIES 0
  173. "${_TIFF_CXX_location}")
  174. unset(_TIFF_CXX_location)
  175. set(TIFF_CXX_FOUND 1)
  176. else ()
  177. set(TIFF_CXX_FOUND 0)
  178. if (TIFF_FIND_REQUIRED_CXX)
  179. set(TIFF_FOUND 0)
  180. list(APPEND TIFF_NOT_FOUND_REASON
  181. "No C++ bindings target found")
  182. endif ()
  183. endif ()
  184. endif ()
  185. set(TIFF_VERSION_STRING "${Tiff_VERSION}")
  186. foreach (_TIFF_component IN LISTS TIFF_FIND_COMPONENTS)
  187. set(TIFF_${_TIFF_component}_FOUND "${Tiff_${_TIFF_component}_FOUND}")
  188. endforeach ()
  189. unset(_TIFF_component)
  190. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  191. find_package_handle_standard_args(TIFF
  192. HANDLE_COMPONENTS
  193. REQUIRED_VARS Tiff_DIR
  194. VERSION_VAR TIFF_VERSION_STRING)
  195. cmake_policy(POP)
  196. return ()
  197. endif ()
  198. find_path(TIFF_INCLUDE_DIR tiff.h)
  199. set(TIFF_NAMES ${TIFF_NAMES} tiff libtiff tiff3 libtiff3)
  200. foreach(name ${TIFF_NAMES})
  201. list(APPEND TIFF_NAMES_DEBUG "${name}d")
  202. endforeach()
  203. if(NOT TIFF_LIBRARY)
  204. find_library(TIFF_LIBRARY_RELEASE NAMES ${TIFF_NAMES})
  205. find_library(TIFF_LIBRARY_DEBUG NAMES ${TIFF_NAMES_DEBUG})
  206. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  207. select_library_configurations(TIFF)
  208. mark_as_advanced(TIFF_LIBRARY_RELEASE TIFF_LIBRARY_DEBUG)
  209. endif()
  210. if(TIFF_INCLUDE_DIR AND EXISTS "${TIFF_INCLUDE_DIR}/tiffvers.h")
  211. file(STRINGS "${TIFF_INCLUDE_DIR}/tiffvers.h" tiff_version_str
  212. REGEX "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version .*")
  213. string(REGEX REPLACE "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version +([^ \\n]*).*"
  214. "\\1" TIFF_VERSION_STRING "${tiff_version_str}")
  215. unset(tiff_version_str)
  216. endif()
  217. foreach(_comp IN LISTS TIFF_FIND_COMPONENTS)
  218. if(_comp STREQUAL "CXX")
  219. if(MSVC)
  220. # C++ bindings are built into the main tiff library.
  221. set(TIFF_CXX_FOUND 1)
  222. else()
  223. foreach(name ${TIFF_NAMES})
  224. list(APPEND TIFFXX_NAMES "${name}xx")
  225. list(APPEND TIFFXX_NAMES_DEBUG "${name}xxd")
  226. endforeach()
  227. find_library(TIFFXX_LIBRARY_RELEASE NAMES ${TIFFXX_NAMES})
  228. find_library(TIFFXX_LIBRARY_DEBUG NAMES ${TIFFXX_NAMES_DEBUG})
  229. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  230. select_library_configurations(TIFFXX)
  231. mark_as_advanced(TIFFXX_LIBRARY_RELEASE TIFFXX_LIBRARY_DEBUG)
  232. unset(TIFFXX_NAMES)
  233. unset(TIFFXX_NAMES_DEBUG)
  234. if(TIFFXX_LIBRARY)
  235. set(TIFF_CXX_FOUND 1)
  236. endif()
  237. endif()
  238. endif()
  239. endforeach()
  240. unset(_comp)
  241. unset(TIFF_NAMES)
  242. unset(TIFF_NAMES_DEBUG)
  243. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  244. find_package_handle_standard_args(TIFF
  245. HANDLE_COMPONENTS
  246. REQUIRED_VARS TIFF_LIBRARY TIFF_INCLUDE_DIR
  247. VERSION_VAR TIFF_VERSION_STRING)
  248. if(TIFF_FOUND)
  249. set(TIFF_LIBRARIES ${TIFF_LIBRARY})
  250. if("CXX" IN_LIST TIFF_FIND_COMPONENTS AND NOT MSVC)
  251. list(APPEND TIFF_LIBRARIES ${TIFFXX_LIBRARY})
  252. endif()
  253. set(TIFF_INCLUDE_DIRS "${TIFF_INCLUDE_DIR}")
  254. if(NOT TARGET TIFF::TIFF)
  255. add_library(TIFF::TIFF UNKNOWN IMPORTED)
  256. if(TIFF_INCLUDE_DIRS)
  257. set_target_properties(TIFF::TIFF PROPERTIES
  258. INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIRS}")
  259. endif()
  260. if(EXISTS "${TIFF_LIBRARY}")
  261. set_target_properties(TIFF::TIFF PROPERTIES
  262. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  263. IMPORTED_LOCATION "${TIFF_LIBRARY}")
  264. endif()
  265. if(EXISTS "${TIFF_LIBRARY_RELEASE}")
  266. set_property(TARGET TIFF::TIFF APPEND PROPERTY
  267. IMPORTED_CONFIGURATIONS RELEASE)
  268. set_target_properties(TIFF::TIFF PROPERTIES
  269. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  270. IMPORTED_LOCATION_RELEASE "${TIFF_LIBRARY_RELEASE}")
  271. endif()
  272. if(EXISTS "${TIFF_LIBRARY_DEBUG}")
  273. set_property(TARGET TIFF::TIFF APPEND PROPERTY
  274. IMPORTED_CONFIGURATIONS DEBUG)
  275. set_target_properties(TIFF::TIFF PROPERTIES
  276. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  277. IMPORTED_LOCATION_DEBUG "${TIFF_LIBRARY_DEBUG}")
  278. endif()
  279. endif()
  280. if(NOT TARGET TIFF::CXX)
  281. if(MSVC)
  282. add_library(TIFF::CXX INTERFACE IMPORTED)
  283. set_property(TARGET TIFF::CXX PROPERTY INTERFACE_LINK_LIBRARIES TIFF::TIFF)
  284. else()
  285. add_library(TIFF::CXX UNKNOWN IMPORTED)
  286. set_property(TARGET TIFF::CXX PROPERTY INTERFACE_LINK_LIBRARIES TIFF::TIFF)
  287. if(TIFF_INCLUDE_DIRS)
  288. set_target_properties(TIFF::CXX PROPERTIES
  289. INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIRS}")
  290. endif()
  291. if(EXISTS "${TIFFXX_LIBRARY}")
  292. set_target_properties(TIFF::CXX PROPERTIES
  293. IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
  294. IMPORTED_LOCATION "${TIFFXX_LIBRARY}")
  295. endif()
  296. if(EXISTS "${TIFFXX_LIBRARY_RELEASE}")
  297. set_property(TARGET TIFF::CXX APPEND PROPERTY
  298. IMPORTED_CONFIGURATIONS RELEASE)
  299. set_target_properties(TIFF::CXX PROPERTIES
  300. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
  301. IMPORTED_LOCATION_RELEASE "${TIFFXX_LIBRARY_RELEASE}")
  302. endif()
  303. if(EXISTS "${TIFFXX_LIBRARY_DEBUG}")
  304. set_property(TARGET TIFF::CXX APPEND PROPERTY
  305. IMPORTED_CONFIGURATIONS DEBUG)
  306. set_target_properties(TIFF::CXX PROPERTIES
  307. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
  308. IMPORTED_LOCATION_DEBUG "${TIFFXX_LIBRARY_DEBUG}")
  309. endif()
  310. endif()
  311. endif()
  312. endif()
  313. mark_as_advanced(TIFF_INCLUDE_DIR)
  314. cmake_policy(POP)