blob: 4444b984e7b52db96ea28967859c5739042addc8 [file] [log] [blame]
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +00001option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON)
2option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF)
Manuel Pégourié-Gonnardcfa9a452015-01-23 13:33:31 +00003option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF)
Moshe Shahar6763fe42019-07-24 14:19:35 +03004option(LINK_WITH_TRUSTED_STORAGE "Explicitly link mbed TLS library to trusted_storage." OFF)
Paul Bakker9d3a7e42011-01-05 15:24:43 +00005
Ashley Duncand85a7e92019-04-29 20:35:06 +12006# 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()
Paul Bakker9d3a7e42011-01-05 15:24:43 +000012
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020013set(src_crypto
14 aes.c
15 aesni.c
16 arc4.c
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000017 aria.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020018 asn1parse.c
19 asn1write.c
20 base64.c
21 bignum.c
22 blowfish.c
23 camellia.c
24 ccm.c
Daniel King34b822c2016-05-15 17:28:08 -030025 chacha20.c
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020026 chachapoly.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020027 cipher.c
28 cipher_wrap.c
Robert Cragiedc5c7b92015-12-11 15:49:45 +000029 cmac.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020030 ctr_drbg.c
31 des.c
32 dhm.c
33 ecdh.c
34 ecdsa.c
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020035 ecjpake.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020036 ecp.c
37 ecp_curves.c
38 entropy.c
39 entropy_poll.c
40 error.c
41 gcm.c
42 havege.c
Thomas Fossati656864b2016-07-17 08:51:22 +010043 hkdf.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020044 hmac_drbg.c
45 md.c
46 md2.c
47 md4.c
48 md5.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020049 memory_buffer_alloc.c
Ron Eldorcb349ac2018-07-15 09:29:47 +030050 nist_kw.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020051 oid.c
52 padlock.c
53 pem.c
54 pk.c
55 pk_wrap.c
56 pkcs12.c
57 pkcs5.c
58 pkparse.c
59 pkwrite.c
60 platform.c
Andres Amaya Garciabc7bdbf2018-04-24 08:29:20 -050061 platform_util.c
Daniel Kingadc32c02016-05-16 18:25:45 -030062 poly1305.c
Gilles Peskinee59236f2018-01-27 23:32:46 +010063 psa_crypto.c
Steven Cooremancd84cb42020-07-16 20:28:36 +020064 psa_crypto_driver_wrappers.c
Gilles Peskinea899a722019-06-24 14:06:43 +020065 psa_crypto_se.c
Gilles Peskine961849f2018-11-30 18:54:54 +010066 psa_crypto_slot_management.c
Darryl Greendb2b8db2018-06-15 13:06:04 +010067 psa_crypto_storage.c
Gilles Peskine6194dc22018-11-16 22:24:15 +010068 psa_its_file.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020069 ripemd160.c
70 rsa.c
Hanno Beckera565f542017-10-11 11:00:19 +010071 rsa_internal.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020072 sha1.c
73 sha256.c
74 sha512.c
75 threading.c
Manuel Pégourié-Gonnard21dcc1e2015-06-25 10:59:15 +020076 timing.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020077 version.c
78 version_features.c
79 xtea.c
80)
81
Christoph M. Wintersteiger655ddab2019-01-21 17:26:19 +000082list(APPEND src_crypto ${thirdparty_src})
83
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020084set(src_x509
85 certs.c
86 pkcs11.c
87 x509.c
88 x509_create.c
89 x509_crl.c
90 x509_crt.c
91 x509_csr.c
92 x509write_crt.c
93 x509write_csr.c
94)
95
96set(src_tls
97 debug.c
Andres AG788aa4a2016-09-14 14:32:09 +010098 net_sockets.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020099 ssl_cache.c
100 ssl_ciphersuites.c
101 ssl_cli.c
102 ssl_cookie.c
Hanno Becker6e7051a2020-02-05 11:33:54 +0000103 ssl_msg.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +0200104 ssl_srv.c
105 ssl_ticket.c
Hanno Becker471c0c92020-02-05 11:32:03 +0000106 ssl_tls.c
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100107 ssl_tls13_keys.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +0200108)
109
Paul Bakker76f03112013-11-28 17:20:04 +0100110if(CMAKE_COMPILER_IS_GNUCC)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200111 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
Paul Bakker76f03112013-11-28 17:20:04 +0100112endif(CMAKE_COMPILER_IS_GNUCC)
113
Manuel Pégourié-Gonnard31855452014-06-25 15:59:50 +0200114if(CMAKE_COMPILER_IS_CLANG)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200115 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
Manuel Pégourié-Gonnard31855452014-06-25 15:59:50 +0200116endif(CMAKE_COMPILER_IS_CLANG)
117
Manuel Pégourié-Gonnardba2c8762015-06-25 09:35:46 +0200118if(WIN32)
119 set(libs ${libs} ws2_32)
120endif(WIN32)
121
Andres Amaya Garciabf7fe4f2018-06-21 20:21:38 +0100122if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
TabascoEye7f3ef272018-04-27 13:14:59 +0200123 SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
124 SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
125 SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
126 SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
Andres Amaya Garciabf7fe4f2018-06-21 20:21:38 +0100127endif()
TabascoEye7f3ef272018-04-27 13:14:59 +0200128
Augustin Cavalier60bc47d2018-04-11 20:27:32 -0400129if(HAIKU)
Simon Butcher6331cb02018-07-10 11:48:42 +0100130 set(libs ${libs} network)
Augustin Cavalier60bc47d2018-04-11 20:27:32 -0400131endif(HAIKU)
132
Manuel Pégourié-Gonnardba2c8762015-06-25 09:35:46 +0200133if(USE_PKCS11_HELPER_LIBRARY)
134 set(libs ${libs} pkcs11-helper)
135endif(USE_PKCS11_HELPER_LIBRARY)
136
137if(ENABLE_ZLIB_SUPPORT)
138 set(libs ${libs} ${ZLIB_LIBRARIES})
139endif(ENABLE_ZLIB_SUPPORT)
140
141if(LINK_WITH_PTHREAD)
142 set(libs ${libs} pthread)
143endif()
144
Moshe Shahar7e367652019-07-15 15:50:19 +0300145if(LINK_WITH_TRUSTED_STORAGE)
146 set(libs ${libs} trusted_storage)
147endif()
148
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000149if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200150 message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000151endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
Paul Bakker9d3a7e42011-01-05 15:24:43 +0000152
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200153set(target_libraries "mbedcrypto" "mbedx509" "mbedtls")
154
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000155if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200156 set(mbedtls_static_target "mbedtls_static")
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200157 set(mbedx509_static_target "mbedx509_static")
158 set(mbedcrypto_static_target "mbedcrypto_static")
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200159 list(APPEND target_libraries
160 "mbedcrypto_static" "mbedx509_static" "mbedtls_static")
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000161elseif(USE_STATIC_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200162 set(mbedtls_static_target "mbedtls")
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200163 set(mbedx509_static_target "mbedx509")
164 set(mbedcrypto_static_target "mbedcrypto")
hasufell3c6409b2014-03-06 15:49:08 +0100165endif()
Paul Bakker9d3a7e42011-01-05 15:24:43 +0000166
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000167if(USE_STATIC_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200168 add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
169 set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
170 target_link_libraries(${mbedcrypto_static_target} ${libs})
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200171
Gilles Peskine55d53532020-03-09 17:48:13 +0100172 add_library(${mbedx509_static_target} STATIC ${src_x509})
173 set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
174 target_link_libraries(${mbedx509_static_target} ${libs} ${mbedcrypto_static_target})
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200175
Gilles Peskine55d53532020-03-09 17:48:13 +0100176 add_library(${mbedtls_static_target} STATIC ${src_tls})
177 set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
178 target_link_libraries(${mbedtls_static_target} ${libs} ${mbedx509_static_target})
Manuel Pégourié-Gonnarde1d34d12015-06-25 14:53:13 +0200179endif(USE_STATIC_MBEDTLS_LIBRARY)
Paul Bakker9d3a7e42011-01-05 15:24:43 +0000180
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000181if(USE_SHARED_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200182 add_library(mbedcrypto SHARED ${src_crypto})
Janos Follath17ffc5d2020-08-26 15:22:45 +0100183 set_target_properties(mbedcrypto PROPERTIES VERSION 2.24.0 SOVERSION 5)
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200184 target_link_libraries(mbedcrypto ${libs})
Paul Bakker2a5c7a82012-05-10 21:54:28 +0000185
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200186 add_library(mbedx509 SHARED ${src_x509})
Janos Follath17ffc5d2020-08-26 15:22:45 +0100187 set_target_properties(mbedx509 PROPERTIES VERSION 2.24.0 SOVERSION 1)
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200188 target_link_libraries(mbedx509 ${libs} mbedcrypto)
189
190 add_library(mbedtls SHARED ${src_tls})
Janos Follath17ffc5d2020-08-26 15:22:45 +0100191 set_target_properties(mbedtls PROPERTIES VERSION 2.24.0 SOVERSION 13)
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200192 target_link_libraries(mbedtls ${libs} mbedx509)
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000193endif(USE_SHARED_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnarde1d34d12015-06-25 14:53:13 +0200194
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200195foreach(target IN LISTS target_libraries)
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200196 # Include public header files from /include and other directories
197 # declared by /3rdparty/**/CMakeLists.txt. Include private header files
198 # from /library and others declared by /3rdparty/**/CMakeLists.txt.
199 # /library needs to be listed explicitly when building .c files outside
200 # of /library (which currently means: under /3rdparty).
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200201 target_include_directories(${target}
202 PUBLIC ${MBEDTLS_DIR}/include/
203 PUBLIC ${thirdparty_inc_public}
Gilles Peskinededff7a2020-07-02 13:13:27 +0200204 PRIVATE ${MBEDTLS_DIR}/library/
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200205 PRIVATE ${thirdparty_inc})
206 target_compile_definitions(${target}
207 PRIVATE ${thirdparty_def})
208 install(TARGETS ${target}
hasufell3c6409b2014-03-06 15:49:08 +0100209 DESTINATION ${LIB_INSTALL_DIR}
Gilles Peskine280165c2020-07-02 13:19:17 +0200210 PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200211endforeach(target)
Manuel Pégourié-Gonnarde1d34d12015-06-25 14:53:13 +0200212
Gilles Peskine55d53532020-03-09 17:48:13 +0100213add_custom_target(lib DEPENDS mbedcrypto mbedx509 mbedtls)
214if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
215 add_dependencies(lib mbedcrypto_static mbedx509_static mbedtls_static)
Manuel Pégourié-Gonnarde1d34d12015-06-25 14:53:13 +0200216endif()