FindPNG.cmake 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. FindPNG
  5. -------
  6. Find libpng, the official reference library for the PNG image format.
  7. Imported targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 3.5
  10. This module defines the following :prop_tgt:`IMPORTED` target:
  11. ``PNG::PNG``
  12. The libpng library, if found.
  13. Result variables
  14. ^^^^^^^^^^^^^^^^
  15. This module will set the following variables in your project:
  16. ``PNG_INCLUDE_DIRS``
  17. where to find png.h, etc.
  18. ``PNG_LIBRARIES``
  19. the libraries to link against to use PNG.
  20. ``PNG_DEFINITIONS``
  21. You should add_definitions(${PNG_DEFINITIONS}) before compiling code
  22. that includes png library files.
  23. ``PNG_FOUND``
  24. If false, do not try to use PNG.
  25. ``PNG_VERSION_STRING``
  26. the version of the PNG library found (since CMake 2.8.8)
  27. Obsolete variables
  28. ^^^^^^^^^^^^^^^^^^
  29. The following variables may also be set, for backwards compatibility:
  30. ``PNG_LIBRARY``
  31. where to find the PNG library.
  32. ``PNG_INCLUDE_DIR``
  33. where to find the PNG headers (same as PNG_INCLUDE_DIRS)
  34. Since PNG depends on the ZLib compression library, none of the above
  35. will be defined unless ZLib can be found.
  36. #]=======================================================================]
  37. cmake_policy(PUSH)
  38. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  39. # Default install location on windows when installing from included cmake build
  40. # From FindZLIB.cmake
  41. set(_PNG_x86 "(x86)")
  42. set(_PNG_INCLUDE_SEARCH_NORMAL
  43. "$ENV{ProgramFiles}/libpng"
  44. "$ENV{ProgramFiles${_PNG_x86}}/libpng")
  45. set(_PNG_LIB_SEARCH_NORMAL
  46. "$ENV{ProgramFiles}/libpng/lib"
  47. "$ENV{ProgramFiles${_PNG_x86}}/libpng/lib")
  48. unset(_PNG_x86)
  49. if(PNG_FIND_QUIETLY)
  50. set(_FIND_ZLIB_ARG QUIET)
  51. endif()
  52. find_package(ZLIB ${_FIND_ZLIB_ARG})
  53. if(ZLIB_FOUND)
  54. set(_PNG_VERSION_SUFFIXES 17 16 15 14 12)
  55. list(APPEND _PNG_INCLUDE_PATH_SUFFIXES include/libpng)
  56. foreach(v IN LISTS _PNG_VERSION_SUFFIXES)
  57. list(APPEND _PNG_INCLUDE_PATH_SUFFIXES include/libpng${v})
  58. endforeach()
  59. find_path(PNG_PNG_INCLUDE_DIR png.h PATH_SUFFIXES ${_PNG_INCLUDE_PATH_SUFFIXES} PATHS ${_PNG_INCLUDE_SEARCH_NORMAL} )
  60. mark_as_advanced(PNG_PNG_INCLUDE_DIR)
  61. list(APPEND PNG_NAMES png libpng)
  62. unset(PNG_NAMES_DEBUG)
  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 PATHS ${_PNG_LIB_SEARCH_NORMAL})
  83. find_library(PNG_LIBRARY_DEBUG NAMES ${PNG_NAMES_DEBUG} NAMES_PER_DIR PATHS ${_PNG_LIB_SEARCH_NORMAL})
  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. unset(_PNG_INCLUDE_PATH_SUFFIXES)
  91. # Set by select_library_configurations(), but we want the one from
  92. # find_package_handle_standard_args() below.
  93. unset(PNG_FOUND)
  94. if (PNG_LIBRARY AND PNG_PNG_INCLUDE_DIR)
  95. # png.h includes zlib.h. Sigh.
  96. set(PNG_INCLUDE_DIRS ${PNG_PNG_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} )
  97. set(PNG_INCLUDE_DIR ${PNG_INCLUDE_DIRS} ) # for backward compatibility
  98. set(PNG_LIBRARIES ${PNG_LIBRARY} ${ZLIB_LIBRARY})
  99. if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
  100. ("${PNG_LIBRARY}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))
  101. list(APPEND PNG_LIBRARIES m)
  102. endif()
  103. if (CYGWIN)
  104. if(BUILD_SHARED_LIBS)
  105. # No need to define PNG_USE_DLL here, because it's default for Cygwin.
  106. else()
  107. set (PNG_DEFINITIONS -DPNG_STATIC)
  108. set(_PNG_COMPILE_DEFINITIONS PNG_STATIC)
  109. endif()
  110. endif ()
  111. if(NOT TARGET PNG::PNG)
  112. add_library(PNG::PNG UNKNOWN IMPORTED)
  113. set_target_properties(PNG::PNG PROPERTIES
  114. INTERFACE_COMPILE_DEFINITIONS "${_PNG_COMPILE_DEFINITIONS}"
  115. INTERFACE_INCLUDE_DIRECTORIES "${PNG_INCLUDE_DIRS}"
  116. INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
  117. if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
  118. ("${PNG_LIBRARY}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))
  119. set_property(TARGET PNG::PNG APPEND PROPERTY
  120. INTERFACE_LINK_LIBRARIES m)
  121. endif()
  122. if(EXISTS "${PNG_LIBRARY}")
  123. set_target_properties(PNG::PNG PROPERTIES
  124. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  125. IMPORTED_LOCATION "${PNG_LIBRARY}")
  126. endif()
  127. if(EXISTS "${PNG_LIBRARY_RELEASE}")
  128. set_property(TARGET PNG::PNG APPEND PROPERTY
  129. IMPORTED_CONFIGURATIONS RELEASE)
  130. set_target_properties(PNG::PNG PROPERTIES
  131. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  132. IMPORTED_LOCATION_RELEASE "${PNG_LIBRARY_RELEASE}")
  133. endif()
  134. if(EXISTS "${PNG_LIBRARY_DEBUG}")
  135. set_property(TARGET PNG::PNG APPEND PROPERTY
  136. IMPORTED_CONFIGURATIONS DEBUG)
  137. set_target_properties(PNG::PNG PROPERTIES
  138. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  139. IMPORTED_LOCATION_DEBUG "${PNG_LIBRARY_DEBUG}")
  140. endif()
  141. endif()
  142. unset(_PNG_COMPILE_DEFINITIONS)
  143. endif ()
  144. if (PNG_PNG_INCLUDE_DIR AND EXISTS "${PNG_PNG_INCLUDE_DIR}/png.h")
  145. file(STRINGS "${PNG_PNG_INCLUDE_DIR}/png.h" png_version_str REGEX "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\".+\"")
  146. string(REGEX REPLACE "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\"([^\"]+)\".*" "\\1" PNG_VERSION_STRING "${png_version_str}")
  147. unset(png_version_str)
  148. endif ()
  149. endif()
  150. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  151. find_package_handle_standard_args(PNG
  152. REQUIRED_VARS PNG_LIBRARY PNG_PNG_INCLUDE_DIR
  153. VERSION_VAR PNG_VERSION_STRING)
  154. cmake_policy(POP)