FindGIF.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. FindGIF
  5. -------
  6. This finds the Graphics Interchange Format (GIF) library (``giflib``)
  7. Imported targets
  8. ^^^^^^^^^^^^^^^^
  9. This module defines the following :prop_tgt:`IMPORTED` target:
  10. ``GIF::GIF``
  11. The ``giflib`` library, if found.
  12. Result variables
  13. ^^^^^^^^^^^^^^^^
  14. This module will set the following variables in your project:
  15. ``GIF_FOUND``
  16. If false, do not try to use GIF.
  17. ``GIF_INCLUDE_DIRS``
  18. where to find gif_lib.h, etc.
  19. ``GIF_LIBRARIES``
  20. the libraries needed to use GIF.
  21. ``GIF_VERSION``
  22. 3, 4 or a full version string (eg 5.1.4) for versions >= 4.1.6.
  23. Cache variables
  24. ^^^^^^^^^^^^^^^
  25. The following cache variables may also be set:
  26. ``GIF_INCLUDE_DIR``
  27. where to find the GIF headers.
  28. ``GIF_LIBRARY``
  29. where to find the GIF library.
  30. Hints
  31. ^^^^^
  32. ``GIF_DIR`` is an environment variable that would correspond to the
  33. ``./configure --prefix=$GIF_DIR``.
  34. #]=======================================================================]
  35. cmake_policy(PUSH)
  36. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  37. # Created by Eric Wing.
  38. # Modifications by Alexander Neundorf, Ben Campbell
  39. find_path(GIF_INCLUDE_DIR gif_lib.h
  40. HINTS
  41. ENV GIF_DIR
  42. PATH_SUFFIXES include
  43. )
  44. # the gif library can have many names :-/
  45. set(POTENTIAL_GIF_LIBS gif libgif ungif libungif giflib giflib4)
  46. find_library(GIF_LIBRARY
  47. NAMES ${POTENTIAL_GIF_LIBS}
  48. NAMES_PER_DIR
  49. HINTS
  50. ENV GIF_DIR
  51. PATH_SUFFIXES lib
  52. )
  53. # Very basic version detection.
  54. # The GIF_LIB_VERSION string in gif_lib.h seems to be unreliable, since it seems
  55. # to be always " Version 2.0, " in versions 3.x of giflib.
  56. # In version 4 the member UserData was added to GifFileType, so we check for this
  57. # one.
  58. # Versions after 4.1.6 define GIFLIB_MAJOR, GIFLIB_MINOR, and GIFLIB_RELEASE
  59. # see http://giflib.sourceforge.net/gif_lib.html#compatibility
  60. if(GIF_INCLUDE_DIR)
  61. include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  62. include(${CMAKE_CURRENT_LIST_DIR}/CheckStructHasMember.cmake)
  63. CMAKE_PUSH_CHECK_STATE()
  64. set(CMAKE_REQUIRED_QUIET ${GIF_FIND_QUIETLY})
  65. set(CMAKE_REQUIRED_INCLUDES "${GIF_INCLUDE_DIR}")
  66. # Check for the specific version defines (>=4.1.6 only)
  67. file(STRINGS ${GIF_INCLUDE_DIR}/gif_lib.h _GIF_DEFS REGEX "^[ \t]*#define[ \t]+GIFLIB_(MAJOR|MINOR|RELEASE)")
  68. if(_GIF_DEFS)
  69. # yay - got exact version info
  70. string(REGEX REPLACE ".*GIFLIB_MAJOR ([0-9]+).*" "\\1" _GIF_MAJ "${_GIF_DEFS}")
  71. string(REGEX REPLACE ".*GIFLIB_MINOR ([0-9]+).*" "\\1" _GIF_MIN "${_GIF_DEFS}")
  72. string(REGEX REPLACE ".*GIFLIB_RELEASE ([0-9]+).*" "\\1" _GIF_REL "${_GIF_DEFS}")
  73. set(GIF_VERSION "${_GIF_MAJ}.${_GIF_MIN}.${_GIF_REL}")
  74. else()
  75. # use UserData field to sniff version instead
  76. CHECK_STRUCT_HAS_MEMBER(GifFileType UserData gif_lib.h GIF_GifFileType_UserData )
  77. if(GIF_GifFileType_UserData)
  78. set(GIF_VERSION 4)
  79. else()
  80. set(GIF_VERSION 3)
  81. endif()
  82. endif()
  83. unset(_GIF_MAJ)
  84. unset(_GIF_MIN)
  85. unset(_GIF_REL)
  86. unset(_GIF_DEFS)
  87. CMAKE_POP_CHECK_STATE()
  88. endif()
  89. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  90. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GIF REQUIRED_VARS GIF_LIBRARY GIF_INCLUDE_DIR
  91. VERSION_VAR GIF_VERSION )
  92. if(GIF_FOUND)
  93. set(GIF_INCLUDE_DIRS "${GIF_INCLUDE_DIR}")
  94. set(GIF_LIBRARIES ${GIF_LIBRARY})
  95. if(NOT TARGET GIF::GIF)
  96. add_library(GIF::GIF UNKNOWN IMPORTED)
  97. set_target_properties(GIF::GIF PROPERTIES
  98. INTERFACE_INCLUDE_DIRECTORIES "${GIF_INCLUDE_DIRS}")
  99. if(EXISTS "${GIF_LIBRARY}")
  100. set_target_properties(GIF::GIF PROPERTIES
  101. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  102. IMPORTED_LOCATION "${GIF_LIBRARY}")
  103. endif()
  104. endif()
  105. endif()
  106. mark_as_advanced(GIF_INCLUDE_DIR GIF_LIBRARY)
  107. cmake_policy(POP)