|
| 1 | +#*************************************************************************** |
| 2 | +# _ _ ____ _ |
| 3 | +# Project ___| | | | _ \| | |
| 4 | +# / __| | | | |_) | | |
| 5 | +# | (__| |_| | _ <| |___ |
| 6 | +# \___|\___/|_| \_\_____| |
| 7 | +# |
| 8 | +# Copyright (C) Daniel Stenberg, <[email protected]>, et al. |
| 9 | +# |
| 10 | +# This software is licensed as described in the file COPYING, which |
| 11 | +# you should have received as part of this distribution. The terms |
| 12 | +# are also available at https://curl.se/docs/copyright.html. |
| 13 | +# |
| 14 | +# You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | +# copies of the Software, and permit persons to whom the Software is |
| 16 | +# furnished to do so, under the terms of the COPYING file. |
| 17 | +# |
| 18 | +# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | +# KIND, either express or implied. |
| 20 | +# |
| 21 | +# SPDX-License-Identifier: curl |
| 22 | +# |
| 23 | +########################################################################### |
| 24 | +# Find the mbedTLS library |
| 25 | +# |
| 26 | +# Input variables: |
| 27 | +# |
| 28 | +# - `MBEDTLS_INCLUDE_DIR`: The mbedTLS include directory. |
| 29 | +# - `MBEDTLS_LIBRARY`: Path to `mbedtls` library. |
| 30 | +# - `MBEDX509_LIBRARY`: Path to `mbedx509` library. |
| 31 | +# - `MBEDCRYPTO_LIBRARY`: Path to `mbedcrypto` library. |
| 32 | +# |
| 33 | +# Result variables: |
| 34 | +# |
| 35 | +# - `MBEDTLS_FOUND`: System has mbedTLS. |
| 36 | +# - `MBEDTLS_INCLUDE_DIRS`: The mbedTLS include directories. |
| 37 | +# - `MBEDTLS_LIBRARIES`: The mbedTLS library names. |
| 38 | +# - `MBEDTLS_LIBRARY_DIRS`: The mbedTLS library directories. |
| 39 | +# - `MBEDTLS_PC_REQUIRES`: The mbedTLS pkg-config packages. |
| 40 | +# - `MBEDTLS_CFLAGS`: Required compiler flags. |
| 41 | +# - `MBEDTLS_VERSION`: Version of mbedTLS. |
| 42 | + |
| 43 | +if(DEFINED MBEDTLS_INCLUDE_DIRS AND NOT DEFINED MBEDTLS_INCLUDE_DIR) |
| 44 | + message(WARNING "MBEDTLS_INCLUDE_DIRS is deprecated, use MBEDTLS_INCLUDE_DIR instead.") |
| 45 | + set(MBEDTLS_INCLUDE_DIR "${MBEDTLS_INCLUDE_DIRS}") |
| 46 | + unset(MBEDTLS_INCLUDE_DIRS) |
| 47 | +endif() |
| 48 | + |
| 49 | +set(MBEDTLS_PC_REQUIRES "mbedtls" "mbedx509" "mbedcrypto") |
| 50 | + |
| 51 | +if(CURL_USE_PKGCONFIG AND |
| 52 | + NOT DEFINED MBEDTLS_INCLUDE_DIR AND |
| 53 | + NOT DEFINED MBEDTLS_LIBRARY AND |
| 54 | + NOT DEFINED MBEDX509_LIBRARY AND |
| 55 | + NOT DEFINED MBEDCRYPTO_LIBRARY) |
| 56 | + find_package(PkgConfig QUIET) |
| 57 | + pkg_check_modules(MBEDTLS ${MBEDTLS_PC_REQUIRES}) |
| 58 | +endif() |
| 59 | + |
| 60 | +if(MBEDTLS_FOUND) |
| 61 | + set(MbedTLS_FOUND TRUE) |
| 62 | + set(MBEDTLS_VERSION "${MBEDTLS_mbedtls_VERSION}") |
| 63 | + string(REPLACE ";" " " MBEDTLS_CFLAGS "${MBEDTLS_CFLAGS}") |
| 64 | + message(STATUS "Found MbedTLS (via pkg-config): ${MBEDTLS_INCLUDE_DIRS} (found version \"${MBEDTLS_VERSION}\")") |
| 65 | +else() |
| 66 | + set(MBEDTLS_PC_REQUIRES "") # Depend on pkg-config only when found via pkg-config |
| 67 | + |
| 68 | + find_path(MBEDTLS_INCLUDE_DIR NAMES "mbedtls/ssl.h") |
| 69 | + find_library(MBEDTLS_LIBRARY NAMES "mbedtls" "libmbedtls") |
| 70 | + find_library(MBEDX509_LIBRARY NAMES "mbedx509" "libmbedx509") |
| 71 | + find_library(MBEDCRYPTO_LIBRARY NAMES "mbedcrypto" "libmbedcrypto") |
| 72 | + |
| 73 | + unset(MBEDTLS_VERSION CACHE) |
| 74 | + if(MBEDTLS_INCLUDE_DIR) |
| 75 | + if(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h") # 3.x |
| 76 | + set(_version_header "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h") |
| 77 | + elseif(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h") # 2.x |
| 78 | + set(_version_header "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h") |
| 79 | + else() |
| 80 | + unset(_version_header) |
| 81 | + endif() |
| 82 | + if(_version_header) |
| 83 | + set(_version_regex "#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"([0-9.]+)\"") |
| 84 | + file(STRINGS "${_version_header}" _version_str REGEX "${_version_regex}") |
| 85 | + string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}") |
| 86 | + set(MBEDTLS_VERSION "${_version_str}") |
| 87 | + unset(_version_regex) |
| 88 | + unset(_version_str) |
| 89 | + unset(_version_header) |
| 90 | + endif() |
| 91 | + endif() |
| 92 | + |
| 93 | + include(FindPackageHandleStandardArgs) |
| 94 | + find_package_handle_standard_args(MbedTLS |
| 95 | + REQUIRED_VARS |
| 96 | + MBEDTLS_INCLUDE_DIR |
| 97 | + MBEDTLS_LIBRARY |
| 98 | + MBEDX509_LIBRARY |
| 99 | + MBEDCRYPTO_LIBRARY |
| 100 | + VERSION_VAR |
| 101 | + MBEDTLS_VERSION |
| 102 | + ) |
| 103 | + |
| 104 | + if(MBEDTLS_FOUND) |
| 105 | + set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR}) |
| 106 | + set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY}) |
| 107 | + |
| 108 | + if(NOT TARGET mbedtls) |
| 109 | + add_library(mbedtls IMPORTED UNKNOWN) |
| 110 | + set_target_properties(mbedtls PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}") |
| 111 | + if(MBEDTLS_LIBRARY MATCHES "/([^/]+)\\.framework$") |
| 112 | + set_target_properties(mbedtls PROPERTIES IMPORTED_LOCATION ${MBEDTLS_LIBRARY}/${CMAKE_MATCH_1}) |
| 113 | + else() |
| 114 | + set_target_properties(mbedtls PROPERTIES IMPORTED_LOCATION ${MBEDTLS_LIBRARY}) |
| 115 | + endif() |
| 116 | + endif() |
| 117 | + |
| 118 | + if(NOT TARGET mbedx509) |
| 119 | + add_library(mbedx509 IMPORTED UNKNOWN) |
| 120 | + set_target_properties(mbedx509 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}") |
| 121 | + if(MBEDX509_LIBRARY MATCHES "/([^/]+)\\.framework$") |
| 122 | + set_target_properties(mbedx509 PROPERTIES IMPORTED_LOCATION ${MBEDX509_LIBRARY}/${CMAKE_MATCH_1}) |
| 123 | + else() |
| 124 | + set_target_properties(mbedx509 PROPERTIES IMPORTED_LOCATION ${MBEDX509_LIBRARY}) |
| 125 | + endif() |
| 126 | + endif() |
| 127 | + |
| 128 | + if(NOT TARGET mbedcrypto) |
| 129 | + add_library(mbedcrypto IMPORTED UNKNOWN) |
| 130 | + set_target_properties(mbedcrypto PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}") |
| 131 | + if(MBEDCRYPTO_LIBRARY MATCHES "/([^/]+)\\.framework$") |
| 132 | + set_target_properties(mbedcrypto PROPERTIES IMPORTED_LOCATION ${MBEDCRYPTO_LIBRARY}/${CMAKE_MATCH_1}) |
| 133 | + else() |
| 134 | + set_target_properties(mbedcrypto PROPERTIES IMPORTED_LOCATION ${MBEDCRYPTO_LIBRARY}) |
| 135 | + endif() |
| 136 | + endif() |
| 137 | + endif() |
| 138 | + |
| 139 | + mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY) |
| 140 | +endif() |
0 commit comments