Browse Source

FindGnuTLS: Add GnuTLS_VERSION

This deprecates the GNUTLS_VERSION result variable.

Instead of setting the version from pkg-config, the header files can be
used. GnuTLS versions 2.7.2 and later define the GNUTLS_VERSION in
`<gnutls/gnutls.h>` header. Earlier versions defined LIBGNUTLS_VERSION.
If finding such string in header is not successful, fallback to version
from pkg-config is used.

Issue: #27088
Peter Kokot 3 months ago
parent
commit
2f8b837a30

+ 3 - 0
Help/release/dev/find-modules.rst

@@ -31,6 +31,9 @@ Find Modules
 * The :module:`FindGnuplot` module now provides a ``Gnuplot_VERSION`` result
   variable.  The ``GNUPLOT_VERSION_STRING`` result variable is deprecated.
 
+* The :module:`FindGnuTLS` module now provides a ``GnuTLS_VERSION`` result
+  variable.  The ``GNUTLS_VERSION`` result variable is deprecated.
+
 * The :module:`FindJasper` module now provides a ``Jasper_VERSION`` result
   variable.  The ``JASPER_VERSION_STRING`` result variable is deprecated.
 

+ 59 - 16
Modules/FindGnuTLS.cmake

@@ -5,12 +5,17 @@
 FindGnuTLS
 ----------
 
-Finds the GNU Transport Layer Security library (GnuTLS).  The GnuTLS
-package includes the main libraries (libgnutls and libdane), as well as the
-optional gnutls-openssl compatibility extra library.  They are all distributed
-as part of the same release.  This module checks for the presence of the main
-libgnutls library and provides usage requirements for integrating GnuTLS into
-CMake projects.
+Finds the GNU Transport Layer Security library (GnuTLS):
+
+.. code-block:: cmake
+
+  find_package(GnuTLS [<version>] [...])
+
+The GnuTLS package includes the main libraries (libgnutls and libdane), as
+well as the optional gnutls-openssl compatibility extra library.  They are
+all distributed as part of the same release.  This module checks for the
+presence of the main libgnutls library and provides usage requirements for
+integrating GnuTLS into CMake projects.
 
 Imported Targets
 ^^^^^^^^^^^^^^^^
@@ -29,12 +34,12 @@ Result Variables
 This module defines the following variables:
 
 ``GnuTLS_FOUND``
-  Boolean indicating whether the (requested version of) GnuTLS is found.  For
+  Boolean indicating whether (the requested version of) GnuTLS is found.  For
   backward compatibility, the ``GNUTLS_FOUND`` variable is also set to the same
   value.
 
-``GNUTLS_VERSION``
-  .. versionadded:: 3.16
+``GnuTLS_VERSION``
+  .. versionadded:: 4.2
 
   The version of GnuTLS found.
 
@@ -61,13 +66,16 @@ The following cache variables may also be set:
 Deprecated Variables
 ^^^^^^^^^^^^^^^^^^^^
 
-These variables are provided for backward compatibility:
+The following variables are provided for backward compatibility:
 
 ``GNUTLS_VERSION_STRING``
   .. deprecated:: 3.16
-    Superseded by ``GNUTLS_VERSION``.
+    Use the ``GnuTLS_VERSION``, which has the same value.
 
-  The version of GnuTLS found.
+``GNUTLS_VERSION``
+  .. versionadded:: 3.16
+  .. deprecated:: 4.2
+    Use the ``GnuTLS_VERSION``, which has the same value.
 
 Examples
 ^^^^^^^^
@@ -80,6 +88,9 @@ Finding GnuTLS and linking it to a project target:
   target_link_libraries(project_target PRIVATE GnuTLS::GnuTLS)
 #]=======================================================================]
 
+cmake_policy(PUSH)
+cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
+
 if (GNUTLS_INCLUDE_DIR AND GNUTLS_LIBRARY)
   # in cache already
   set(gnutls_FIND_QUIETLY TRUE)
@@ -94,9 +105,6 @@ if (NOT WIN32)
     pkg_check_modules(PC_GNUTLS QUIET gnutls)
   endif()
   set(GNUTLS_DEFINITIONS ${PC_GNUTLS_CFLAGS_OTHER})
-  set(GNUTLS_VERSION ${PC_GNUTLS_VERSION})
-  # keep for backward compatibility
-  set(GNUTLS_VERSION_STRING ${PC_GNUTLS_VERSION})
 endif ()
 
 find_path(GNUTLS_INCLUDE_DIR gnutls/gnutls.h
@@ -113,10 +121,43 @@ find_library(GNUTLS_LIBRARY NAMES gnutls libgnutls
 
 mark_as_advanced(GNUTLS_INCLUDE_DIR GNUTLS_LIBRARY)
 
+if(GNUTLS_INCLUDE_DIR AND EXISTS "${GNUTLS_INCLUDE_DIR}/gnutls/gnutls.h")
+  file(
+    STRINGS
+    "${GNUTLS_INCLUDE_DIR}/gnutls/gnutls.h"
+    gnutls_version
+    # GnuTLS versions prior to 2.7.2 defined LIBGNUTLS_VERSION instead of the
+    # current GNUTLS_VERSION.
+    REGEX "^#define[\t ]+(LIB)?GNUTLS_VERSION[\t ]+\".*\""
+  )
+
+  string(
+    REGEX REPLACE
+    "^.*GNUTLS_VERSION[\t ]+\"([^\"]*)\".*$"
+    "\\1"
+    GnuTLS_VERSION
+    "${gnutls_version}"
+  )
+  unset(gnutls_version)
+
+  # Fallback to version defined by pkg-config if not successful.
+  if(
+    NOT GnuTLS_VERSION
+    AND PC_GNUTLS_VERSION
+    AND GNUTLS_INCLUDE_DIR IN_LIST PC_GNUTLS_INCLUDE_DIRS
+  )
+    set(GnuTLS_VERSION "${PC_GNUTLS_VERSION}")
+  endif()
+
+  # For backward compatibility.
+  set(GNUTLS_VERSION "${GnuTLS_VERSION}")
+  set(GNUTLS_VERSION_STRING "${GnuTLS_VERSION}")
+endif()
+
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(GnuTLS
                                   REQUIRED_VARS GNUTLS_LIBRARY GNUTLS_INCLUDE_DIR
-                                  VERSION_VAR GNUTLS_VERSION_STRING)
+                                  VERSION_VAR GnuTLS_VERSION)
 
 if(GnuTLS_FOUND)
   set(GNUTLS_LIBRARIES    ${GNUTLS_LIBRARY})
@@ -131,3 +172,5 @@ if(GnuTLS_FOUND)
       IMPORTED_LOCATION "${GNUTLS_LIBRARIES}")
   endif()
 endif()
+
+cmake_policy(POP)

+ 2 - 2
Tests/CMakeOnly/AllFindModules/CMakeLists.txt

@@ -97,7 +97,7 @@ foreach(
     CUPS CURL
     EXPAT
     FREETYPE
-    GETTEXT GIT
+    GETTEXT GIT GNUTLS
     HG HSPELL
     ICOTOOL
     JASPER
@@ -118,7 +118,7 @@ foreach(
     Doxygen DOXYGEN
     EXPAT
     FLEX Freetype
-    Gettext GIF GTK2
+    Gettext GIF GnuTLS GNUTLS GTK2
     HDF5
     Jasper JPEG
     LibArchive LibLZMA LIBLZMA LibXml2 LibXslt LTTngUST

+ 1 - 1
Tests/FindGnuTLS/Test/CMakeLists.txt

@@ -4,7 +4,7 @@ include(CTest)
 
 find_package(GnuTLS REQUIRED)
 
-add_definitions(-DCMAKE_EXPECTED_GNUTLS_VERSION="${GNUTLS_VERSION}")
+add_definitions(-DCMAKE_EXPECTED_GNUTLS_VERSION="${GnuTLS_VERSION}")
 
 add_executable(test_tgt main.c)
 target_link_libraries(test_tgt GnuTLS::GnuTLS)