FindGIF.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. FindGIF
  5. -------
  6. Finds the Graphics Interchange Format (GIF) library (``giflib``):
  7. .. code-block:: cmake
  8. find_package(GIF [<version>] [...])
  9. Imported Targets
  10. ^^^^^^^^^^^^^^^^
  11. This module provides the following :ref:`Imported Targets`:
  12. ``GIF::GIF``
  13. .. versionadded:: 3.14
  14. Target that encapsulates the usage requirements of the GIF library, available
  15. when the library is found.
  16. Result Variables
  17. ^^^^^^^^^^^^^^^^
  18. This module defines the following variables:
  19. ``GIF_FOUND``
  20. Boolean indicating whether the (requested version of) GIF library was found.
  21. ``GIF_VERSION``
  22. Version string of the GIF library found (for example, ``5.1.4``). For GIF
  23. library versions prior to 4.1.6, version string will be set only to ``3`` or
  24. ``4`` as these versions did not provide version information in their headers.
  25. ``GIF_INCLUDE_DIRS``
  26. Include directories needed to use the GIF library.
  27. ``GIF_LIBRARIES``
  28. Libraries needed to link to the GIF library.
  29. Cache Variables
  30. ^^^^^^^^^^^^^^^
  31. The following cache variables may also be set:
  32. ``GIF_INCLUDE_DIR``
  33. Directory containing the ``gif_lib.h`` and other GIF library headers.
  34. ``GIF_LIBRARY``
  35. Path to the GIF library.
  36. Hints
  37. ^^^^^
  38. This module accepts the following variables:
  39. ``GIF_DIR``
  40. Environment variable that can be set to help locate a GIF library installed in
  41. a custom location. It should point to the installation destination that was
  42. used when configuring, building, and installing GIF library:
  43. ``./configure --prefix=$GIF_DIR``.
  44. Examples
  45. ^^^^^^^^
  46. Finding GIF library and linking it to a project target:
  47. .. code-block:: cmake
  48. find_package(GIF)
  49. target_link_libraries(project_target PRIVATE GIF::GIF)
  50. #]=======================================================================]
  51. cmake_policy(PUSH)
  52. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  53. # Created by Eric Wing.
  54. # Modifications by Alexander Neundorf, Ben Campbell
  55. find_path(GIF_INCLUDE_DIR gif_lib.h
  56. HINTS
  57. ENV GIF_DIR
  58. PATH_SUFFIXES include
  59. )
  60. # the gif library can have many names :-/
  61. set(POTENTIAL_GIF_LIBS gif libgif ungif libungif giflib giflib4)
  62. find_library(GIF_LIBRARY
  63. NAMES ${POTENTIAL_GIF_LIBS}
  64. NAMES_PER_DIR
  65. HINTS
  66. ENV GIF_DIR
  67. PATH_SUFFIXES lib
  68. )
  69. # Very basic version detection.
  70. # The GIF_LIB_VERSION string in gif_lib.h seems to be unreliable, since it seems
  71. # to be always " Version 2.0, " in versions 3.x of giflib.
  72. # In version 4 the member UserData was added to GifFileType, so we check for this
  73. # one.
  74. # Versions after 4.1.6 define GIFLIB_MAJOR, GIFLIB_MINOR, and GIFLIB_RELEASE
  75. # see http://giflib.sourceforge.net/gif_lib.html#compatibility
  76. if(GIF_INCLUDE_DIR)
  77. include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  78. include(${CMAKE_CURRENT_LIST_DIR}/CheckStructHasMember.cmake)
  79. cmake_push_check_state()
  80. set(CMAKE_REQUIRED_QUIET ${GIF_FIND_QUIETLY})
  81. set(CMAKE_REQUIRED_INCLUDES "${GIF_INCLUDE_DIR}")
  82. # Check for the specific version defines (>=4.1.6 only)
  83. file(STRINGS ${GIF_INCLUDE_DIR}/gif_lib.h _GIF_DEFS REGEX "^[ \t]*#define[ \t]+GIFLIB_(MAJOR|MINOR|RELEASE)")
  84. if(_GIF_DEFS)
  85. # yay - got exact version info
  86. string(REGEX REPLACE ".*GIFLIB_MAJOR ([0-9]+).*" "\\1" _GIF_MAJ "${_GIF_DEFS}")
  87. string(REGEX REPLACE ".*GIFLIB_MINOR ([0-9]+).*" "\\1" _GIF_MIN "${_GIF_DEFS}")
  88. string(REGEX REPLACE ".*GIFLIB_RELEASE ([0-9]+).*" "\\1" _GIF_REL "${_GIF_DEFS}")
  89. set(GIF_VERSION "${_GIF_MAJ}.${_GIF_MIN}.${_GIF_REL}")
  90. else()
  91. # use UserData field to sniff version instead
  92. check_struct_has_member(GifFileType UserData gif_lib.h GIF_GifFileType_UserData )
  93. if(GIF_GifFileType_UserData)
  94. set(GIF_VERSION 4)
  95. else()
  96. set(GIF_VERSION 3)
  97. endif()
  98. endif()
  99. unset(_GIF_MAJ)
  100. unset(_GIF_MIN)
  101. unset(_GIF_REL)
  102. unset(_GIF_DEFS)
  103. cmake_pop_check_state()
  104. endif()
  105. include(FindPackageHandleStandardArgs)
  106. find_package_handle_standard_args(GIF REQUIRED_VARS GIF_LIBRARY GIF_INCLUDE_DIR
  107. VERSION_VAR GIF_VERSION )
  108. if(GIF_FOUND)
  109. set(GIF_INCLUDE_DIRS "${GIF_INCLUDE_DIR}")
  110. set(GIF_LIBRARIES ${GIF_LIBRARY})
  111. if(NOT TARGET GIF::GIF)
  112. add_library(GIF::GIF UNKNOWN IMPORTED)
  113. set_target_properties(GIF::GIF PROPERTIES
  114. INTERFACE_INCLUDE_DIRECTORIES "${GIF_INCLUDE_DIRS}")
  115. if(EXISTS "${GIF_LIBRARY}")
  116. set_target_properties(GIF::GIF PROPERTIES
  117. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  118. IMPORTED_LOCATION "${GIF_LIBRARY}")
  119. endif()
  120. endif()
  121. endif()
  122. mark_as_advanced(GIF_INCLUDE_DIR GIF_LIBRARY)
  123. cmake_policy(POP)