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    arc4.c
17    aria.c
18    asn1parse.c
19    asn1write.c
20    base64.c
21    bignum.c
22    blowfish.c
23    camellia.c
24    ccm.c
25    chacha20.c
26    chachapoly.c
27    cipher.c
28    cipher_wrap.c
29    constant_time.c
30    cmac.c
31    ctr_drbg.c
32    des.c
33    dhm.c
34    ecdh.c
35    ecdsa.c
36    ecjpake.c
37    ecp.c
38    ecp_curves.c
39    entropy.c
40    entropy_poll.c
41    error.c
42    gcm.c
43    havege.c
44    hkdf.c
45    hmac_drbg.c
46    md.c
47    md2.c
48    md4.c
49    md5.c
50    memory_buffer_alloc.c
51    mps_reader.c
52    mps_trace.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_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_internal.c
82    sha1.c
83    sha256.c
84    sha512.c
85    threading.c
86    timing.c
87    version.c
88    version_features.c
89    xtea.c
90)
91
92list(APPEND src_crypto ${thirdparty_src})
93
94set(src_x509
95    certs.c
96    pkcs11.c
97    x509.c
98    x509_create.c
99    x509_crl.c
100    x509_crt.c
101    x509_csr.c
102    x509write_crt.c
103    x509write_csr.c
104)
105
106set(src_tls
107    debug.c
108    net_sockets.c
109    ssl_cache.c
110    ssl_ciphersuites.c
111    ssl_cli.c
112    ssl_cookie.c
113    ssl_msg.c
114    ssl_srv.c
115    ssl_ticket.c
116    ssl_tls.c
117    ssl_tls13_keys.c
118)
119
120if(CMAKE_COMPILER_IS_GNUCC)
121    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
122endif(CMAKE_COMPILER_IS_GNUCC)
123
124if(CMAKE_COMPILER_IS_CLANG)
125    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
126endif(CMAKE_COMPILER_IS_CLANG)
127
128if(WIN32)
129    set(libs ${libs} ws2_32)
130endif(WIN32)
131
132if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
133    SET(CMAKE_C_ARCHIVE_CREATE   "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
134    SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
135    SET(CMAKE_C_ARCHIVE_FINISH   "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
136    SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
137endif()
138
139if(HAIKU)
140    set(libs ${libs} network)
141endif(HAIKU)
142
143if(USE_PKCS11_HELPER_LIBRARY)
144    set(libs ${libs} pkcs11-helper)
145endif(USE_PKCS11_HELPER_LIBRARY)
146
147if(ENABLE_ZLIB_SUPPORT)
148    set(libs ${libs} ${ZLIB_LIBRARIES})
149endif(ENABLE_ZLIB_SUPPORT)
150
151if(LINK_WITH_PTHREAD)
152    set(libs ${libs} pthread)
153endif()
154
155if(LINK_WITH_TRUSTED_STORAGE)
156    set(libs ${libs} trusted_storage)
157endif()
158
159if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
160    message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
161endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
162
163set(mbedtls_target    "${MBEDTLS_TARGET_PREFIX}mbedtls")
164set(mbedx509_target   "${MBEDTLS_TARGET_PREFIX}mbedx509")
165set(mbedcrypto_target "${MBEDTLS_TARGET_PREFIX}mbedcrypto")
166
167set(mbedtls_target    ${mbedtls_target}    PARENT_SCOPE)
168set(mbedx509_target   ${mbedx509_target}   PARENT_SCOPE)
169set(mbedcrypto_target ${mbedcrypto_target} PARENT_SCOPE)
170
171if (USE_STATIC_MBEDTLS_LIBRARY)
172    set(mbedtls_static_target    ${mbedtls_target})
173    set(mbedx509_static_target   ${mbedx509_target})
174    set(mbedcrypto_static_target ${mbedcrypto_target})
175endif()
176
177set(target_libraries ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
178
179if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
180    string(APPEND mbedtls_static_target    "_static")
181    string(APPEND mbedx509_static_target   "_static")
182    string(APPEND mbedcrypto_static_target "_static")
183
184    list(APPEND target_libraries
185        ${mbedcrypto_static_target}
186        ${mbedx509_static_target}
187        ${mbedtls_static_target})
188endif()
189
190if(USE_STATIC_MBEDTLS_LIBRARY)
191    add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
192    set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
193    target_link_libraries(${mbedcrypto_static_target} PUBLIC ${libs})
194
195    add_library(${mbedx509_static_target} STATIC ${src_x509})
196    set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
197    target_link_libraries(${mbedx509_static_target} PUBLIC ${libs} ${mbedcrypto_static_target})
198
199    add_library(${mbedtls_static_target} STATIC ${src_tls})
200    set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
201    target_link_libraries(${mbedtls_static_target} PUBLIC ${libs} ${mbedx509_static_target})
202endif(USE_STATIC_MBEDTLS_LIBRARY)
203
204if(USE_SHARED_MBEDTLS_LIBRARY)
205    add_library(${mbedcrypto_target} SHARED ${src_crypto})
206    set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 2.28.0 SOVERSION 7)
207    target_link_libraries(${mbedcrypto_target} PUBLIC ${libs})
208
209    add_library(${mbedx509_target} SHARED ${src_x509})
210    set_target_properties(${mbedx509_target} PROPERTIES VERSION 2.28.0 SOVERSION 1)
211    target_link_libraries(${mbedx509_target} PUBLIC ${libs} ${mbedcrypto_target})
212
213    add_library(${mbedtls_target} SHARED ${src_tls})
214    set_target_properties(${mbedtls_target} PROPERTIES VERSION 2.28.0 SOVERSION 14)
215    target_link_libraries(${mbedtls_target} PUBLIC ${libs} ${mbedx509_target})
216endif(USE_SHARED_MBEDTLS_LIBRARY)
217
218foreach(target IN LISTS target_libraries)
219    # Include public header files from /include and other directories
220    # declared by /3rdparty/**/CMakeLists.txt. Include private header files
221    # from /library and others declared by /3rdparty/**/CMakeLists.txt.
222    # /library needs to be listed explicitly when building .c files outside
223    # of /library (which currently means: under /3rdparty).
224    target_include_directories(${target}
225        PUBLIC ${MBEDTLS_DIR}/include/
226        PUBLIC ${thirdparty_inc_public}
227        PRIVATE ${MBEDTLS_DIR}/library/
228        PRIVATE ${thirdparty_inc})
229    target_compile_definitions(${target}
230        PRIVATE ${thirdparty_def})
231    install(TARGETS ${target}
232            DESTINATION ${LIB_INSTALL_DIR}
233            PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
234endforeach(target)
235
236set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")
237
238add_custom_target(${lib_target} DEPENDS ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
239if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
240    add_dependencies(${lib_target} ${mbedcrypto_static_target} ${mbedx509_static_target} ${mbedtls_static_target})
241endif()
242