FindImageMagick.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindImageMagick
  5. ---------------
  6. Finds ImageMagick, a software suite for displaying, converting, and manipulating
  7. raster images:
  8. .. code-block:: cmake
  9. find_package(ImageMagick [<version>] [COMPONENTS <components>...] [...])
  10. .. versionadded:: 3.9
  11. Support for ImageMagick 7.
  12. Components
  13. ^^^^^^^^^^
  14. This module supports components and searches for a set of ImageMagick tools.
  15. Typical components include the names of ImageMagick executables, but are not
  16. limited to the following (future versions of ImageMagick may provide additional
  17. components not listed here):
  18. * ``animate``
  19. * ``compare``
  20. * ``composite``
  21. * ``conjure``
  22. * ``convert``
  23. * ``display``
  24. * ``identify``
  25. * ``import``
  26. * ``mogrify``
  27. * ``montage``
  28. * ``stream``
  29. There are also components for the following ImageMagick APIs:
  30. ``Magick++``
  31. Finds the ImageMagick C++ API.
  32. ``MagickWand``
  33. Finds the ImageMagick MagickWand C API.
  34. ``MagickCore``
  35. Finds the ImageMagick MagickCore low-level C API.
  36. Components can be specified using the :command:`find_package` command:
  37. .. code-block:: cmake
  38. find_package(ImageMagick [COMPONENTS <components>...])
  39. If no components are specified, the module only searches for the ImageMagick
  40. executable directory.
  41. Imported Targets
  42. ^^^^^^^^^^^^^^^^
  43. This module provides the following :ref:`Imported Targets`:
  44. ``ImageMagick::Magick++``
  45. .. versionadded:: 3.26
  46. Target encapsulating the ImageMagick C++ API usage requirements, available if
  47. ImageMagick C++ is found.
  48. ``ImageMagick::MagickWand``
  49. .. versionadded:: 3.26
  50. Target encapsulating the ImageMagick MagickWand C API usage requirements,
  51. available if MagickWand is found.
  52. ``ImageMagick::MagickCore``
  53. .. versionadded:: 3.26
  54. Target encapsulating the ImageMagick MagickCore low-level C API usage
  55. requirements, available if MagickCore is found.
  56. Result Variables
  57. ^^^^^^^^^^^^^^^^
  58. This module defines the following variables:
  59. ``ImageMagick_FOUND``
  60. Boolean indicating whether ImageMagick and all its requested components are
  61. found.
  62. ``ImageMagick_VERSION_STRING``
  63. The version of ImageMagick found.
  64. .. note::
  65. Version detection is available only for ImageMagick 6 and later.
  66. ``ImageMagick_INCLUDE_DIRS``
  67. All include directories needed to use ImageMagick.
  68. ``ImageMagick_LIBRARIES``
  69. Libraries needed to link against to use ImageMagick.
  70. ``ImageMagick_COMPILE_OPTIONS``
  71. .. versionadded:: 3.26
  72. Compile options of all libraries.
  73. ``ImageMagick_<component>_FOUND``
  74. Boolean indicating whether the ImageMagick ``<component>`` is found.
  75. ``ImageMagick_<component>_EXECUTABLE``
  76. The full path to ``<component>`` executable.
  77. ``ImageMagick_<component>_INCLUDE_DIRS``
  78. Include directories containing headers needed to use the ImageMagick
  79. ``<component>``.
  80. ``ImageMagick_<component>_COMPILE_OPTIONS``
  81. .. versionadded:: 3.26
  82. Compile options of the ImageMagick ``<component>``.
  83. ``ImageMagick_<component>_LIBRARIES``
  84. .. versionadded:: 3.31
  85. Libraries needed to link against to use the ImageMagick ``<component>``.
  86. Cache Variables
  87. ^^^^^^^^^^^^^^^
  88. The following cache variables may also be set:
  89. ``ImageMagick_EXECUTABLE_DIR``
  90. The full path to directory containing ImageMagick executables.
  91. Examples
  92. ^^^^^^^^
  93. Finding ImageMagick with its component ``Magick++`` and linking it to a project
  94. target:
  95. .. code-block:: cmake
  96. find_package(ImageMagick COMPONENTS Magick++)
  97. target_link_libraries(example PRIVATE ImageMagick::Magick++)
  98. #]=======================================================================]
  99. find_package(PkgConfig QUIET)
  100. #---------------------------------------------------------------------
  101. # Helper functions
  102. #---------------------------------------------------------------------
  103. function(FIND_IMAGEMAGICK_API component header)
  104. set(ImageMagick_${component}_FOUND FALSE PARENT_SCOPE)
  105. if(PkgConfig_FOUND)
  106. pkg_check_modules(PC_${component} QUIET ${component})
  107. endif()
  108. find_path(ImageMagick_${component}_INCLUDE_DIR
  109. NAMES ${header}
  110. HINTS
  111. ${PC_${component}_INCLUDEDIR}
  112. ${PC_${component}_INCLUDE_DIRS}
  113. PATHS
  114. ${ImageMagick_INCLUDE_DIRS}
  115. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\ImageMagick\\Current;BinPath]/include"
  116. PATH_SUFFIXES
  117. ImageMagick ImageMagick-6 ImageMagick-7
  118. DOC "Path to the ImageMagick arch-independent include dir."
  119. NO_DEFAULT_PATH
  120. )
  121. find_path(ImageMagick_${component}_ARCH_INCLUDE_DIR
  122. NAMES
  123. magick/magick-baseconfig.h
  124. MagickCore/magick-baseconfig.h
  125. HINTS
  126. ${PC_${component}_INCLUDEDIR}
  127. ${PC_${component}_INCLUDE_DIRS}
  128. PATHS
  129. ${ImageMagick_INCLUDE_DIRS}
  130. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\ImageMagick\\Current;BinPath]/include"
  131. PATH_SUFFIXES
  132. ImageMagick ImageMagick-6 ImageMagick-7
  133. DOC "Path to the ImageMagick arch-specific include dir."
  134. NO_DEFAULT_PATH
  135. )
  136. find_library(ImageMagick_${component}_LIBRARY
  137. NAMES ${ARGN}
  138. HINTS
  139. ${PC_${component}_LIBDIR}
  140. ${PC_${component}_LIB_DIRS}
  141. PATHS
  142. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\ImageMagick\\Current;BinPath]/lib"
  143. DOC "Path to the ImageMagick Magick++ library."
  144. NO_DEFAULT_PATH
  145. )
  146. # old version have only indep dir
  147. if(ImageMagick_${component}_INCLUDE_DIR AND ImageMagick_${component}_LIBRARY)
  148. set(ImageMagick_${component}_FOUND TRUE PARENT_SCOPE)
  149. # Construct per-component include directories.
  150. set(ImageMagick_${component}_INCLUDE_DIRS
  151. ${ImageMagick_${component}_INCLUDE_DIR}
  152. )
  153. if(ImageMagick_${component}_ARCH_INCLUDE_DIR)
  154. list(APPEND ImageMagick_${component}_INCLUDE_DIRS
  155. ${ImageMagick_${component}_ARCH_INCLUDE_DIR})
  156. endif()
  157. list(REMOVE_DUPLICATES ImageMagick_${component}_INCLUDE_DIRS)
  158. set(ImageMagick_${component}_INCLUDE_DIRS
  159. ${ImageMagick_${component}_INCLUDE_DIRS} PARENT_SCOPE)
  160. set(ImageMagick_${component}_LIBRARIES
  161. ${ImageMagick_${component}_LIBRARY}
  162. )
  163. set(ImageMagick_${component}_LIBRARIES
  164. ${ImageMagick_${component}_LIBRARIES} PARENT_SCOPE)
  165. set(ImageMagick_${component}_COMPILE_OPTIONS ${PC_${component}_CFLAGS_OTHER})
  166. # Add the per-component include directories to the full include dirs.
  167. list(APPEND ImageMagick_INCLUDE_DIRS ${ImageMagick_${component}_INCLUDE_DIRS})
  168. list(REMOVE_DUPLICATES ImageMagick_INCLUDE_DIRS)
  169. set(ImageMagick_INCLUDE_DIRS ${ImageMagick_INCLUDE_DIRS} PARENT_SCOPE)
  170. list(APPEND ImageMagick_LIBRARIES
  171. ${ImageMagick_${component}_LIBRARY}
  172. )
  173. set(ImageMagick_LIBRARIES ${ImageMagick_LIBRARIES} PARENT_SCOPE)
  174. list(APPEND ImageMagick_COMPILE_OPTIONS
  175. ${ImageMagick_${component}_COMPILE_OPTIONS}
  176. )
  177. set(ImageMagick_COMPILE_OPTIONS ${ImageMagick_COMPILE_OPTIONS} PARENT_SCOPE)
  178. if(NOT TARGET ImageMagick::${component})
  179. add_library(ImageMagick::${component} UNKNOWN IMPORTED)
  180. set_target_properties(ImageMagick::${component} PROPERTIES
  181. INTERFACE_INCLUDE_DIRECTORIES "${ImageMagick_${component}_INCLUDE_DIRS}"
  182. INTERFACE_COMPILE_OPTIONS "${ImageMagick_${component}_COMPILE_OPTIONS}"
  183. IMPORTED_LOCATION "${ImageMagick_${component}_LIBRARY}")
  184. endif()
  185. endif()
  186. endfunction()
  187. function(FIND_IMAGEMAGICK_EXE component)
  188. set(_IMAGEMAGICK_EXECUTABLE
  189. ${ImageMagick_EXECUTABLE_DIR}/${component}${CMAKE_EXECUTABLE_SUFFIX})
  190. if(EXISTS ${_IMAGEMAGICK_EXECUTABLE})
  191. set(ImageMagick_${component}_EXECUTABLE
  192. ${_IMAGEMAGICK_EXECUTABLE}
  193. PARENT_SCOPE
  194. )
  195. set(ImageMagick_${component}_FOUND TRUE PARENT_SCOPE)
  196. else()
  197. set(ImageMagick_${component}_FOUND FALSE PARENT_SCOPE)
  198. endif()
  199. endfunction()
  200. #---------------------------------------------------------------------
  201. # Start Actual Work
  202. #---------------------------------------------------------------------
  203. # Try to find a ImageMagick installation binary path.
  204. find_path(ImageMagick_EXECUTABLE_DIR
  205. NAMES mogrify${CMAKE_EXECUTABLE_SUFFIX}
  206. PATHS
  207. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\ImageMagick\\Current;BinPath]"
  208. DOC "Path to the ImageMagick binary directory."
  209. NO_DEFAULT_PATH
  210. )
  211. find_path(ImageMagick_EXECUTABLE_DIR
  212. NAMES mogrify${CMAKE_EXECUTABLE_SUFFIX}
  213. )
  214. # Find each component. Search for all tools in same dir
  215. # <ImageMagick_EXECUTABLE_DIR>; otherwise they should be found
  216. # independently and not in a cohesive module such as this one.
  217. unset(ImageMagick_REQUIRED_VARS)
  218. unset(ImageMagick_DEFAULT_EXECUTABLES)
  219. foreach(component ${ImageMagick_FIND_COMPONENTS}
  220. # DEPRECATED: forced components for backward compatibility
  221. convert mogrify import montage composite
  222. )
  223. if(component STREQUAL "Magick++")
  224. FIND_IMAGEMAGICK_API(Magick++ Magick++.h
  225. Magick++ CORE_RL_Magick++_
  226. Magick++-6 Magick++-7
  227. Magick++-Q8 Magick++-Q16 Magick++-Q16HDRI Magick++-Q8HDRI
  228. Magick++-6.Q64 Magick++-6.Q32 Magick++-6.Q64HDRI Magick++-6.Q32HDRI
  229. Magick++-6.Q16 Magick++-6.Q8 Magick++-6.Q16HDRI Magick++-6.Q8HDRI
  230. Magick++-7.Q64 Magick++-7.Q32 Magick++-7.Q64HDRI Magick++-7.Q32HDRI
  231. Magick++-7.Q16 Magick++-7.Q8 Magick++-7.Q16HDRI Magick++-7.Q8HDRI
  232. )
  233. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_Magick++_LIBRARY)
  234. elseif(component STREQUAL "MagickWand")
  235. FIND_IMAGEMAGICK_API(MagickWand "wand/MagickWand.h;MagickWand/MagickWand.h"
  236. Wand MagickWand CORE_RL_wand_ CORE_RL_MagickWand_
  237. MagickWand-6 MagickWand-7
  238. MagickWand-Q16 MagickWand-Q8 MagickWand-Q16HDRI MagickWand-Q8HDRI
  239. MagickWand-6.Q64 MagickWand-6.Q32 MagickWand-6.Q64HDRI MagickWand-6.Q32HDRI
  240. MagickWand-6.Q16 MagickWand-6.Q8 MagickWand-6.Q16HDRI MagickWand-6.Q8HDRI
  241. MagickWand-7.Q64 MagickWand-7.Q32 MagickWand-7.Q64HDRI MagickWand-7.Q32HDRI
  242. MagickWand-7.Q16 MagickWand-7.Q8 MagickWand-7.Q16HDRI MagickWand-7.Q8HDRI
  243. )
  244. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_MagickWand_LIBRARY)
  245. elseif(component STREQUAL "MagickCore")
  246. FIND_IMAGEMAGICK_API(MagickCore "magick/MagickCore.h;MagickCore/MagickCore.h"
  247. Magick MagickCore CORE_RL_magick_ CORE_RL_MagickCore_
  248. MagickCore-6 MagickCore-7
  249. MagickCore-Q16 MagickCore-Q8 MagickCore-Q16HDRI MagickCore-Q8HDRI
  250. MagickCore-6.Q64 MagickCore-6.Q32 MagickCore-6.Q64HDRI MagickCore-6.Q32HDRI
  251. MagickCore-6.Q16 MagickCore-6.Q8 MagickCore-6.Q16HDRI MagickCore-6.Q8HDRI
  252. MagickCore-7.Q64 MagickCore-7.Q32 MagickCore-7.Q64HDRI MagickCore-7.Q32HDRI
  253. MagickCore-7.Q16 MagickCore-7.Q8 MagickCore-7.Q16HDRI MagickCore-7.Q8HDRI
  254. )
  255. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_MagickCore_LIBRARY)
  256. else()
  257. if(ImageMagick_EXECUTABLE_DIR)
  258. FIND_IMAGEMAGICK_EXE(${component})
  259. endif()
  260. if(ImageMagick_FIND_COMPONENTS)
  261. list(FIND ImageMagick_FIND_COMPONENTS ${component} is_requested)
  262. if(is_requested GREATER -1)
  263. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_${component}_EXECUTABLE)
  264. endif()
  265. elseif(ImageMagick_${component}_EXECUTABLE)
  266. # if no components were requested explicitly put all (default) executables
  267. # in the list
  268. list(APPEND ImageMagick_DEFAULT_EXECUTABLES ImageMagick_${component}_EXECUTABLE)
  269. endif()
  270. endif()
  271. endforeach()
  272. if(NOT ImageMagick_FIND_COMPONENTS AND NOT ImageMagick_DEFAULT_EXECUTABLES)
  273. # No components were requested, and none of the default components were
  274. # found. Just insert mogrify into the list of the default components to
  275. # find so FPHSA below has something to check
  276. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_mogrify_EXECUTABLE)
  277. elseif(ImageMagick_DEFAULT_EXECUTABLES)
  278. list(APPEND ImageMagick_REQUIRED_VARS ${ImageMagick_DEFAULT_EXECUTABLES})
  279. endif()
  280. set(ImageMagick_INCLUDE_DIRS ${ImageMagick_INCLUDE_DIRS})
  281. set(ImageMagick_LIBRARIES ${ImageMagick_LIBRARIES})
  282. if(ImageMagick_mogrify_EXECUTABLE)
  283. execute_process(COMMAND ${ImageMagick_mogrify_EXECUTABLE} -version
  284. OUTPUT_VARIABLE imagemagick_version
  285. ERROR_QUIET
  286. OUTPUT_STRIP_TRAILING_WHITESPACE)
  287. if(imagemagick_version MATCHES "^Version: ImageMagick ([-0-9\\.]+)")
  288. set(ImageMagick_VERSION_STRING "${CMAKE_MATCH_1}")
  289. endif()
  290. unset(imagemagick_version)
  291. endif()
  292. #---------------------------------------------------------------------
  293. # Standard Package Output
  294. #---------------------------------------------------------------------
  295. include(FindPackageHandleStandardArgs)
  296. find_package_handle_standard_args(ImageMagick
  297. REQUIRED_VARS ${ImageMagick_REQUIRED_VARS}
  298. VERSION_VAR ImageMagick_VERSION_STRING
  299. )
  300. #---------------------------------------------------------------------
  301. # DEPRECATED: Setting variables for backward compatibility.
  302. #---------------------------------------------------------------------
  303. set(IMAGEMAGICK_BINARY_PATH ${ImageMagick_EXECUTABLE_DIR}
  304. CACHE PATH "Path to the ImageMagick binary directory.")
  305. set(IMAGEMAGICK_CONVERT_EXECUTABLE ${ImageMagick_convert_EXECUTABLE}
  306. CACHE FILEPATH "Path to ImageMagick's convert executable.")
  307. set(IMAGEMAGICK_MOGRIFY_EXECUTABLE ${ImageMagick_mogrify_EXECUTABLE}
  308. CACHE FILEPATH "Path to ImageMagick's mogrify executable.")
  309. set(IMAGEMAGICK_IMPORT_EXECUTABLE ${ImageMagick_import_EXECUTABLE}
  310. CACHE FILEPATH "Path to ImageMagick's import executable.")
  311. set(IMAGEMAGICK_MONTAGE_EXECUTABLE ${ImageMagick_montage_EXECUTABLE}
  312. CACHE FILEPATH "Path to ImageMagick's montage executable.")
  313. set(IMAGEMAGICK_COMPOSITE_EXECUTABLE ${ImageMagick_composite_EXECUTABLE}
  314. CACHE FILEPATH "Path to ImageMagick's composite executable.")
  315. mark_as_advanced(
  316. IMAGEMAGICK_BINARY_PATH
  317. IMAGEMAGICK_CONVERT_EXECUTABLE
  318. IMAGEMAGICK_MOGRIFY_EXECUTABLE
  319. IMAGEMAGICK_IMPORT_EXECUTABLE
  320. IMAGEMAGICK_MONTAGE_EXECUTABLE
  321. IMAGEMAGICK_COMPOSITE_EXECUTABLE
  322. )