FindZLIB.cmake 6.0 KB

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