FindGnuTLS.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. FindGnuTLS
  5. ----------
  6. Finds the GNU Transport Layer Security library (GnuTLS):
  7. .. code-block:: cmake
  8. find_package(GnuTLS [<version>] [...])
  9. The GnuTLS package includes the main libraries (libgnutls and libdane), as
  10. well as the optional gnutls-openssl compatibility extra library. They are
  11. all distributed as part of the same release. This module checks for the
  12. presence of the main libgnutls library and provides usage requirements for
  13. integrating GnuTLS into CMake projects.
  14. Imported Targets
  15. ^^^^^^^^^^^^^^^^
  16. This module provides the following :ref:`Imported Targets`:
  17. ``GnuTLS::GnuTLS``
  18. .. versionadded:: 3.16
  19. Target encapsulating the GnuTLS usage requirements, available if GnuTLS is
  20. found.
  21. Result Variables
  22. ^^^^^^^^^^^^^^^^
  23. This module defines the following variables:
  24. ``GnuTLS_FOUND``
  25. Boolean indicating whether (the requested version of) GnuTLS is found. For
  26. backward compatibility, the ``GNUTLS_FOUND`` variable is also set to the same
  27. value.
  28. ``GnuTLS_VERSION``
  29. .. versionadded:: 4.2
  30. The version of GnuTLS found.
  31. ``GNUTLS_INCLUDE_DIRS``
  32. Include directories needed to use GnuTLS.
  33. ``GNUTLS_LIBRARIES``
  34. Libraries needed to link against to use GnuTLS.
  35. ``GNUTLS_DEFINITIONS``
  36. Compiler options required for using GnuTLS.
  37. Cache Variables
  38. ^^^^^^^^^^^^^^^
  39. The following cache variables may also be set:
  40. ``GNUTLS_INCLUDE_DIR``
  41. The directory containing the ``gnutls/gnutls.h`` header file.
  42. ``GNUTLS_LIBRARY``
  43. The path to the GnuTLS library.
  44. Deprecated Variables
  45. ^^^^^^^^^^^^^^^^^^^^
  46. The following variables are provided for backward compatibility:
  47. ``GNUTLS_VERSION_STRING``
  48. .. deprecated:: 3.16
  49. Use the ``GnuTLS_VERSION``, which has the same value.
  50. ``GNUTLS_VERSION``
  51. .. versionadded:: 3.16
  52. .. deprecated:: 4.2
  53. Use the ``GnuTLS_VERSION``, which has the same value.
  54. Examples
  55. ^^^^^^^^
  56. Finding GnuTLS and linking it to a project target:
  57. .. code-block:: cmake
  58. find_package(GnuTLS)
  59. target_link_libraries(project_target PRIVATE GnuTLS::GnuTLS)
  60. #]=======================================================================]
  61. cmake_policy(PUSH)
  62. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  63. if (GNUTLS_INCLUDE_DIR AND GNUTLS_LIBRARY)
  64. # in cache already
  65. set(gnutls_FIND_QUIETLY TRUE)
  66. endif ()
  67. if (NOT WIN32)
  68. # try using pkg-config to get the directories and then use these values
  69. # in the find_path() and find_library() calls
  70. # also fills in GNUTLS_DEFINITIONS, although that isn't normally useful
  71. find_package(PkgConfig QUIET)
  72. if(PKG_CONFIG_FOUND)
  73. pkg_check_modules(PC_GNUTLS QUIET gnutls)
  74. endif()
  75. set(GNUTLS_DEFINITIONS ${PC_GNUTLS_CFLAGS_OTHER})
  76. endif ()
  77. find_path(GNUTLS_INCLUDE_DIR gnutls/gnutls.h
  78. HINTS
  79. ${PC_GNUTLS_INCLUDEDIR}
  80. ${PC_GNUTLS_INCLUDE_DIRS}
  81. )
  82. find_library(GNUTLS_LIBRARY NAMES gnutls libgnutls
  83. HINTS
  84. ${PC_GNUTLS_LIBDIR}
  85. ${PC_GNUTLS_LIBRARY_DIRS}
  86. )
  87. mark_as_advanced(GNUTLS_INCLUDE_DIR GNUTLS_LIBRARY)
  88. if(GNUTLS_INCLUDE_DIR AND EXISTS "${GNUTLS_INCLUDE_DIR}/gnutls/gnutls.h")
  89. file(
  90. STRINGS
  91. "${GNUTLS_INCLUDE_DIR}/gnutls/gnutls.h"
  92. gnutls_version
  93. # GnuTLS versions prior to 2.7.2 defined LIBGNUTLS_VERSION instead of the
  94. # current GNUTLS_VERSION.
  95. REGEX "^#define[\t ]+(LIB)?GNUTLS_VERSION[\t ]+\".*\""
  96. )
  97. string(
  98. REGEX REPLACE
  99. "^.*GNUTLS_VERSION[\t ]+\"([^\"]*)\".*$"
  100. "\\1"
  101. GnuTLS_VERSION
  102. "${gnutls_version}"
  103. )
  104. unset(gnutls_version)
  105. # Fallback to version defined by pkg-config if not successful.
  106. if(
  107. NOT GnuTLS_VERSION
  108. AND PC_GNUTLS_VERSION
  109. AND GNUTLS_INCLUDE_DIR IN_LIST PC_GNUTLS_INCLUDE_DIRS
  110. )
  111. set(GnuTLS_VERSION "${PC_GNUTLS_VERSION}")
  112. endif()
  113. # For backward compatibility.
  114. set(GNUTLS_VERSION "${GnuTLS_VERSION}")
  115. set(GNUTLS_VERSION_STRING "${GnuTLS_VERSION}")
  116. endif()
  117. include(FindPackageHandleStandardArgs)
  118. find_package_handle_standard_args(GnuTLS
  119. REQUIRED_VARS GNUTLS_LIBRARY GNUTLS_INCLUDE_DIR
  120. VERSION_VAR GnuTLS_VERSION)
  121. if(GnuTLS_FOUND)
  122. set(GNUTLS_LIBRARIES ${GNUTLS_LIBRARY})
  123. set(GNUTLS_INCLUDE_DIRS ${GNUTLS_INCLUDE_DIR})
  124. if(NOT TARGET GnuTLS::GnuTLS)
  125. add_library(GnuTLS::GnuTLS UNKNOWN IMPORTED)
  126. set_target_properties(GnuTLS::GnuTLS PROPERTIES
  127. INTERFACE_INCLUDE_DIRECTORIES "${GNUTLS_INCLUDE_DIRS}"
  128. INTERFACE_COMPILE_DEFINITIONS "${GNUTLS_DEFINITIONS}"
  129. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  130. IMPORTED_LOCATION "${GNUTLS_LIBRARIES}")
  131. endif()
  132. endif()
  133. cmake_policy(POP)