FindImageMagick.cmake 13 KB

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