• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON)
2option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF)
3option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF)
4option(LINK_WITH_TRUSTED_STORAGE "Explicitly link mbed TLS library to trusted_storage." OFF)
5
6# Set the project root directory if it's not already defined, as may happen if
7# the library folder is included directly by a parent project, without
8# including the top level CMakeLists.txt.
9if(NOT DEFINED MBEDTLS_DIR)
10    set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
11endif()
12
13set(src_crypto
14    aes.c
15    aesni.c
16    aria.c
17    asn1parse.c
18    asn1write.c
19    base64.c
20    bignum.c
21    camellia.c
22    ccm.c
23    chacha20.c
24    chachapoly.c
25    cipher.c
26    cipher_wrap.c
27    constant_time.c
28    cmac.c
29    ctr_drbg.c
30    des.c
31    dhm.c
32    ecdh.c
33    ecdsa.c
34    ecjpake.c
35    ecp.c
36    ecp_curves.c
37    entropy.c
38    entropy_poll.c
39    error.c
40    gcm.c
41    hkdf.c
42    hmac_drbg.c
43    md.c
44    md5.c
45    memory_buffer_alloc.c
46    mps_reader.c
47    mps_trace.c
48    nist_kw.c
49    oid.c
50    padlock.c
51    pem.c
52    pk.c
53    pk_wrap.c
54    pkcs12.c
55    pkcs5.c
56    pkparse.c
57    pkwrite.c
58    platform.c
59    platform_util.c
60    poly1305.c
61    psa_crypto.c
62    psa_crypto_aead.c
63    psa_crypto_cipher.c
64    psa_crypto_client.c
65    psa_crypto_driver_wrappers.c
66    psa_crypto_ecp.c
67    psa_crypto_hash.c
68    psa_crypto_mac.c
69    psa_crypto_rsa.c
70    psa_crypto_se.c
71    psa_crypto_slot_management.c
72    psa_crypto_storage.c
73    psa_its_file.c
74    ripemd160.c
75    rsa.c
76    rsa_alt_helpers.c
77    sha1.c
78    sha256.c
79    sha512.c
80    ssl_debug_helpers_generated.c
81    threading.c
82    timing.c
83    version.c
84    version_features.c
85)
86
87set(src_x509
88    x509.c
89    x509_create.c
90    x509_crl.c
91    x509_crt.c
92    x509_csr.c
93    x509write_crt.c
94    x509write_csr.c
95)
96
97set(src_tls
98    debug.c
99    net_sockets.c
100    ssl_cache.c
101    ssl_ciphersuites.c
102    ssl_cli.c
103    ssl_cookie.c
104    ssl_msg.c
105    ssl_srv.c
106    ssl_ticket.c
107    ssl_tls.c
108    ssl_tls13_keys.c
109    ssl_tls13_server.c
110    ssl_tls13_client.c
111    ssl_tls13_generic.c
112)
113
114if(GEN_FILES)
115    find_package(Perl REQUIRED)
116
117    file(GLOB error_headers ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls/*.h)
118    add_custom_command(
119        OUTPUT
120            ${CMAKE_CURRENT_BINARY_DIR}/error.c
121        COMMAND
122            ${PERL_EXECUTABLE}
123                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_errors.pl
124                ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls
125                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files
126                ${CMAKE_CURRENT_BINARY_DIR}/error.c
127        DEPENDS
128            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_errors.pl
129            ${error_headers}
130            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/error.fmt
131    )
132
133    add_custom_command(
134        OUTPUT
135            ${CMAKE_CURRENT_BINARY_DIR}/version_features.c
136        COMMAND
137            ${PERL_EXECUTABLE}
138                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_features.pl
139                ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls
140                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files
141                ${CMAKE_CURRENT_BINARY_DIR}/version_features.c
142        DEPENDS
143            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_features.pl
144            ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls/mbedtls_config.h
145            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/version_features.fmt
146    )
147
148    add_custom_command(
149        OUTPUT
150            ${CMAKE_CURRENT_BINARY_DIR}/ssl_debug_helpers_generated.h
151            ${CMAKE_CURRENT_BINARY_DIR}/ssl_debug_helpers_generated.c
152        COMMAND
153            ${MBEDTLS_PYTHON_EXECUTABLE}
154                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py
155                --mbedtls-root ${CMAKE_CURRENT_SOURCE_DIR}/..
156                ${CMAKE_CURRENT_BINARY_DIR}
157        DEPENDS
158            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py
159            ${error_headers}
160    )
161else()
162    link_to_source(error.c)
163    link_to_source(version_features.c)
164    link_to_source(ssl_debug_helpers_generated.c)
165    link_to_source(ssl_debug_helpers_generated.h)
166endif()
167
168if(CMAKE_COMPILER_IS_GNUCC)
169    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
170endif(CMAKE_COMPILER_IS_GNUCC)
171
172if(CMAKE_COMPILER_IS_CLANG)
173    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
174endif(CMAKE_COMPILER_IS_CLANG)
175
176if(WIN32)
177    set(libs ${libs} ws2_32)
178endif(WIN32)
179
180if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
181    SET(CMAKE_C_ARCHIVE_CREATE   "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
182    SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
183    SET(CMAKE_C_ARCHIVE_FINISH   "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
184    SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
185endif()
186
187if(HAIKU)
188    set(libs ${libs} network)
189endif(HAIKU)
190
191if(LINK_WITH_PTHREAD)
192    set(libs ${libs} pthread)
193endif()
194
195if(LINK_WITH_TRUSTED_STORAGE)
196    set(libs ${libs} trusted_storage)
197endif()
198
199if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
200    message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
201endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
202
203set(mbedtls_target    "${MBEDTLS_TARGET_PREFIX}mbedtls")
204set(mbedx509_target   "${MBEDTLS_TARGET_PREFIX}mbedx509")
205set(mbedcrypto_target "${MBEDTLS_TARGET_PREFIX}mbedcrypto")
206
207set(mbedtls_target    ${mbedtls_target}    PARENT_SCOPE)
208set(mbedx509_target   ${mbedx509_target}   PARENT_SCOPE)
209set(mbedcrypto_target ${mbedcrypto_target} PARENT_SCOPE)
210
211if (USE_STATIC_MBEDTLS_LIBRARY)
212    set(mbedtls_static_target    ${mbedtls_target})
213    set(mbedx509_static_target   ${mbedx509_target})
214    set(mbedcrypto_static_target ${mbedcrypto_target})
215endif()
216
217set(target_libraries ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
218
219if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
220    string(APPEND mbedtls_static_target    "_static")
221    string(APPEND mbedx509_static_target   "_static")
222    string(APPEND mbedcrypto_static_target "_static")
223
224    list(APPEND target_libraries
225        ${mbedcrypto_static_target}
226        ${mbedx509_static_target}
227        ${mbedtls_static_target})
228endif()
229
230if(USE_STATIC_MBEDTLS_LIBRARY)
231    add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
232    set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
233    target_link_libraries(${mbedcrypto_static_target} PUBLIC ${libs})
234
235    if(TARGET everest)
236        target_link_libraries(${mbedcrypto_static_target} PUBLIC everest)
237    endif()
238
239    add_library(${mbedx509_static_target} STATIC ${src_x509})
240    set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
241    target_link_libraries(${mbedx509_static_target} PUBLIC ${libs} ${mbedcrypto_static_target})
242
243    add_library(${mbedtls_static_target} STATIC ${src_tls})
244    set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
245    target_link_libraries(${mbedtls_static_target} PUBLIC ${libs} ${mbedx509_static_target})
246endif(USE_STATIC_MBEDTLS_LIBRARY)
247
248if(USE_SHARED_MBEDTLS_LIBRARY)
249    add_library(${mbedcrypto_target} SHARED ${src_crypto})
250    set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 3.1.0 SOVERSION 11)
251    target_link_libraries(${mbedcrypto_target} PUBLIC ${libs})
252
253    if(TARGET everest)
254        target_link_libraries(${mbedcrypto_target} PUBLIC everest)
255    endif()
256
257    add_library(${mbedx509_target} SHARED ${src_x509})
258    set_target_properties(${mbedx509_target} PROPERTIES VERSION 3.1.0 SOVERSION 4)
259    target_link_libraries(${mbedx509_target} PUBLIC ${libs} ${mbedcrypto_target})
260
261    add_library(${mbedtls_target} SHARED ${src_tls})
262    set_target_properties(${mbedtls_target} PROPERTIES VERSION 3.1.0 SOVERSION 17)
263    target_link_libraries(${mbedtls_target} PUBLIC ${libs} ${mbedx509_target})
264endif(USE_SHARED_MBEDTLS_LIBRARY)
265
266foreach(target IN LISTS target_libraries)
267    # Include public header files from /include and other directories
268    # declared by /3rdparty/**/CMakeLists.txt. Include private header files
269    # from /library and others declared by /3rdparty/**/CMakeLists.txt.
270    # /library needs to be listed explicitly when building .c files outside
271    # of /library (which currently means: under /3rdparty).
272    target_include_directories(${target}
273        PUBLIC $<BUILD_INTERFACE:${MBEDTLS_DIR}/include/>
274               $<INSTALL_INTERFACE:include/>
275        PRIVATE ${MBEDTLS_DIR}/library/)
276    install(
277        TARGETS ${target}
278        EXPORT MbedTLSTargets
279        DESTINATION ${LIB_INSTALL_DIR}
280        PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
281endforeach(target)
282
283set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")
284
285add_custom_target(${lib_target} DEPENDS ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
286if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
287    add_dependencies(${lib_target} ${mbedcrypto_static_target} ${mbedx509_static_target} ${mbedtls_static_target})
288endif()
289