1# mbed TLS (PolarSSL) LIBMBEDTLS_CHECK_CONFIG ([DEFAULT-ACTION])
2# ----------------------------------------------------------
3# Derived from libssh2.m4 written by
4#    Alexander Vladishev                      Oct-26-2009
5#    Dmitry Borovikov                         Feb-13-2010
6#
7# Checks for mbed TLS (PolarSSL) library libpolarssl.  DEFAULT-ACTION is the
8# string yes or no to specify whether to default to --with-mbedtls or
9# --without-mbedtls. If not supplied, DEFAULT-ACTION is no.
10#
11# This macro #defines HAVE_POLARSSL if a required header files are
12# found, and sets @MBEDTLS_LDFLAGS@, @MBEDTLS_CFLAGS@ and @MBEDTLS_LIBS@
13# to the necessary values.
14#
15# Users may override the detected values by doing something like:
16# MBEDTLS_LIBS="-lpolarssl" MBEDTLS_CFLAGS="-I/usr/myinclude" ./configure
17#
18# This macro is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21
22AC_DEFUN([LIBMBEDTLS_TRY_LINK],
23[
24AC_TRY_LINK(
25[
26#include <polarssl/ssl.h>
27],
28[
29	ssl_context	ssl;
30
31	ssl_init(&ssl);
32],
33found_mbedtls="yes",)
34])dnl
35
36AC_DEFUN([LIBMBEDTLS_ACCEPT_VERSION],
37[
38	# Zabbix minimal supported version of libmbedtls:
39	minimal_mbedtls_version_major=1
40	minimal_mbedtls_version_minor=3
41	minimal_mbedtls_version_patch=9
42
43	# get version
44	found_mbedtls_version_major=`cat $1 | $EGREP \#define.*POLARSSL_VERSION_MAJOR | $AWK '{print @S|@3;}'`
45	found_mbedtls_version_minor=`cat $1 | $EGREP \#define.*POLARSSL_VERSION_MINOR | $AWK '{print @S|@3;}'`
46	found_mbedtls_version_patch=`cat $1 | $EGREP \#define.*POLARSSL_VERSION_PATCH | $AWK '{print @S|@3;}'`
47
48	if test $((found_mbedtls_version_major)) -gt $((minimal_mbedtls_version_major)); then
49		accept_mbedtls_version="yes"
50	elif test $((found_mbedtls_version_major)) -lt $((minimal_mbedtls_version_major)); then
51		accept_mbedtls_version="no"
52	elif test $((found_mbedtls_version_minor)) -gt $((minimal_mbedtls_version_minor)); then
53		accept_mbedtls_version="yes"
54	elif test $((found_mbedtls_version_minor)) -lt $((minimal_mbedtls_version_minor)); then
55		accept_mbedtls_version="no"
56	elif test $((found_mbedtls_version_patch)) -ge $((minimal_mbedtls_version_patch)); then
57		accept_mbedtls_version="yes"
58	else
59		accept_mbedtls_version="no"
60	fi;
61])dnl
62
63AC_DEFUN([LIBMBEDTLS_CHECK_CONFIG],
64[
65  AC_ARG_WITH(mbedtls,[
66If you want to use encryption provided by mbed TLS (PolarSSL) library:
67AC_HELP_STRING([--with-mbedtls@<:@=DIR@:>@],[use mbed TLS (PolarSSL) package @<:@default=no@:>@, DIR is the libpolarssl install directory.])],
68    [
69	if test "$withval" = "no"; then
70	    want_mbedtls="no"
71	    _libmbedtls_dir="no"
72	elif test "$withval" = "yes"; then
73	    want_mbedtls="yes"
74	    _libmbedtls_dir="no"
75	else
76	    want_mbedtls="yes"
77	    _libmbedtls_dir=$withval
78	fi
79	accept_mbedtls_version="no"
80    ],[want_mbedtls=ifelse([$1],,[no],[$1])]
81  )
82
83  if test "x$want_mbedtls" = "xyes"; then
84     AC_MSG_CHECKING(for mbed TLS (PolarSSL) support)
85
86     if test "x$_libmbedtls_dir" = "xno"; then
87       if test -f /usr/local/include/polarssl/version.h; then
88         MBEDTLS_CFLAGS=-I/usr/local/include
89         MBEDTLS_LDFLAGS=-L/usr/local/lib
90         MBEDTLS_LIBS="-lpolarssl"
91         found_mbedtls="yes"
92         LIBMBEDTLS_ACCEPT_VERSION([/usr/local/include/polarssl/version.h])
93       elif test -f /usr/include/polarssl/version.h; then
94         MBEDTLS_CFLAGS=-I/usr/include
95         MBEDTLS_LDFLAGS=-L/usr/lib
96         MBEDTLS_LIBS="-lpolarssl"
97         found_mbedtls="yes"
98         LIBMBEDTLS_ACCEPT_VERSION([/usr/include/polarssl/version.h])
99       else			# libraries are not found in default directories
100         found_mbedtls="no"
101         AC_MSG_RESULT(no)
102       fi
103     else
104       if test -f $_libmbedtls_dir/include/polarssl/version.h; then
105         MBEDTLS_CFLAGS=-I$_libmbedtls_dir/include
106         MBEDTLS_LDFLAGS=-L$_libmbedtls_dir/lib
107         MBEDTLS_LIBS="-lpolarssl"
108         found_mbedtls="yes"
109         LIBMBEDTLS_ACCEPT_VERSION([$_libmbedtls_dir/include/polarssl/version.h])
110       else
111         found_mbedtls="no"
112         AC_MSG_RESULT(no)
113       fi
114     fi
115  fi
116
117  if test "x$found_mbedtls" = "xyes"; then
118    am_save_cflags="$CFLAGS"
119    am_save_ldflags="$LDFLAGS"
120    am_save_libs="$LIBS"
121
122    CFLAGS="$CFLAGS $MBEDTLS_CFLAGS"
123    LDFLAGS="$LDFLAGS $MBEDTLS_LDFLAGS"
124    LIBS="$LIBS $MBEDTLS_LIBS"
125
126    found_mbedtls="no"
127    LIBMBEDTLS_TRY_LINK([no])
128
129    CFLAGS="$am_save_cflags"
130    LDFLAGS="$am_save_ldflags"
131    LIBS="$am_save_libs"
132
133    if test "x$found_mbedtls" = "xyes"; then
134      AC_DEFINE([HAVE_POLARSSL], 1, [Define to 1 if you have the 'libpolarssl' library (-lpolarssl)])
135      AC_MSG_RESULT(yes)
136    else
137      AC_MSG_RESULT(no)
138      MBEDTLS_CFLAGS=""
139      MBEDTLS_LDFLAGS=""
140      MBEDTLS_LIBS=""
141    fi
142  fi
143
144  AC_SUBST(MBEDTLS_CFLAGS)
145  AC_SUBST(MBEDTLS_LDFLAGS)
146  AC_SUBST(MBEDTLS_LIBS)
147
148])dnl
149