FindImageMagick.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 (the requested version of) ImageMagick and all
  61. its requested components were found.
  62. ``ImageMagick_VERSION``
  63. .. versionadded:: 4.2
  64. The version of ImageMagick found in form of
  65. ``<major>.<minor>.<patch>-<addendum>`` (e.g., ``6.9.12-98``, where ``98``
  66. is the addendum release number).
  67. .. note::
  68. Version detection is available only for ImageMagick 6 and later.
  69. ``ImageMagick_INCLUDE_DIRS``
  70. All include directories needed to use ImageMagick.
  71. ``ImageMagick_LIBRARIES``
  72. Libraries needed to link against to use ImageMagick.
  73. ``ImageMagick_COMPILE_OPTIONS``
  74. .. versionadded:: 3.26
  75. Compile options of all libraries.
  76. ``ImageMagick_<component>_FOUND``
  77. Boolean indicating whether the ImageMagick ``<component>`` is found.
  78. ``ImageMagick_<component>_EXECUTABLE``
  79. The full path to ``<component>`` executable.
  80. ``ImageMagick_<component>_INCLUDE_DIRS``
  81. Include directories containing headers needed to use the ImageMagick
  82. ``<component>``.
  83. ``ImageMagick_<component>_COMPILE_OPTIONS``
  84. .. versionadded:: 3.26
  85. Compile options of the ImageMagick ``<component>``.
  86. ``ImageMagick_<component>_LIBRARIES``
  87. .. versionadded:: 3.31
  88. Libraries needed to link against to use the ImageMagick ``<component>``.
  89. Cache Variables
  90. ^^^^^^^^^^^^^^^
  91. The following cache variables may also be set:
  92. ``ImageMagick_EXECUTABLE_DIR``
  93. The full path to directory containing ImageMagick executables.
  94. Deprecated Variables
  95. ^^^^^^^^^^^^^^^^^^^^
  96. The following variables are provided for backward compatibility:
  97. ``ImageMagick_VERSION_STRING``
  98. .. deprecated:: 4.2
  99. Use ``ImageMagick_VERSION``, which has the same value.
  100. The version of ImageMagick found.
  101. Examples
  102. ^^^^^^^^
  103. Finding ImageMagick with its component ``Magick++`` and linking it to a project
  104. target:
  105. .. code-block:: cmake
  106. find_package(ImageMagick COMPONENTS Magick++)
  107. target_link_libraries(example PRIVATE ImageMagick::Magick++)
  108. #]=======================================================================]
  109. cmake_policy(PUSH)
  110. cmake_policy(SET CMP0140 NEW)
  111. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  112. find_package(PkgConfig QUIET)
  113. #---------------------------------------------------------------------
  114. # Helper functions
  115. #---------------------------------------------------------------------
  116. function(FIND_IMAGEMAGICK_API component header)
  117. set(ImageMagick_${component}_FOUND FALSE PARENT_SCOPE)
  118. if(PkgConfig_FOUND)
  119. pkg_check_modules(PC_${component} QUIET ${component})
  120. endif()
  121. find_path(ImageMagick_${component}_INCLUDE_DIR
  122. NAMES ${header}
  123. HINTS
  124. ${PC_${component}_INCLUDEDIR}
  125. ${PC_${component}_INCLUDE_DIRS}
  126. PATHS
  127. ${ImageMagick_INCLUDE_DIRS}
  128. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\ImageMagick\\Current;BinPath]/include"
  129. PATH_SUFFIXES
  130. ImageMagick ImageMagick-6 ImageMagick-7
  131. DOC "Path to the ImageMagick arch-independent include dir."
  132. NO_DEFAULT_PATH
  133. )
  134. find_path(ImageMagick_${component}_ARCH_INCLUDE_DIR
  135. NAMES
  136. magick/magick-baseconfig.h
  137. MagickCore/magick-baseconfig.h
  138. HINTS
  139. ${PC_${component}_INCLUDEDIR}
  140. ${PC_${component}_INCLUDE_DIRS}
  141. PATHS
  142. ${ImageMagick_INCLUDE_DIRS}
  143. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\ImageMagick\\Current;BinPath]/include"
  144. PATH_SUFFIXES
  145. ImageMagick ImageMagick-6 ImageMagick-7
  146. DOC "Path to the ImageMagick arch-specific include dir."
  147. NO_DEFAULT_PATH
  148. )
  149. find_library(ImageMagick_${component}_LIBRARY
  150. NAMES ${ARGN}
  151. HINTS
  152. ${PC_${component}_LIBDIR}
  153. ${PC_${component}_LIB_DIRS}
  154. PATHS
  155. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\ImageMagick\\Current;BinPath]/lib"
  156. DOC "Path to the ImageMagick Magick++ library."
  157. NO_DEFAULT_PATH
  158. )
  159. # old version have only indep dir
  160. if(ImageMagick_${component}_INCLUDE_DIR AND ImageMagick_${component}_LIBRARY)
  161. set(ImageMagick_${component}_FOUND TRUE PARENT_SCOPE)
  162. # Construct per-component include directories.
  163. set(ImageMagick_${component}_INCLUDE_DIRS
  164. ${ImageMagick_${component}_INCLUDE_DIR}
  165. )
  166. if(ImageMagick_${component}_ARCH_INCLUDE_DIR)
  167. list(APPEND ImageMagick_${component}_INCLUDE_DIRS
  168. ${ImageMagick_${component}_ARCH_INCLUDE_DIR})
  169. endif()
  170. list(REMOVE_DUPLICATES ImageMagick_${component}_INCLUDE_DIRS)
  171. set(ImageMagick_${component}_INCLUDE_DIRS
  172. ${ImageMagick_${component}_INCLUDE_DIRS} PARENT_SCOPE)
  173. set(ImageMagick_${component}_LIBRARIES
  174. ${ImageMagick_${component}_LIBRARY}
  175. )
  176. set(ImageMagick_${component}_LIBRARIES
  177. ${ImageMagick_${component}_LIBRARIES} PARENT_SCOPE)
  178. set(ImageMagick_${component}_COMPILE_OPTIONS ${PC_${component}_CFLAGS_OTHER})
  179. # Add the per-component include directories to the full include dirs.
  180. list(APPEND ImageMagick_INCLUDE_DIRS ${ImageMagick_${component}_INCLUDE_DIRS})
  181. list(REMOVE_DUPLICATES ImageMagick_INCLUDE_DIRS)
  182. set(ImageMagick_INCLUDE_DIRS ${ImageMagick_INCLUDE_DIRS} PARENT_SCOPE)
  183. list(APPEND ImageMagick_LIBRARIES
  184. ${ImageMagick_${component}_LIBRARY}
  185. )
  186. set(ImageMagick_LIBRARIES ${ImageMagick_LIBRARIES} PARENT_SCOPE)
  187. list(APPEND ImageMagick_COMPILE_OPTIONS
  188. ${ImageMagick_${component}_COMPILE_OPTIONS}
  189. )
  190. set(ImageMagick_COMPILE_OPTIONS ${ImageMagick_COMPILE_OPTIONS} PARENT_SCOPE)
  191. if(NOT TARGET ImageMagick::${component})
  192. add_library(ImageMagick::${component} UNKNOWN IMPORTED)
  193. set_target_properties(ImageMagick::${component} PROPERTIES
  194. INTERFACE_INCLUDE_DIRECTORIES "${ImageMagick_${component}_INCLUDE_DIRS}"
  195. INTERFACE_COMPILE_OPTIONS "${ImageMagick_${component}_COMPILE_OPTIONS}"
  196. IMPORTED_LOCATION "${ImageMagick_${component}_LIBRARY}")
  197. endif()
  198. endif()
  199. endfunction()
  200. function(FIND_IMAGEMAGICK_EXE component)
  201. set(_IMAGEMAGICK_EXECUTABLE
  202. ${ImageMagick_EXECUTABLE_DIR}/${component}${CMAKE_EXECUTABLE_SUFFIX})
  203. if(EXISTS ${_IMAGEMAGICK_EXECUTABLE})
  204. set(ImageMagick_${component}_EXECUTABLE
  205. ${_IMAGEMAGICK_EXECUTABLE}
  206. PARENT_SCOPE
  207. )
  208. set(ImageMagick_${component}_FOUND TRUE PARENT_SCOPE)
  209. else()
  210. set(ImageMagick_${component}_FOUND FALSE PARENT_SCOPE)
  211. endif()
  212. endfunction()
  213. function(_ImageMagick_GetVersion)
  214. unset(version)
  215. if(ImageMagick_mogrify_EXECUTABLE)
  216. execute_process(
  217. COMMAND ${ImageMagick_mogrify_EXECUTABLE} -version
  218. OUTPUT_VARIABLE version
  219. ERROR_QUIET
  220. OUTPUT_STRIP_TRAILING_WHITESPACE
  221. )
  222. if(version MATCHES "^Version: ImageMagick ([-0-9.]+)")
  223. set(version "${CMAKE_MATCH_1}")
  224. endif()
  225. elseif(ImageMagick_INCLUDE_DIRS)
  226. # MagickLibSubversion was used in ImageMagick <= 6.5.
  227. set(
  228. regex
  229. "^[\t ]*#[\t ]*define[\t ]+(MagickLibVersionText|MagickLibAddendum|MagickLibSubversion)[\t ]+\"([-0-9.]+)\""
  230. )
  231. foreach(dir IN LISTS ImageMagick_INCLUDE_DIRS)
  232. if(EXISTS ${dir}/magick/version.h)
  233. file(STRINGS "${dir}/magick/version.h" results REGEX "${regex}")
  234. foreach(line ${results})
  235. if(line MATCHES "${regex}")
  236. if(DEFINED version)
  237. string(APPEND version "${CMAKE_MATCH_2}")
  238. else()
  239. set(version "${CMAKE_MATCH_2}")
  240. endif()
  241. if(CMAKE_MATCH_1 STREQUAL "MagickLibAddendum")
  242. break()
  243. endif()
  244. endif()
  245. endforeach()
  246. endif()
  247. if(DEFINED version)
  248. break()
  249. endif()
  250. endforeach()
  251. endif()
  252. if(DEFINED version)
  253. set(ImageMagick_VERSION "${version}")
  254. set(ImageMagick_VERSION_STRING "${ImageMagick_VERSION}")
  255. endif()
  256. return(PROPAGATE ImageMagick_VERSION ImageMagick_VERSION_STRING)
  257. endfunction()
  258. #---------------------------------------------------------------------
  259. # Start Actual Work
  260. #---------------------------------------------------------------------
  261. # Try to find a ImageMagick installation binary path.
  262. find_path(ImageMagick_EXECUTABLE_DIR
  263. NAMES mogrify${CMAKE_EXECUTABLE_SUFFIX}
  264. PATHS
  265. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\ImageMagick\\Current;BinPath]"
  266. DOC "Path to the ImageMagick binary directory."
  267. NO_DEFAULT_PATH
  268. )
  269. find_path(ImageMagick_EXECUTABLE_DIR
  270. NAMES mogrify${CMAKE_EXECUTABLE_SUFFIX}
  271. )
  272. # Find each component. Search for all tools in same dir
  273. # <ImageMagick_EXECUTABLE_DIR>; otherwise they should be found
  274. # independently and not in a cohesive module such as this one.
  275. unset(ImageMagick_REQUIRED_VARS)
  276. unset(ImageMagick_DEFAULT_EXECUTABLES)
  277. foreach(component ${ImageMagick_FIND_COMPONENTS}
  278. # DEPRECATED: forced components for backward compatibility
  279. convert mogrify import montage composite
  280. )
  281. if(component STREQUAL "Magick++")
  282. FIND_IMAGEMAGICK_API(Magick++ Magick++.h
  283. Magick++ CORE_RL_Magick++_
  284. Magick++-6 Magick++-7
  285. Magick++-Q8 Magick++-Q16 Magick++-Q16HDRI Magick++-Q8HDRI
  286. Magick++-6.Q64 Magick++-6.Q32 Magick++-6.Q64HDRI Magick++-6.Q32HDRI
  287. Magick++-6.Q16 Magick++-6.Q8 Magick++-6.Q16HDRI Magick++-6.Q8HDRI
  288. Magick++-7.Q64 Magick++-7.Q32 Magick++-7.Q64HDRI Magick++-7.Q32HDRI
  289. Magick++-7.Q16 Magick++-7.Q8 Magick++-7.Q16HDRI Magick++-7.Q8HDRI
  290. )
  291. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_Magick++_LIBRARY)
  292. elseif(component STREQUAL "MagickWand")
  293. FIND_IMAGEMAGICK_API(MagickWand "wand/MagickWand.h;MagickWand/MagickWand.h"
  294. Wand MagickWand CORE_RL_wand_ CORE_RL_MagickWand_
  295. MagickWand-6 MagickWand-7
  296. MagickWand-Q16 MagickWand-Q8 MagickWand-Q16HDRI MagickWand-Q8HDRI
  297. MagickWand-6.Q64 MagickWand-6.Q32 MagickWand-6.Q64HDRI MagickWand-6.Q32HDRI
  298. MagickWand-6.Q16 MagickWand-6.Q8 MagickWand-6.Q16HDRI MagickWand-6.Q8HDRI
  299. MagickWand-7.Q64 MagickWand-7.Q32 MagickWand-7.Q64HDRI MagickWand-7.Q32HDRI
  300. MagickWand-7.Q16 MagickWand-7.Q8 MagickWand-7.Q16HDRI MagickWand-7.Q8HDRI
  301. )
  302. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_MagickWand_LIBRARY)
  303. elseif(component STREQUAL "MagickCore")
  304. FIND_IMAGEMAGICK_API(MagickCore "magick/MagickCore.h;MagickCore/MagickCore.h"
  305. Magick MagickCore CORE_RL_magick_ CORE_RL_MagickCore_
  306. MagickCore-6 MagickCore-7
  307. MagickCore-Q16 MagickCore-Q8 MagickCore-Q16HDRI MagickCore-Q8HDRI
  308. MagickCore-6.Q64 MagickCore-6.Q32 MagickCore-6.Q64HDRI MagickCore-6.Q32HDRI
  309. MagickCore-6.Q16 MagickCore-6.Q8 MagickCore-6.Q16HDRI MagickCore-6.Q8HDRI
  310. MagickCore-7.Q64 MagickCore-7.Q32 MagickCore-7.Q64HDRI MagickCore-7.Q32HDRI
  311. MagickCore-7.Q16 MagickCore-7.Q8 MagickCore-7.Q16HDRI MagickCore-7.Q8HDRI
  312. )
  313. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_MagickCore_LIBRARY)
  314. else()
  315. if(ImageMagick_EXECUTABLE_DIR)
  316. FIND_IMAGEMAGICK_EXE(${component})
  317. endif()
  318. if(ImageMagick_FIND_COMPONENTS)
  319. list(FIND ImageMagick_FIND_COMPONENTS ${component} is_requested)
  320. if(is_requested GREATER -1)
  321. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_${component}_EXECUTABLE)
  322. endif()
  323. elseif(ImageMagick_${component}_EXECUTABLE)
  324. # if no components were requested explicitly put all (default) executables
  325. # in the list
  326. list(APPEND ImageMagick_DEFAULT_EXECUTABLES ImageMagick_${component}_EXECUTABLE)
  327. endif()
  328. endif()
  329. endforeach()
  330. if(NOT ImageMagick_FIND_COMPONENTS AND NOT ImageMagick_DEFAULT_EXECUTABLES)
  331. # No components were requested, and none of the default components were
  332. # found. Just insert mogrify into the list of the default components to
  333. # find so FPHSA below has something to check
  334. list(APPEND ImageMagick_REQUIRED_VARS ImageMagick_mogrify_EXECUTABLE)
  335. elseif(ImageMagick_DEFAULT_EXECUTABLES)
  336. list(APPEND ImageMagick_REQUIRED_VARS ${ImageMagick_DEFAULT_EXECUTABLES})
  337. endif()
  338. set(ImageMagick_INCLUDE_DIRS ${ImageMagick_INCLUDE_DIRS})
  339. set(ImageMagick_LIBRARIES ${ImageMagick_LIBRARIES})
  340. _ImageMagick_GetVersion()
  341. #---------------------------------------------------------------------
  342. # Standard Package Output
  343. #---------------------------------------------------------------------
  344. include(FindPackageHandleStandardArgs)
  345. find_package_handle_standard_args(ImageMagick
  346. REQUIRED_VARS ${ImageMagick_REQUIRED_VARS}
  347. VERSION_VAR ImageMagick_VERSION
  348. )
  349. #---------------------------------------------------------------------
  350. # DEPRECATED: Setting variables for backward compatibility.
  351. #---------------------------------------------------------------------
  352. set(IMAGEMAGICK_BINARY_PATH ${ImageMagick_EXECUTABLE_DIR}
  353. CACHE PATH "Path to the ImageMagick binary directory.")
  354. set(IMAGEMAGICK_CONVERT_EXECUTABLE ${ImageMagick_convert_EXECUTABLE}
  355. CACHE FILEPATH "Path to ImageMagick's convert executable.")
  356. set(IMAGEMAGICK_MOGRIFY_EXECUTABLE ${ImageMagick_mogrify_EXECUTABLE}
  357. CACHE FILEPATH "Path to ImageMagick's mogrify executable.")
  358. set(IMAGEMAGICK_IMPORT_EXECUTABLE ${ImageMagick_import_EXECUTABLE}
  359. CACHE FILEPATH "Path to ImageMagick's import executable.")
  360. set(IMAGEMAGICK_MONTAGE_EXECUTABLE ${ImageMagick_montage_EXECUTABLE}
  361. CACHE FILEPATH "Path to ImageMagick's montage executable.")
  362. set(IMAGEMAGICK_COMPOSITE_EXECUTABLE ${ImageMagick_composite_EXECUTABLE}
  363. CACHE FILEPATH "Path to ImageMagick's composite executable.")
  364. mark_as_advanced(
  365. IMAGEMAGICK_BINARY_PATH
  366. IMAGEMAGICK_CONVERT_EXECUTABLE
  367. IMAGEMAGICK_MOGRIFY_EXECUTABLE
  368. IMAGEMAGICK_IMPORT_EXECUTABLE
  369. IMAGEMAGICK_MONTAGE_EXECUTABLE
  370. IMAGEMAGICK_COMPOSITE_EXECUTABLE
  371. )
  372. cmake_policy(POP)