FindMbedTLS.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # original file from obs-studio:
  2. # https://github.com/obsproject/obs-studio
  3. # /cmake/Modules/FindMbedTLS.cmake
  4. #
  5. # Once done these will be defined:
  6. #
  7. # LIBMBEDTLS_FOUND
  8. # LIBMBEDTLS_INCLUDE_DIRS
  9. # LIBMBEDTLS_LIBRARIES
  10. #
  11. # For use in OBS:
  12. #
  13. # MBEDTLS_INCLUDE_DIR
  14. find_package(PkgConfig QUIET)
  15. if (PKG_CONFIG_FOUND)
  16. pkg_check_modules(_MBEDTLS QUIET mbedtls)
  17. endif()
  18. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  19. set(_lib_suffix 64)
  20. else()
  21. set(_lib_suffix 32)
  22. endif()
  23. # If we're on MacOS or Linux, please try to statically-link mbedtls.
  24. if(STATIC_MBEDTLS AND (APPLE OR UNIX))
  25. set(_MBEDTLS_LIBRARIES libmbedtls.a)
  26. set(_MBEDCRYPTO_LIBRARIES libmbedcrypto.a)
  27. set(_MBEDX509_LIBRARIES libmbedx509.a)
  28. endif()
  29. find_path(MBEDTLS_INCLUDE_DIR
  30. NAMES mbedtls/ssl.h
  31. HINTS
  32. ${MBEDTLS_PREFIX}
  33. PATHS
  34. /usr/include /usr/local/include /opt/local/include /sw/include
  35. PATH_SUFFIXES
  36. include)
  37. find_library(MBEDTLS_LIB
  38. NAMES ${_MBEDTLS_LIBRARIES} mbedtls libmbedtls
  39. HINTS
  40. ${MBEDTLS_PREFIX}
  41. PATHS
  42. /usr/lib /usr/local/lib /opt/local/lib /sw/lib
  43. PATH_SUFFIXES
  44. lib${_lib_suffix} lib
  45. libs${_lib_suffix} libs
  46. bin${_lib_suffix} bin
  47. ../lib${_lib_suffix} ../lib
  48. ../libs${_lib_suffix} ../libs
  49. ../bin${_lib_suffix} ../bin)
  50. find_library(MBEDCRYPTO_LIB
  51. NAMES ${_MBEDCRYPTO_LIBRARIES} mbedcrypto libmbedcrypto
  52. HINTS
  53. ${MBEDTLS_PREFIX}
  54. PATHS
  55. /usr/lib /usr/local/lib /opt/local/lib /sw/lib
  56. PATH_SUFFIXES
  57. lib${_lib_suffix} lib
  58. libs${_lib_suffix} libs
  59. bin${_lib_suffix} bin
  60. ../lib${_lib_suffix} ../lib
  61. ../libs${_lib_suffix} ../libs
  62. ../bin${_lib_suffix} ../bin)
  63. find_library(MBEDX509_LIB
  64. NAMES ${_MBEDX509_LIBRARIES} mbedx509 libmbedx509
  65. HINTS
  66. ${MBEDTLS_PREFIX}
  67. PATHS
  68. /usr/lib /usr/local/lib /opt/local/lib /sw/lib
  69. PATH_SUFFIXES
  70. lib${_lib_suffix} lib
  71. libs${_lib_suffix} libs
  72. bin${_lib_suffix} bin
  73. ../lib${_lib_suffix} ../lib
  74. ../libs${_lib_suffix} ../libs
  75. ../bin${_lib_suffix} ../bin)
  76. # Sometimes mbedtls is split between three libs, and sometimes it isn't.
  77. # If it isn't, let's check if the symbols we need are all in MBEDTLS_LIB.
  78. if(MBEDTLS_LIB AND NOT MBEDCRYPTO_LIB AND NOT MBEDX509_LIB)
  79. set(CMAKE_REQUIRED_LIBRARIES ${MBEDTLS_LIB})
  80. set(CMAKE_REQUIRED_INCLUDES ${MBEDTLS_INCLUDE_DIR})
  81. check_symbol_exists(mbedtls_x509_crt_init "mbedtls/x509_crt.h" MBEDTLS_INCLUDES_X509)
  82. check_symbol_exists(mbedtls_sha256_init "mbedtls/sha256.h" MBEDTLS_INCLUDES_CRYPTO)
  83. unset(CMAKE_REQUIRED_INCLUDES)
  84. unset(CMAKE_REQUIRED_LIBRARIES)
  85. endif()
  86. # If we find all three libraries, then go ahead.
  87. if(MBEDTLS_LIB AND MBEDCRYPTO_LIB AND MBEDX509_LIB)
  88. set(LIBMBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
  89. set(LIBMBEDTLS_LIBRARIES ${MBEDTLS_LIB} ${MBEDCRYPTO_LIB} ${MBEDX509_LIB})
  90. set(MBEDTLS_INCLUDE_DIRS ${LIBMBEDTLS_INCLUDE_DIRS})
  91. set(MBEDTLS_LIBRARIES ${LIBMBEDTLS_LIBRARIES})
  92. # Otherwise, if we find MBEDTLS_LIB, and it has both CRYPTO and x509
  93. # within the single lib (i.e. a windows build environment), then also
  94. # feel free to go ahead.
  95. elseif(MBEDTLS_LIB AND MBEDTLS_INCLUDES_CRYPTO AND MBEDTLS_INCLUDES_X509)
  96. set(LIBMBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
  97. set(LIBMBEDTLS_LIBRARIES ${MBEDTLS_LIB})
  98. set(MBEDTLS_INCLUDE_DIRS ${LIBMBEDTLS_INCLUDE_DIRS})
  99. set(MBEDTLS_LIBRARIES ${LIBMBEDTLS_LIBRARIES})
  100. endif()
  101. # Now we've accounted for the 3-vs-1 library case:
  102. include(FindPackageHandleStandardArgs)
  103. find_package_handle_standard_args(MbedTLS DEFAULT_MSG MBEDTLS_LIBRARIES MBEDTLS_INCLUDE_DIRS)
  104. mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES MBEDTLS_INCLUDE_DIRS)