thanks to Dolby for discovering this fuckup
[asdcplib.git] / m4 / ax_lib_openssl.m4
1 # ===========================================================================
2 #             ax_lib_openssl.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 #   AX_LIB_OPENSSL([MINIMUM-VERSION])
8 #
9 # DESCRIPTION
10 #
11 #   This macro provides tests of availability of OpenSSL of
12 #   particular version or newer. This macros checks for OpenSSL
13 #   headers and libraries and defines compilation flags
14 #
15 #   Macro supports following options and their values:
16 #
17 #   1) Single-option usage:
18 #
19 #     --with-openssl - yes, no or path to OpenSSL installation prefix
20 #
21 #   This macro calls:
22 #
23 #     AC_SUBST(OPENSSL_CFLAGS)
24 #     AC_SUBST(OPENSSL_LDFLAGS)
25 #     AC_SUBST(OPENSSL_VERSION) - only if version requirement is used
26 #
27 #   And sets:
28 #
29 #     HAVE_OPENSSL
30 #
31 # LAST MODIFICATION
32 #
33 #   2008-04-12
34 #
35 # COPYLEFT
36 #
37 #   Copyright (c) 2008 Mateusz Loskot <mateusz@loskot.net>
38 #
39 #   Copying and distribution of this file, with or without modification, are
40 #   permitted in any medium without royalty provided the copyright notice
41 #   and this notice are preserved.
42
43 AC_DEFUN([AX_LIB_OPENSSL],
44 [
45     AC_ARG_WITH([openssl],
46         AC_HELP_STRING([--with-openssl=@<:@ARG@:>@],
47             [use OpenSSL from given prefix (ARG=path); check standard prefixes (ARG=yes); disable (ARG=no)]
48         ),
49         [
50         if test "$withval" = "yes"; then
51             if test -d /var/local/ssl/include ; then
52                 openssl_prefix=/var/local/ssl
53             elif test -d /usr/local/ssl/include ; then
54                 openssl_prefix=/usr/local/ssl
55             elif test -d /usr/lib/ssl/include ; then
56                 openssl_prefix=/usr/lib/ssl
57             elif test -d /usr/include/openssl ; then
58                 openssl_prefix=/usr
59             else
60                 openssl_prefix=""
61             fi
62             openssl_requested="yes"
63         elif test -d "$withval"; then
64             openssl_prefix="$withval"
65             openssl_requested="yes"
66         else
67             openssl_prefix=""
68             openssl_requested="no"
69         fi
70         ],
71         [
72         dnl Default behavior is implicit yes
73         if test -d /var/local/ssl/include ; then
74             openssl_prefix=/var/local/ssl
75         elif test -d /usr/local/ssl/include ; then
76             openssl_prefix=/usr/local/ssl
77         elif test -d /usr/lib/ssl/include ; then
78             openssl_prefix=/usr/lib/ssl
79         elif test -d /usr/include/openssl ; then
80             openssl_prefix=/usr
81         else
82             openssl_prefix=""
83         fi
84         ]
85     )
86
87     OPENSSL_CPPFLAGS=""
88     OPENSSL_LDFLAGS=""
89     OPENSSL_VERSION=""
90
91     dnl
92     dnl Collect include/lib paths and flags
93     dnl
94     run_openssl_test="no"
95
96     if test -n "$openssl_prefix"; then
97         openssl_include_dir="$openssl_prefix/include"
98         openssl_ldflags="-L$openssl_prefix/lib"
99         run_openssl_test="yes"
100     elif test "$openssl_requested" = "yes"; then
101         if test -n "$openssl_include_dir" -a -n "$openssl_lib_flags"; then
102             run_openssl_test="yes"
103         fi
104     else
105         run_openssl_test="no"
106     fi
107
108     openssl_libs="-lssl -lcrypto"
109
110     dnl
111     dnl Check OpenSSL files
112     dnl
113     if test "$run_openssl_test" = "yes"; then
114
115         saved_CPPFLAGS="$CPPFLAGS"
116         CPPFLAGS="$CPPFLAGS -I$openssl_include_dir"
117
118         saved_LDFLAGS="$LDFLAGS"
119         LDFLAGS="$LDFLAGS $openssl_ldflags"
120
121         saved_LIBS="$LIBS"
122         LIBS="$openssl_libs $LIBS"
123
124         dnl
125         dnl Check OpenSSL headers
126         dnl
127         AC_MSG_CHECKING([for OpenSSL headers in $openssl_include_dir])
128
129         AC_LANG_PUSH([C])
130         AC_COMPILE_IFELSE([
131             AC_LANG_PROGRAM(
132                 [[
133 @%:@include <openssl/opensslv.h>
134 @%:@include <openssl/ssl.h>
135 @%:@include <openssl/crypto.h>
136                 ]],
137                 [[]]
138             )],
139             [
140             OPENSSL_CPPFLAGS="-I$openssl_include_dir"
141             openssl_header_found="yes"
142             AC_MSG_RESULT([found])
143             ],
144             [
145             openssl_header_found="no"
146             AC_MSG_RESULT([not found])
147             ]
148         )
149         AC_LANG_POP([C])
150
151         dnl
152         dnl Check OpenSSL libraries
153         dnl
154         if test "$openssl_header_found" = "yes"; then
155
156             AC_MSG_CHECKING([for OpenSSL libraries])
157
158             AC_LANG_PUSH([C])
159             AC_LINK_IFELSE([
160                 AC_LANG_PROGRAM(
161                     [[
162 @%:@include <openssl/opensslv.h>
163 @%:@include <openssl/ssl.h>
164 @%:@include <openssl/crypto.h>
165 #if (OPENSSL_VERSION_NUMBER < 0x0090700f)
166 #endif
167                     ]],
168                     [[
169 SSL_library_init();
170 SSLeay();
171                     ]]
172                 )],
173                 [
174                 OPENSSL_LDFLAGS="$openssl_ldflags"
175                 OPENSSL_LIBS="$openssl_libs"
176                 openssl_lib_found="yes"
177                 AC_MSG_RESULT([found])
178                 ],
179                 [
180                 openssl_lib_found="no"
181                 AC_MSG_RESULT([not found])
182                 ]
183             )
184             AC_LANG_POP([C])
185         fi
186
187         CPPFLAGS="$saved_CPPFLAGS"
188         LDFLAGS="$saved_LDFLAGS"
189         LIBS="$saved_LIBS"
190     fi
191
192     AC_MSG_CHECKING([for OpenSSL])
193
194     if test "$run_openssl_test" = "yes"; then
195         if test "$openssl_header_found" = "yes" -a "$openssl_lib_found" = "yes"; then
196
197             AC_SUBST([OPENSSL_CPPFLAGS])
198             AC_SUBST([OPENSSL_LDFLAGS])
199             AC_SUBST([OPENSSL_LIBS])
200
201             HAVE_OPENSSL="yes"
202         else
203             HAVE_OPENSSL="no"
204         fi
205
206         AC_MSG_RESULT([$HAVE_OPENSSL])
207
208         dnl
209         dnl Check OpenSSL version
210         dnl
211         if test "$HAVE_OPENSSL" = "yes"; then
212
213             openssl_version_req=ifelse([$1], [], [], [$1])
214
215             if test  -n "$openssl_version_req"; then
216
217                 AC_MSG_CHECKING([if OpenSSL version is >= $openssl_version_req])
218
219                 if test -f "$openssl_include_dir/openssl/opensslv.h"; then
220
221                     OPENSSL_VERSION=`grep OPENSSL_VERSION_TEXT $openssl_include_dir/openssl/opensslv.h \
222                                     | grep -v fips | grep -v PTEXT | cut -f 2 | tr -d \"`
223                     AC_SUBST([OPENSSL_VERSION])
224
225                     dnl Decompose required version string and calculate numerical representation
226                     openssl_version_req_major=`expr $openssl_version_req : '\([[0-9]]*\)'`
227                     openssl_version_req_minor=`expr $openssl_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
228                     openssl_version_req_revision=`expr $openssl_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
229                     openssl_version_req_patch=`expr $openssl_version_req : '[[0-9]]*\.[[0-9]]*\.[[0-9]]*\([[a-z]]*\)'`
230                     if test "x$openssl_version_req_revision" = "x"; then
231                         openssl_version_req_revision="0"
232                     fi
233                     if test "x$openssl_version_req_patch" = "x"; then
234                         openssl_version_req_patch="\`"
235                     fi
236
237                     openssl_version_req_number=`expr $openssl_version_req_major \* $((0x10000000)) \
238                                                \+ $openssl_version_req_minor \* $((0x100000)) \
239                                                \+ $openssl_version_req_revision \* $((0x1000)) \
240                                                \+ $((1 + $(printf "%d" \'$openssl_version_req_patch) - $(printf "%d" \'a))) \* $((0x10)) \
241                                                \+ $((0xf))`
242
243                     dnl Calculate numerical representation of detected version
244                     openssl_version_number=`expr $(($(grep OPENSSL_VERSION_NUMBER $openssl_include_dir/openssl/opensslv.h | cut -f 2 | tr -d L)))`
245
246                     openssl_version_check=`expr $openssl_version_number \>\= $openssl_version_req_number`
247                     if test "$openssl_version_check" = "1"; then
248                         AC_MSG_RESULT([yes])
249                     else
250                         AC_MSG_RESULT([no])
251                         AC_MSG_WARN([Found $OPENSSL_VERSION, which is older than required. Possible compilation failure.])
252                     fi
253                 else
254                     AC_MSG_RESULT([no])
255                     AC_MSG_WARN([Missing header openssl/opensslv.h. Unable to determine OpenSSL version.])
256                 fi
257             fi
258         fi
259
260     else
261         HAVE_OPENSSL="no"
262         AC_MSG_RESULT([$HAVE_OPENSSL])
263
264         if test "$openssl_requested" = "yes"; then
265             AC_MSG_WARN([OpenSSL support requested but headers or library not found. Specify valid prefix of OpenSSL using --with-openssl=@<:@DIR@:>@])
266         fi
267     fi
268     if test "$HAVE_OPENSSL" = "yes"; then
269         CPPFLAGS="$CPPFLAGS $OPENSSL_CPPFLAGS -DHAVE_SSL=1"
270         LDFLAGS="$LDFLAGS $OPENSSL_LDFLAGS $OPENSSL_LIBS"
271     fi
272 ])