FindMbedTLS.cmake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. # Find the mbedTLS library
  25. #
  26. # Input variables:
  27. #
  28. # - `MBEDTLS_INCLUDE_DIR`: The mbedTLS include directory.
  29. # - `MBEDTLS_LIBRARY`: Path to `mbedtls` library.
  30. # - `MBEDX509_LIBRARY`: Path to `mbedx509` library.
  31. # - `MBEDCRYPTO_LIBRARY`: Path to `mbedcrypto` library.
  32. #
  33. # Result variables:
  34. #
  35. # - `MBEDTLS_FOUND`: System has mbedTLS.
  36. # - `MBEDTLS_INCLUDE_DIRS`: The mbedTLS include directories.
  37. # - `MBEDTLS_LIBRARIES`: The mbedTLS library names.
  38. # - `MBEDTLS_LIBRARY_DIRS`: The mbedTLS library directories.
  39. # - `MBEDTLS_PC_REQUIRES`: The mbedTLS pkg-config packages.
  40. # - `MBEDTLS_CFLAGS`: Required compiler flags.
  41. # - `MBEDTLS_VERSION`: Version of mbedTLS.
  42. if(DEFINED MBEDTLS_INCLUDE_DIRS AND NOT DEFINED MBEDTLS_INCLUDE_DIR)
  43. message(WARNING "MBEDTLS_INCLUDE_DIRS is deprecated, use MBEDTLS_INCLUDE_DIR instead.")
  44. set(MBEDTLS_INCLUDE_DIR "${MBEDTLS_INCLUDE_DIRS}")
  45. unset(MBEDTLS_INCLUDE_DIRS)
  46. endif()
  47. if(CURL_USE_PKGCONFIG AND
  48. NOT DEFINED MBEDTLS_INCLUDE_DIR AND
  49. NOT DEFINED MBEDTLS_LIBRARY AND
  50. NOT DEFINED MBEDX509_LIBRARY AND
  51. NOT DEFINED MBEDCRYPTO_LIBRARY)
  52. find_package(PkgConfig QUIET)
  53. pkg_check_modules(MBEDTLS "mbedtls")
  54. pkg_check_modules(MBEDX509 "mbedx509")
  55. pkg_check_modules(MBEDCRYPTO "mbedcrypto")
  56. endif()
  57. if(MBEDTLS_FOUND AND MBEDX509_FOUND AND MBEDCRYPTO_FOUND)
  58. list(APPEND MBEDTLS_LIBRARIES ${MBEDX509_LIBRARIES} ${MBEDCRYPTO_LIBRARIES})
  59. list(REMOVE_DUPLICATES MBEDTLS_LIBRARIES)
  60. set(MBEDTLS_PC_REQUIRES "mbedtls")
  61. string(REPLACE ";" " " MBEDTLS_CFLAGS "${MBEDTLS_CFLAGS}")
  62. message(STATUS "Found MbedTLS (via pkg-config): ${MBEDTLS_INCLUDE_DIRS} (found version \"${MBEDTLS_VERSION}\")")
  63. else()
  64. find_path(MBEDTLS_INCLUDE_DIR NAMES "mbedtls/ssl.h")
  65. find_library(MBEDTLS_LIBRARY NAMES "mbedtls" "libmbedtls")
  66. find_library(MBEDX509_LIBRARY NAMES "mbedx509" "libmbedx509")
  67. find_library(MBEDCRYPTO_LIBRARY NAMES "mbedcrypto" "libmbedcrypto")
  68. unset(MBEDTLS_VERSION CACHE)
  69. if(MBEDTLS_INCLUDE_DIR)
  70. if(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h") # 3.x
  71. set(_version_header "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
  72. elseif(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h") # 2.x
  73. set(_version_header "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
  74. else()
  75. unset(_version_header)
  76. endif()
  77. if(_version_header)
  78. set(_version_regex "#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"([0-9.]+)\"")
  79. file(STRINGS "${_version_header}" _version_str REGEX "${_version_regex}")
  80. string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}")
  81. set(MBEDTLS_VERSION "${_version_str}")
  82. unset(_version_regex)
  83. unset(_version_str)
  84. unset(_version_header)
  85. endif()
  86. endif()
  87. include(FindPackageHandleStandardArgs)
  88. find_package_handle_standard_args(MbedTLS
  89. REQUIRED_VARS
  90. MBEDTLS_INCLUDE_DIR
  91. MBEDTLS_LIBRARY
  92. MBEDX509_LIBRARY
  93. MBEDCRYPTO_LIBRARY
  94. VERSION_VAR
  95. MBEDTLS_VERSION
  96. )
  97. if(MBEDTLS_FOUND)
  98. set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
  99. set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY})
  100. endif()
  101. mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
  102. endif()