FindPNG.cmake 4.3 KB

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