FindBZip2.cmake 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindBZip2
  5. ---------
  6. Finds the BZip2 data compression library (libbz2):
  7. .. code-block:: cmake
  8. find_package(BZip2 [<version>] [...])
  9. Imported Targets
  10. ^^^^^^^^^^^^^^^^
  11. This module provides the following :ref:`Imported Targets`:
  12. ``BZip2::BZip2``
  13. .. versionadded:: 3.12
  14. Target encapsulating the usage requirements of BZip2 library. This target is
  15. available only when BZip2 is found.
  16. Result Variables
  17. ^^^^^^^^^^^^^^^^
  18. This module defines the following variables:
  19. ``BZip2_FOUND``
  20. Boolean indicating whether (the requested version of) BZip2 library is
  21. found. For backward compatibility, the ``BZIP2_FOUND`` variable is also
  22. set to the same value.
  23. ``BZip2_VERSION``
  24. .. versionadded:: 4.2
  25. The version of BZip2 found.
  26. ``BZIP2_INCLUDE_DIRS``
  27. .. versionadded:: 3.12
  28. Include directories needed to use BZip2 library.
  29. ``BZIP2_LIBRARIES``
  30. Libraries needed for linking to use BZip2.
  31. Cache Variables
  32. ^^^^^^^^^^^^^^^
  33. The following cache variables may also be set:
  34. ``BZIP2_INCLUDE_DIR``
  35. The directory containing the BZip2 headers.
  36. ``BZIP2_LIBRARY_RELEASE``
  37. The path to the BZip2 library for release configurations.
  38. ``BZIP2_LIBRARY_DEBUG``
  39. The path to the BZip2 library for debug configurations.
  40. ``BZIP2_NEED_PREFIX``
  41. Boolean indicating whether BZip2 functions are prefixed with ``BZ2_``
  42. (e.g., ``BZ2_bzCompressInit()``). Versions of BZip2 prior to 1.0.0 used
  43. unprefixed function names (e.g., ``bzCompressInit()``).
  44. Deprecated Variables
  45. ^^^^^^^^^^^^^^^^^^^^
  46. The following variables are provided for backward compatibility:
  47. ``BZIP2_VERSION_STRING``
  48. .. deprecated:: 3.26
  49. Superseded by the ``BZIP2_VERSION`` (and ``BZip2_VERSION``).
  50. The version of BZip2 found.
  51. ``BZIP2_VERSION``
  52. .. versionadded:: 3.26
  53. .. deprecated:: 4.2
  54. Superseded by the ``BZip2_VERSION``.
  55. The version of BZip2 found.
  56. Examples
  57. ^^^^^^^^
  58. Finding BZip2 library and linking it to a project target:
  59. .. code-block:: cmake
  60. find_package(BZip2)
  61. target_link_libraries(project_target PRIVATE BZip2::BZip2)
  62. #]=======================================================================]
  63. cmake_policy(PUSH)
  64. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  65. set(_BZIP2_PATHS PATHS
  66. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\Bzip2;InstallPath]"
  67. )
  68. find_path(BZIP2_INCLUDE_DIR bzlib.h ${_BZIP2_PATHS} PATH_SUFFIXES include)
  69. if (NOT BZIP2_LIBRARIES)
  70. find_library(BZIP2_LIBRARY_RELEASE NAMES bz2 bzip2 libbz2 libbzip2 NAMES_PER_DIR ${_BZIP2_PATHS} PATH_SUFFIXES lib)
  71. find_library(BZIP2_LIBRARY_DEBUG NAMES bz2d bzip2d libbz2d libbzip2d NAMES_PER_DIR ${_BZIP2_PATHS} PATH_SUFFIXES lib)
  72. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  73. select_library_configurations(BZIP2)
  74. else ()
  75. file(TO_CMAKE_PATH "${BZIP2_LIBRARIES}" BZIP2_LIBRARIES)
  76. endif ()
  77. if (BZIP2_INCLUDE_DIR AND EXISTS "${BZIP2_INCLUDE_DIR}/bzlib.h")
  78. file(STRINGS "${BZIP2_INCLUDE_DIR}/bzlib.h" BZLIB_H REGEX "bzip2/libbzip2 version [0-9]+\\.[^ ]+ of [0-9]+ ")
  79. string(REGEX REPLACE ".* bzip2/libbzip2 version ([0-9]+\\.[^ ]+) of [0-9]+ .*" "\\1" BZIP2_VERSION_STRING "${BZLIB_H}")
  80. set(BZIP2_VERSION ${BZIP2_VERSION_STRING})
  81. set(BZip2_VERSION ${BZIP2_VERSION_STRING})
  82. endif ()
  83. include(FindPackageHandleStandardArgs)
  84. find_package_handle_standard_args(BZip2
  85. REQUIRED_VARS BZIP2_LIBRARIES BZIP2_INCLUDE_DIR
  86. VERSION_VAR BZip2_VERSION)
  87. if (BZip2_FOUND)
  88. set(BZIP2_INCLUDE_DIRS ${BZIP2_INCLUDE_DIR})
  89. include(${CMAKE_CURRENT_LIST_DIR}/CheckSymbolExists.cmake)
  90. include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  91. cmake_push_check_state()
  92. set(CMAKE_REQUIRED_QUIET ${BZip2_FIND_QUIETLY})
  93. set(CMAKE_REQUIRED_INCLUDES ${BZIP2_INCLUDE_DIR})
  94. set(CMAKE_REQUIRED_LIBRARIES ${BZIP2_LIBRARIES})
  95. # Versions before 1.0.2 required <stdio.h> for the FILE definition.
  96. set(BZip2_headers "bzlib.h")
  97. if(BZip2_VERSION VERSION_LESS "1.0.2")
  98. list(PREPEND BZip2_headers "stdio.h")
  99. endif()
  100. check_symbol_exists(BZ2_bzCompressInit "${BZip2_headers}" BZIP2_NEED_PREFIX)
  101. unset(BZip2_headers)
  102. cmake_pop_check_state()
  103. if(NOT TARGET BZip2::BZip2)
  104. add_library(BZip2::BZip2 UNKNOWN IMPORTED)
  105. set_target_properties(BZip2::BZip2 PROPERTIES
  106. INTERFACE_INCLUDE_DIRECTORIES "${BZIP2_INCLUDE_DIRS}")
  107. if(BZIP2_LIBRARY_RELEASE)
  108. set_property(TARGET BZip2::BZip2 APPEND PROPERTY
  109. IMPORTED_CONFIGURATIONS RELEASE)
  110. set_target_properties(BZip2::BZip2 PROPERTIES
  111. IMPORTED_LOCATION_RELEASE "${BZIP2_LIBRARY_RELEASE}")
  112. endif()
  113. if(BZIP2_LIBRARY_DEBUG)
  114. set_property(TARGET BZip2::BZip2 APPEND PROPERTY
  115. IMPORTED_CONFIGURATIONS DEBUG)
  116. set_target_properties(BZip2::BZip2 PROPERTIES
  117. IMPORTED_LOCATION_DEBUG "${BZIP2_LIBRARY_DEBUG}")
  118. endif()
  119. if(NOT BZIP2_LIBRARY_RELEASE AND NOT BZIP2_LIBRARY_DEBUG)
  120. set_property(TARGET BZip2::BZip2 APPEND PROPERTY
  121. IMPORTED_LOCATION "${BZIP2_LIBRARY}")
  122. endif()
  123. endif()
  124. endif ()
  125. mark_as_advanced(BZIP2_INCLUDE_DIR)
  126. cmake_policy(POP)