roman | 25263e9 | 2024-05-13 15:43:32 +0200 | [diff] [blame^] | 1 | # - Try to find MbedTLS |
| 2 | # Once done this will define |
| 3 | # |
| 4 | # MBEDTLS_FOUND - MbedTLS was found |
| 5 | # MBEDTLS_INCLUDE_DIR - MbedTLS include directories |
| 6 | # MBEDTLS_LIBRARIES - link these to use MbedTLS |
| 7 | # MBEDTLS_VERSION - version of MbedTLS |
| 8 | # |
| 9 | # Author Roman Janota <janota@cesnet.cz> |
| 10 | # Copyright (c) 2024 CESNET, z.s.p.o. |
| 11 | # |
| 12 | # Redistribution and use in source and binary forms, with or without |
| 13 | # modification, are permitted provided that the following conditions |
| 14 | # are met: |
| 15 | # |
| 16 | # 1. Redistributions of source code must retain the copyright |
| 17 | # notice, this list of conditions and the following disclaimer. |
| 18 | # 2. Redistributions in binary form must reproduce the copyright |
| 19 | # notice, this list of conditions and the following disclaimer in the |
| 20 | # documentation and/or other materials provided with the distribution. |
| 21 | # 3. The name of the author may not be used to endorse or promote products |
| 22 | # derived from this software without specific prior written permission. |
| 23 | # |
| 24 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 25 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 26 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 27 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 29 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 33 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | # |
| 35 | include(FindPackageHandleStandardArgs) |
| 36 | |
| 37 | if(MBEDTLS_LIBRARIES AND MBEDTLS_INCLUDE_DIR) |
| 38 | # in cache already |
| 39 | set(MBEDTLS_FOUND TRUE) |
| 40 | else() |
| 41 | find_path(MBEDTLS_INCLUDE_DIR |
| 42 | NAMES |
| 43 | mbedtls/ssl.h |
| 44 | PATHS |
| 45 | /opt/local/include |
| 46 | /sw/include |
| 47 | ${CMAKE_INCLUDE_PATH} |
| 48 | ${CMAKE_INSTALL_PREFIX}/include |
| 49 | ) |
| 50 | |
| 51 | find_library(MBEDTLS_LIBRARY |
| 52 | NAMES |
| 53 | libmbedtls.so |
| 54 | PATHS |
| 55 | /usr/lib |
| 56 | /usr/lib64 |
| 57 | /opt/local/lib |
| 58 | /sw/lib |
| 59 | ${CMAKE_LIBRARY_PATH} |
| 60 | ${CMAKE_INSTALL_PREFIX}/lib |
| 61 | ) |
| 62 | |
| 63 | find_library(MBEDX509_LIBRARY |
| 64 | NAMES |
| 65 | libmbedx509.so |
| 66 | PATHS |
| 67 | /usr/lib |
| 68 | /usr/lib64 |
| 69 | /opt/local/lib |
| 70 | /sw/lib |
| 71 | ${CMAKE_LIBRARY_PATH} |
| 72 | ${CMAKE_INSTALL_PREFIX}/lib |
| 73 | ) |
| 74 | |
| 75 | find_library(MBEDCRYPTO_LIBRARY |
| 76 | NAMES |
| 77 | libmbedcrypto.so |
| 78 | PATHS |
| 79 | /usr/lib |
| 80 | /usr/lib64 |
| 81 | /opt/local/lib |
| 82 | /sw/lib |
| 83 | ${CMAKE_LIBRARY_PATH} |
| 84 | ${CMAKE_INSTALL_PREFIX}/lib |
| 85 | ) |
| 86 | |
| 87 | if(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY) |
| 88 | # learn MbedTLS version |
| 89 | if(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h") |
| 90 | file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h" MBEDTLS_VERSION |
| 91 | REGEX "#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+\"([0-9]+\.[0-9]+\.[0-9]+)\"") |
| 92 | string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" MBEDTLS_VERSION ${MBEDTLS_VERSION}) |
| 93 | endif() |
| 94 | if(NOT MBEDTLS_VERSION) |
| 95 | message(STATUS "MBEDTLS_VERSION not found, assuming MbedTLS is too old and cannot be used!") |
| 96 | set(MBEDTLS_INCLUDE_DIR "MBEDTLS_INCLUDE_DIR-NOTFOUND") |
| 97 | set(MBEDTLS_LIBRARY "MBEDTLS_LIBRARY-NOTFOUND") |
| 98 | endif() |
| 99 | endif() |
| 100 | |
| 101 | set(MBEDTLS_INCLUDE_DIR ${MBEDTLS_INCLUDE_DIR}) |
| 102 | set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY}) |
| 103 | |
| 104 | find_package_handle_standard_args(MbedTLS FOUND_VAR MBEDTLS_FOUND |
| 105 | REQUIRED_VARS MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES |
| 106 | VERSION_VAR MBEDTLS_VERSION) |
| 107 | |
| 108 | # show the MBEDTLS_INCLUDE_DIR and MBEDTLS_LIBRARIES variables only in the advanced view |
| 109 | mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES) |
| 110 | endif() |