mirror of
https://github.com/libssh2/libssh2.git
synced 2026-10-24 03:48:58 +03:00
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:8fce3e17e6https://github.com/curl/curl/pull/20814a56ab9dbc8https://github.com/curl/curl/pull/21098 Follow-up todd5f3fff39#2262 Closes #2264
102 lines
3.6 KiB
CMake
102 lines
3.6 KiB
CMake
# Copyright (C) The libssh2 project and its contributors.
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
#
|
|
###########################################################################
|
|
# Find the mbedTLS library
|
|
#
|
|
# Input variables:
|
|
#
|
|
# - `MBEDTLS_INCLUDE_DIR`: Absolute path to mbedTLS include directory.
|
|
# - `MBEDCRYPTO_LIBRARY`: Absolute path to `mbedcrypto` library.
|
|
# - `MBEDTLS_USE_STATIC_LIBS`: Configure for static mbedTLS libraries.
|
|
#
|
|
# Defines:
|
|
#
|
|
# - `MBEDTLS_FOUND`: System has mbedTLS.
|
|
# - `MBEDTLS_VERSION`: Version of mbedTLS.
|
|
# - `libssh2::mbedcrypto`: mbedcrypto library target.
|
|
|
|
set(_mbedtls_pc_requires "mbedcrypto")
|
|
|
|
if(NOT DEFINED MBEDTLS_INCLUDE_DIR AND
|
|
NOT DEFINED MBEDCRYPTO_LIBRARY)
|
|
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)
|
|
set(MbedTLS_FOUND TRUE)
|
|
set(MBEDTLS_FOUND TRUE)
|
|
set(MBEDTLS_VERSION ${_mbedtls_VERSION})
|
|
if(MBEDTLS_USE_STATIC_LIBS)
|
|
set(_mbedtls_CFLAGS "${_mbedtls_STATIC_CFLAGS}")
|
|
set(_mbedtls_INCLUDE_DIRS "${_mbedtls_STATIC_INCLUDE_DIRS}")
|
|
set(_mbedtls_LIBRARY_DIRS "${_mbedtls_STATIC_LIBRARY_DIRS}")
|
|
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 "")
|
|
|
|
find_path(MBEDTLS_INCLUDE_DIR NAMES "mbedtls/version.h")
|
|
if(MBEDTLS_USE_STATIC_LIBS)
|
|
find_library(MBEDCRYPTO_LIBRARY NAMES "mbedcrypto_static" "libmbedcrypto_static" "mbedcrypto" "libmbedcrypto")
|
|
else()
|
|
find_library(MBEDCRYPTO_LIBRARY NAMES "mbedcrypto" "libmbedcrypto")
|
|
endif()
|
|
|
|
unset(MBEDTLS_VERSION CACHE)
|
|
if(MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
|
|
set(_version_regex "#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"([0-9.]+)\"")
|
|
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h" _version_str REGEX "${_version_regex}")
|
|
string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}")
|
|
set(MBEDTLS_VERSION "${_version_str}")
|
|
unset(_version_regex)
|
|
unset(_version_str)
|
|
endif()
|
|
|
|
include(FindPackageHandleStandardArgs)
|
|
find_package_handle_standard_args(MbedTLS
|
|
REQUIRED_VARS
|
|
MBEDTLS_INCLUDE_DIR
|
|
MBEDCRYPTO_LIBRARY
|
|
VERSION_VAR
|
|
MBEDTLS_VERSION
|
|
)
|
|
|
|
if(MBEDTLS_FOUND)
|
|
set(_mbedtls_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
|
|
set(_mbedtls_LIBRARIES ${MBEDCRYPTO_LIBRARY})
|
|
endif()
|
|
|
|
mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDCRYPTO_LIBRARY)
|
|
endif()
|
|
|
|
if(MBEDTLS_FOUND)
|
|
if(NOT TARGET libssh2::mbedcrypto)
|
|
add_library(libssh2::mbedcrypto INTERFACE IMPORTED)
|
|
set_target_properties(libssh2::mbedcrypto PROPERTIES
|
|
INTERFACE_LIBSSH2_PC_MODULES "${_mbedtls_pc_requires}"
|
|
INTERFACE_COMPILE_OPTIONS "${_mbedtls_CFLAGS}"
|
|
INTERFACE_INCLUDE_DIRECTORIES "${_mbedtls_INCLUDE_DIRS}"
|
|
INTERFACE_LINK_DIRECTORIES "${_mbedtls_LIBRARY_DIRS}"
|
|
INTERFACE_LINK_LIBRARIES "${_mbedtls_LIBRARIES}")
|
|
endif()
|
|
endif()
|