Debian Codegraph @

cmake/Modules/FindMbedTLS.cmake

raw
1 # - Try to find mbedTLS
2 # Once done this will define
3 #
4 # MBEDTLS_FOUND - system has mbedTLS
5 # MBEDTLS_INCLUDE_DIRS - the mbedTLS include directory
6 # MBEDTLS_LIBRARIES - Link these to use mbedTLS
7 # MBEDTLS_DEFINITIONS - Compiler switches required for using mbedTLS
8 #=============================================================================
9 # Copyright (c) 2017 Sartura d.o.o.
10 #
11 # Author: Juraj Vijtiuk <[email protected]>
12 #
13 # Distributed under the OSI-approved BSD License (the "License");
14 # see accompanying file Copyright.txt for details.
15 #
16 # This software is distributed WITHOUT ANY WARRANTY; without even the
17 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 # See the License for more information.
19 #=============================================================================
20 #
21
22
23 set(_MBEDTLS_ROOT_HINTS
24 $ENV{MBEDTLS_ROOT_DIR}
25 ${MBEDTLS_ROOT_DIR})
26
27 set(_MBEDTLS_ROOT_PATHS
28 "$ENV{PROGRAMFILES}/libmbedtls")
29
30 set(_MBEDTLS_ROOT_HINTS_AND_PATHS
31 HINTS ${_MBEDTLS_ROOT_HINTS}
32 PATHS ${_MBEDTLS_ROOT_PATHS})
33
34
35 find_path(MBEDTLS_INCLUDE_DIR
36 NAMES
37 mbedtls/ssl.h
38 HINTS
39 ${_MBEDTLS_ROOT_HINTS_AND_PATHS}
40 PATH_SUFFIXES
41 include
42 )
43
44 find_library(MBEDTLS_SSL_LIBRARY
45 NAMES
46 mbedtls
47 HINTS
48 ${_MBEDTLS_ROOT_HINTS_AND_PATHS}
49 PATH_SUFFIXES
50 lib
51
52 )
53
54 find_library(MBEDTLS_CRYPTO_LIBRARY
55 NAMES
56 mbedcrypto
57 HINTS
58 ${_MBEDTLS_ROOT_HINTS_AND_PATHS}
59 PATH_SUFFIXES
60 lib
61 )
62
63 find_library(MBEDTLS_X509_LIBRARY
64 NAMES
65 mbedx509
66 HINTS
67 ${_MBEDTLS_ROOT_HINTS_AND_PATHS}
68 PATH_SUFFIXES
69 lib
70 )
71
72 set(MBEDTLS_LIBRARIES ${MBEDTLS_SSL_LIBRARY} ${MBEDTLS_CRYPTO_LIBRARY}
73 ${MBEDTLS_X509_LIBRARY})
74
75 # mbedtls 2.8
76 if (MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
77 file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h" _mbedtls_version_str REGEX
78 "^#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"[0-9]+.[0-9]+.[0-9]+\"")
79
80 string(REGEX REPLACE "^.*MBEDTLS_VERSION_STRING.*([0-9]+\\.[0-9]+\\.[0-9]+).*$"
81 "\\1" MBEDTLS_VERSION "${_mbedtls_version_str}")
82 endif()
83
84 # mbedtls 3.6
85 if (NOT MBEDTLS_VERSION AND MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
86 file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h" _mbedtls_version_str REGEX
87 "^#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"[0-9]+.[0-9]+.[0-9]+\"")
88
89 string(REGEX REPLACE "^.*MBEDTLS_VERSION_STRING.*([0-9]+\\.[0-9]+\\.[0-9]+).*$"
90 "\\1" MBEDTLS_VERSION "${_mbedtls_version_str}")
91 endif()
92
93 include(FindPackageHandleStandardArgs)
94 if (MBEDTLS_VERSION)
95 find_package_handle_standard_args(MbedTLS
96 REQUIRED_VARS
97 MBEDTLS_INCLUDE_DIR
98 MBEDTLS_LIBRARIES
99 VERSION_VAR
100 MBEDTLS_VERSION
101 FAIL_MESSAGE
102 "Could NOT find mbedTLS, try to set the path to mbedTLS root folder
103 in the system variable MBEDTLS_ROOT_DIR"
104 )
105 else (MBEDTLS_VERSION)
106 find_package_handle_standard_args(MbedTLS
107 "Could NOT find mbedTLS, try to set the path to mbedTLS root folder in
108 the system variable MBEDTLS_ROOT_DIR"
109 MBEDTLS_INCLUDE_DIR
110 MBEDTLS_LIBRARIES)
111 endif (MBEDTLS_VERSION)
112
113 # show the MBEDTLS_INCLUDE_DIRS and MBEDTLS_LIBRARIES variables only in the advanced view
114 mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES)
115
116 if(MBEDTLS_FOUND)
117 if(NOT TARGET MbedTLS::mbedcrypto)
118 add_library(MbedTLS::mbedcrypto UNKNOWN IMPORTED)
119 set_target_properties(MbedTLS::mbedcrypto PROPERTIES
120 INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
121 INTERFACE_LINK_LIBRARIES MbedTLS::mbedcrypto
122 IMPORTED_LINK_INTERFACE_LANGUAGES "C"
123 IMPORTED_LOCATION "${MBEDTLS_CRYPTO_LIBRARY}")
124 endif()
125
126 if(NOT TARGET MbedTLS::mbedx509)
127 add_library(MbedTLS::mbedx509 UNKNOWN IMPORTED)
128 set_target_properties(MbedTLS::mbedx509 PROPERTIES
129 INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
130 INTERFACE_LINK_LIBRARIES MbedTLS::mbedx509
131 IMPORTED_LINK_INTERFACE_LANGUAGES "C"
132 IMPORTED_LOCATION "${MBEDTLS_X509_LIBRARY}")
133 endif()
134
135 if(NOT TARGET MbedTLS::mbedtls)
136 add_library(MbedTLS::mbedtls UNKNOWN IMPORTED)
137 set_target_properties(MbedTLS::mbedtls PROPERTIES
138 INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
139 INTERFACE_LINK_LIBRARIES MbedTLS::mbedtls
140 IMPORTED_LINK_INTERFACE_LANGUAGES "C"
141 IMPORTED_LOCATION "${MBEDTLS_LIBRARY}")
142 endif()
143 endif()