FindMbedTLS.cmake 6.7 KB

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