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