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    aesce.c
17    aria.c
18    asn1parse.c
19    asn1write.c
20    base64.c
21    bignum.c
22    bignum_core.c
23    bignum_mod.c
24    bignum_mod_raw.c
25    camellia.c
26    ccm.c
27    chacha20.c
28    chachapoly.c
29    cipher.c
30    cipher_wrap.c
31    constant_time.c
32    cmac.c
33    ctr_drbg.c
34    des.c
35    dhm.c
36    ecdh.c
37    ecdsa.c
38    ecjpake.c
39    ecp.c
40    ecp_curves.c
41    ecp_curves_new.c
42    entropy.c
43    entropy_poll.c
44    error.c
45    gcm.c
46    hkdf.c
47    hmac_drbg.c
48    lmots.c
49    lms.c
50    md.c
51    md5.c
52    memory_buffer_alloc.c
53    nist_kw.c
54    oid.c
55    padlock.c
56    pem.c
57    pk.c
58    pk_wrap.c
59    pkcs12.c
60    pkcs5.c
61    pkparse.c
62    pkwrite.c
63    platform.c
64    platform_util.c
65    poly1305.c
66    psa_crypto.c
67    psa_crypto_aead.c
68    psa_crypto_cipher.c
69    psa_crypto_client.c
70    psa_crypto_driver_wrappers_no_static.c
71    psa_crypto_ecp.c
72    psa_crypto_ffdh.c
73    psa_crypto_hash.c
74    psa_crypto_mac.c
75    psa_crypto_pake.c
76    psa_crypto_rsa.c
77    psa_crypto_se.c
78    psa_crypto_slot_management.c
79    psa_crypto_storage.c
80    psa_its_file.c
81    psa_util.c
82    ripemd160.c
83    rsa.c
84    rsa_alt_helpers.c
85    sha1.c
86    sha256.c
87    sha512.c
88    sha3.c
89    threading.c
90    timing.c
91    version.c
92    version_features.c
93)
94
95set(src_x509
96    pkcs7.c
97    x509.c
98    x509_create.c
99    x509_crl.c
100    x509_crt.c
101    x509_csr.c
102    x509write.c
103    x509write_crt.c
104    x509write_csr.c
105)
106
107set(src_tls
108    debug.c
109    mps_reader.c
110    mps_trace.c
111    net_sockets.c
112    ssl_cache.c
113    ssl_ciphersuites.c
114    ssl_client.c
115    ssl_cookie.c
116    ssl_debug_helpers_generated.c
117    ssl_msg.c
118    ssl_ticket.c
119    ssl_tls.c
120    ssl_tls12_client.c
121    ssl_tls12_server.c
122    ssl_tls13_keys.c
123    ssl_tls13_server.c
124    ssl_tls13_client.c
125    ssl_tls13_generic.c
126)
127
128if(GEN_FILES)
129    find_package(Perl REQUIRED)
130
131    file(GLOB error_headers ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls/*.h)
132    add_custom_command(
133        OUTPUT
134            ${CMAKE_CURRENT_BINARY_DIR}/error.c
135        COMMAND
136            ${PERL_EXECUTABLE}
137                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_errors.pl
138                ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls
139                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files
140                ${CMAKE_CURRENT_BINARY_DIR}/error.c
141        DEPENDS
142            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_errors.pl
143            ${error_headers}
144            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/error.fmt
145    )
146
147    add_custom_command(
148        OUTPUT
149            ${CMAKE_CURRENT_BINARY_DIR}/version_features.c
150        COMMAND
151            ${PERL_EXECUTABLE}
152                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_features.pl
153                ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls
154                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files
155                ${CMAKE_CURRENT_BINARY_DIR}/version_features.c
156        DEPENDS
157            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_features.pl
158            ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls/mbedtls_config.h
159            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/version_features.fmt
160    )
161
162    add_custom_command(
163        OUTPUT
164            ${CMAKE_CURRENT_BINARY_DIR}/ssl_debug_helpers_generated.c
165        COMMAND
166            ${MBEDTLS_PYTHON_EXECUTABLE}
167                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py
168                --mbedtls-root ${CMAKE_CURRENT_SOURCE_DIR}/..
169                ${CMAKE_CURRENT_BINARY_DIR}
170        DEPENDS
171            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py
172            ${error_headers}
173    )
174
175    add_custom_command(
176        OUTPUT
177            ${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers.h
178            ${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers_no_static.c
179        COMMAND
180            ${MBEDTLS_PYTHON_EXECUTABLE}
181                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
182                ${CMAKE_CURRENT_BINARY_DIR}
183        DEPENDS
184            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
185            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.h.jinja
186            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers_no_static.c.jinja
187    )
188
189
190else()
191    link_to_source(error.c)
192    link_to_source(version_features.c)
193    link_to_source(ssl_debug_helpers_generated.c)
194    link_to_source(psa_crypto_driver_wrappers_no_static.c)
195endif()
196
197if(CMAKE_COMPILER_IS_GNUCC)
198    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
199endif(CMAKE_COMPILER_IS_GNUCC)
200
201if(CMAKE_COMPILER_IS_CLANG)
202    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
203endif(CMAKE_COMPILER_IS_CLANG)
204
205if(CMAKE_COMPILER_IS_MSVC)
206    option(MSVC_STATIC_RUNTIME "Build the libraries with /MT compiler flag" OFF)
207    if(MSVC_STATIC_RUNTIME)
208        foreach(flag_var
209            CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
210            CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
211            CMAKE_C_FLAGS_CHECK)
212            string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
213        endforeach(flag_var)
214    endif()
215endif()
216
217if(WIN32)
218    set(libs ${libs} ws2_32 bcrypt)
219endif(WIN32)
220
221if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
222    SET(CMAKE_C_ARCHIVE_CREATE   "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
223    SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
224    SET(CMAKE_C_ARCHIVE_FINISH   "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
225    SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
226endif()
227
228if(HAIKU)
229    set(libs ${libs} network)
230endif(HAIKU)
231
232if(LINK_WITH_PTHREAD)
233    set(libs ${libs} pthread)
234endif()
235
236if(LINK_WITH_TRUSTED_STORAGE)
237    set(libs ${libs} trusted_storage)
238endif()
239
240if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
241    message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
242endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
243
244set(mbedtls_target    "${MBEDTLS_TARGET_PREFIX}mbedtls")
245set(mbedx509_target   "${MBEDTLS_TARGET_PREFIX}mbedx509")
246set(mbedcrypto_target "${MBEDTLS_TARGET_PREFIX}mbedcrypto")
247
248set(mbedtls_target    ${mbedtls_target}    PARENT_SCOPE)
249set(mbedx509_target   ${mbedx509_target}   PARENT_SCOPE)
250set(mbedcrypto_target ${mbedcrypto_target} PARENT_SCOPE)
251
252if (USE_STATIC_MBEDTLS_LIBRARY)
253    set(mbedtls_static_target    ${mbedtls_target})
254    set(mbedx509_static_target   ${mbedx509_target})
255    set(mbedcrypto_static_target ${mbedcrypto_target})
256endif()
257
258set(target_libraries ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
259
260if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
261    string(APPEND mbedtls_static_target    "_static")
262    string(APPEND mbedx509_static_target   "_static")
263    string(APPEND mbedcrypto_static_target "_static")
264
265    list(APPEND target_libraries
266        ${mbedcrypto_static_target}
267        ${mbedx509_static_target}
268        ${mbedtls_static_target})
269endif()
270
271set(p256m_target "${MBEDTLS_TARGET_PREFIX}p256m")
272set(everest_target "${MBEDTLS_TARGET_PREFIX}everest")
273
274if(USE_STATIC_MBEDTLS_LIBRARY)
275    add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
276    set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
277    target_link_libraries(${mbedcrypto_static_target} PUBLIC ${libs})
278
279    if(TARGET ${everest_target})
280        target_link_libraries(${mbedcrypto_static_target} PUBLIC ${everest_target})
281    endif()
282
283    if(TARGET ${p256m_target})
284        target_link_libraries(${mbedcrypto_static_target} PUBLIC ${p256m_target})
285    endif()
286
287    add_library(${mbedx509_static_target} STATIC ${src_x509})
288    set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
289    target_link_libraries(${mbedx509_static_target} PUBLIC ${libs} ${mbedcrypto_static_target})
290
291    add_library(${mbedtls_static_target} STATIC ${src_tls})
292    set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
293    target_link_libraries(${mbedtls_static_target} PUBLIC ${libs} ${mbedx509_static_target})
294endif(USE_STATIC_MBEDTLS_LIBRARY)
295
296if(USE_SHARED_MBEDTLS_LIBRARY)
297    set(CMAKE_LIBRARY_PATH ${CMAKE_CURRENT_BINARY_DIR})
298    add_library(${mbedcrypto_target} SHARED ${src_crypto})
299    set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 3.5.2 SOVERSION 15)
300    target_link_libraries(${mbedcrypto_target} PUBLIC ${libs})
301
302    if(TARGET ${everest_target})
303        target_link_libraries(${mbedcrypto_target} PUBLIC ${everest_target})
304    endif()
305
306    if(TARGET ${p256m_target})
307        target_link_libraries(${mbedcrypto_target} PUBLIC ${p256m_target})
308    endif()
309
310    add_library(${mbedx509_target} SHARED ${src_x509})
311    set_target_properties(${mbedx509_target} PROPERTIES VERSION 3.5.2 SOVERSION 6)
312    target_link_libraries(${mbedx509_target} PUBLIC ${libs} ${mbedcrypto_target})
313
314    add_library(${mbedtls_target} SHARED ${src_tls})
315    set_target_properties(${mbedtls_target} PROPERTIES VERSION 3.5.2 SOVERSION 20)
316    target_link_libraries(${mbedtls_target} PUBLIC ${libs} ${mbedx509_target})
317endif(USE_SHARED_MBEDTLS_LIBRARY)
318
319foreach(target IN LISTS target_libraries)
320    add_library(MbedTLS::${target} ALIAS ${target})  # add_subdirectory support
321    # Include public header files from /include and other directories
322    # declared by /3rdparty/**/CMakeLists.txt. Include private header files
323    # from /library and others declared by /3rdparty/**/CMakeLists.txt.
324    # /library needs to be listed explicitly when building .c files outside
325    # of /library (which currently means: under /3rdparty).
326    target_include_directories(${target}
327        PUBLIC $<BUILD_INTERFACE:${MBEDTLS_DIR}/include/>
328               $<INSTALL_INTERFACE:include/>
329        PRIVATE ${MBEDTLS_DIR}/library/
330                # Needed to include psa_crypto_driver_wrappers.h
331                ${CMAKE_CURRENT_BINARY_DIR})
332    # Pass-through MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE
333    if(MBEDTLS_CONFIG_FILE)
334        target_compile_definitions(${target}
335            PUBLIC MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}")
336    endif()
337    if(MBEDTLS_USER_CONFIG_FILE)
338        target_compile_definitions(${target}
339            PUBLIC MBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}")
340    endif()
341    install(
342        TARGETS ${target}
343        EXPORT MbedTLSTargets
344        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
345        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
346        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
347        PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
348endforeach(target)
349
350set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")
351
352add_custom_target(${lib_target} DEPENDS ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
353if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
354    add_dependencies(${lib_target} ${mbedcrypto_static_target} ${mbedx509_static_target} ${mbedtls_static_target})
355endif()
356