FindPNG.cmake 5.1 KB

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