| 1 |
# Copyright (C) The libssh2 project and its contributors. |
| 2 |
# SPDX-License-Identifier: BSD-3-Clause |
| 3 |
# |
| 4 |
########################################################################### |
| 5 |
# Find the mbedtls library |
| 6 |
# |
| 7 |
# Input variables: |
| 8 |
# |
| 9 |
# MBEDTLS_INCLUDE_DIR The mbedtls include directory |
| 10 |
# MBEDCRYPTO_LIBRARY Path to mbedcrypto library |
| 11 |
# |
| 12 |
# Result variables: |
| 13 |
# |
| 14 |
# MBEDTLS_FOUND System has mbedtls |
| 15 |
# MBEDTLS_INCLUDE_DIRS The mbedtls include directories |
| 16 |
# MBEDTLS_LIBRARIES The mbedtls library names |
| 17 |
# MBEDTLS_LIBRARY_DIRS The mbedtls library directories |
| 18 |
# MBEDTLS_CFLAGS Required compiler flags |
| 19 |
# MBEDTLS_VERSION Version of mbedtls |
| 20 |
|
| 21 |
if((UNIX OR VCPKG_TOOLCHAIN OR (MINGW AND NOT CMAKE_CROSSCOMPILING)) AND |
| 22 |
NOT DEFINED MBEDTLS_INCLUDE_DIR AND |
| 23 |
NOT DEFINED MBEDCRYPTO_LIBRARY) |
| 24 |
find_package(PkgConfig QUIET) |
| 25 |
pkg_check_modules(MBEDTLS "mbedcrypto") |
| 26 |
endif() |
| 27 |
|
| 28 |
if(MBEDTLS_FOUND) |
| 29 |
string(REPLACE ";" " " MBEDTLS_CFLAGS "${MBEDTLS_CFLAGS}") |
| 30 |
message(STATUS "Found MbedTLS (via pkg-config): ${MBEDTLS_INCLUDE_DIRS} (found version \"${MBEDTLS_VERSION}\")") |
| 31 |
else() |
| 32 |
find_path(MBEDTLS_INCLUDE_DIR NAMES "mbedtls/version.h") |
| 33 |
find_library(MBEDCRYPTO_LIBRARY NAMES "mbedcrypto" "libmbedcrypto") |
| 34 |
|
| 35 |
if(MBEDTLS_INCLUDE_DIR) |
| 36 |
if(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h") # 3.x |
| 37 |
set(_version_header "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h") |
| 38 |
elseif(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h") # 2.x |
| 39 |
set(_version_header "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h") |
| 40 |
else() |
| 41 |
unset(_version_header) |
| 42 |
endif() |
| 43 |
if(_version_header) |
| 44 |
set(_version_regex "#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"([0-9.]+)\"") |
| 45 |
file(STRINGS "${_version_header}" _version_str REGEX "${_version_regex}") |
| 46 |
string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}") |
| 47 |
set(MBEDTLS_VERSION "${_version_str}") |
| 48 |
unset(_version_regex) |
| 49 |
unset(_version_str) |
| 50 |
unset(_version_header) |
| 51 |
endif() |
| 52 |
endif() |
| 53 |
|
| 54 |
include(FindPackageHandleStandardArgs) |
| 55 |
find_package_handle_standard_args(MbedTLS |
| 56 |
REQUIRED_VARS |
| 57 |
MBEDTLS_INCLUDE_DIR |
| 58 |
MBEDCRYPTO_LIBRARY |
| 59 |
VERSION_VAR |
| 60 |
MBEDTLS_VERSION |
| 61 |
) |
| 62 |
|
| 63 |
if(MBEDTLS_FOUND) |
| 64 |
set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR}) |
| 65 |
set(MBEDTLS_LIBRARIES ${MBEDCRYPTO_LIBRARY}) |
| 66 |
endif() |
| 67 |
|
| 68 |
mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDCRYPTO_LIBRARY) |
| 69 |
endif() |