FindMbedTLS.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Once done these will be defined:
  2. #
  3. # * MBEDTLS_FOUND
  4. # * MBEDTLS_INCLUDE_DIRS
  5. # * MBEDTLS_LIBRARIES
  6. #
  7. find_package(PkgConfig QUIET)
  8. if(PKG_CONFIG_FOUND)
  9. pkg_check_modules(_MBEDTLS QUIET mbedtls)
  10. endif()
  11. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  12. set(_lib_suffix 64)
  13. else()
  14. set(_lib_suffix 32)
  15. endif()
  16. # If we're on MacOS or Linux, please try to statically-link mbedtls.
  17. if(ENABLE_STATIC_MBEDTLS AND (APPLE OR UNIX))
  18. set(_MBEDTLS_LIBRARIES libmbedtls.a)
  19. set(_MBEDCRYPTO_LIBRARIES libmbedcrypto.a)
  20. set(_MBEDX509_LIBRARIES libmbedx509.a)
  21. endif()
  22. find_path(
  23. MBEDTLS_INCLUDE_DIR
  24. NAMES mbedtls/ssl.h
  25. HINTS ENV MBEDTLS_PATH ${MBEDTLS_PATH} ${CMAKE_SOURCE_DIR}/${MBEDTLS_PATH}
  26. ${_MBEDTLS_INCLUDE_DIRS}
  27. PATHS /usr/include /usr/local/include /opt/local/include /sw/include
  28. PATH_SUFFIXES include)
  29. find_library(
  30. MBEDTLS_LIB
  31. NAMES ${_MBEDTLS_LIBRARIES} mbedtls libmbedtls
  32. HINTS ENV MBEDTLS_PATH ${MBEDTLS_PATH} ${CMAKE_SOURCE_DIR}/${MBEDTLS_PATH}
  33. ${_MBEDTLS_LIBRARY_DIRS}
  34. PATHS /usr/lib /usr/local/lib /opt/local/lib /sw/lib
  35. PATH_SUFFIXES
  36. lib${_lib_suffix}
  37. lib
  38. libs${_lib_suffix}
  39. libs
  40. bin${_lib_suffix}
  41. bin
  42. ../lib${_lib_suffix}
  43. ../lib
  44. ../libs${_lib_suffix}
  45. ../libs
  46. ../bin${_lib_suffix}
  47. ../bin)
  48. find_library(
  49. MBEDCRYPTO_LIB
  50. NAMES ${_MBEDCRYPTO_LIBRARIES} mbedcrypto libmbedcrypto
  51. HINTS ENV MBEDCRYPTO_PATH ${MBEDCRYPTO_PATH}
  52. ${CMAKE_SOURCE_DIR}/${MBEDCRYPTO_PATH} ${_MBEDCRYPTO_LIBRARY_DIRS}
  53. PATHS /usr/lib /usr/local/lib /opt/local/lib /sw/lib
  54. PATH_SUFFIXES
  55. lib${_lib_suffix}
  56. lib
  57. libs${_lib_suffix}
  58. libs
  59. bin${_lib_suffix}
  60. bin
  61. ../lib${_lib_suffix}
  62. ../lib
  63. ../libs${_lib_suffix}
  64. ../libs
  65. ../bin${_lib_suffix}
  66. ../bin)
  67. find_library(
  68. MBEDX509_LIB
  69. NAMES ${_MBEDX509_LIBRARIES} mbedx509 libmbedx509
  70. HINTS ENV MBEDX509_PATH ${MBEDX509_PATH} ${CMAKE_SOURCE_DIR}/${MBEDX509_PATH}
  71. ${_MBEDX509_LIBRARY_DIRS}
  72. PATHS /usr/lib /usr/local/lib /opt/local/lib /sw/lib
  73. PATH_SUFFIXES
  74. lib${_lib_suffix}
  75. lib
  76. libs${_lib_suffix}
  77. libs
  78. bin${_lib_suffix}
  79. bin
  80. ../lib${_lib_suffix}
  81. ../lib
  82. ../libs${_lib_suffix}
  83. ../libs
  84. ../bin${_lib_suffix}
  85. ../bin)
  86. # Sometimes mbedtls is split between three libs, and sometimes it isn't. If it
  87. # isn't, let's check if the symbols we need are all in MBEDTLS_LIB.
  88. if(MBEDTLS_LIB
  89. AND NOT MBEDCRYPTO_LIB
  90. AND NOT MBEDX509_LIB)
  91. set(CMAKE_REQUIRED_LIBRARIES ${MBEDTLS_LIB})
  92. set(CMAKE_REQUIRED_INCLUDES ${MBEDTLS_INCLUDE_DIR})
  93. check_symbol_exists(mbedtls_x509_crt_init "mbedtls/x509_crt.h"
  94. MBEDTLS_INCLUDES_X509)
  95. check_symbol_exists(mbedtls_sha256_init "mbedtls/sha256.h"
  96. MBEDTLS_INCLUDES_CRYPTO)
  97. unset(CMAKE_REQUIRED_INCLUDES)
  98. unset(CMAKE_REQUIRED_LIBRARIES)
  99. endif()
  100. # If we find all three libraries, then go ahead.
  101. if(MBEDTLS_LIB
  102. AND MBEDCRYPTO_LIB
  103. AND MBEDX509_LIB)
  104. set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
  105. set(MBEDTLS_LIBRARIES ${MBEDTLS_LIB} ${MBEDCRYPTO_LIB} ${MBEDX509_LIB})
  106. foreach(component TLS CRYPTO X509)
  107. if(NOT TARGET Mbedtls::${component} AND MBED${component}_LIB)
  108. if(IS_ABSOLUTE "${MBED${component}_LIB}")
  109. add_library(Mbedtls::${component} UNKNOWN IMPORTED)
  110. set_target_properties(
  111. Mbedtls::${component} PROPERTIES IMPORTED_LOCATION
  112. "${MBED${component}_LIB}")
  113. else()
  114. add_library(Mbedtls::${component} INTERFACE IMPORTED)
  115. set_target_properties(
  116. Mbedtls::${component} PROPERTIES IMPORTED_LIBNAME
  117. "${MBED${component}_LIB}")
  118. endif()
  119. set_target_properties(
  120. Mbedtls::${component} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  121. "${MBED${component}_INCLUDE_DIR}")
  122. endif()
  123. endforeach()
  124. if(NOT TARGET Mbedtls::Mbedtls)
  125. add_library(Mbedtls::Mbedtls INTERFACE IMPORTED)
  126. target_link_libraries(Mbedtls::Mbedtls
  127. INTERFACE Mbedtls::TLS Mbedtls::CRYPTO Mbedtls::X509)
  128. endif()
  129. # Otherwise, if we find MBEDTLS_LIB, and it has both CRYPTO and x509 within
  130. # the single lib (i.e. a windows build environment), then also feel free to go
  131. # ahead.
  132. elseif(
  133. MBEDTLS_LIB
  134. AND MBEDTLS_INCLUDES_CRYPTO
  135. AND MBEDTLS_INCLUDES_X509)
  136. set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
  137. set(MBEDTLS_LIBRARIES ${MBEDTLS_LIB})
  138. if(NOT TARGET Mbedtls::Mbedtls)
  139. if(IS_ABSOLUTE "${MBED${component}_LIB}")
  140. add_library(Mbedtls::${component} UNKNOWN IMPORTED)
  141. set_target_properties(Mbedtls::${component}
  142. PROPERTIES IMPORTED_LOCATION "${MBEDTLS_LIBRARIES}")
  143. else()
  144. add_library(Mbedtls::${component} INTERFACE IMPORTED)
  145. set_target_properties(Mbedtls::${component}
  146. PROPERTIES IMPORTED_LIBNAME "${MBEDTLS_LIBRARIES}")
  147. endif()
  148. set_target_properties(
  149. Mbedtls::${component} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  150. "${MBEDTLS_INCLUDE_DIRS}")
  151. endif()
  152. endif()
  153. # Now we've accounted for the 3-vs-1 library case:
  154. include(FindPackageHandleStandardArgs)
  155. find_package_handle_standard_args(MbedTLS DEFAULT_MSG MBEDTLS_LIBRARIES
  156. MBEDTLS_INCLUDE_DIRS)
  157. mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIB MBEDCRYPTO_LIB MBEDX509_LIB)