1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindGIF
6-------
7
8This finds the Graphics Interchange Format (GIF) library (``giflib``)
9
10Imported targets
11^^^^^^^^^^^^^^^^
12
13This module defines the following :prop_tgt:`IMPORTED` target:
14
15``GIF::GIF``
16  The ``giflib`` library, if found.
17
18Result variables
19^^^^^^^^^^^^^^^^
20
21This module will set the following variables in your project:
22
23``GIF_FOUND``
24  If false, do not try to use GIF.
25``GIF_INCLUDE_DIRS``
26  where to find gif_lib.h, etc.
27``GIF_LIBRARIES``
28  the libraries needed to use GIF.
29``GIF_VERSION``
30  3, 4 or a full version string (eg 5.1.4) for versions >= 4.1.6.
31
32Cache variables
33^^^^^^^^^^^^^^^
34
35The following cache variables may also be set:
36
37``GIF_INCLUDE_DIR``
38  where to find the GIF headers.
39``GIF_LIBRARY``
40  where to find the GIF library.
41
42Hints
43^^^^^
44
45``GIF_DIR`` is an environment variable that would correspond to the
46``./configure --prefix=$GIF_DIR``.
47#]=======================================================================]
48
49# Created by Eric Wing.
50# Modifications by Alexander Neundorf, Ben Campbell
51
52find_path(GIF_INCLUDE_DIR gif_lib.h
53  HINTS
54    ENV GIF_DIR
55  PATH_SUFFIXES include
56)
57
58# the gif library can have many names :-/
59set(POTENTIAL_GIF_LIBS gif libgif ungif libungif giflib giflib4)
60
61find_library(GIF_LIBRARY
62  NAMES ${POTENTIAL_GIF_LIBS}
63  NAMES_PER_DIR
64  HINTS
65    ENV GIF_DIR
66  PATH_SUFFIXES lib
67)
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
76if(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
83  # Check for the specific version defines (>=4.1.6 only)
84  file(STRINGS ${GIF_INCLUDE_DIR}/gif_lib.h _GIF_DEFS REGEX "^[ \t]*#define[ \t]+GIFLIB_(MAJOR|MINOR|RELEASE)")
85  if(_GIF_DEFS)
86    # yay - got exact version info
87    string(REGEX REPLACE ".*GIFLIB_MAJOR ([0-9]+).*" "\\1" _GIF_MAJ "${_GIF_DEFS}")
88    string(REGEX REPLACE ".*GIFLIB_MINOR ([0-9]+).*" "\\1" _GIF_MIN "${_GIF_DEFS}")
89    string(REGEX REPLACE ".*GIFLIB_RELEASE ([0-9]+).*" "\\1" _GIF_REL "${_GIF_DEFS}")
90    set(GIF_VERSION "${_GIF_MAJ}.${_GIF_MIN}.${_GIF_REL}")
91  else()
92    # use UserData field to sniff version instead
93    CHECK_STRUCT_HAS_MEMBER(GifFileType UserData gif_lib.h GIF_GifFileType_UserData )
94    if(GIF_GifFileType_UserData)
95      set(GIF_VERSION 4)
96    else()
97      set(GIF_VERSION 3)
98    endif()
99  endif()
100
101  unset(_GIF_MAJ)
102  unset(_GIF_MIN)
103  unset(_GIF_REL)
104  unset(_GIF_DEFS)
105  CMAKE_POP_CHECK_STATE()
106endif()
107
108include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
109FIND_PACKAGE_HANDLE_STANDARD_ARGS(GIF  REQUIRED_VARS  GIF_LIBRARY  GIF_INCLUDE_DIR
110                                       VERSION_VAR GIF_VERSION )
111
112if(GIF_FOUND)
113  set(GIF_INCLUDE_DIRS "${GIF_INCLUDE_DIR}")
114  set(GIF_LIBRARIES ${GIF_LIBRARY})
115
116  if(NOT TARGET GIF::GIF)
117    add_library(GIF::GIF UNKNOWN IMPORTED)
118    set_target_properties(GIF::GIF PROPERTIES
119      INTERFACE_INCLUDE_DIRECTORIES "${GIF_INCLUDE_DIRS}")
120    if(EXISTS "${GIF_LIBRARY}")
121      set_target_properties(GIF::GIF PROPERTIES
122        IMPORTED_LINK_INTERFACE_LANGUAGES "C"
123        IMPORTED_LOCATION "${GIF_LIBRARY}")
124    endif()
125  endif()
126endif()
127
128mark_as_advanced(GIF_INCLUDE_DIR GIF_LIBRARY)
129