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    entropy.c
42    entropy_poll.c
43    error.c
44    gcm.c
45    hash_info.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.c
71    psa_crypto_ecp.c
72    psa_crypto_hash.c
73    psa_crypto_mac.c
74    psa_crypto_pake.c
75    psa_crypto_rsa.c
76    psa_crypto_se.c
77    psa_crypto_slot_management.c
78    psa_crypto_storage.c
79    psa_its_file.c
80    psa_util.c
81    ripemd160.c
82    rsa.c
83    rsa_alt_helpers.c
84    sha1.c
85    sha256.c
86    sha512.c
87    threading.c
88    timing.c
89    version.c
90    version_features.c
91)
92
93set(src_x509
94    pkcs7.c
95    x509.c
96    x509_create.c
97    x509_crl.c
98    x509_crt.c
99    x509_csr.c
100    x509write_crt.c
101    x509write_csr.c
102)
103
104set(src_tls
105    debug.c
106    mps_reader.c
107    mps_trace.c
108    net_sockets.c
109    ssl_cache.c
110    ssl_ciphersuites.c
111    ssl_client.c
112    ssl_cookie.c
113    ssl_debug_helpers_generated.c
114    ssl_msg.c
115    ssl_ticket.c
116    ssl_tls.c
117    ssl_tls12_client.c
118    ssl_tls12_server.c
119    ssl_tls13_keys.c
120    ssl_tls13_server.c
121    ssl_tls13_client.c
122    ssl_tls13_generic.c
123)
124
125if(GEN_FILES)
126    find_package(Perl REQUIRED)
127
128    file(GLOB error_headers ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls/*.h)
129    add_custom_command(
130        OUTPUT
131            ${CMAKE_CURRENT_BINARY_DIR}/error.c
132        COMMAND
133            ${PERL_EXECUTABLE}
134                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_errors.pl
135                ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls
136                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files
137                ${CMAKE_CURRENT_BINARY_DIR}/error.c
138        DEPENDS
139            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_errors.pl
140            ${error_headers}
141            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/error.fmt
142    )
143
144    add_custom_command(
145        OUTPUT
146            ${CMAKE_CURRENT_BINARY_DIR}/version_features.c
147        COMMAND
148            ${PERL_EXECUTABLE}
149                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_features.pl
150                ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls
151                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files
152                ${CMAKE_CURRENT_BINARY_DIR}/version_features.c
153        DEPENDS
154            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_features.pl
155            ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls/mbedtls_config.h
156            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/version_features.fmt
157    )
158
159    add_custom_command(
160        OUTPUT
161            ${CMAKE_CURRENT_BINARY_DIR}/ssl_debug_helpers_generated.c
162        COMMAND
163            ${MBEDTLS_PYTHON_EXECUTABLE}
164                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py
165                --mbedtls-root ${CMAKE_CURRENT_SOURCE_DIR}/..
166                ${CMAKE_CURRENT_BINARY_DIR}
167        DEPENDS
168            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py
169            ${error_headers}
170    )
171
172    add_custom_command(
173        OUTPUT
174            ${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers.c
175        COMMAND
176            ${MBEDTLS_PYTHON_EXECUTABLE}
177                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
178                ${CMAKE_CURRENT_BINARY_DIR}
179        DEPENDS
180            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
181            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja
182    )
183
184
185else()
186    link_to_source(error.c)
187    link_to_source(version_features.c)
188    link_to_source(ssl_debug_helpers_generated.c)
189    link_to_source(psa_crypto_driver_wrappers.c)
190endif()
191
192if(CMAKE_COMPILER_IS_GNUCC)
193    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
194endif(CMAKE_COMPILER_IS_GNUCC)
195
196if(CMAKE_COMPILER_IS_CLANG)
197    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
198endif(CMAKE_COMPILER_IS_CLANG)
199
200if(CMAKE_COMPILER_IS_MSVC)
201    option(MSVC_STATIC_RUNTIME "Build the libraries with /MT compiler flag" OFF)
202    if(MSVC_STATIC_RUNTIME)
203        foreach(flag_var
204            CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
205            CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
206            CMAKE_C_FLAGS_CHECK)
207            string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
208        endforeach(flag_var)
209    endif()
210endif()
211
212if(WIN32)
213    set(libs ${libs} ws2_32)
214endif(WIN32)
215
216if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
217    SET(CMAKE_C_ARCHIVE_CREATE   "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
218    SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
219    SET(CMAKE_C_ARCHIVE_FINISH   "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
220    SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
221endif()
222
223if(HAIKU)
224    set(libs ${libs} network)
225endif(HAIKU)
226
227if(LINK_WITH_PTHREAD)
228    set(libs ${libs} pthread)
229endif()
230
231if(LINK_WITH_TRUSTED_STORAGE)
232    set(libs ${libs} trusted_storage)
233endif()
234
235if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
236    message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
237endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
238
239set(mbedtls_target    "${MBEDTLS_TARGET_PREFIX}mbedtls")
240set(mbedx509_target   "${MBEDTLS_TARGET_PREFIX}mbedx509")
241set(mbedcrypto_target "${MBEDTLS_TARGET_PREFIX}mbedcrypto")
242
243set(mbedtls_target    ${mbedtls_target}    PARENT_SCOPE)
244set(mbedx509_target   ${mbedx509_target}   PARENT_SCOPE)
245set(mbedcrypto_target ${mbedcrypto_target} PARENT_SCOPE)
246
247if (USE_STATIC_MBEDTLS_LIBRARY)
248    set(mbedtls_static_target    ${mbedtls_target})
249    set(mbedx509_static_target   ${mbedx509_target})
250    set(mbedcrypto_static_target ${mbedcrypto_target})
251endif()
252
253set(target_libraries ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
254
255if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
256    string(APPEND mbedtls_static_target    "_static")
257    string(APPEND mbedx509_static_target   "_static")
258    string(APPEND mbedcrypto_static_target "_static")
259
260    list(APPEND target_libraries
261        ${mbedcrypto_static_target}
262        ${mbedx509_static_target}
263        ${mbedtls_static_target})
264endif()
265
266if(USE_STATIC_MBEDTLS_LIBRARY)
267    add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
268    set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
269    target_link_libraries(${mbedcrypto_static_target} PUBLIC ${libs})
270
271    if(TARGET everest)
272        target_link_libraries(${mbedcrypto_static_target} PUBLIC everest)
273    endif()
274
275    add_library(${mbedx509_static_target} STATIC ${src_x509})
276    set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
277    target_link_libraries(${mbedx509_static_target} PUBLIC ${libs} ${mbedcrypto_static_target})
278
279    add_library(${mbedtls_static_target} STATIC ${src_tls})
280    set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
281    target_link_libraries(${mbedtls_static_target} PUBLIC ${libs} ${mbedx509_static_target})
282endif(USE_STATIC_MBEDTLS_LIBRARY)
283
284if(USE_SHARED_MBEDTLS_LIBRARY)
285    set(CMAKE_LIBRARY_PATH ${CMAKE_CURRENT_BINARY_DIR})
286    add_library(${mbedcrypto_target} SHARED ${src_crypto})
287    set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 3.4.0 SOVERSION 14)
288    target_link_libraries(${mbedcrypto_target} PUBLIC ${libs})
289
290    if(TARGET everest)
291        target_link_libraries(${mbedcrypto_target} PUBLIC everest)
292    endif()
293
294    add_library(${mbedx509_target} SHARED ${src_x509})
295    set_target_properties(${mbedx509_target} PROPERTIES VERSION 3.4.0 SOVERSION 5)
296    target_link_libraries(${mbedx509_target} PUBLIC ${libs} ${mbedcrypto_target})
297
298    add_library(${mbedtls_target} SHARED ${src_tls})
299    set_target_properties(${mbedtls_target} PROPERTIES VERSION 3.4.0 SOVERSION 19)
300    target_link_libraries(${mbedtls_target} PUBLIC ${libs} ${mbedx509_target})
301endif(USE_SHARED_MBEDTLS_LIBRARY)
302
303foreach(target IN LISTS target_libraries)
304    add_library(MbedTLS::${target} ALIAS ${target})  # add_subdirectory support
305    # Include public header files from /include and other directories
306    # declared by /3rdparty/**/CMakeLists.txt. Include private header files
307    # from /library and others declared by /3rdparty/**/CMakeLists.txt.
308    # /library needs to be listed explicitly when building .c files outside
309    # of /library (which currently means: under /3rdparty).
310    target_include_directories(${target}
311        PUBLIC $<BUILD_INTERFACE:${MBEDTLS_DIR}/include/>
312               $<INSTALL_INTERFACE:include/>
313        PRIVATE ${MBEDTLS_DIR}/library/)
314    install(
315        TARGETS ${target}
316        EXPORT MbedTLSTargets
317        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
318        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
319        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
320        PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
321endforeach(target)
322
323set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")
324
325add_custom_target(${lib_target} DEPENDS ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
326if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
327    add_dependencies(${lib_target} ${mbedcrypto_static_target} ${mbedx509_static_target} ${mbedtls_static_target})
328endif()
329