FindZLIB.cmake 5.2 KB

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