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:
5FindPNG
6-------
7
8Find libpng, the official reference library for the PNG image format.
9
10Imported targets
11^^^^^^^^^^^^^^^^
12
13.. versionadded:: 3.5
14
15This module defines the following :prop_tgt:`IMPORTED` target:
16
17``PNG::PNG``
18  The libpng library, if found.
19
20Result variables
21^^^^^^^^^^^^^^^^
22
23This module will set the following variables in your project:
24
25``PNG_INCLUDE_DIRS``
26  where to find png.h, etc.
27``PNG_LIBRARIES``
28  the libraries to link against to use PNG.
29``PNG_DEFINITIONS``
30  You should add_definitions(${PNG_DEFINITIONS}) before compiling code
31  that includes png library files.
32``PNG_FOUND``
33  If false, do not try to use PNG.
34``PNG_VERSION_STRING``
35  the version of the PNG library found (since CMake 2.8.8)
36
37Obsolete variables
38^^^^^^^^^^^^^^^^^^
39
40The following variables may also be set, for backwards compatibility:
41
42``PNG_LIBRARY``
43  where to find the PNG library.
44``PNG_INCLUDE_DIR``
45  where to find the PNG headers (same as PNG_INCLUDE_DIRS)
46
47Since PNG depends on the ZLib compression library, none of the above
48will be defined unless ZLib can be found.
49#]=======================================================================]
50
51if(PNG_FIND_QUIETLY)
52  set(_FIND_ZLIB_ARG QUIET)
53endif()
54find_package(ZLIB ${_FIND_ZLIB_ARG})
55
56if(ZLIB_FOUND)
57  find_path(PNG_PNG_INCLUDE_DIR png.h PATH_SUFFIXES include/libpng)
58  mark_as_advanced(PNG_PNG_INCLUDE_DIR)
59
60  list(APPEND PNG_NAMES png libpng)
61  unset(PNG_NAMES_DEBUG)
62  set(_PNG_VERSION_SUFFIXES 17 16 15 14 12)
63  if (PNG_FIND_VERSION MATCHES "^([0-9]+)\\.([0-9]+)(\\..*)?$")
64    set(_PNG_VERSION_SUFFIX_MIN "${CMAKE_MATCH_1}${CMAKE_MATCH_2}")
65    if (PNG_FIND_VERSION_EXACT)
66      set(_PNG_VERSION_SUFFIXES ${_PNG_VERSION_SUFFIX_MIN})
67    else ()
68      string(REGEX REPLACE
69          "${_PNG_VERSION_SUFFIX_MIN}.*" "${_PNG_VERSION_SUFFIX_MIN}"
70          _PNG_VERSION_SUFFIXES "${_PNG_VERSION_SUFFIXES}")
71    endif ()
72    unset(_PNG_VERSION_SUFFIX_MIN)
73  endif ()
74  foreach(v IN LISTS _PNG_VERSION_SUFFIXES)
75    list(APPEND PNG_NAMES png${v} libpng${v} libpng${v}_static)
76    list(APPEND PNG_NAMES_DEBUG png${v}d libpng${v}d libpng${v}_staticd)
77  endforeach()
78  unset(_PNG_VERSION_SUFFIXES)
79  # For compatibility with versions prior to this multi-config search, honor
80  # any PNG_LIBRARY that is already specified and skip the search.
81  if(NOT PNG_LIBRARY)
82    find_library(PNG_LIBRARY_RELEASE NAMES ${PNG_NAMES} NAMES_PER_DIR)
83    find_library(PNG_LIBRARY_DEBUG NAMES ${PNG_NAMES_DEBUG} NAMES_PER_DIR)
84    include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
85    select_library_configurations(PNG)
86    mark_as_advanced(PNG_LIBRARY_RELEASE PNG_LIBRARY_DEBUG)
87  endif()
88  unset(PNG_NAMES)
89  unset(PNG_NAMES_DEBUG)
90
91  # Set by select_library_configurations(), but we want the one from
92  # find_package_handle_standard_args() below.
93  unset(PNG_FOUND)
94
95  if (PNG_LIBRARY AND PNG_PNG_INCLUDE_DIR)
96      # png.h includes zlib.h. Sigh.
97      set(PNG_INCLUDE_DIRS ${PNG_PNG_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} )
98      set(PNG_INCLUDE_DIR ${PNG_INCLUDE_DIRS} ) # for backward compatibility
99      set(PNG_LIBRARIES ${PNG_LIBRARY} ${ZLIB_LIBRARY})
100      if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
101         ("${PNG_LIBRARY}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))
102        list(APPEND PNG_LIBRARIES m)
103      endif()
104
105      if (CYGWIN)
106        if(BUILD_SHARED_LIBS)
107           # No need to define PNG_USE_DLL here, because it's default for Cygwin.
108        else()
109          set (PNG_DEFINITIONS -DPNG_STATIC)
110          set(_PNG_COMPILE_DEFINITIONS PNG_STATIC)
111        endif()
112      endif ()
113
114      if(NOT TARGET PNG::PNG)
115        add_library(PNG::PNG UNKNOWN IMPORTED)
116        set_target_properties(PNG::PNG PROPERTIES
117          INTERFACE_COMPILE_DEFINITIONS "${_PNG_COMPILE_DEFINITIONS}"
118          INTERFACE_INCLUDE_DIRECTORIES "${PNG_INCLUDE_DIRS}"
119          INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
120        if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
121           ("${PNG_LIBRARY}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))
122          set_property(TARGET PNG::PNG APPEND PROPERTY
123            INTERFACE_LINK_LIBRARIES m)
124        endif()
125
126        if(EXISTS "${PNG_LIBRARY}")
127          set_target_properties(PNG::PNG PROPERTIES
128            IMPORTED_LINK_INTERFACE_LANGUAGES "C"
129            IMPORTED_LOCATION "${PNG_LIBRARY}")
130        endif()
131        if(EXISTS "${PNG_LIBRARY_RELEASE}")
132          set_property(TARGET PNG::PNG APPEND PROPERTY
133            IMPORTED_CONFIGURATIONS RELEASE)
134          set_target_properties(PNG::PNG PROPERTIES
135            IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
136            IMPORTED_LOCATION_RELEASE "${PNG_LIBRARY_RELEASE}")
137        endif()
138        if(EXISTS "${PNG_LIBRARY_DEBUG}")
139          set_property(TARGET PNG::PNG APPEND PROPERTY
140            IMPORTED_CONFIGURATIONS DEBUG)
141          set_target_properties(PNG::PNG PROPERTIES
142            IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
143            IMPORTED_LOCATION_DEBUG "${PNG_LIBRARY_DEBUG}")
144        endif()
145      endif()
146
147      unset(_PNG_COMPILE_DEFINITIONS)
148  endif ()
149
150  if (PNG_PNG_INCLUDE_DIR AND EXISTS "${PNG_PNG_INCLUDE_DIR}/png.h")
151      file(STRINGS "${PNG_PNG_INCLUDE_DIR}/png.h" png_version_str REGEX "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\".+\"")
152
153      string(REGEX REPLACE "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\"([^\"]+)\".*" "\\1" PNG_VERSION_STRING "${png_version_str}")
154      unset(png_version_str)
155  endif ()
156endif()
157
158include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
159find_package_handle_standard_args(PNG
160                                  REQUIRED_VARS PNG_LIBRARY PNG_PNG_INCLUDE_DIR
161                                  VERSION_VAR PNG_VERSION_STRING)
162