From 86dcc0ae40cfd12ae169cee6f32ae5a6b271edf6 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Thu, 9 Jul 2026 12:30:02 +0200 Subject: [PATCH] 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: https://github.com/curl/curl/commit/8fce3e17e6cb310cd6dbe38ff14869b8fe5827d2 https://github.com/curl/curl/pull/20814 https://github.com/curl/curl/commit/a56ab9dbc84d6f20c3a91af8a11bc0edc2897b56 https://github.com/curl/curl/pull/21098 Follow-up to dd5f3fff39bd755d815c45a3cbbfdfbf4efdb69a #2262 Closes #2264 --- CMakeLists.txt | 8 ++++++++ cmake/FindMbedTLS.cmake | 24 +++++++++++++++++++----- cmake/FindWolfSSL.cmake | 18 ++++++++++++++---- cmake/libssh2-config.in.cmake | 2 ++ src/CMakeLists.txt | 1 + tests/cmake/CMakeLists.txt | 1 + 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ce8bad1..c9a95d2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/cmake/FindMbedTLS.cmake b/cmake/FindMbedTLS.cmake index 391f10f4..3fec1752 100644 --- a/cmake/FindMbedTLS.cmake +++ b/cmake/FindMbedTLS.cmake @@ -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) diff --git a/cmake/FindWolfSSL.cmake b/cmake/FindWolfSSL.cmake index d8424917..7d01a2fb 100644 --- a/cmake/FindWolfSSL.cmake +++ b/cmake/FindWolfSSL.cmake @@ -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") diff --git a/cmake/libssh2-config.in.cmake b/cmake/libssh2-config.in.cmake index 6dd43c0e..db577af0 100644 --- a/cmake/libssh2-config.in.cmake +++ b/cmake/libssh2-config.in.cmake @@ -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@") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 746e7cf6..74787ee1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) diff --git a/tests/cmake/CMakeLists.txt b/tests/cmake/CMakeLists.txt index 60ff4e16..554492d3 100644 --- a/tests/cmake/CMakeLists.txt +++ b/tests/cmake/CMakeLists.txt @@ -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}")