Skip to content

Commit ca9119f

Browse files
committedFeb 20, 2020
Use cmake/module/Findmbedtls.cmake script
1 parent 31b5c5c commit ca9119f

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed
 

‎client-binance.com/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ add_library(${project_name}-lib
2020

2121
## link libs
2222

23+
include(FindPkgConfig)
24+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake/module") # <-- use Findmbedtls.cmake in /cmake/module folder
25+
26+
find_package(mbedtls 2.16.0 REQUIRED)
27+
2328
find_package(oatpp 1.0.0 REQUIRED)
2429
find_package(oatpp-websocket 1.0.0 REQUIRED)
2530
find_package(oatpp-mbedtls 1.0.0 REQUIRED)

‎client-mbedtls/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ add_library(${project_name}-lib
1515

1616
## link libs
1717

18+
include(FindPkgConfig)
19+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake/module") # <-- use Findmbedtls.cmake in /cmake/module folder
20+
21+
find_package(mbedtls 2.16.0 REQUIRED)
22+
1823
find_package(oatpp 1.0.0 REQUIRED)
1924
find_package(oatpp-websocket 1.0.0 REQUIRED)
2025
find_package(oatpp-mbedtls 1.0.0 REQUIRED)

‎cmake/module/Findmbedtls.cmake

+260
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
#[=======================================================================[
2+
3+
Copyright (c) 2020 Benedikt-Alexander Mokroß <bam@icognize.de>
4+
5+
Permission to use, copy, modify, and distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+
17+
FindmbedTLS
18+
------------
19+
20+
Find the mbedTLS encryption library.
21+
22+
Optional Components
23+
^^^^^^^^^^^^^^^^^^^
24+
25+
This module supports two optional components: SSL and TLS. Both
26+
components have associated imported targets, as described below.
27+
28+
Imported Targets
29+
^^^^^^^^^^^^^^^^
30+
31+
This module defines the following imported targets:
32+
33+
mbedtls::Crypto
34+
The mbedTLS crypto library, if found.
35+
36+
mbedtls::X509
37+
The mbedTLS x509 library, if found.
38+
39+
mbedtls::SSL
40+
The mbedTLS ssl library, if found. Requires and includes mbedtls::Crypto automatically.
41+
42+
mbedtls::TLS
43+
The mbedTLS tls library, if found. Requires and includes mbedtls::SSL and mbedtls::Crypto automatically.
44+
45+
Result Variables
46+
^^^^^^^^^^^^^^^^
47+
48+
This module will set the following variables in your project:
49+
50+
MBEDTLS_FOUND
51+
System has the mbedTLS library. If no components are requested it only requires the crypto library.
52+
MBEDTLS_INCLUDE_DIR
53+
The mbedTLS include directory.
54+
MBEDTLS_X509_LIBRARY
55+
The mbedTLS crypto library.
56+
MBEDTLS_CRYPTO_LIBRARY
57+
The mbedTLS crypto library.
58+
MBEDTLS_SSL_LIBRARY
59+
The mbedTLS SSL library.
60+
MBEDTLS_TLS_LIBRARY
61+
The mbedTLS TLS library.
62+
MBEDTLS_LIBRARIES
63+
All mbedTLS libraries.
64+
MBEDTLS_VERSION
65+
This is set to $major.$minor.$revision (e.g. 2.6.8).
66+
67+
Hints
68+
^^^^^
69+
70+
Set MBEDTLS_ROOT_DIR to the root directory of an mbedTLS installation.
71+
72+
]=======================================================================]
73+
74+
# Set Hints
75+
set(_MBEDTLS_ROOT_HINTS
76+
${MBEDTLS_ROOT_DIR}
77+
ENV MBEDTLS_ROOT_DIR
78+
)
79+
80+
# Set Paths
81+
if (WIN32)
82+
file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles)
83+
set(_MBEDTLS_ROOT_PATHS
84+
"${_programfiles}/mbedTLS"
85+
)
86+
unset(_programfiles)
87+
else()
88+
set(_MBEDTLS_ROOT_PATHS
89+
"/usr/local/"
90+
)
91+
endif()
92+
93+
# Combine
94+
set(_MBEDTLS_ROOT_HINTS_AND_PATHS
95+
HINTS ${_MBEDTLS_ROOT_HINTS}
96+
PATHS ${_MBEDTLS_ROOT_PATHS}
97+
)
98+
99+
# Find Include Path
100+
find_path(MBEDTLS_INCLUDE_DIR
101+
NAMES
102+
mbedtls
103+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
104+
PATH_SUFFIXES
105+
include
106+
)
107+
108+
# Find Crypto Library
109+
find_library(MBEDTLS_CRYPTO_LIBRARY
110+
NAMES
111+
libmbedcrypto
112+
mbedcrypto
113+
NAMES_PER_DIR
114+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
115+
PATH_SUFFIXES
116+
lib
117+
)
118+
119+
# Find x509 Library
120+
find_library(MBEDTLS_X509_LIBRARY
121+
NAMES
122+
libmbedx509
123+
mbedx509
124+
NAMES_PER_DIR
125+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
126+
PATH_SUFFIXES
127+
lib
128+
)
129+
130+
# Find SSL Library
131+
find_library(MBEDTLS_SSL_LIBRARY
132+
NAMES
133+
libmbedx509
134+
mbedx509
135+
NAMES_PER_DIR
136+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
137+
PATH_SUFFIXES
138+
lib
139+
)
140+
141+
# Find TLS Library
142+
find_library(MBEDTLS_TLS_LIBRARY
143+
NAMES
144+
libmbedtls
145+
mbedtls
146+
NAMES_PER_DIR
147+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
148+
PATH_SUFFIXES
149+
lib
150+
)
151+
152+
# Set Libraries
153+
set(MBEDTLS_LIBRARIES ${MBEDTLS_CRYPTO_LIBRARY} ${MBEDTLS_X509_LIBRARY} ${MBEDTLS_SSL_LIBRARY} ${MBEDTLS_TLS_LIBRARY})
154+
155+
# Mark Variables As Advanced
156+
mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES MBEDTLS_CRYPTO_LIBRARY MBEDTLS_X509_LIBRARY MBEDTLS_SSL_LIBRARY MBEDTLS_TLS_LIBRARY)
157+
158+
# Find Version File
159+
if(MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
160+
161+
# Get Version From File
162+
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h" VERSIONH REGEX "#define MBEDTLS_VERSION_STRING[ ]+\".*\"")
163+
164+
# Match Version String
165+
string(REGEX REPLACE ".*\".*([0-9]+)\\.([0-9]+)\\.([0-9]+)\"" "\\1;\\2;\\3" MBEDTLS_VERSION_LIST "${VERSIONH}")
166+
167+
# Split Parts
168+
list(GET MBEDTLS_VERSION_LIST 0 MBEDTLS_VERSION_MAJOR)
169+
list(GET MBEDTLS_VERSION_LIST 1 MBEDTLS_VERSION_MINOR)
170+
list(GET MBEDTLS_VERSION_LIST 2 MBEDTLS_VERSION_REVISION)
171+
172+
# Set Version String
173+
set(MBEDTLS_VERSION "${MBEDTLS_VERSION_MAJOR}.${MBEDTLS_VERSION_MINOR}.${MBEDTLS_VERSION_REVISION}")
174+
175+
endif()
176+
177+
# Set Find Package Arguments
178+
find_package_handle_standard_args(mbedTLS
179+
REQUIRED_VARS
180+
MBEDTLS_X509_LIBRARY
181+
MBEDTLS_TLS_LIBRARY
182+
MBEDTLS_SSL_LIBRARY
183+
MBEDTLS_INCLUDE_DIR
184+
VERSION_VAR
185+
MBEDTLS_VERSION
186+
HANDLE_COMPONENTS
187+
FAIL_MESSAGE
188+
"Could NOT find mbedTLS, try setting the path to mbedTLS using the MBEDTLS_ROOT_DIR environment variable"
189+
)
190+
191+
# mbedTLS Found
192+
if(MBEDTLS_FOUND)
193+
194+
# Set mbedtls::Crypto
195+
if(NOT TARGET mbedtls::Crypto AND EXISTS "${MBEDTLS_CRYPTO_LIBRARY}")
196+
197+
# Add Library
198+
add_library(mbedtls::Crypto UNKNOWN IMPORTED)
199+
200+
# Set Properties
201+
set_target_properties(
202+
mbedtls::Crypto
203+
PROPERTIES
204+
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
205+
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
206+
IMPORTED_LOCATION "${MBEDTLS_CRYPTO_LIBRARY}"
207+
)
208+
209+
endif() # mbedtls::Crypto
210+
211+
# Set mbedtls::X509
212+
if(NOT TARGET mbedtls::X509 AND EXISTS "${MBEDTLS_X509_LIBRARY}")
213+
214+
# Add Library
215+
add_library(mbedtls::X509 UNKNOWN IMPORTED)
216+
217+
# Set Properties
218+
set_target_properties(
219+
mbedtls::X509
220+
PROPERTIES
221+
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
222+
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
223+
IMPORTED_LOCATION "${MBEDTLS_X509_LIBRARY}"
224+
)
225+
226+
endif() # mbedtls::X509
227+
228+
# Set mbedtls::SSL
229+
if(NOT TARGET mbedtls::SSL AND EXISTS "${MBEDTLS_SSL_LIBRARY}")
230+
231+
# Add Library
232+
add_library(mbedtls::SSL UNKNOWN IMPORTED)
233+
234+
# Set Properties
235+
set_target_properties(
236+
mbedtls::SSL
237+
PROPERTIES
238+
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
239+
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
240+
IMPORTED_LOCATION "${MBEDTLS_SSL_LIBRARY}"
241+
INTERFACE_LINK_LIBRARIES mbedtls::Crypto
242+
)
243+
244+
endif() # mbedtls::SSL
245+
246+
# Set mbedtls::TLS
247+
if(NOT TARGET mbedtls::TLS AND EXISTS "${MBEDTLS_TLS_LIBRARY}")
248+
add_library(mbedtls::TLS UNKNOWN IMPORTED)
249+
set_target_properties(
250+
mbedtls::TLS
251+
PROPERTIES
252+
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
253+
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
254+
IMPORTED_LOCATION "${MBEDTLS_TLS_LIBRARY}"
255+
INTERFACE_LINK_LIBRARIES mbedtls::SSL
256+
)
257+
258+
endif() # mbedtls::TLS
259+
260+
endif(MBEDTLS_FOUND)

0 commit comments

Comments
 (0)
Please sign in to comment.