FindZstd.cmake 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. # Find the zstd library
  25. #
  26. # Input variables:
  27. #
  28. # - `ZSTD_INCLUDE_DIR`: The zstd include directory.
  29. # - `ZSTD_LIBRARY`: Path to `zstd` library.
  30. #
  31. # Result variables:
  32. #
  33. # - `ZSTD_FOUND`: System has zstd.
  34. # - `ZSTD_INCLUDE_DIRS`: The zstd include directories.
  35. # - `ZSTD_LIBRARIES`: The zstd library names.
  36. # - `ZSTD_LIBRARY_DIRS`: The zstd library directories.
  37. # - `ZSTD_CFLAGS`: Required compiler flags.
  38. # - `ZSTD_VERSION`: Version of zstd.
  39. if(DEFINED Zstd_INCLUDE_DIR AND NOT DEFINED ZSTD_INCLUDE_DIR)
  40. message(WARNING "Zstd_INCLUDE_DIR is deprecated, use ZSTD_INCLUDE_DIR instead.")
  41. set(ZSTD_INCLUDE_DIR "${Zstd_INCLUDE_DIR}")
  42. endif()
  43. if(DEFINED Zstd_LIBRARY AND NOT DEFINED ZSTD_LIBRARY)
  44. message(WARNING "Zstd_LIBRARY is deprecated, use ZSTD_LIBRARY instead.")
  45. set(ZSTD_LIBRARY "${Zstd_LIBRARY}")
  46. endif()
  47. if(CURL_USE_PKGCONFIG AND
  48. NOT DEFINED ZSTD_INCLUDE_DIR AND
  49. NOT DEFINED ZSTD_LIBRARY)
  50. find_package(PkgConfig QUIET)
  51. pkg_check_modules(ZSTD "libzstd")
  52. endif()
  53. if(ZSTD_FOUND)
  54. string(REPLACE ";" " " ZSTD_CFLAGS "${ZSTD_CFLAGS}")
  55. message(STATUS "Found Zstd (via pkg-config): ${ZSTD_INCLUDE_DIRS} (found version \"${ZSTD_VERSION}\")")
  56. else()
  57. find_path(ZSTD_INCLUDE_DIR NAMES "zstd.h")
  58. find_library(ZSTD_LIBRARY NAMES "zstd")
  59. unset(ZSTD_VERSION CACHE)
  60. if(ZSTD_INCLUDE_DIR AND EXISTS "${ZSTD_INCLUDE_DIR}/zstd.h")
  61. set(_version_regex1 "#[\t ]*define[ \t]+ZSTD_VERSION_MAJOR[ \t]+([0-9]+).*")
  62. set(_version_regex2 "#[\t ]*define[ \t]+ZSTD_VERSION_MINOR[ \t]+([0-9]+).*")
  63. set(_version_regex3 "#[\t ]*define[ \t]+ZSTD_VERSION_RELEASE[ \t]+([0-9]+).*")
  64. file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h" _version_str1 REGEX "${_version_regex1}")
  65. file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h" _version_str2 REGEX "${_version_regex2}")
  66. file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h" _version_str3 REGEX "${_version_regex3}")
  67. string(REGEX REPLACE "${_version_regex1}" "\\1" _version_str1 "${_version_str1}")
  68. string(REGEX REPLACE "${_version_regex2}" "\\1" _version_str2 "${_version_str2}")
  69. string(REGEX REPLACE "${_version_regex3}" "\\1" _version_str3 "${_version_str3}")
  70. set(ZSTD_VERSION "${_version_str1}.${_version_str2}.${_version_str3}")
  71. unset(_version_regex1)
  72. unset(_version_regex2)
  73. unset(_version_regex3)
  74. unset(_version_str1)
  75. unset(_version_str2)
  76. unset(_version_str3)
  77. endif()
  78. include(FindPackageHandleStandardArgs)
  79. find_package_handle_standard_args(Zstd
  80. REQUIRED_VARS
  81. ZSTD_INCLUDE_DIR
  82. ZSTD_LIBRARY
  83. VERSION_VAR
  84. ZSTD_VERSION
  85. )
  86. if(ZSTD_FOUND)
  87. set(ZSTD_INCLUDE_DIRS ${ZSTD_INCLUDE_DIR})
  88. set(ZSTD_LIBRARIES ${ZSTD_LIBRARY})
  89. endif()
  90. mark_as_advanced(ZSTD_INCLUDE_DIR ZSTD_LIBRARY)
  91. endif()