FindZLIB.cmake 4.6 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. # FindZLIB
  5. # --------
  6. #
  7. # Find the native ZLIB includes and library.
  8. #
  9. # IMPORTED Targets
  10. # ^^^^^^^^^^^^^^^^
  11. #
  12. # This module defines :prop_tgt:`IMPORTED` target ``ZLIB::ZLIB``, if
  13. # ZLIB has been found.
  14. #
  15. # Result Variables
  16. # ^^^^^^^^^^^^^^^^
  17. #
  18. # This module defines the following variables:
  19. #
  20. # ::
  21. #
  22. # ZLIB_INCLUDE_DIRS - where to find zlib.h, etc.
  23. # ZLIB_LIBRARIES - List of libraries when using zlib.
  24. # ZLIB_FOUND - True if zlib found.
  25. #
  26. # ::
  27. #
  28. # ZLIB_VERSION_STRING - The version of zlib found (x.y.z)
  29. # ZLIB_VERSION_MAJOR - The major version of zlib
  30. # ZLIB_VERSION_MINOR - The minor version of zlib
  31. # ZLIB_VERSION_PATCH - The patch version of zlib
  32. # ZLIB_VERSION_TWEAK - The tweak version of zlib
  33. #
  34. # Backward Compatibility
  35. # ^^^^^^^^^^^^^^^^^^^^^^
  36. #
  37. # The following variable are provided for backward compatibility
  38. #
  39. # ::
  40. #
  41. # ZLIB_MAJOR_VERSION - The major version of zlib
  42. # ZLIB_MINOR_VERSION - The minor version of zlib
  43. # ZLIB_PATCH_VERSION - The patch version of zlib
  44. #
  45. # Hints
  46. # ^^^^^
  47. #
  48. # A user may set ``ZLIB_ROOT`` to a zlib installation root to tell this
  49. # module where to look.
  50. set(_ZLIB_SEARCHES)
  51. # Search ZLIB_ROOT first if it is set.
  52. if(ZLIB_ROOT)
  53. set(_ZLIB_SEARCH_ROOT PATHS ${ZLIB_ROOT} NO_DEFAULT_PATH)
  54. list(APPEND _ZLIB_SEARCHES _ZLIB_SEARCH_ROOT)
  55. endif()
  56. # Normal search.
  57. set(_ZLIB_SEARCH_NORMAL
  58. PATHS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\Zlib;InstallPath]"
  59. "$ENV{PROGRAMFILES}/zlib"
  60. )
  61. list(APPEND _ZLIB_SEARCHES _ZLIB_SEARCH_NORMAL)
  62. set(ZLIB_NAMES z zlib zdll zlib1)
  63. set(ZLIB_NAMES_DEBUG zlibd zlibd1)
  64. # Try each search configuration.
  65. foreach(search ${_ZLIB_SEARCHES})
  66. find_path(ZLIB_INCLUDE_DIR NAMES zlib.h ${${search}} PATH_SUFFIXES include)
  67. endforeach()
  68. # Allow ZLIB_LIBRARY to be set manually, as the location of the zlib library
  69. if(NOT ZLIB_LIBRARY)
  70. foreach(search ${_ZLIB_SEARCHES})
  71. find_library(ZLIB_LIBRARY_RELEASE NAMES ${ZLIB_NAMES} ${${search}} PATH_SUFFIXES lib)
  72. find_library(ZLIB_LIBRARY_DEBUG NAMES ${ZLIB_NAMES_DEBUG} ${${search}} PATH_SUFFIXES lib)
  73. endforeach()
  74. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  75. select_library_configurations(ZLIB)
  76. endif()
  77. unset(ZLIB_NAMES)
  78. unset(ZLIB_NAMES_DEBUG)
  79. mark_as_advanced(ZLIB_INCLUDE_DIR)
  80. if(ZLIB_INCLUDE_DIR AND EXISTS "${ZLIB_INCLUDE_DIR}/zlib.h")
  81. file(STRINGS "${ZLIB_INCLUDE_DIR}/zlib.h" ZLIB_H REGEX "^#define ZLIB_VERSION \"[^\"]*\"$")
  82. string(REGEX REPLACE "^.*ZLIB_VERSION \"([0-9]+).*$" "\\1" ZLIB_VERSION_MAJOR "${ZLIB_H}")
  83. string(REGEX REPLACE "^.*ZLIB_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" ZLIB_VERSION_MINOR "${ZLIB_H}")
  84. string(REGEX REPLACE "^.*ZLIB_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" ZLIB_VERSION_PATCH "${ZLIB_H}")
  85. set(ZLIB_VERSION_STRING "${ZLIB_VERSION_MAJOR}.${ZLIB_VERSION_MINOR}.${ZLIB_VERSION_PATCH}")
  86. # only append a TWEAK version if it exists:
  87. set(ZLIB_VERSION_TWEAK "")
  88. if( "${ZLIB_H}" MATCHES "ZLIB_VERSION \"[0-9]+\\.[0-9]+\\.[0-9]+\\.([0-9]+)")
  89. set(ZLIB_VERSION_TWEAK "${CMAKE_MATCH_1}")
  90. string(APPEND ZLIB_VERSION_STRING ".${ZLIB_VERSION_TWEAK}")
  91. endif()
  92. set(ZLIB_MAJOR_VERSION "${ZLIB_VERSION_MAJOR}")
  93. set(ZLIB_MINOR_VERSION "${ZLIB_VERSION_MINOR}")
  94. set(ZLIB_PATCH_VERSION "${ZLIB_VERSION_PATCH}")
  95. endif()
  96. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  97. FIND_PACKAGE_HANDLE_STANDARD_ARGS(ZLIB REQUIRED_VARS ZLIB_LIBRARY ZLIB_INCLUDE_DIR
  98. VERSION_VAR ZLIB_VERSION_STRING)
  99. if(ZLIB_FOUND)
  100. set(ZLIB_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR})
  101. if(NOT ZLIB_LIBRARIES)
  102. set(ZLIB_LIBRARIES ${ZLIB_LIBRARY})
  103. endif()
  104. if(NOT TARGET ZLIB::ZLIB)
  105. add_library(ZLIB::ZLIB UNKNOWN IMPORTED)
  106. set_target_properties(ZLIB::ZLIB PROPERTIES
  107. INTERFACE_INCLUDE_DIRECTORIES "${ZLIB_INCLUDE_DIRS}")
  108. if(ZLIB_LIBRARY_RELEASE)
  109. set_property(TARGET ZLIB::ZLIB APPEND PROPERTY
  110. IMPORTED_CONFIGURATIONS RELEASE)
  111. set_target_properties(ZLIB::ZLIB PROPERTIES
  112. IMPORTED_LOCATION_RELEASE "${ZLIB_LIBRARY_RELEASE}")
  113. endif()
  114. if(ZLIB_LIBRARY_DEBUG)
  115. set_property(TARGET ZLIB::ZLIB APPEND PROPERTY
  116. IMPORTED_CONFIGURATIONS DEBUG)
  117. set_target_properties(ZLIB::ZLIB PROPERTIES
  118. IMPORTED_LOCATION_DEBUG "${ZLIB_LIBRARY_DEBUG}")
  119. endif()
  120. if(NOT ZLIB_LIBRARY_RELEASE AND NOT ZLIB_LIBRARY_DEBUG)
  121. set_property(TARGET ZLIB::ZLIB APPEND PROPERTY
  122. IMPORTED_LOCATION "${ZLIB_LIBRARY}")
  123. endif()
  124. endif()
  125. endif()