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:
5FindJPEG
6--------
7
8Find the Joint Photographic Experts Group (JPEG) library (``libjpeg``)
9
10Imported targets
11^^^^^^^^^^^^^^^^
12
13This module defines the following :prop_tgt:`IMPORTED` targets:
14
15``JPEG::JPEG``
16  The JPEG library, if found.
17
18Result variables
19^^^^^^^^^^^^^^^^
20
21This module will set the following variables in your project:
22
23``JPEG_FOUND``
24  If false, do not try to use JPEG.
25``JPEG_INCLUDE_DIRS``
26  where to find jpeglib.h, etc.
27``JPEG_LIBRARIES``
28  the libraries needed to use JPEG.
29``JPEG_VERSION``
30  the version of the JPEG library found
31
32Cache variables
33^^^^^^^^^^^^^^^
34
35The following cache variables may also be set:
36
37``JPEG_INCLUDE_DIRS``
38  where to find jpeglib.h, etc.
39``JPEG_LIBRARY_RELEASE``
40  where to find the JPEG library (optimized).
41``JPEG_LIBRARY_DEBUG``
42  where to find the JPEG library (debug).
43
44Obsolete variables
45^^^^^^^^^^^^^^^^^^
46
47``JPEG_INCLUDE_DIR``
48  where to find jpeglib.h, etc. (same as JPEG_INCLUDE_DIRS)
49``JPEG_LIBRARY``
50  where to find the JPEG library.
51#]=======================================================================]
52
53find_path(JPEG_INCLUDE_DIR jpeglib.h)
54
55set(jpeg_names ${JPEG_NAMES} jpeg jpeg-static libjpeg libjpeg-static)
56foreach(name ${jpeg_names})
57  list(APPEND jpeg_names_debug "${name}d")
58endforeach()
59
60if(NOT JPEG_LIBRARY)
61  find_library(JPEG_LIBRARY_RELEASE NAMES ${jpeg_names})
62  find_library(JPEG_LIBRARY_DEBUG NAMES ${jpeg_names_debug})
63  include(SelectLibraryConfigurations)
64  select_library_configurations(JPEG)
65  mark_as_advanced(JPEG_LIBRARY_RELEASE JPEG_LIBRARY_DEBUG)
66endif()
67unset(jpeg_names)
68unset(jpeg_names_debug)
69
70if(JPEG_INCLUDE_DIR)
71  file(GLOB _JPEG_CONFIG_HEADERS_FEDORA "${JPEG_INCLUDE_DIR}/jconfig*.h")
72  file(GLOB _JPEG_CONFIG_HEADERS_DEBIAN "${JPEG_INCLUDE_DIR}/*/jconfig.h")
73  set(_JPEG_CONFIG_HEADERS
74    "${JPEG_INCLUDE_DIR}/jpeglib.h"
75    ${_JPEG_CONFIG_HEADERS_FEDORA}
76    ${_JPEG_CONFIG_HEADERS_DEBIAN})
77  foreach (_JPEG_CONFIG_HEADER IN LISTS _JPEG_CONFIG_HEADERS)
78    if (NOT EXISTS "${_JPEG_CONFIG_HEADER}")
79      continue ()
80    endif ()
81    file(STRINGS "${_JPEG_CONFIG_HEADER}"
82      jpeg_lib_version REGEX "^#define[\t ]+JPEG_LIB_VERSION[\t ]+.*")
83
84    if (NOT jpeg_lib_version)
85      continue ()
86    endif ()
87
88    string(REGEX REPLACE "^#define[\t ]+JPEG_LIB_VERSION[\t ]+([0-9]+).*"
89      "\\1" JPEG_VERSION "${jpeg_lib_version}")
90    break ()
91  endforeach ()
92  unset(jpeg_lib_version)
93  unset(_JPEG_CONFIG_HEADER)
94  unset(_JPEG_CONFIG_HEADERS)
95  unset(_JPEG_CONFIG_HEADERS_FEDORA)
96  unset(_JPEG_CONFIG_HEADERS_DEBIAN)
97endif()
98
99include(FindPackageHandleStandardArgs)
100find_package_handle_standard_args(JPEG
101  REQUIRED_VARS JPEG_LIBRARY JPEG_INCLUDE_DIR
102  VERSION_VAR JPEG_VERSION)
103
104if(JPEG_FOUND)
105  set(JPEG_LIBRARIES ${JPEG_LIBRARY})
106  set(JPEG_INCLUDE_DIRS "${JPEG_INCLUDE_DIR}")
107
108  if(NOT TARGET JPEG::JPEG)
109    add_library(JPEG::JPEG UNKNOWN IMPORTED)
110    if(JPEG_INCLUDE_DIRS)
111      set_target_properties(JPEG::JPEG PROPERTIES
112        INTERFACE_INCLUDE_DIRECTORIES "${JPEG_INCLUDE_DIRS}")
113    endif()
114    if(EXISTS "${JPEG_LIBRARY}")
115      set_target_properties(JPEG::JPEG PROPERTIES
116        IMPORTED_LINK_INTERFACE_LANGUAGES "C"
117        IMPORTED_LOCATION "${JPEG_LIBRARY}")
118    endif()
119    if(EXISTS "${JPEG_LIBRARY_RELEASE}")
120      set_property(TARGET JPEG::JPEG APPEND PROPERTY
121        IMPORTED_CONFIGURATIONS RELEASE)
122      set_target_properties(JPEG::JPEG PROPERTIES
123        IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
124        IMPORTED_LOCATION_RELEASE "${JPEG_LIBRARY_RELEASE}")
125    endif()
126    if(EXISTS "${JPEG_LIBRARY_DEBUG}")
127      set_property(TARGET JPEG::JPEG APPEND PROPERTY
128        IMPORTED_CONFIGURATIONS DEBUG)
129      set_target_properties(JPEG::JPEG PROPERTIES
130        IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
131        IMPORTED_LOCATION_DEBUG "${JPEG_LIBRARY_DEBUG}")
132    endif()
133  endif()
134endif()
135
136mark_as_advanced(JPEG_LIBRARY JPEG_INCLUDE_DIR)
137