|
| 1 | +#[=======================================================================[.rst |
| 2 | +FindMbedTLS |
| 3 | +----------- |
| 4 | +
|
| 5 | +FindModule for MbedTLS and associated libraries |
| 6 | +
|
| 7 | +Components |
| 8 | +^^^^^^^^^^ |
| 9 | +
|
| 10 | +This module contains provides several components: |
| 11 | +
|
| 12 | +``MbedCrypto`` |
| 13 | +``MbedTLS`` |
| 14 | +``MbedX509`` |
| 15 | +
|
| 16 | +Import targets exist for each component. |
| 17 | +
|
| 18 | +Imported Targets |
| 19 | +^^^^^^^^^^^^^^^^ |
| 20 | +
|
| 21 | +This module defines the :prop_tgt:`IMPORTED` targets: |
| 22 | +
|
| 23 | +``MbedTLS::MbedCrypto`` |
| 24 | + Crypto component |
| 25 | +
|
| 26 | +``MbedTLS::MbedTLS`` |
| 27 | + TLS component |
| 28 | +
|
| 29 | +``MbedTLS::MbedX509`` |
| 30 | + X509 component |
| 31 | +
|
| 32 | +Result Variables |
| 33 | +^^^^^^^^^^^^^^^^ |
| 34 | +
|
| 35 | +This module sets the following variables: |
| 36 | +
|
| 37 | +``MbedTLS_FOUND`` |
| 38 | + True, if all required components and the core library were found. |
| 39 | +``MbedTLS_VERSION`` |
| 40 | + Detected version of found MbedTLS libraries. |
| 41 | +
|
| 42 | +``MbedTLS_<COMPONENT>_VERSION`` |
| 43 | + Detected version of found MbedTLS component library. |
| 44 | +
|
| 45 | +Cache variables |
| 46 | +^^^^^^^^^^^^^^^ |
| 47 | +
|
| 48 | +The following cache variables may also be set: |
| 49 | +
|
| 50 | +``MbedTLS_<COMPONENT>_LIBRARY`` |
| 51 | + Path to the library component of MbedTLS. |
| 52 | +``MbedTLS_<COMPONENT>_INCLUDE_DIR`` |
| 53 | + Directory containing ``<COMPONENT>.h``. |
| 54 | +
|
| 55 | +Distributed under the MIT License, see accompanying LICENSE file or |
| 56 | +https://github.com/PatTheMav/cmake-finders/blob/master/LICENSE for details. |
| 57 | +(c) 2023 Patrick Heyer |
| 58 | +
|
| 59 | +#]=======================================================================] |
| 60 | + |
| 61 | +# cmake-format: off |
| 62 | +# cmake-lint: disable=C0103 |
| 63 | +# cmake-lint: disable=C0301 |
| 64 | +# cmake-lint: disable=C0307 |
| 65 | +# cmake-format: on |
| 66 | + |
| 67 | +include(FindPackageHandleStandardArgs) |
| 68 | + |
| 69 | +find_package(PkgConfig QUIET) |
| 70 | +if(PKG_CONFIG_FOUND) |
| 71 | + pkg_check_modules(PC_MbedTLS QUIET mbedtls mbedcrypto mbedx509) |
| 72 | +endif() |
| 73 | + |
| 74 | +# MbedTLS_set_soname: Set SONAME on imported library targets |
| 75 | +macro(MbedTLS_set_soname component) |
| 76 | + if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin") |
| 77 | + execute_process( |
| 78 | + COMMAND sh -c "otool -D '${Mbed${component}_LIBRARY}' | grep -v '${Mbed${component}_LIBRARY}'" |
| 79 | + OUTPUT_VARIABLE _output |
| 80 | + RESULT_VARIABLE _result) |
| 81 | + |
| 82 | + if(_result EQUAL 0 AND _output MATCHES "^@rpath/") |
| 83 | + set_property(TARGET MbedTLS::Mbed${component} PROPERTY IMPORTED_SONAME "${_output}") |
| 84 | + endif() |
| 85 | + elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD") |
| 86 | + execute_process( |
| 87 | + COMMAND sh -c "objdump -p '${Mbed${component}_LIBRARY}' | grep SONAME" |
| 88 | + OUTPUT_VARIABLE _output |
| 89 | + RESULT_VARIABLE _result) |
| 90 | + |
| 91 | + if(_result EQUAL 0) |
| 92 | + string(REGEX REPLACE "[ \t]+SONAME[ \t]+([^ \t]+)" "\\1" _soname "${_output}") |
| 93 | + set_property(TARGET MbedTLS::Mbed${component} PROPERTY IMPORTED_SONAME "${_soname}") |
| 94 | + unset(_soname) |
| 95 | + endif() |
| 96 | + endif() |
| 97 | + unset(_output) |
| 98 | + unset(_result) |
| 99 | +endmacro() |
| 100 | + |
| 101 | +find_path( |
| 102 | + MbedTLS_INCLUDE_DIR |
| 103 | + NAMES mbedtls/ssl.h |
| 104 | + HINTS "${PC_MbedTLS_INCLUDE_DIRS}" |
| 105 | + PATHS /usr/include /usr/local/include |
| 106 | + DOC "MbedTLS include directory") |
| 107 | + |
| 108 | +if(PC_MbedTLS_VERSION VERSION_GREATER 0) |
| 109 | + set(MbedTLS_VERSION ${PC_MbedTLS_VERSION}) |
| 110 | +elseif(EXISTS "${MbedTLS_INCLUDE_DIR}/mbedtls/build_info.h") |
| 111 | + file(STRINGS "${MbedTLS_INCLUDE_DIR}/mbedtls/build_info.h" _VERSION_STRING |
| 112 | + REGEX "#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+.+") |
| 113 | + string(REGEX REPLACE ".*#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+\"(.+)\".*" "\\1" MbedTLS_VERSION |
| 114 | + "${_VERSION_STRING}") |
| 115 | +else() |
| 116 | + if(NOT MbedTLS_FIND_QUIETLY) |
| 117 | + message(AUTHOR_WARNING "Failed to find MbedTLS version.") |
| 118 | + endif() |
| 119 | + set(MbedTLS_VERSION 0.0.0) |
| 120 | +endif() |
| 121 | + |
| 122 | +find_library( |
| 123 | + MbedTLS_LIBRARY |
| 124 | + NAMES libmbedtls mbedtls |
| 125 | + HINTS "${PC_MbedTLS_LIBRARY_DIRS}" |
| 126 | + PATHS /usr/lib /usr/local/lib |
| 127 | + DOC "MbedTLS location") |
| 128 | + |
| 129 | +find_library( |
| 130 | + MbedCrypto_LIBRARY |
| 131 | + NAMES libmbedcrypto mbedcrypto |
| 132 | + HINTS "${PC_MbedTLS_LIBRARY_DIRS}" |
| 133 | + PATHS /usr/lib /usr/local/lib |
| 134 | + DOC "MbedCrypto location") |
| 135 | + |
| 136 | +find_library( |
| 137 | + MbedX509_LIBRARY |
| 138 | + NAMES libmbedx509 mbedx509 |
| 139 | + HINTS "${PC_MbedTLS_LIBRARY_DIRS}" |
| 140 | + PATHS /usr/lib /usr/local/lib |
| 141 | + DOC "MbedX509 location") |
| 142 | + |
| 143 | +if(MbedTLS_LIBRARY |
| 144 | + AND NOT MbedCrypto_LIBRARY |
| 145 | + AND NOT MbedX509_LIBRARY) |
| 146 | + set(CMAKE_REQUIRED_LIBRARIES "${MbedTLS_LIBRARY}") |
| 147 | + set(CMAKE_REQUIRED_INCLUDES "${MbedTLS_INCLUDE_DIR}") |
| 148 | + |
| 149 | + check_symbol_exists(mbedtls_x509_crt_init "mbedtls/x590_crt.h" MbedTLS_INCLUDES_X509) |
| 150 | + check_symbol_exists(mbedtls_sha256_init "mbedtls/sha256.h" MbedTLS_INCLUDES_CRYPTO) |
| 151 | + unset(CMAKE_REQUIRED_LIBRARIES) |
| 152 | + unset(CMAKE_REQUIRED_INCLUDES) |
| 153 | +endif() |
| 154 | + |
| 155 | +if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin|Windows") |
| 156 | + set(MbedTLS_ERROR_REASON "Ensure that an MbedTLS distribution is provided as part of CMAKE_PREFIX_PATH.") |
| 157 | +elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD") |
| 158 | + set(MbedTLS_ERROR_REASON "Ensure that MbedTLS is installed on the system.") |
| 159 | +endif() |
| 160 | + |
| 161 | +if(MbedTLS_INCLUDES_X509 AND MbedTLS_INCLUDES_CRYPTO) |
| 162 | + find_package_handle_standard_args( |
| 163 | + MbedTLS |
| 164 | + REQUIRED_VARS MbedTLS_LIBRARY MbedTLS_INCLUDE_DIR |
| 165 | + VERSION_VAR MbedTLS_VERSION REASON_FAILURE_MESSAGE "${MbedTLS_ERROR_REASON}") |
| 166 | + mark_as_advanced(MbedTLS_LIBRARY MbedTLS_INCLUDE_DIR) |
| 167 | + list(APPEND _COMPONENTS TLS) |
| 168 | +else() |
| 169 | + find_package_handle_standard_args( |
| 170 | + MbedTLS |
| 171 | + REQUIRED_VARS MbedTLS_LIBRARY MbedCrypto_LIBRARY MbedX509_LIBRARY MbedTLS_INCLUDE_DIR |
| 172 | + VERSION_VAR MbedTLS_VERSION REASON_FAILURE_MESSAGE "${MbedTLS_ERROR_REASON}") |
| 173 | + mark_as_advanced(MbedTLS_LIBRARY MbedCrypto_LIBRARY MbedX509_LIBRARY MbedTLS_INCLUDE_DIR) |
| 174 | + list(APPEND _COMPONENTS TLS Crypto X509) |
| 175 | +endif() |
| 176 | +unset(MbedTLS_ERROR_REASON) |
| 177 | + |
| 178 | +if(MbedTLS_FOUND) |
| 179 | + foreach(component IN LISTS _COMPONENTS) |
| 180 | + if(NOT TARGET MbedTLS::Mbed${component}) |
| 181 | + if(IS_ABSOLUTE "${Mbed${component}_LIBRARY}") |
| 182 | + add_library(MbedTLS::Mbed${component} UNKNOWN IMPORTED) |
| 183 | + set_property(TARGET MbedTLS::Mbed${component} PROPERTY IMPORTED_LOCATION "${Mbed${component}_LIBRARY}") |
| 184 | + else() |
| 185 | + add_library(MbedTLS::Mbed${component} INTERFACE IMPORTED) |
| 186 | + set_property(TARGET MbedTLS::Mbed${component} PROPERTY IMPORTED_LIBNAME "${Mbed${component}_LIBRARY}") |
| 187 | + endif() |
| 188 | + |
| 189 | + mbedtls_set_soname(${component}) |
| 190 | + set_target_properties( |
| 191 | + MbedTLS::MbedTLS |
| 192 | + PROPERTIES INTERFACE_COMPILE_OPTIONS "${PC_MbedTLS_CFLAGS_OTHER}" |
| 193 | + INTERFACE_INCLUDE_DIRECTORIES "${MbedTLS_INCLUDE_DIR}" |
| 194 | + VERSION ${MbedTLS_VERSION}) |
| 195 | + endif() |
| 196 | + endforeach() |
| 197 | + |
| 198 | + if(MbedTLS_INCLUDES_X509 AND MbedTLS_INCLUDES_CRYPTO) |
| 199 | + set(MbedTLS_LIBRARIES ${MbedTLS_LIBRARY}) |
| 200 | + set(MBEDTLS_INCLUDE_DIRS ${MbedTLS_INCLUDE_DIR}) |
| 201 | + else() |
| 202 | + set(MbedTLS_LIBRARIES ${MbedTLS_LIBRARY} ${MbedCrypto_LIBRARY} ${MbedX509_LIBRARY}) |
| 203 | + set_property(TARGET MbedTLS::MbedTLS PROPERTY INTERFACE_LINK_LIBRARIES MbedTLS::MbedCrypto MbedTLS::MbedX509) |
| 204 | + set(MBEDTLS_INCLUDE_DIRS ${MbedTLS_INCLUDE_DIR}) |
| 205 | + endif() |
| 206 | +endif() |
| 207 | + |
| 208 | +include(FeatureSummary) |
| 209 | +set_package_properties( |
| 210 | + MbedTLS PROPERTIES |
| 211 | + URL "https://www.trustedfirmware.org/projects/mbed-tls" |
| 212 | + DESCRIPTION |
| 213 | + "A C library implementing cryptographic primitives, X.509 certificate manipulation, and the SSL/TLS and DTLS protocols." |
| 214 | +) |
0 commit comments