xref: /curl/m4/curl-mbedtls.m4 (revision 26ab9027)
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
25dnl ----------------------------------------------------
26dnl check for mbedTLS
27dnl ----------------------------------------------------
28AC_DEFUN([CURL_WITH_MBEDTLS], [
29
30if test "x$OPT_MBEDTLS" != xno; then
31  _cppflags=$CPPFLAGS
32  _ldflags=$LDFLAGS
33  ssl_msg=
34
35  if test X"$OPT_MBEDTLS" != Xno; then
36
37    if test "$OPT_MBEDTLS" = "yes"; then
38      OPT_MBEDTLS=""
39    fi
40
41    if test -z "$OPT_MBEDTLS" ; then
42      dnl check for lib first without setting any new path
43
44      AC_CHECK_LIB(mbedtls, mbedtls_havege_init,
45      dnl libmbedtls found, set the variable
46      [
47        AC_DEFINE(USE_MBEDTLS, 1, [if mbedTLS is enabled])
48        AC_SUBST(USE_MBEDTLS, [1])
49        MBEDTLS_ENABLED=1
50        USE_MBEDTLS="yes"
51        ssl_msg="mbedTLS"
52        test mbedtls != "$DEFAULT_SSL_BACKEND" || VALID_DEFAULT_SSL_BACKEND=yes
53      ], [], -lmbedx509 -lmbedcrypto)
54    fi
55
56    addld=""
57    addlib=""
58    addcflags=""
59    mbedtlslib=""
60
61    if test "x$USE_MBEDTLS" != "xyes"; then
62      dnl add the path and test again
63      addld=-L$OPT_MBEDTLS/lib$libsuff
64      addcflags=-I$OPT_MBEDTLS/include
65      mbedtlslib=$OPT_MBEDTLS/lib$libsuff
66
67      LDFLAGS="$LDFLAGS $addld"
68      if test "$addcflags" != "-I/usr/include"; then
69        CPPFLAGS="$CPPFLAGS $addcflags"
70      fi
71
72      AC_CHECK_LIB(mbedtls, mbedtls_ssl_init,
73        [
74        AC_DEFINE(USE_MBEDTLS, 1, [if mbedTLS is enabled])
75        AC_SUBST(USE_MBEDTLS, [1])
76        MBEDTLS_ENABLED=1
77        USE_MBEDTLS="yes"
78        ssl_msg="mbedTLS"
79        test mbedtls != "$DEFAULT_SSL_BACKEND" || VALID_DEFAULT_SSL_BACKEND=yes
80        ],
81        [
82          CPPFLAGS=$_cppflags
83          LDFLAGS=$_ldflags
84        ], -lmbedx509 -lmbedcrypto)
85    fi
86
87    if test "x$USE_MBEDTLS" = "xyes"; then
88      AC_MSG_NOTICE([detected mbedTLS])
89      check_for_ca_bundle=1
90
91      LIBS="-lmbedtls -lmbedx509 -lmbedcrypto $LIBS"
92
93      if test -n "$mbedtlslib"; then
94        dnl when shared libs were found in a path that the run-time
95        dnl linker doesn't search through, we need to add it to
96        dnl CURL_LIBRARY_PATH to prevent further configure tests to fail
97        dnl due to this
98        if test "x$cross_compiling" != "xyes"; then
99          CURL_LIBRARY_PATH="$CURL_LIBRARY_PATH:$mbedtlslib"
100          export CURL_LIBRARY_PATH
101          AC_MSG_NOTICE([Added $mbedtlslib to CURL_LIBRARY_PATH])
102        fi
103      fi
104      LIBCURL_PC_REQUIRES_PRIVATE="$LIBCURL_PC_REQUIRES_PRIVATE mbedtls"
105    fi
106
107  fi dnl mbedTLS not disabled
108
109  test -z "$ssl_msg" || ssl_backends="${ssl_backends:+$ssl_backends, }$ssl_msg"
110fi
111
112])
113