CMakeLists.txt 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON)
  2. option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF)
  3. option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF)
  4. option(LINK_WITH_TRUSTED_STORAGE "Explicitly link mbed TLS library to trusted_storage." OFF)
  5. # Set the project root directory if it's not already defined, as may happen if
  6. # the library folder is included directly by a parent project, without
  7. # including the top level CMakeLists.txt.
  8. if(NOT DEFINED MBEDTLS_DIR)
  9. set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
  10. endif()
  11. set(src_crypto
  12. aes.c
  13. aesni.c
  14. arc4.c
  15. aria.c
  16. asn1parse.c
  17. asn1write.c
  18. base64.c
  19. bignum.c
  20. blowfish.c
  21. camellia.c
  22. ccm.c
  23. chacha20.c
  24. chachapoly.c
  25. cipher.c
  26. cipher_wrap.c
  27. constant_time.c
  28. cmac.c
  29. ctr_drbg.c
  30. des.c
  31. dhm.c
  32. ecdh.c
  33. ecdsa.c
  34. ecjpake.c
  35. ecp.c
  36. ecp_curves.c
  37. entropy.c
  38. entropy_poll.c
  39. error.c
  40. gcm.c
  41. havege.c
  42. hkdf.c
  43. hmac_drbg.c
  44. md.c
  45. md2.c
  46. md4.c
  47. md5.c
  48. memory_buffer_alloc.c
  49. mps_reader.c
  50. mps_trace.c
  51. nist_kw.c
  52. oid.c
  53. padlock.c
  54. pem.c
  55. pk.c
  56. pk_wrap.c
  57. pkcs12.c
  58. pkcs5.c
  59. pkparse.c
  60. pkwrite.c
  61. platform.c
  62. platform_util.c
  63. poly1305.c
  64. psa_crypto.c
  65. psa_crypto_aead.c
  66. psa_crypto_cipher.c
  67. psa_crypto_client.c
  68. psa_crypto_driver_wrappers.c
  69. psa_crypto_ecp.c
  70. psa_crypto_hash.c
  71. psa_crypto_mac.c
  72. psa_crypto_rsa.c
  73. psa_crypto_se.c
  74. psa_crypto_slot_management.c
  75. psa_crypto_storage.c
  76. psa_its_file.c
  77. ripemd160.c
  78. rsa.c
  79. rsa_internal.c
  80. sha1.c
  81. sha256.c
  82. sha512.c
  83. threading.c
  84. timing.c
  85. version.c
  86. version_features.c
  87. xtea.c
  88. )
  89. list(APPEND src_crypto ${thirdparty_src})
  90. set(src_x509
  91. certs.c
  92. pkcs11.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. set(src_tls
  102. debug.c
  103. net_sockets.c
  104. ssl_cache.c
  105. ssl_ciphersuites.c
  106. ssl_cli.c
  107. ssl_cookie.c
  108. ssl_msg.c
  109. ssl_srv.c
  110. ssl_ticket.c
  111. ssl_tls.c
  112. ssl_tls13_keys.c
  113. )
  114. if(CMAKE_COMPILER_IS_GNUCC)
  115. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
  116. endif(CMAKE_COMPILER_IS_GNUCC)
  117. if(CMAKE_COMPILER_IS_CLANG)
  118. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wunreachable-code")
  119. endif(CMAKE_COMPILER_IS_CLANG)
  120. if(WIN32)
  121. set(libs ${libs} ws2_32)
  122. endif(WIN32)
  123. if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  124. SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
  125. SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
  126. SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
  127. SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
  128. endif()
  129. if(HAIKU)
  130. set(libs ${libs} network)
  131. endif(HAIKU)
  132. if(USE_PKCS11_HELPER_LIBRARY)
  133. set(libs ${libs} pkcs11-helper)
  134. endif(USE_PKCS11_HELPER_LIBRARY)
  135. if(ENABLE_ZLIB_SUPPORT)
  136. set(libs ${libs} ${ZLIB_LIBRARIES})
  137. endif(ENABLE_ZLIB_SUPPORT)
  138. if(LINK_WITH_PTHREAD)
  139. set(libs ${libs} pthread)
  140. endif()
  141. if(LINK_WITH_TRUSTED_STORAGE)
  142. set(libs ${libs} trusted_storage)
  143. endif()
  144. if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
  145. message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
  146. endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
  147. set(mbedtls_target "${MBEDTLS_TARGET_PREFIX}mbedtls")
  148. set(mbedx509_target "${MBEDTLS_TARGET_PREFIX}mbedx509")
  149. set(mbedcrypto_target "${MBEDTLS_TARGET_PREFIX}mbedcrypto")
  150. set(mbedtls_target ${mbedtls_target} PARENT_SCOPE)
  151. set(mbedx509_target ${mbedx509_target} PARENT_SCOPE)
  152. set(mbedcrypto_target ${mbedcrypto_target} PARENT_SCOPE)
  153. if (USE_STATIC_MBEDTLS_LIBRARY)
  154. set(mbedtls_static_target ${mbedtls_target})
  155. set(mbedx509_static_target ${mbedx509_target})
  156. set(mbedcrypto_static_target ${mbedcrypto_target})
  157. endif()
  158. set(target_libraries ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
  159. if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
  160. string(APPEND mbedtls_static_target "_static")
  161. string(APPEND mbedx509_static_target "_static")
  162. string(APPEND mbedcrypto_static_target "_static")
  163. list(APPEND target_libraries
  164. ${mbedcrypto_static_target}
  165. ${mbedx509_static_target}
  166. ${mbedtls_static_target})
  167. endif()
  168. if(USE_STATIC_MBEDTLS_LIBRARY)
  169. add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
  170. dolphin_disable_warnings(${mbedcrypto_static_target})
  171. set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
  172. target_link_libraries(${mbedcrypto_static_target} PUBLIC ${libs})
  173. add_library(${mbedx509_static_target} STATIC ${src_x509})
  174. dolphin_disable_warnings(${mbedx509_static_target})
  175. set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
  176. target_link_libraries(${mbedx509_static_target} PUBLIC ${libs} ${mbedcrypto_static_target})
  177. add_library(${mbedtls_static_target} STATIC ${src_tls})
  178. dolphin_disable_warnings(${mbedtls_static_target})
  179. set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
  180. target_link_libraries(${mbedtls_static_target} PUBLIC ${libs} ${mbedx509_static_target})
  181. endif(USE_STATIC_MBEDTLS_LIBRARY)
  182. if(USE_SHARED_MBEDTLS_LIBRARY)
  183. add_library(${mbedcrypto_target} SHARED ${src_crypto})
  184. dolphin_disable_warnings(${mbedcrypto_target})
  185. set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 2.28.0 SOVERSION 7)
  186. target_link_libraries(${mbedcrypto_target} PUBLIC ${libs})
  187. add_library(${mbedx509_target} SHARED ${src_x509})
  188. dolphin_disable_warnings(${mbedx509_target})
  189. set_target_properties(${mbedx509_target} PROPERTIES VERSION 2.28.0 SOVERSION 1)
  190. target_link_libraries(${mbedx509_target} PUBLIC ${libs} ${mbedcrypto_target})
  191. add_library(${mbedtls_target} SHARED ${src_tls})
  192. dolphin_disable_warnings(${mbedtls_target})
  193. set_target_properties(${mbedtls_target} PROPERTIES VERSION 2.28.0 SOVERSION 14)
  194. target_link_libraries(${mbedtls_target} PUBLIC ${libs} ${mbedx509_target})
  195. endif(USE_SHARED_MBEDTLS_LIBRARY)
  196. foreach(target IN LISTS target_libraries)
  197. add_library(MbedTLS::${target} ALIAS ${target}) # add_subdirectory support
  198. # Include public header files from /include and other directories
  199. # declared by /3rdparty/**/CMakeLists.txt. Include private header files
  200. # from /library and others declared by /3rdparty/**/CMakeLists.txt.
  201. # /library needs to be listed explicitly when building .c files outside
  202. # of /library (which currently means: under /3rdparty).
  203. target_include_directories(${target}
  204. PUBLIC ${MBEDTLS_DIR}/include/
  205. PUBLIC ${thirdparty_inc_public}
  206. PRIVATE ${MBEDTLS_DIR}/library/
  207. PRIVATE ${thirdparty_inc})
  208. target_compile_definitions(${target}
  209. PRIVATE ${thirdparty_def})
  210. install(TARGETS ${target}
  211. DESTINATION ${LIB_INSTALL_DIR}
  212. PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
  213. endforeach(target)
  214. set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")
  215. add_custom_target(${lib_target} DEPENDS ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
  216. if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
  217. add_dependencies(${lib_target} ${mbedcrypto_static_target} ${mbedx509_static_target} ${mbedtls_static_target})
  218. endif()