FindMbedTLS.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #[=======================================================================[.rst
  2. FindMbedTLS
  3. -----------
  4. FindModule for MbedTLS and associated libraries
  5. .. versionchanged:: 3.0
  6. Updated FindModule to CMake standards
  7. Components
  8. ^^^^^^^^^^
  9. .. versionadded:: 1.0
  10. This module contains provides several components:
  11. ``mbedcrypto``
  12. ``mbedtls``
  13. ``mbedx509``
  14. Import targets exist for each component.
  15. Imported Targets
  16. ^^^^^^^^^^^^^^^^
  17. .. versionadded:: 2.0
  18. This module defines the :prop_tgt:`IMPORTED` targets:
  19. ``MbedTLS::mbedcrypto``
  20. Crypto component
  21. ``MbedTLS::mbedtls``
  22. TLS component
  23. ``MbedTLS::mbedX509``
  24. X509 component
  25. Result Variables
  26. ^^^^^^^^^^^^^^^^
  27. This module sets the following variables:
  28. ``MbedTLS_FOUND``
  29. True, if all required components and the core library were found.
  30. ``MbedTLS_VERSION``
  31. Detected version of found MbedTLS libraries.
  32. ``MbedTLS_<COMPONENT>_VERSION``
  33. Detected version of found MbedTLS component library.
  34. Cache variables
  35. ^^^^^^^^^^^^^^^
  36. The following cache variables may also be set:
  37. ``MbedTLS_<COMPONENT>_LIBRARY``
  38. Path to the library component of MbedTLS.
  39. ``MbedTLS_<COMPONENT>_INCLUDE_DIR``
  40. Directory containing ``<COMPONENT>.h``.
  41. #]=======================================================================]
  42. include(FindPackageHandleStandardArgs)
  43. find_package(PkgConfig QUIET)
  44. if(PKG_CONFIG_FOUND)
  45. pkg_check_modules(PC_MbedTLS QUIET mbedtls mbedcrypto mbedx509)
  46. endif()
  47. # MbedTLS_set_soname: Set SONAME on imported library targets
  48. macro(MbedTLS_set_soname component)
  49. if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
  50. execute_process(
  51. COMMAND sh -c "otool -D '${Mbed${component}_LIBRARY}' | grep -v '${Mbed${component}_LIBRARY}'"
  52. OUTPUT_VARIABLE _output
  53. RESULT_VARIABLE _result
  54. )
  55. if(_result EQUAL 0 AND _output MATCHES "^@rpath/")
  56. set_property(TARGET MbedTLS::mbed${component} PROPERTY IMPORTED_SONAME "${_output}")
  57. endif()
  58. elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")
  59. execute_process(
  60. COMMAND sh -c "objdump -p '${Mbed${component}_LIBRARY}' | grep SONAME"
  61. OUTPUT_VARIABLE _output
  62. RESULT_VARIABLE _result
  63. )
  64. if(_result EQUAL 0)
  65. string(REGEX REPLACE "[ \t]+SONAME[ \t]+([^ \t]+)" "\\1" _soname "${_output}")
  66. set_property(TARGET MbedTLS::mbed${component} PROPERTY IMPORTED_SONAME "${_soname}")
  67. unset(_soname)
  68. endif()
  69. endif()
  70. unset(_output)
  71. unset(_result)
  72. endmacro()
  73. find_path(
  74. MbedTLS_INCLUDE_DIR
  75. NAMES mbedtls/ssl.h
  76. HINTS "${PC_MbedTLS_INCLUDE_DIRS}"
  77. PATHS /usr/include /usr/local/include
  78. DOC "MbedTLS include directory"
  79. )
  80. if(PC_MbedTLS_VERSION VERSION_GREATER 0)
  81. set(MbedTLS_VERSION ${PC_MbedTLS_VERSION})
  82. elseif(EXISTS "${MbedTLS_INCLUDE_DIR}/mbedtls/build_info.h")
  83. file(
  84. STRINGS
  85. "${MbedTLS_INCLUDE_DIR}/mbedtls/build_info.h"
  86. _VERSION_STRING
  87. REGEX "#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+.+"
  88. )
  89. string(
  90. REGEX REPLACE
  91. ".*#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+\"(.+)\".*"
  92. "\\1"
  93. MbedTLS_VERSION
  94. "${_VERSION_STRING}"
  95. )
  96. elseif(EXISTS "${MbedTLS_INCLUDE_DIR}/mbedtls/version.h")
  97. file(
  98. STRINGS
  99. "${MbedTLS_INCLUDE_DIR}/mbedtls/version.h"
  100. _VERSION_STRING
  101. REGEX "#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+.+"
  102. )
  103. string(
  104. REGEX REPLACE
  105. ".*#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+\"(.+)\".*"
  106. "\\1"
  107. MbedTLS_VERSION
  108. "${_VERSION_STRING}"
  109. )
  110. else()
  111. if(NOT MbedTLS_FIND_QUIETLY)
  112. message(AUTHOR_WARNING "Failed to find MbedTLS version.")
  113. endif()
  114. set(MbedTLS_VERSION 0.0.0)
  115. endif()
  116. if(MbedTLS_VERSION VERSION_GREATER_EQUAL 3.6.0)
  117. message(
  118. DEPRECATION
  119. "Use of the custom CMake find module for MbedTLS versions >= 3.6.0 is not supported - build errors might occur!"
  120. )
  121. endif()
  122. find_library(
  123. Mbedtls_LIBRARY
  124. NAMES libmbedtls mbedtls
  125. HINTS "${PC_MbedTLS_LIBRARY_DIRS}"
  126. PATHS /usr/lib /usr/local/lib
  127. DOC "MbedTLS location"
  128. )
  129. find_library(
  130. Mbedcrypto_LIBRARY
  131. NAMES libmbedcrypto mbedcrypto
  132. HINTS "${PC_MbedTLS_LIBRARY_DIRS}"
  133. PATHS /usr/lib /usr/local/lib
  134. DOC "MbedCrypto location"
  135. )
  136. find_library(
  137. Mbedx509_LIBRARY
  138. NAMES libmbedx509 mbedx509
  139. HINTS "${PC_MbedTLS_LIBRARY_DIRS}"
  140. PATHS /usr/lib /usr/local/lib
  141. DOC "MbedX509 location"
  142. )
  143. if(Mbedtls_LIBRARY AND NOT Mbedcrypto_LIBRARY AND NOT Mbedx509_LIBRARY)
  144. set(CMAKE_REQUIRED_LIBRARIES "${MbedTLS_LIBRARY}")
  145. set(CMAKE_REQUIRED_INCLUDES "${MbedTLS_INCLUDE_DIR}")
  146. check_symbol_exists(mbedtls_x509_crt_init "mbedtls/x590_crt.h" MbedTLS_INCLUDES_X509)
  147. check_symbol_exists(mbedtls_sha256_init "mbedtls/sha256.h" MbedTLS_INCLUDES_CRYPTO)
  148. unset(CMAKE_REQUIRED_LIBRARIES)
  149. unset(CMAKE_REQUIRED_INCLUDES)
  150. endif()
  151. if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin|Windows")
  152. set(MbedTLS_ERROR_REASON "Ensure that obs-deps is provided as part of CMAKE_PREFIX_PATH.")
  153. elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")
  154. set(MbedTLS_ERROR_REASON "Ensure that MbedTLS is installed on the system.")
  155. endif()
  156. if(MbedTLS_INCLUDES_X509 AND MbedTLS_INCLUDES_CRYPTO)
  157. find_package_handle_standard_args(
  158. MbedTLS
  159. REQUIRED_VARS Mbedtls_LIBRARY MbedTLS_INCLUDE_DIR
  160. VERSION_VAR MbedTLS_VERSION
  161. REASON_FAILURE_MESSAGE "${MbedTLS_ERROR_REASON}"
  162. )
  163. mark_as_advanced(Mbedtls_LIBRARY MbedTLS_INCLUDE_DIR)
  164. list(APPEND _COMPONENTS tls)
  165. else()
  166. find_package_handle_standard_args(
  167. MbedTLS
  168. REQUIRED_VARS Mbedtls_LIBRARY Mbedcrypto_LIBRARY Mbedx509_LIBRARY MbedTLS_INCLUDE_DIR
  169. VERSION_VAR MbedTLS_VERSION
  170. REASON_FAILURE_MESSAGE "${MbedTLS_ERROR_REASON}"
  171. )
  172. mark_as_advanced(Mbedtls_LIBRARY Mbedcrypto_LIBRARY Mbedx509_LIBRARY MbedTLS_INCLUDE_DIR)
  173. list(APPEND _COMPONENTS tls crypto x509)
  174. endif()
  175. unset(MbedTLS_ERROR_REASON)
  176. if(MbedTLS_FOUND)
  177. foreach(component IN LISTS _COMPONENTS)
  178. if(NOT TARGET MbedTLS::mbed${component})
  179. if(IS_ABSOLUTE "${Mbed${component}_LIBRARY}")
  180. add_library(MbedTLS::mbed${component} UNKNOWN IMPORTED)
  181. set_property(TARGET MbedTLS::mbed${component} PROPERTY IMPORTED_LOCATION "${Mbed${component}_LIBRARY}")
  182. else()
  183. add_library(MbedTLS::mbed${component} INTERFACE IMPORTED)
  184. set_property(TARGET MbedTLS::mbed${component} PROPERTY IMPORTED_LIBNAME "${Mbed${component}_LIBRARY}")
  185. endif()
  186. mbedtls_set_soname(${component})
  187. set_target_properties(
  188. MbedTLS::mbedtls
  189. PROPERTIES
  190. INTERFACE_COMPILE_OPTIONS "${PC_MbedTLS_CFLAGS_OTHER}"
  191. INTERFACE_INCLUDE_DIRECTORIES "${MbedTLS_INCLUDE_DIR}"
  192. INTERFACE_LINK_OPTIONS "$<$<AND:$<PLATFORM_ID:Windows>,$<CONFIG:DEBUG>>:/NODEFAULTLIB:MSVCRT>"
  193. VERSION ${MbedTLS_VERSION}
  194. )
  195. endif()
  196. endforeach()
  197. if(MbedTLS_INCLUDES_X509 AND MbedTLS_INCLUDES_CRYPTO)
  198. set(MbedTLS_LIBRARIES ${MbedTLS_LIBRARY})
  199. else()
  200. set(MbedTLS_LIBRARIES ${MbedTLS_LIBRARY} ${MbedCrypto_LIBRARY} ${MbedX509_LIBRARY})
  201. set_property(TARGET MbedTLS::mbedtls PROPERTY INTERFACE_LINK_LIBRARIES MbedTLS::mbedcrypto MbedTLS::mbedx509)
  202. endif()
  203. endif()
  204. include(FeatureSummary)
  205. set_package_properties(
  206. MbedTLS
  207. PROPERTIES
  208. URL "https://www.trustedfirmware.org/projects/mbed-tls"
  209. DESCRIPTION
  210. "A C library implementing cryptographic primitives, X.509 certificate manipulation, and the SSL/TLS and DTLS protocols."
  211. )