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)
4
5set(src_crypto
6    aes.c
7    aesni.c
8    arc4.c
9    asn1parse.c
10    asn1write.c
11    base64.c
12    bignum.c
13    blowfish.c
14    camellia.c
15    ccm.c
16    cipher.c
17    cipher_wrap.c
18    cmac.c
19    ctr_drbg.c
20    des.c
21    dhm.c
22    ecdh.c
23    ecdsa.c
24    ecjpake.c
25    ecp.c
26    ecp_curves.c
27    entropy.c
28    entropy_poll.c
29    error.c
30    gcm.c
31    havege.c
32    hmac_drbg.c
33    md.c
34    md2.c
35    md4.c
36    md5.c
37    md_wrap.c
38    memory_buffer_alloc.c
39    oid.c
40    padlock.c
41    pem.c
42    pk.c
43    pk_wrap.c
44    pkcs12.c
45    pkcs5.c
46    pkparse.c
47    pkwrite.c
48    platform.c
49    ripemd160.c
50    rsa.c
51    sha1.c
52    sha256.c
53    sha512.c
54    threading.c
55    timing.c
56    version.c
57    version_features.c
58    xtea.c
59)
60
61set(src_x509
62    certs.c
63    pkcs11.c
64    x509.c
65    x509_create.c
66    x509_crl.c
67    x509_crt.c
68    x509_csr.c
69    x509write_crt.c
70    x509write_csr.c
71)
72
73set(src_tls
74    debug.c
75    net_sockets.c
76    ssl_cache.c
77    ssl_ciphersuites.c
78    ssl_cli.c
79    ssl_cookie.c
80    ssl_srv.c
81    ssl_ticket.c
82    ssl_tls.c
83)
84
85if(CMAKE_COMPILER_IS_GNUCC)
86    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
87endif(CMAKE_COMPILER_IS_GNUCC)
88
89if(CMAKE_COMPILER_IS_CLANG)
90    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
91endif(CMAKE_COMPILER_IS_CLANG)
92
93if(WIN32)
94    set(libs ${libs} ws2_32)
95endif(WIN32)
96
97if(USE_PKCS11_HELPER_LIBRARY)
98    set(libs ${libs} pkcs11-helper)
99endif(USE_PKCS11_HELPER_LIBRARY)
100
101if(ENABLE_ZLIB_SUPPORT)
102    set(libs ${libs} ${ZLIB_LIBRARIES})
103endif(ENABLE_ZLIB_SUPPORT)
104
105if(LINK_WITH_PTHREAD)
106    set(libs ${libs} pthread)
107endif()
108
109if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
110    message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
111endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
112
113if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
114    set(mbedtls_static_target "mbedtls_static")
115    set(mbedx509_static_target "mbedx509_static")
116    set(mbedcrypto_static_target "mbedcrypto_static")
117elseif(USE_STATIC_MBEDTLS_LIBRARY)
118    set(mbedtls_static_target "mbedtls")
119    set(mbedx509_static_target "mbedx509")
120    set(mbedcrypto_static_target "mbedcrypto")
121endif()
122
123if(USE_STATIC_MBEDTLS_LIBRARY)
124    add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
125    set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
126    target_link_libraries(${mbedcrypto_static_target} ${libs})
127
128    add_library(${mbedx509_static_target} STATIC ${src_x509})
129    set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
130    target_link_libraries(${mbedx509_static_target} ${libs} ${mbedcrypto_static_target})
131
132    add_library(${mbedtls_static_target} STATIC ${src_tls})
133    set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
134    target_link_libraries(${mbedtls_static_target} ${libs} ${mbedx509_static_target})
135
136    install(TARGETS ${mbedtls_static_target} ${mbedx509_static_target} ${mbedcrypto_static_target}
137            DESTINATION ${LIB_INSTALL_DIR}
138            PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
139endif(USE_STATIC_MBEDTLS_LIBRARY)
140
141if(USE_SHARED_MBEDTLS_LIBRARY)
142    add_library(mbedcrypto SHARED ${src_crypto})
143    set_target_properties(mbedcrypto PROPERTIES VERSION 2.4.0 SOVERSION 0)
144    target_link_libraries(mbedcrypto ${libs})
145
146    add_library(mbedx509 SHARED ${src_x509})
147    set_target_properties(mbedx509 PROPERTIES VERSION 2.4.0 SOVERSION 0)
148    target_link_libraries(mbedx509 ${libs} mbedcrypto)
149
150    add_library(mbedtls SHARED ${src_tls})
151    set_target_properties(mbedtls PROPERTIES VERSION 2.4.0 SOVERSION 10)
152    target_link_libraries(mbedtls ${libs} mbedx509)
153
154    install(TARGETS mbedtls mbedx509 mbedcrypto
155            DESTINATION ${LIB_INSTALL_DIR}
156            PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
157endif(USE_SHARED_MBEDTLS_LIBRARY)
158
159add_custom_target(lib DEPENDS mbedcrypto mbedx509 mbedtls)
160if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
161    add_dependencies(lib mbedcrypto_static mbedx509_static mbedtls_static)
162endif()
163