FindPNG.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #.rst:
  2. # FindPNG
  3. # -------
  4. #
  5. # Find libpng, the official reference library for the PNG image format.
  6. #
  7. # Imported targets
  8. # ^^^^^^^^^^^^^^^^
  9. #
  10. # This module defines the following :prop_tgt:`IMPORTED` target:
  11. #
  12. # ``PNG::PNG``
  13. # The libpng library, if found.
  14. #
  15. # Result variables
  16. # ^^^^^^^^^^^^^^^^
  17. #
  18. # This module will set the following variables in your project:
  19. #
  20. # ``PNG_INCLUDE_DIRS``
  21. # where to find png.h, etc.
  22. # ``PNG_LIBRARIES``
  23. # the libraries to link against to use PNG.
  24. # ``PNG_DEFINITIONS``
  25. # You should add_definitons(${PNG_DEFINITIONS}) before compiling code
  26. # that includes png library files.
  27. # ``PNG_FOUND``
  28. # If false, do not try to use PNG.
  29. # ``PNG_VERSION_STRING``
  30. # the version of the PNG library found (since CMake 2.8.8)
  31. #
  32. # Obsolete variables
  33. # ^^^^^^^^^^^^^^^^^^
  34. #
  35. # The following variables may also be set, for backwards compatibility:
  36. #
  37. # ``PNG_LIBRARY``
  38. # where to find the PNG library.
  39. # ``PNG_INCLUDE_DIR``
  40. # where to find the PNG headers (same as PNG_INCLUDE_DIRS)
  41. #
  42. # Since PNG depends on the ZLib compression library, none of the above
  43. # will be defined unless ZLib can be found.
  44. #=============================================================================
  45. # Copyright 2002-2009 Kitware, Inc.
  46. # Copyright 2016 Raumfeld
  47. #
  48. # Distributed under the OSI-approved BSD License (the "License");
  49. # see accompanying file Copyright.txt for details.
  50. #
  51. # This software is distributed WITHOUT ANY WARRANTY; without even the
  52. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  53. # See the License for more information.
  54. #=============================================================================
  55. # (To distribute this file outside of CMake, substitute the full
  56. # License text for the above reference.)
  57. if(PNG_FIND_QUIETLY)
  58. set(_FIND_ZLIB_ARG QUIET)
  59. endif()
  60. find_package(ZLIB ${_FIND_ZLIB_ARG})
  61. if(ZLIB_FOUND)
  62. find_path(PNG_PNG_INCLUDE_DIR png.h
  63. /usr/local/include/libpng # OpenBSD
  64. )
  65. list(APPEND PNG_NAMES png libpng)
  66. unset(PNG_NAMES_DEBUG)
  67. set(_PNG_VERSION_SUFFIXES 17 16 15 14 12)
  68. if (PNG_FIND_VERSION MATCHES "^([0-9]+)\\.([0-9]+)(\\..*)?$")
  69. set(_PNG_VERSION_SUFFIX_MIN "${CMAKE_MATCH_1}${CMAKE_MATCH_2}")
  70. if (PNG_FIND_VERSION_EXACT)
  71. set(_PNG_VERSION_SUFFIXES ${_PNG_VERSION_SUFFIX_MIN})
  72. else ()
  73. string(REGEX REPLACE
  74. "${_PNG_VERSION_SUFFIX_MIN}.*" "${_PNG_VERSION_SUFFIX_MIN}"
  75. _PNG_VERSION_SUFFIXES "${_PNG_VERSION_SUFFIXES}")
  76. endif ()
  77. unset(_PNG_VERSION_SUFFIX_MIN)
  78. endif ()
  79. foreach(v IN LISTS _PNG_VERSION_SUFFIXES)
  80. list(APPEND PNG_NAMES png${v} libpng${v})
  81. list(APPEND PNG_NAMES_DEBUG png${v}d libpng${v}d)
  82. endforeach()
  83. unset(_PNG_VERSION_SUFFIXES)
  84. # For compatibility with versions prior to this multi-config search, honor
  85. # any PNG_LIBRARY that is already specified and skip the search.
  86. if(NOT PNG_LIBRARY)
  87. find_library(PNG_LIBRARY_RELEASE NAMES ${PNG_NAMES})
  88. find_library(PNG_LIBRARY_DEBUG NAMES ${PNG_NAMES_DEBUG})
  89. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  90. select_library_configurations(PNG)
  91. mark_as_advanced(PNG_LIBRARY_RELEASE PNG_LIBRARY_DEBUG)
  92. endif()
  93. unset(PNG_NAMES)
  94. unset(PNG_NAMES_DEBUG)
  95. # Set by select_library_configurations(), but we want the one from
  96. # find_package_handle_standard_args() below.
  97. unset(PNG_FOUND)
  98. if (PNG_LIBRARY AND PNG_PNG_INCLUDE_DIR)
  99. # png.h includes zlib.h. Sigh.
  100. set(PNG_INCLUDE_DIRS ${PNG_PNG_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} )
  101. set(PNG_INCLUDE_DIR ${PNG_INCLUDE_DIRS} ) # for backward compatibility
  102. set(PNG_LIBRARIES ${PNG_LIBRARY} ${ZLIB_LIBRARY})
  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. endif()
  109. endif ()
  110. if(NOT TARGET PNG::PNG)
  111. add_library(PNG::PNG UNKNOWN IMPORTED)
  112. set_target_properties(PNG::PNG PROPERTIES
  113. INTERFACE_COMPILE_DEFINITIONS "${PNG_DEFINITIONS}"
  114. INTERFACE_INCLUDE_DIRECTORIES "${PNG_INCLUDE_DIRS}"
  115. INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
  116. if(EXISTS "${PNG_LIBRARY}")
  117. set_target_properties(PNG::PNG PROPERTIES
  118. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  119. IMPORTED_LOCATION "${PNG_LIBRARY}")
  120. endif()
  121. if(EXISTS "${PNG_LIBRARY_DEBUG}")
  122. set_property(TARGET PNG::PNG APPEND PROPERTY
  123. IMPORTED_CONFIGURATIONS DEBUG)
  124. set_target_properties(PNG::PNG PROPERTIES
  125. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  126. IMPORTED_LOCATION_DEBUG "${PNG_LIBRARY_DEBUG}")
  127. endif()
  128. if(EXISTS "${PNG_LIBRARY_RELEASE}")
  129. set_property(TARGET PNG::PNG APPEND PROPERTY
  130. IMPORTED_CONFIGURATIONS RELEASE)
  131. set_target_properties(PNG::PNG PROPERTIES
  132. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  133. IMPORTED_LOCATION_RELEASE "${PNG_LIBRARY_RELEASE}")
  134. endif()
  135. endif()
  136. endif ()
  137. if (PNG_PNG_INCLUDE_DIR AND EXISTS "${PNG_PNG_INCLUDE_DIR}/png.h")
  138. file(STRINGS "${PNG_PNG_INCLUDE_DIR}/png.h" png_version_str REGEX "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\".+\"")
  139. string(REGEX REPLACE "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\"([^\"]+)\".*" "\\1" PNG_VERSION_STRING "${png_version_str}")
  140. unset(png_version_str)
  141. endif ()
  142. endif()
  143. # handle the QUIETLY and REQUIRED arguments and set PNG_FOUND to TRUE if
  144. # all listed variables are TRUE
  145. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  146. find_package_handle_standard_args(PNG
  147. REQUIRED_VARS PNG_LIBRARY PNG_PNG_INCLUDE_DIR
  148. VERSION_VAR PNG_VERSION_STRING)
  149. mark_as_advanced(PNG_PNG_INCLUDE_DIR PNG_LIBRARY )