1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-12-03 13:31:12 +03:00
Files
libssh2/cmake/FindLibgcrypt.cmake
Viktor Szakats df0563a857 cmake: make Find modules use INTERFACE
- move dependency properties (libs, libdirs, C flags, header dirs,
  pkg-config module names) from global lists to imported target
  `INTERFACE` properties. Rework FInd modules to return their results
  like this and update the libssh2 build process to use it. It makes
  Find modules re-usable from the cmake-config script by libssh2
  consumers, to integrate with libssh2 dependencies.

- define libssh2 dependencies as "imported targets" by the name:
  `libssh2::<depname>`, e.g. `libssh2::libgcrypt`.

- cmake-config: add fall-back logic for CMake without
  CMP0099 (v3.17 2020-03-20) to set lib directories.

- generate `libssh2.pc` based on imported target properties (instead of
  global lists).

- add target property dump debug function.

- ci/GHA: also test cmake integration on macOS.

Follow-up to 96d7f404e7 #1534

Closes #1535
2025-03-19 18:31:19 +01:00

80 lines
2.6 KiB
CMake

# Copyright (C) The libssh2 project and its contributors.
# SPDX-License-Identifier: BSD-3-Clause
#
###########################################################################
# Find the Libgcrypt library
#
# Input variables:
#
# - `LIBGCRYPT_INCLUDE_DIR`: The Libgcrypt include directory.
# - `LIBGCRYPT_LIBRARY`: Path to `libgcrypt` library.
#
# Defines:
#
# - `LIBGCRYPT_FOUND`: System has Libgcrypt.
# - `LIBGCRYPT_VERSION`: Version of Libgcrypt.
# - `libssh2::libgcrypt`: libgcrypt library target.
set(_libgcrypt_pc_requires "libgcrypt")
if(LIBSSH2_USE_PKGCONFIG AND
NOT DEFINED LIBGCRYPT_INCLUDE_DIR AND
NOT DEFINED LIBGCRYPT_LIBRARY)
find_package(PkgConfig QUIET)
pkg_check_modules(_libgcrypt ${_libgcrypt_pc_requires})
endif()
if(_libgcrypt_FOUND)
set(Libgcrypt_FOUND TRUE)
set(LIBGCRYPT_FOUND TRUE)
set(LIBGCRYPT_VERSION ${_libgcrypt_VERSION})
string(REPLACE ";" " " _libgcrypt_CFLAGS "${_libgcrypt_CFLAGS}")
message(STATUS "Found Libgcrypt (via pkg-config): ${_libgcrypt_INCLUDE_DIRS} (found version \"${LIBGCRYPT_VERSION}\")")
else()
find_path(LIBGCRYPT_INCLUDE_DIR NAMES "gcrypt.h")
find_library(LIBGCRYPT_LIBRARY NAMES "gcrypt" "libgcrypt")
unset(LIBGCRYPT_VERSION CACHE)
if(LIBGCRYPT_INCLUDE_DIR AND EXISTS "${LIBGCRYPT_INCLUDE_DIR}/gcrypt.h")
set(_version_regex "#[\t ]*define[\t ]+GCRYPT_VERSION[\t ]+\"([^\"]*)\"")
file(STRINGS "${LIBGCRYPT_INCLUDE_DIR}/gcrypt.h" _version_str REGEX "${_version_regex}")
string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}")
set(LIBGCRYPT_VERSION "${_version_str}")
unset(_version_regex)
unset(_version_str)
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Libgcrypt
REQUIRED_VARS
LIBGCRYPT_INCLUDE_DIR
LIBGCRYPT_LIBRARY
VERSION_VAR
LIBGCRYPT_VERSION
)
if(LIBGCRYPT_FOUND)
set(_libgcrypt_INCLUDE_DIRS ${LIBGCRYPT_INCLUDE_DIR})
set(_libgcrypt_LIBRARIES ${LIBGCRYPT_LIBRARY})
endif()
mark_as_advanced(LIBGCRYPT_INCLUDE_DIR LIBGCRYPT_LIBRARY)
endif()
if(LIBGCRYPT_FOUND)
if(CMAKE_VERSION VERSION_LESS 3.13)
link_directories(${_libgcrypt_LIBRARY_DIRS})
endif()
if(NOT TARGET libssh2::libgcrypt)
add_library(libssh2::libgcrypt INTERFACE IMPORTED)
set_target_properties(libssh2::libgcrypt PROPERTIES
VERSION "${LIBGRCYPT_VERSION}"
LIBSSH2_PC_MODULES "${_libgcrypt_pc_requires}"
INTERFACE_COMPILE_OPTIONS "${_libgcrypt_CFLAGS}"
INTERFACE_INCLUDE_DIRECTORIES "${_libgcrypt_INCLUDE_DIRS}"
INTERFACE_LINK_DIRECTORIES "${_libgcrypt_LIBRARY_DIRS}"
INTERFACE_LINK_LIBRARIES "${_libgcrypt_LIBRARIES}")
endif()
endif()