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