FindNGTCP2.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <[email protected]>, 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 ngtcp2 library
  25. #
  26. # This module accepts optional COMPONENTS to control the crypto library (these are
  27. # mutually exclusive):
  28. #
  29. # - BoringSSL: Use `libngtcp2_crypto_boringssl`. (also for AWS-LC)
  30. # - GnuTLS: Use `libngtcp2_crypto_gnutls`.
  31. # - LibreSSL: Use `libngtcp2_crypto_libressl`. (requires ngtcp2 1.15.0+)
  32. # - ossl: Use `libngtcp2_crypto_ossl`.
  33. # - quictls: Use `libngtcp2_crypto_quictls`. (also for LibreSSL with ngtcp2 <1.15.0)
  34. # - wolfSSL: Use `libngtcp2_crypto_wolfssl`.
  35. #
  36. # Input variables:
  37. #
  38. # - `NGTCP2_INCLUDE_DIR`: The ngtcp2 include directory.
  39. # - `NGTCP2_LIBRARY`: Path to `ngtcp2` library.
  40. # - `NGTCP2_CRYPTO_BORINGSSL_LIBRARY`: Path to `ngtcp2_crypto_boringssl` library.
  41. # - `NGTCP2_CRYPTO_GNUTLS_LIBRARY`: Path to `ngtcp2_crypto_gnutls` library.
  42. # - `NGTCP2_CRYPTO_LIBRESSL_LIBRARY`: Path to `ngtcp2_crypto_libressl` library.
  43. # - `NGTCP2_CRYPTO_OSSL_LIBRARY`: Path to `ngtcp2_crypto_ossl` library.
  44. # - `NGTCP2_CRYPTO_QUICTLS_LIBRARY`: Path to `ngtcp2_crypto_quictls` library.
  45. # - `NGTCP2_CRYPTO_WOLFSSL_LIBRARY`: Path to `ngtcp2_crypto_wolfssl` library.
  46. #
  47. # Result variables:
  48. #
  49. # - `NGTCP2_FOUND`: System has ngtcp2.
  50. # - `NGTCP2_INCLUDE_DIRS`: The ngtcp2 include directories.
  51. # - `NGTCP2_LIBRARIES`: The ngtcp2 library names.
  52. # - `NGTCP2_LIBRARY_DIRS`: The ngtcp2 library directories.
  53. # - `NGTCP2_PC_REQUIRES`: The ngtcp2 pkg-config packages.
  54. # - `NGTCP2_CFLAGS`: Required compiler flags.
  55. # - `NGTCP2_VERSION`: Version of ngtcp2.
  56. if(NGTCP2_FIND_COMPONENTS)
  57. set(_ngtcp2_crypto_backend "")
  58. foreach(_component IN LISTS NGTCP2_FIND_COMPONENTS)
  59. if(_component MATCHES "^(BoringSSL|GnuTLS|LibreSSL|ossl|quictls|wolfSSL)")
  60. if(_ngtcp2_crypto_backend)
  61. message(FATAL_ERROR "NGTCP2: Only one crypto library can be selected")
  62. endif()
  63. set(_ngtcp2_crypto_backend ${_component})
  64. endif()
  65. endforeach()
  66. if(_ngtcp2_crypto_backend)
  67. string(TOLOWER "ngtcp2_crypto_${_ngtcp2_crypto_backend}" _crypto_library_lower)
  68. string(TOUPPER "ngtcp2_crypto_${_ngtcp2_crypto_backend}" _crypto_library_upper)
  69. endif()
  70. endif()
  71. set(NGTCP2_PC_REQUIRES "libngtcp2")
  72. if(_ngtcp2_crypto_backend)
  73. list(APPEND NGTCP2_PC_REQUIRES "lib${_crypto_library_lower}")
  74. endif()
  75. set(_tried_pkgconfig FALSE)
  76. if(CURL_USE_PKGCONFIG AND
  77. NOT DEFINED NGTCP2_INCLUDE_DIR AND
  78. NOT DEFINED NGTCP2_LIBRARY)
  79. find_package(PkgConfig QUIET)
  80. pkg_check_modules(NGTCP2 ${NGTCP2_PC_REQUIRES})
  81. set(_tried_pkgconfig TRUE)
  82. endif()
  83. if(NGTCP2_FOUND)
  84. set(NGTCP2_VERSION "${NGTCP2_libngtcp2_VERSION}")
  85. string(REPLACE ";" " " NGTCP2_CFLAGS "${NGTCP2_CFLAGS}")
  86. message(STATUS "Found NGTCP2 (via pkg-config): ${NGTCP2_INCLUDE_DIRS} (found version \"${NGTCP2_VERSION}\")")
  87. else()
  88. find_path(NGTCP2_INCLUDE_DIR NAMES "ngtcp2/ngtcp2.h")
  89. find_library(NGTCP2_LIBRARY NAMES "ngtcp2")
  90. unset(NGTCP2_VERSION CACHE)
  91. if(NGTCP2_INCLUDE_DIR AND EXISTS "${NGTCP2_INCLUDE_DIR}/ngtcp2/version.h")
  92. set(_version_regex "#[\t ]*define[\t ]+NGTCP2_VERSION[\t ]+\"([^\"]*)\"")
  93. file(STRINGS "${NGTCP2_INCLUDE_DIR}/ngtcp2/version.h" _version_str REGEX "${_version_regex}")
  94. string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}")
  95. set(NGTCP2_VERSION "${_version_str}")
  96. unset(_version_regex)
  97. unset(_version_str)
  98. endif()
  99. if(_ngtcp2_crypto_backend)
  100. get_filename_component(_ngtcp2_library_dir "${NGTCP2_LIBRARY}" DIRECTORY)
  101. find_library(${_crypto_library_upper}_LIBRARY NAMES ${_crypto_library_lower} HINTS ${_ngtcp2_library_dir})
  102. if(${_crypto_library_upper}_LIBRARY)
  103. set(NGTCP2_${_ngtcp2_crypto_backend}_FOUND TRUE)
  104. set(NGTCP2_CRYPTO_LIBRARY ${${_crypto_library_upper}_LIBRARY})
  105. endif()
  106. endif()
  107. include(FindPackageHandleStandardArgs)
  108. find_package_handle_standard_args(NGTCP2
  109. REQUIRED_VARS
  110. NGTCP2_INCLUDE_DIR
  111. NGTCP2_LIBRARY
  112. VERSION_VAR
  113. NGTCP2_VERSION
  114. HANDLE_COMPONENTS
  115. )
  116. if(NGTCP2_FOUND)
  117. set(NGTCP2_INCLUDE_DIRS ${NGTCP2_INCLUDE_DIR})
  118. set(NGTCP2_LIBRARIES ${NGTCP2_LIBRARY} ${NGTCP2_CRYPTO_LIBRARY})
  119. endif()
  120. mark_as_advanced(NGTCP2_INCLUDE_DIR NGTCP2_LIBRARY NGTCP2_CRYPTO_LIBRARY)
  121. if(NOT NGTCP2_FOUND AND _tried_pkgconfig) # reset variables to allow another round of detection
  122. unset(NGTCP2_INCLUDE_DIR CACHE)
  123. unset(NGTCP2_LIBRARY CACHE)
  124. endif()
  125. endif()