FindMbedTLS.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ############################################################################
  2. # FindMdebTLS.txt
  3. # Copyright (C) 2015 Belledonne Communications, Grenoble France
  4. #
  5. ############################################################################
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. ############################################################################
  22. #
  23. # - Find the mbedTLS include file and library
  24. #
  25. # MBEDTLS_FOUND - system has mbedTLS
  26. # MBEDTLS_INCLUDE_DIRS - the mbedTLS include directory
  27. # MBEDTLS_LIBRARIES - The libraries needed to use mbedTLS
  28. include(CMakePushCheckState)
  29. include(CheckIncludeFile)
  30. include(CheckCSourceCompiles)
  31. include(CheckSymbolExists)
  32. find_path(MBEDTLS_INCLUDE_DIRS
  33. NAMES mbedtls/ssl.h
  34. PATH_SUFFIXES include
  35. )
  36. # find the three mbedtls library
  37. find_library(MBEDTLS_LIBRARY
  38. NAMES mbedtls
  39. )
  40. find_library(MBEDX509_LIBRARY
  41. NAMES mbedx509
  42. )
  43. find_library(MBEDCRYPTO_LIBRARY
  44. NAMES mbedcrypto
  45. )
  46. cmake_push_check_state(RESET)
  47. set(CMAKE_REQUIRED_INCLUDES ${MBEDTLS_INCLUDE_DIRS} ${CMAKE_REQUIRED_INCLUDES_${BUILD_TYPE}})
  48. list(APPEND CMAKE_REQUIRED_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY})
  49. # check we have a mbedTLS version 2 or above(all functions are prefixed mbedtls_)
  50. if(MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY)
  51. check_symbol_exists(mbedtls_ssl_init "mbedtls/ssl.h" MBEDTLS_V2)
  52. if(NOT MBEDTLS_V2)
  53. message ("MESSAGE: NO MBEDTLS_V2")
  54. message ("MESSAGE: MBEDTLS_LIBRARY=" ${MBEDTLS_LIBRARY})
  55. message ("MESSAGE: MBEDX509_LIBRARY=" ${MBEDX509_LIBRARY})
  56. message ("MESSAGE: MBEDCRYPTO_LIBRARY=" ${MBEDCRYPTO_LIBRARY})
  57. set (MBEDTLS_VERSION_1 On)
  58. else()
  59. # Are we mbdetls 2 or 3?
  60. # from version 3 and on, version number is given in include/mbedtls/build_info.h.
  61. # This file does not exists before version 3
  62. if (EXISTS "${MBEDTLS_INCLUDE_DIRS}/mbedtls/build_info.h")
  63. set (MBEDTLS_VERSION_3 On)
  64. else()
  65. set (MBEDTLS_VERSION_2 On)
  66. endif()
  67. endif()
  68. endif()
  69. check_symbol_exists(mbedtls_ssl_conf_dtls_srtp_protection_profiles "mbedtls/ssl.h" DTLS_SRTP_AVAILABLE)
  70. # Define the imported target for the three mbedtls libraries
  71. foreach(targetname "mbedtls" "mbedx509" "mbedcrypto")
  72. string(TOUPPER ${targetname} varprefix)
  73. add_library(${targetname} SHARED IMPORTED)
  74. if (WIN32)
  75. set_target_properties(${targetname} PROPERTIES
  76. INTERFACE_INCLUDE_DIRECTORIES "${${varprefix}_INCLUDE_DIRS}"
  77. IMPORTED_IMPLIB "${${varprefix}_LIBRARY}"
  78. )
  79. else()
  80. set_target_properties(${targetname} PROPERTIES
  81. INTERFACE_INCLUDE_DIRECTORIES "${${varprefix}_INCLUDE_DIRS}"
  82. IMPORTED_LOCATION "${${varprefix}_LIBRARY}"
  83. )
  84. endif()
  85. endforeach()
  86. unset(varprefix)
  87. # MBEDTLS_LIBRARIES only needs to contain the name of the targets
  88. set (MBEDTLS_LIBRARIES
  89. mbedtls
  90. mbedx509
  91. mbedcrypto
  92. )
  93. include(FindPackageHandleStandardArgs)
  94. find_package_handle_standard_args(MbedTLS
  95. DEFAULT_MSG
  96. MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARIES
  97. )
  98. cmake_pop_check_state()
  99. mark_as_advanced(MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARIES DTLS_SRTP_AVAILABLE)