FindZLIB.cmake 4.8 KB

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