FindMBEDTLS.cmake 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. find_path(MBEDTLS_INCLUDE_DIR mbedtls/ssl.h PATH_SUFFIXES mbedtls2)
  2. find_library(MBEDTLS_LIBRARY mbedtls PATH_SUFFIXES mbedtls2)
  3. find_library(MBEDX509_LIBRARY mbedx509 PATH_SUFFIXES mbedtls2)
  4. find_library(MBEDCRYPTO_LIBRARY mbedcrypto PATH_SUFFIXES mbedtls2)
  5. set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
  6. set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY})
  7. if(NOT MBEDTLS_INCLUDE_DIR STREQUAL "MBEDTLS_INCLUDE_DIR-NOTFOUND")
  8. if(EXISTS ${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h)
  9. file(STRINGS ${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h MBEDTLS_VERSION_STR REGEX "^#define[ \t]+MBEDTLS_VERSION_STRING[\t ].*")
  10. else()
  11. file(STRINGS ${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h MBEDTLS_VERSION_STR REGEX "^#define[ \t]+MBEDTLS_VERSION_STRING[\t ].*")
  12. endif()
  13. string(REGEX REPLACE "^#define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"([.0-9]+)\".*" "\\1" MBEDTLS_VERSION ${MBEDTLS_VERSION_STR})
  14. endif()
  15. if(NOT MBEDTLS_INCLUDE_DIR STREQUAL "MBEDTLS_INCLUDE_DIR-NOTFOUND" AND MBEDTLS_VERSION VERSION_GREATER_EQUAL 3)
  16. # Once CMake 3.19 is required, we can enable HANDLE_VERSION_RANGE and use that
  17. if(MBEDTLS_FIND_REQUIRED)
  18. set(type FATAL_ERROR)
  19. else()
  20. set(type STATUS)
  21. endif()
  22. if(MBEDTLS_FIND_REQUIRED OR NOT MBEDTLS_FIND_QUIETLY)
  23. message(${type} "Could NOT find MBEDTLS: Found unsuitable version \"${MBEDTLS_VERSION}\", but a 2.x version is required (found ${MBEDTLS_INCLUDE_DIR})")
  24. endif()
  25. set(MBEDTLS_FOUND FALSE)
  26. else()
  27. include(FindPackageHandleStandardArgs)
  28. find_package_handle_standard_args(MBEDTLS
  29. REQUIRED_VARS MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY
  30. VERSION_VAR MBEDTLS_VERSION)
  31. endif()
  32. mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
  33. if(MBEDTLS_FOUND)
  34. add_library(MbedTLS::mbedcrypto UNKNOWN IMPORTED)
  35. set_target_properties(MbedTLS::mbedcrypto PROPERTIES
  36. IMPORTED_LOCATION "${MBEDCRYPTO_LIBRARY}"
  37. INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
  38. )
  39. add_library(MbedTLS::mbedx509 UNKNOWN IMPORTED)
  40. set_target_properties(MbedTLS::mbedx509 PROPERTIES
  41. IMPORTED_LOCATION "${MBEDX509_LIBRARY}"
  42. INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
  43. INTERFACE_LINK_LIBRARIES MbedTLS::mbedcrypto
  44. )
  45. add_library(MbedTLS::mbedtls UNKNOWN IMPORTED)
  46. set_target_properties(MbedTLS::mbedtls PROPERTIES
  47. IMPORTED_LOCATION "${MBEDTLS_LIBRARY}"
  48. INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
  49. INTERFACE_LINK_LIBRARIES MbedTLS::mbedx509
  50. )
  51. endif()