xref: /curl/CMake/FindMbedTLS.cmake (revision 3e60f174)
1#***************************************************************************
2#                                  _   _ ____  _
3#  Project                     ___| | | |  _ \| |
4#                             / __| | | | |_) | |
5#                            | (__| |_| |  _ <| |___
6#                             \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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_INCLUDE_DIRS  The mbedtls include directory (deprecated)
30# MBEDTLS_LIBRARY       Path to mbedtls library
31# MBEDX509_LIBRARY      Path to mbedx509 library
32# MBEDCRYPTO_LIBRARY    Path to mbedcrypto library
33#
34# Result variables:
35#
36# MBEDTLS_FOUND         System has mbedtls
37# MBEDTLS_INCLUDE_DIRS  The mbedtls include directories
38# MBEDTLS_LIBRARIES     The mbedtls library names
39# MBEDTLS_VERSION       Version of mbedtls
40
41if(DEFINED MBEDTLS_INCLUDE_DIRS AND NOT DEFINED MBEDTLS_INCLUDE_DIR)
42  message(WARNING "MBEDTLS_INCLUDE_DIRS is deprecated, use MBEDTLS_INCLUDE_DIR instead.")
43  set(MBEDTLS_INCLUDE_DIR "${MBEDTLS_INCLUDE_DIRS}")
44  unset(MBEDTLS_INCLUDE_DIRS)
45endif()
46
47if(CURL_USE_PKGCONFIG)
48  find_package(PkgConfig QUIET)
49  pkg_check_modules(PC_MBEDTLS "mbedtls")
50endif()
51
52find_path(MBEDTLS_INCLUDE_DIR NAMES "mbedtls/ssl.h"
53  HINTS
54    ${PC_MBEDTLS_INCLUDEDIR}
55    ${PC_MBEDTLS_INCLUDE_DIRS}
56)
57
58find_library(MBEDTLS_LIBRARY NAMES "mbedtls"
59  HINTS
60    ${PC_MBEDTLS_LIBDIR}
61    ${PC_MBEDTLS_LIBRARY_DIRS}
62)
63find_library(MBEDX509_LIBRARY NAMES "mbedx509"
64  HINTS
65    ${PC_MBEDTLS_LIBDIR}
66    ${PC_MBEDTLS_LIBRARY_DIRS}
67)
68find_library(MBEDCRYPTO_LIBRARY NAMES "mbedcrypto"
69  HINTS
70    ${PC_MBEDTLS_LIBDIR}
71    ${PC_MBEDTLS_LIBRARY_DIRS}
72)
73
74if(PC_MBEDTLS_VERSION)
75  set(MBEDTLS_VERSION ${PC_MBEDTLS_VERSION})
76elseif(MBEDTLS_INCLUDE_DIR)
77  if(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")  # 3.x
78    set(_version_header "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
79  elseif(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")  # 2.x
80    set(_version_header "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
81  else()
82    unset(_version_header)
83  endif()
84  if(_version_header)
85    set(_version_regex "#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"([0-9.]+)\"")
86    file(STRINGS "${_version_header}" _version_str REGEX "${_version_regex}")
87    string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}")
88    set(MBEDTLS_VERSION "${_version_str}")
89    unset(_version_regex)
90    unset(_version_str)
91    unset(_version_header)
92  endif()
93endif()
94
95include(FindPackageHandleStandardArgs)
96find_package_handle_standard_args(MbedTLS
97  REQUIRED_VARS
98    MBEDTLS_INCLUDE_DIR
99    MBEDTLS_LIBRARY
100    MBEDX509_LIBRARY
101    MBEDCRYPTO_LIBRARY
102  VERSION_VAR
103    MBEDTLS_VERSION
104)
105
106if(MBEDTLS_FOUND)
107  set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
108  set(MBEDTLS_LIBRARIES    ${MBEDTLS_LIBRARY} ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY})
109endif()
110
111mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
112