1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindPNG
-------
Finds libpng, the official reference library for the PNG image format.
.. note::
The PNG library depends on the ZLib compression library, which must be found
for this module to succeed.
Imported Targets
^^^^^^^^^^^^^^^^
.. versionadded:: 3.5
This module defines the following :ref:`Imported Targets`:
``PNG::PNG``
The libpng library, if found.
Result Variables
^^^^^^^^^^^^^^^^
This module sets the following variables:
``PNG_INCLUDE_DIRS``
Directory containing the PNG headers (e.g., ``png.h``).
``PNG_LIBRARIES``
PNG libraries required for linking.
``PNG_DEFINITIONS``
Compile definitions for using PNG, if any. They can be added with
:command:`target_compile_definitions` command when not using the ``PNG::PNG``
imported target.
``PNG_FOUND``
True if PNG library is found.
``PNG_VERSION_STRING``
The version of the PNG library found.
Obsolete Variables
^^^^^^^^^^^^^^^^^^
The following variables may also be set for backward compatibility:
``PNG_LIBRARY``
Path to the PNG library.
``PNG_INCLUDE_DIR``
Directory containing the PNG headers (same as ``PNG_INCLUDE_DIRS``).
Examples
^^^^^^^^
Finding PNG library and using it in a project:
.. code-block:: cmake
find_package(PNG)
target_link_libraries(project_target PRIVATE PNG::PNG)
#]=======================================================================]
cmake_policy(PUSH)
cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
# Default install location on windows when installing from included cmake build
# From FindZLIB.cmake
set(_PNG_x86 "(x86)")
set(_PNG_INCLUDE_SEARCH_NORMAL
"$ENV{ProgramFiles}/libpng"
"$ENV{ProgramFiles${_PNG_x86}}/libpng")
set(_PNG_LIB_SEARCH_NORMAL
"$ENV{ProgramFiles}/libpng/lib"
"$ENV{ProgramFiles${_PNG_x86}}/libpng/lib")
unset(_PNG_x86)
if(PNG_FIND_QUIETLY)
set(_FIND_ZLIB_ARG QUIET)
endif()
find_package(ZLIB ${_FIND_ZLIB_ARG})
if(ZLIB_FOUND)
set(_PNG_VERSION_SUFFIXES 17 16 15 14 12)
list(APPEND _PNG_INCLUDE_PATH_SUFFIXES include/libpng)
foreach(v IN LISTS _PNG_VERSION_SUFFIXES)
list(APPEND _PNG_INCLUDE_PATH_SUFFIXES include/libpng${v})
endforeach()
find_path(PNG_PNG_INCLUDE_DIR png.h PATH_SUFFIXES ${_PNG_INCLUDE_PATH_SUFFIXES} PATHS ${_PNG_INCLUDE_SEARCH_NORMAL} )
mark_as_advanced(PNG_PNG_INCLUDE_DIR)
list(APPEND PNG_NAMES png libpng)
unset(PNG_NAMES_DEBUG)
if (PNG_FIND_VERSION MATCHES "^([0-9]+)\\.([0-9]+)(\\..*)?$")
set(_PNG_VERSION_SUFFIX_MIN "${CMAKE_MATCH_1}${CMAKE_MATCH_2}")
if (PNG_FIND_VERSION_EXACT)
set(_PNG_VERSION_SUFFIXES ${_PNG_VERSION_SUFFIX_MIN})
else ()
string(REGEX REPLACE
"${_PNG_VERSION_SUFFIX_MIN}.*" "${_PNG_VERSION_SUFFIX_MIN}"
_PNG_VERSION_SUFFIXES "${_PNG_VERSION_SUFFIXES}")
endif ()
unset(_PNG_VERSION_SUFFIX_MIN)
endif ()
foreach(v IN LISTS _PNG_VERSION_SUFFIXES)
list(APPEND PNG_NAMES png${v} libpng${v} libpng${v}_static)
list(APPEND PNG_NAMES_DEBUG png${v}d libpng${v}d libpng${v}_staticd)
endforeach()
unset(_PNG_VERSION_SUFFIXES)
# For compatibility with versions prior to this multi-config search, honor
# any PNG_LIBRARY that is already specified and skip the search.
if(NOT PNG_LIBRARY)
find_library(PNG_LIBRARY_RELEASE NAMES ${PNG_NAMES} NAMES_PER_DIR PATHS ${_PNG_LIB_SEARCH_NORMAL})
find_library(PNG_LIBRARY_DEBUG NAMES ${PNG_NAMES_DEBUG} NAMES_PER_DIR PATHS ${_PNG_LIB_SEARCH_NORMAL})
include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
select_library_configurations(PNG)
mark_as_advanced(PNG_LIBRARY_RELEASE PNG_LIBRARY_DEBUG)
endif()
unset(PNG_NAMES)
unset(PNG_NAMES_DEBUG)
unset(_PNG_INCLUDE_PATH_SUFFIXES)
# Set by select_library_configurations(), but we want the one from
# find_package_handle_standard_args() below.
unset(PNG_FOUND)
if (PNG_LIBRARY AND PNG_PNG_INCLUDE_DIR)
# png.h includes zlib.h. Sigh.
set(PNG_INCLUDE_DIRS ${PNG_PNG_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} )
set(PNG_INCLUDE_DIR ${PNG_INCLUDE_DIRS} ) # for backward compatibility
set(PNG_LIBRARIES ${PNG_LIBRARY} ${ZLIB_LIBRARY})
if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
("${PNG_LIBRARY}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))
list(APPEND PNG_LIBRARIES m)
endif()
if (CYGWIN)
if(BUILD_SHARED_LIBS)
# No need to define PNG_USE_DLL here, because it's default for Cygwin.
else()
set (PNG_DEFINITIONS -DPNG_STATIC)
set(_PNG_COMPILE_DEFINITIONS PNG_STATIC)
endif()
endif ()
if(NOT TARGET PNG::PNG)
add_library(PNG::PNG UNKNOWN IMPORTED)
set_target_properties(PNG::PNG PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "${_PNG_COMPILE_DEFINITIONS}"
INTERFACE_INCLUDE_DIRECTORIES "${PNG_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
("${PNG_LIBRARY}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))
set_property(TARGET PNG::PNG APPEND PROPERTY
INTERFACE_LINK_LIBRARIES m)
endif()
if(EXISTS "${PNG_LIBRARY}")
set_target_properties(PNG::PNG PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${PNG_LIBRARY}")
endif()
if(EXISTS "${PNG_LIBRARY_RELEASE}")
set_property(TARGET PNG::PNG APPEND PROPERTY
IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(PNG::PNG PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
IMPORTED_LOCATION_RELEASE "${PNG_LIBRARY_RELEASE}")
endif()
if(EXISTS "${PNG_LIBRARY_DEBUG}")
set_property(TARGET PNG::PNG APPEND PROPERTY
IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(PNG::PNG PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
IMPORTED_LOCATION_DEBUG "${PNG_LIBRARY_DEBUG}")
endif()
endif()
unset(_PNG_COMPILE_DEFINITIONS)
endif ()
if (PNG_PNG_INCLUDE_DIR AND EXISTS "${PNG_PNG_INCLUDE_DIR}/png.h")
file(STRINGS "${PNG_PNG_INCLUDE_DIR}/png.h" png_version_str REGEX "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\".+\"")
string(REGEX REPLACE "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\"([^\"]+)\".*" "\\1" PNG_VERSION_STRING "${png_version_str}")
unset(png_version_str)
endif ()
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PNG
REQUIRED_VARS PNG_LIBRARY PNG_PNG_INCLUDE_DIR
VERSION_VAR PNG_VERSION_STRING)
cmake_policy(POP)
|