FindPNG.cmake 5.3 KB

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