FindPNG.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #.rst:
  2. # FindPNG
  3. # -------
  4. #
  5. # Find the native PNG includes and library
  6. #
  7. #
  8. #
  9. # This module searches libpng, the library for working with PNG images.
  10. #
  11. # It defines the following variables
  12. #
  13. # ::
  14. #
  15. # PNG_INCLUDE_DIRS, where to find png.h, etc.
  16. # PNG_LIBRARIES, the libraries to link against to use PNG.
  17. # PNG_DEFINITIONS - You should add_definitons(${PNG_DEFINITIONS}) before compiling code that includes png library files.
  18. # PNG_FOUND, If false, do not try to use PNG.
  19. # PNG_VERSION_STRING - the version of the PNG library found (since CMake 2.8.8)
  20. #
  21. # Also defined, but not for general use are
  22. #
  23. # ::
  24. #
  25. # PNG_LIBRARY, where to find the PNG library.
  26. #
  27. # For backward compatiblity the variable PNG_INCLUDE_DIR is also set.
  28. # It has the same value as PNG_INCLUDE_DIRS.
  29. #
  30. # Since PNG depends on the ZLib compression library, none of the above
  31. # will be defined unless ZLib can be found.
  32. #=============================================================================
  33. # Copyright 2002-2009 Kitware, Inc.
  34. #
  35. # Distributed under the OSI-approved BSD License (the "License");
  36. # see accompanying file Copyright.txt for details.
  37. #
  38. # This software is distributed WITHOUT ANY WARRANTY; without even the
  39. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  40. # See the License for more information.
  41. #=============================================================================
  42. # (To distribute this file outside of CMake, substitute the full
  43. # License text for the above reference.)
  44. if(PNG_FIND_QUIETLY)
  45. set(_FIND_ZLIB_ARG QUIET)
  46. endif()
  47. find_package(ZLIB ${_FIND_ZLIB_ARG})
  48. if(ZLIB_FOUND)
  49. find_path(PNG_PNG_INCLUDE_DIR png.h
  50. HINTS ENV PNG_DIR
  51. PATH_SUFFIXES include/libpng include
  52. PATHS
  53. ~/Library/Frameworks
  54. /Library/Frameworks
  55. /usr/local
  56. /usr
  57. /sw # Fink
  58. /opt/local # DarwinPorts
  59. /opt/csw # Blastwave
  60. /opt
  61. )
  62. list(APPEND PNG_NAMES png libpng)
  63. unset(PNG_NAMES_DEBUG)
  64. set(_PNG_VERSION_SUFFIXES 17 16 15 14 12)
  65. if (PNG_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\..*)?$")
  66. string(REGEX REPLACE
  67. "^([0-9]+)\\.([0-9]+).*" "\\1\\2"
  68. _PNG_VERSION_SUFFIX_MIN "${PNG_FIND_VERSION}")
  69. if (PNG_FIND_VERSION_EXACT)
  70. set(_PNG_VERSION_SUFFIXES ${_PNG_VERSION_SUFFIX_MIN})
  71. else ()
  72. string(REGEX REPLACE
  73. "${_PNG_VERSION_SUFFIX_MIN}.*" "${_PNG_VERSION_SUFFIX_MIN}"
  74. _PNG_VERSION_SUFFIXES "${_PNG_VERSION_SUFFIXES}")
  75. endif ()
  76. unset(_PNG_VERSION_SUFFIX_MIN)
  77. endif ()
  78. foreach(v IN LISTS _PNG_VERSION_SUFFIXES)
  79. list(APPEND PNG_NAMES png${v} libpng${v})
  80. list(APPEND PNG_NAMES_DEBUG png${v}d libpng${v}d)
  81. endforeach()
  82. unset(_PNG_VERSION_SUFFIXES)
  83. # For compatiblity with versions prior to this multi-config search, honor
  84. # any PNG_LIBRARY that is already specified and skip the search.
  85. if(NOT PNG_LIBRARY)
  86. find_library(PNG_LIBRARY_RELEASE
  87. NAMES ${PNG_NAMES}
  88. HINTS ENV PNG_DIR
  89. PATH_SUFFIXES lib
  90. PATHS
  91. ~/Library/Frameworks
  92. /Library/Frameworks
  93. /usr/local
  94. /usr
  95. /sw
  96. /opt/local
  97. /opt/csw
  98. /opt
  99. )
  100. find_library(PNG_LIBRARY_DEBUG
  101. NAMES ${PNG_NAMES_DEBUG}
  102. HINTS ENV PNG_DIR
  103. PATH_SUFFIXES lib
  104. PATHS
  105. ~/Library/Frameworks
  106. /Library/Frameworks
  107. /usr/local
  108. /usr
  109. /sw
  110. /opt/local
  111. /opt/csw
  112. /opt
  113. )
  114. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  115. select_library_configurations(PNG)
  116. mark_as_advanced(PNG_LIBRARY_RELEASE PNG_LIBRARY_DEBUG)
  117. endif()
  118. unset(PNG_NAMES)
  119. unset(PNG_NAMES_DEBUG)
  120. # Set by select_library_configurations(), but we want the one from
  121. # find_package_handle_standard_args() below.
  122. unset(PNG_FOUND)
  123. if (PNG_LIBRARY AND PNG_PNG_INCLUDE_DIR)
  124. # png.h includes zlib.h. Sigh.
  125. set(PNG_INCLUDE_DIRS ${PNG_PNG_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} )
  126. set(PNG_INCLUDE_DIR ${PNG_INCLUDE_DIRS} ) # for backward compatiblity
  127. set(PNG_LIBRARIES ${PNG_LIBRARY} ${ZLIB_LIBRARY})
  128. if (CYGWIN)
  129. if(BUILD_SHARED_LIBS)
  130. # No need to define PNG_USE_DLL here, because it's default for Cygwin.
  131. else()
  132. set (PNG_DEFINITIONS -DPNG_STATIC)
  133. endif()
  134. endif ()
  135. endif ()
  136. if (PNG_PNG_INCLUDE_DIR AND EXISTS "${PNG_PNG_INCLUDE_DIR}/png.h")
  137. file(STRINGS "${PNG_PNG_INCLUDE_DIR}/png.h" png_version_str REGEX "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\".+\"")
  138. string(REGEX REPLACE "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\"([^\"]+)\".*" "\\1" PNG_VERSION_STRING "${png_version_str}")
  139. unset(png_version_str)
  140. endif ()
  141. endif()
  142. # handle the QUIETLY and REQUIRED arguments and set PNG_FOUND to TRUE if
  143. # all listed variables are TRUE
  144. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  145. find_package_handle_standard_args(PNG
  146. REQUIRED_VARS PNG_LIBRARY PNG_PNG_INCLUDE_DIR
  147. VERSION_VAR PNG_VERSION_STRING)
  148. mark_as_advanced(PNG_PNG_INCLUDE_DIR PNG_LIBRARY )