1
0
mirror of https://github.com/libssh2/libssh2.git synced 2026-10-24 03:48:58 +03:00

cmake: add CMake Config-based dependency detection for mbedTLS, wolfSSL

After limiting `find_package()`/`find_dependency()` calls to libssh2
local Find modules via the `MODULES` keyword, it became possible to
detect dependencies via CMake Configs from within those local Find
modules, by calling `find_package()` again with the `CONFIG` keyword.
This patch implements this. Then maps detection results to the result
variables and libssh2-specific imported targets the rest of the build
expects.

This adds CMake Configs as an alternative to the existing `pkg-config`
and `find_path()`/`find_library()` auto-detection methods.

Enabled by default for MSVC, outside vcpkg and when not cross-building.
To enable for other cases, or override the default, you can use
`-DLIBSSH2_USE_CMAKECONFIG=ON` or `OFF`.

When enabled, Config detection happens after `pkg-config` and before
`find_path()`/`find_library()`. Using CMake's built-in options, you may
also manually point to the absolute directory holding Config files:

`MbedTLS_DIR`, `wolfssl_DIR`

E.g. `-DMbedTLS_DIR=/path/to/mbedtls/lib/cmake/MbedTLS`

mbedTLS needs to be built with CMake for this to work. wolfSSL 5.9.0+
also supports it when it was built with autotools.

Experimental.

Refs:
8fce3e17e6
https://github.com/curl/curl/pull/20814
a56ab9dbc8
https://github.com/curl/curl/pull/21098

Follow-up to dd5f3fff39 #2262

Closes #2264
This commit is contained in:
Viktor Szakats
2026-07-09 12:30:02 +02:00
parent d462f15894
commit 86dcc0ae40
6 changed files with 45 additions and 9 deletions

View File

@@ -217,6 +217,14 @@ else()
set(LIB_SELECTED ${LIB_STATIC})
endif()
# Override to force-disable or force-enable the use of CMake Configs.
if(MSVC AND NOT VCPKG_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
set(_libssh2_use_cmakeconfig_default ON)
else()
set(_libssh2_use_cmakeconfig_default OFF)
endif()
option(LIBSSH2_USE_CMAKECONFIG "Enable detecting dependencies via CMake Config" ${_libssh2_use_cmakeconfig_default})
# Override to force-disable or force-enable the use of pkg-config.
if((UNIX AND NOT ANDROID AND (NOT APPLE OR CMAKE_SYSTEM_NAME MATCHES "Darwin")) OR
VCPKG_TOOLCHAIN OR

View File

@@ -18,11 +18,15 @@
set(_mbedtls_pc_requires "mbedcrypto")
if(LIBSSH2_USE_PKGCONFIG AND
NOT DEFINED MBEDTLS_INCLUDE_DIR AND
if(NOT DEFINED MBEDTLS_INCLUDE_DIR AND
NOT DEFINED MBEDCRYPTO_LIBRARY)
find_package(PkgConfig QUIET)
pkg_check_modules(_mbedtls ${_mbedtls_pc_requires})
if(LIBSSH2_USE_PKGCONFIG)
find_package(PkgConfig QUIET)
pkg_check_modules(_mbedtls ${_mbedtls_pc_requires})
endif()
if(NOT _mbedtls_FOUND AND LIBSSH2_USE_CMAKECONFIG)
find_package(MbedTLS CONFIG QUIET)
endif()
endif()
if(_mbedtls_FOUND)
@@ -36,6 +40,17 @@ if(_mbedtls_FOUND)
set(_mbedtls_LIBRARIES "${_mbedtls_STATIC_LIBRARIES}")
endif()
message(STATUS "Found MbedTLS (via pkg-config): ${_mbedtls_INCLUDE_DIRS} (found version \"${MBEDTLS_VERSION}\")")
elseif(MbedTLS_CONFIG)
set(MbedTLS_FOUND TRUE)
set(MBEDTLS_FOUND TRUE)
set(MBEDTLS_VERSION ${MbedTLS_VERSION})
if(MBEDTLS_VERSION GREATER_EQUAL 4.0.0)
set(_mbedtls_LIBRARIES MbedTLS::tfpsacrypto)
else()
set(_mbedtls_LIBRARIES MbedTLS::mbedcrypto)
endif()
list(APPEND _mbedtls_LIBRARIES MbedTLS::mbedx509 MbedTLS::mbedtls)
message(STATUS "Found MbedTLS (via CMake Config): ${MbedTLS_CONFIG} (found version \"${MBEDTLS_VERSION}\")")
else()
set(_mbedtls_pc_requires "")
@@ -54,7 +69,6 @@ else()
set(MBEDTLS_VERSION "${_version_str}")
unset(_version_regex)
unset(_version_str)
unset(_version_header)
endif()
include(FindPackageHandleStandardArgs)

View File

@@ -17,11 +17,15 @@
set(_wolfssl_pc_requires "wolfssl")
if(LIBSSH2_USE_PKGCONFIG AND
NOT DEFINED WOLFSSL_INCLUDE_DIR AND
if(NOT DEFINED WOLFSSL_INCLUDE_DIR AND
NOT DEFINED WOLFSSL_LIBRARY)
find_package(PkgConfig QUIET)
pkg_check_modules(_wolfssl ${_wolfssl_pc_requires})
if(LIBSSH2_USE_PKGCONFIG)
find_package(PkgConfig QUIET)
pkg_check_modules(_wolfssl ${_wolfssl_pc_requires})
endif()
if(NOT _wolfssl_FOUND AND LIBSSH2_USE_CMAKECONFIG)
find_package(wolfssl CONFIG QUIET)
endif()
endif()
if(_wolfssl_FOUND)
@@ -29,6 +33,12 @@ if(_wolfssl_FOUND)
set(WOLFSSL_FOUND TRUE)
set(WOLFSSL_VERSION ${_wolfssl_VERSION})
message(STATUS "Found WolfSSL (via pkg-config): ${_wolfssl_INCLUDE_DIRS} (found version \"${WOLFSSL_VERSION}\")")
elseif(wolfssl_CONFIG)
set(WolfSSL_FOUND TRUE)
set(WOLFSSL_FOUND TRUE)
set(WOLFSSL_VERSION ${wolfssl_VERSION})
set(_wolfssl_LIBRARIES wolfssl::wolfssl)
message(STATUS "Found WolfSSL (via CMake Config): ${wolfssl_CONFIG} (found version \"${WOLFSSL_VERSION}\")")
else()
find_path(WOLFSSL_INCLUDE_DIR NAMES "wolfssl/options.h")
find_library(WOLFSSL_LIBRARY NAMES "wolfssl")

View File

@@ -1,6 +1,8 @@
# Copyright (C) The libssh2 project and its contributors.
# SPDX-License-Identifier: BSD-3-Clause
option(LIBSSH2_USE_CMAKECONFIG "Enable detecting @PROJECT_NAME@ dependencies via CMake Config. Default: @LIBSSH2_USE_CMAKECONFIG@"
"@LIBSSH2_USE_CMAKECONFIG@")
option(LIBSSH2_USE_PKGCONFIG "Enable pkg-config to detect @PROJECT_NAME@ dependencies. Default: @LIBSSH2_USE_PKGCONFIG@"
"@LIBSSH2_USE_PKGCONFIG@")

View File

@@ -373,6 +373,7 @@ if(NOT LIBSSH2_DISABLE_INSTALL)
# LIB_SELECTED
# LIB_SHARED
# LIB_STATIC
# LIBSSH2_USE_CMAKECONFIG
# LIBSSH2_USE_PKGCONFIG
# ZLIB_FOUND
include(CMakePackageConfigHelpers)

View File

@@ -62,6 +62,7 @@ elseif(TEST_INTEGRATION_MODE STREQUAL "ExternalProject")
${_download_extract_timestamp}
PREFIX "${CMAKE_BINARY_DIR}/libssh2-external"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${_libssh2_install_dir}" -DBUILD_SHARED_LIBS=OFF
-DLIBSSH2_USE_CMAKECONFIG=OFF -DLIBSSH2_USE_PKGCONFIG=OFF
${LIBSSH2_TEST_OPTS} -DENABLE_ZLIB_COMPRESSION=OFF
BUILD_BYPRODUCTS "${_libssh2_static_lib}")