FindBZip2.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. FindBZip2
  5. ---------
  6. Try to find BZip2
  7. IMPORTED Targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 3.12
  10. This module defines :prop_tgt:`IMPORTED` target ``BZip2::BZip2``, if
  11. BZip2 has been found.
  12. Result Variables
  13. ^^^^^^^^^^^^^^^^
  14. This module defines the following variables:
  15. ``BZIP2_FOUND``
  16. system has BZip2
  17. ``BZIP2_INCLUDE_DIRS``
  18. .. versionadded:: 3.12
  19. the BZip2 include directories
  20. ``BZIP2_LIBRARIES``
  21. Link these to use BZip2
  22. ``BZIP2_NEED_PREFIX``
  23. this is set if the functions are prefixed with ``BZ2_``
  24. ``BZIP2_VERSION``
  25. .. versionadded:: 3.26
  26. the version of BZip2 found.
  27. See also legacy variable ``BZIP2_VERSION_STRING``.
  28. Cache variables
  29. ^^^^^^^^^^^^^^^
  30. The following cache variables may also be set:
  31. ``BZIP2_INCLUDE_DIR``
  32. the directory containing the BZip2 headers
  33. ``BZIP2_LIBRARY_RELEASE``
  34. the path to the BZip2 library for release configurations
  35. ``BZIP2_LIBRARY_DEBUG``
  36. the path to the BZip2 library for debug configurations
  37. Legacy Variables
  38. ^^^^^^^^^^^^^^^^
  39. The following variables are provided for backward compatibility:
  40. ``BZIP2_VERSION_STRING``
  41. the version of BZip2 found.
  42. .. versionchanged:: 3.26
  43. Superseded by ``BZIP2_VERSION``.
  44. #]=======================================================================]
  45. cmake_policy(PUSH)
  46. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  47. set(_BZIP2_PATHS PATHS
  48. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\Bzip2;InstallPath]"
  49. )
  50. find_path(BZIP2_INCLUDE_DIR bzlib.h ${_BZIP2_PATHS} PATH_SUFFIXES include)
  51. if (NOT BZIP2_LIBRARIES)
  52. find_library(BZIP2_LIBRARY_RELEASE NAMES bz2 bzip2 libbz2 libbzip2 NAMES_PER_DIR ${_BZIP2_PATHS} PATH_SUFFIXES lib)
  53. find_library(BZIP2_LIBRARY_DEBUG NAMES bz2d bzip2d libbz2d libbzip2d NAMES_PER_DIR ${_BZIP2_PATHS} PATH_SUFFIXES lib)
  54. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  55. select_library_configurations(BZIP2)
  56. else ()
  57. file(TO_CMAKE_PATH "${BZIP2_LIBRARIES}" BZIP2_LIBRARIES)
  58. endif ()
  59. if (BZIP2_INCLUDE_DIR AND EXISTS "${BZIP2_INCLUDE_DIR}/bzlib.h")
  60. file(STRINGS "${BZIP2_INCLUDE_DIR}/bzlib.h" BZLIB_H REGEX "bzip2/libbzip2 version [0-9]+\\.[^ ]+ of [0-9]+ ")
  61. string(REGEX REPLACE ".* bzip2/libbzip2 version ([0-9]+\\.[^ ]+) of [0-9]+ .*" "\\1" BZIP2_VERSION_STRING "${BZLIB_H}")
  62. set(BZIP2_VERSION ${BZIP2_VERSION_STRING})
  63. endif ()
  64. include(FindPackageHandleStandardArgs)
  65. find_package_handle_standard_args(BZip2
  66. REQUIRED_VARS BZIP2_LIBRARIES BZIP2_INCLUDE_DIR
  67. VERSION_VAR BZIP2_VERSION)
  68. if (BZIP2_FOUND)
  69. set(BZIP2_INCLUDE_DIRS ${BZIP2_INCLUDE_DIR})
  70. include(${CMAKE_CURRENT_LIST_DIR}/CheckSymbolExists.cmake)
  71. include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  72. cmake_push_check_state()
  73. set(CMAKE_REQUIRED_QUIET ${BZip2_FIND_QUIETLY})
  74. set(CMAKE_REQUIRED_INCLUDES ${BZIP2_INCLUDE_DIR})
  75. set(CMAKE_REQUIRED_LIBRARIES ${BZIP2_LIBRARIES})
  76. check_symbol_exists(BZ2_bzCompressInit "bzlib.h" BZIP2_NEED_PREFIX)
  77. cmake_pop_check_state()
  78. if(NOT TARGET BZip2::BZip2)
  79. add_library(BZip2::BZip2 UNKNOWN IMPORTED)
  80. set_target_properties(BZip2::BZip2 PROPERTIES
  81. INTERFACE_INCLUDE_DIRECTORIES "${BZIP2_INCLUDE_DIRS}")
  82. if(BZIP2_LIBRARY_RELEASE)
  83. set_property(TARGET BZip2::BZip2 APPEND PROPERTY
  84. IMPORTED_CONFIGURATIONS RELEASE)
  85. set_target_properties(BZip2::BZip2 PROPERTIES
  86. IMPORTED_LOCATION_RELEASE "${BZIP2_LIBRARY_RELEASE}")
  87. endif()
  88. if(BZIP2_LIBRARY_DEBUG)
  89. set_property(TARGET BZip2::BZip2 APPEND PROPERTY
  90. IMPORTED_CONFIGURATIONS DEBUG)
  91. set_target_properties(BZip2::BZip2 PROPERTIES
  92. IMPORTED_LOCATION_DEBUG "${BZIP2_LIBRARY_DEBUG}")
  93. endif()
  94. if(NOT BZIP2_LIBRARY_RELEASE AND NOT BZIP2_LIBRARY_DEBUG)
  95. set_property(TARGET BZip2::BZip2 APPEND PROPERTY
  96. IMPORTED_LOCATION "${BZIP2_LIBRARY}")
  97. endif()
  98. endif()
  99. endif ()
  100. mark_as_advanced(BZIP2_INCLUDE_DIR)
  101. cmake_policy(POP)