SLVS has a weird UL and was not being recognized as a channel label
[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 a
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 # Copyright (c) 2008-2009 CineCert, LLC
32 # All rights reserved.
33 #
34 # Redistribution and use in source and binary forms, with or without
35 # modification, are permitted provided that the following conditions
36 # are met:
37 # 1. Redistributions of source code must retain the above copyright
38 #    notice, this list of conditions and the following disclaimer.
39 # 2. Redistributions in binary form must reproduce the above copyright
40 #    notice, this list of conditions and the following disclaimer in the
41 #    documentation and/or other materials provided with the distribution.
42 # 3. The name of the author may not be used to endorse or promote products
43 #    derived from this software without specific prior written permission.
44 #
45 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 #
56
57 AC_DEFUN([AX_LIB_OPENSSL],
58 [
59     AC_ARG_WITH([openssl],
60         AC_HELP_STRING([--with-openssl=@<:@ARG@:>@],
61             [use OpenSSL from given prefix (ARG=path); check standard prefixes (ARG=yes); disable (ARG=no)]
62         ),
63         [
64         if test "$withval" = "yes"; then
65             if test -d /var/local/ssl/include ; then
66                 openssl_prefix=/var/local/ssl
67             elif test -d /var/local/include/openssl ; then
68                 openssl_prefix=/var/local
69             elif test -d /usr/local/ssl/include ; then
70                 openssl_prefix=/usr/local/ssl
71             elif test -d /usr/lib/ssl/include ; then
72                 openssl_prefix=/usr/lib/ssl
73             elif test -d /usr/include/openssl ; then
74                 openssl_prefix=/usr
75             else
76                 openssl_prefix=""
77             fi
78             openssl_requested="yes"
79         elif test -d "$withval"; then
80             openssl_prefix="$withval"
81             openssl_requested="yes"
82         else
83             openssl_prefix=""
84             openssl_requested="no"
85         fi
86         ],
87         [
88         dnl Default behavior is implicit yes
89         if test -d /var/local/ssl/include ; then
90             openssl_prefix=/var/local/ssl
91         elif test -d /var/local/include/openssl ; then
92             openssl_prefix=/var/local
93         elif test -d /usr/local/ssl/include ; then
94             openssl_prefix=/usr/local/ssl
95         elif test -d /usr/lib/ssl/include ; then
96             openssl_prefix=/usr/lib/ssl
97         elif test -d /usr/include/openssl ; then
98             openssl_prefix=/usr
99         else
100             openssl_prefix=""
101         fi
102         ]
103     )
104
105     OPENSSL_CPPFLAGS=""
106     OPENSSL_LDFLAGS=""
107     OPENSSL_VERSION=""
108
109     dnl
110     dnl Collect include/lib paths and flags
111     dnl
112     run_openssl_test="no"
113
114     if test -n "$openssl_prefix"; then
115         openssl_include_dir="$openssl_prefix/include"
116         openssl_ldflags="-L$openssl_prefix/lib64 -L$openssl_prefix/lib"
117         run_openssl_test="yes"
118     elif test "$openssl_requested" = "yes"; then
119         if test -n "$openssl_include_dir" -a -n "$openssl_lib_flags"; then
120             run_openssl_test="yes"
121         fi
122     else
123         run_openssl_test="no"
124     fi
125
126     openssl_libs="-lssl -lcrypto"
127
128     dnl
129     dnl Check OpenSSL files
130     dnl
131     if test "$run_openssl_test" = "yes"; then
132
133         saved_CPPFLAGS="$CPPFLAGS"
134         CPPFLAGS="$CPPFLAGS -I$openssl_include_dir"
135
136         saved_LDFLAGS="$LDFLAGS"
137         LDFLAGS="$LDFLAGS $openssl_ldflags"
138
139         saved_LIBS="$LIBS"
140         LIBS="$openssl_libs $LIBS"
141
142         dnl
143         dnl Check OpenSSL headers
144         dnl
145         AC_MSG_CHECKING([for OpenSSL headers in $openssl_include_dir])
146
147         AC_LANG_PUSH([C])
148         AC_COMPILE_IFELSE([
149             AC_LANG_PROGRAM(
150                 [[
151 @%:@include <openssl/opensslv.h>
152 @%:@include <openssl/ssl.h>
153 @%:@include <openssl/crypto.h>
154                 ]],
155                 [[]]
156             )],
157             [
158             OPENSSL_CPPFLAGS="-I$openssl_include_dir"
159             openssl_header_found="yes"
160             AC_MSG_RESULT([found])
161             ],
162             [
163             openssl_header_found="no"
164             AC_MSG_RESULT([not found])
165             ]
166         )
167         AC_LANG_POP([C])
168
169         dnl
170         dnl Check OpenSSL libraries
171         dnl
172         if test "$openssl_header_found" = "yes"; then
173
174             AC_MSG_CHECKING([for OpenSSL libraries])
175
176             AC_LANG_PUSH([C])
177             AC_LINK_IFELSE([
178                 AC_LANG_PROGRAM(
179                     [[
180 @%:@include <openssl/opensslv.h>
181 @%:@include <openssl/ssl.h>
182 @%:@include <openssl/crypto.h>
183 #if (OPENSSL_VERSION_NUMBER < 0x0090700f)
184 #endif
185                     ]],
186                     [[
187 SSL_library_init();
188 SSLeay();
189                     ]]
190                 )],
191                 [
192                 OPENSSL_LDFLAGS="$openssl_ldflags"
193                 OPENSSL_LIBS="$openssl_libs"
194                 openssl_lib_found="yes"
195                 AC_MSG_RESULT([found])
196                 ],
197                 [
198                 openssl_lib_found="no"
199                 AC_MSG_RESULT([not found])
200                 ]
201             )
202             AC_LANG_POP([C])
203         fi
204
205         CPPFLAGS="$saved_CPPFLAGS"
206         LDFLAGS="$saved_LDFLAGS"
207         LIBS="$saved_LIBS"
208     fi
209
210     AC_MSG_CHECKING([for OpenSSL])
211
212     if test "$run_openssl_test" = "yes"; then
213         if test "$openssl_header_found" = "yes" -a "$openssl_lib_found" = "yes"; then
214
215             AC_SUBST([OPENSSL_CPPFLAGS])
216             AC_SUBST([OPENSSL_LDFLAGS])
217             AC_SUBST([OPENSSL_LIBS])
218
219             HAVE_OPENSSL="yes"
220         else
221             HAVE_OPENSSL="no"
222         fi
223
224         AC_MSG_RESULT([$HAVE_OPENSSL])
225
226         dnl
227         dnl Check OpenSSL version
228         dnl
229         if test "$HAVE_OPENSSL" = "yes"; then
230
231             openssl_version_req=ifelse([$1], [], [], [$1])
232
233             if test  -n "$openssl_version_req"; then
234
235                 AC_MSG_CHECKING([if OpenSSL version is >= $openssl_version_req])
236
237                 if test -f "$openssl_include_dir/openssl/opensslv.h"; then
238
239                     OPENSSL_VERSION=`grep OPENSSL_VERSION_TEXT $openssl_include_dir/openssl/opensslv.h \
240                                     | grep -v fips | grep -v PTEXT | cut -f 2 | tr -d \"`
241                     AC_SUBST([OPENSSL_VERSION])
242
243                     dnl Decompose required version string and calculate numerical representation
244                     openssl_version_req_major=`expr $openssl_version_req : '\([[0-9]]*\)'`
245                     openssl_version_req_minor=`expr $openssl_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
246                     openssl_version_req_revision=`expr $openssl_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
247                     openssl_version_req_patch=`expr $openssl_version_req : '[[0-9]]*\.[[0-9]]*\.[[0-9]]*\([[a-z]]*\)'`
248                     if test "x$openssl_version_req_revision" = "x"; then
249                         openssl_version_req_revision="0"
250                     fi
251                     if test "x$openssl_version_req_patch" = "x"; then
252                         openssl_version_req_patch="\`"
253                     fi
254
255                     openssl_version_req_number=`expr $openssl_version_req_major \* $((0x10000000)) \
256                                                \+ $openssl_version_req_minor \* $((0x100000)) \
257                                                \+ $openssl_version_req_revision \* $((0x1000)) \
258                                                \+ $((1 + $(printf "%d" \'$openssl_version_req_patch) - $(printf "%d" \'a))) \* $((0x10)) \
259                                                \+ $((0xf))`
260
261                     dnl Calculate numerical representation of detected version
262                     openssl_version_number=`expr $(($(grep OPENSSL_VERSION_NUMBER $openssl_include_dir/openssl/opensslv.h | cut -f 2 | tr -d L)))`
263
264                     openssl_version_check=`expr $openssl_version_number \>\= $openssl_version_req_number`
265                     if test "$openssl_version_check" = "1"; then
266                         AC_MSG_RESULT([yes])
267                     else
268                         AC_MSG_RESULT([no])
269                         AC_MSG_WARN([Found $OPENSSL_VERSION, which is older than required. Possible compilation failure.])
270                     fi
271                 else
272                     AC_MSG_RESULT([no])
273                     AC_MSG_WARN([Missing header openssl/opensslv.h. Unable to determine OpenSSL version.])
274                 fi
275             fi
276         fi
277
278     else
279         HAVE_OPENSSL="no"
280         AC_MSG_RESULT([$HAVE_OPENSSL])
281
282         if test "$openssl_requested" = "yes"; then
283             AC_MSG_WARN([OpenSSL support requested but headers or library not found. Specify valid prefix of OpenSSL using --with-openssl=@<:@DIR@:>@])
284         fi
285     fi
286     if test "$HAVE_OPENSSL" = "yes"; then
287         CPPFLAGS="$CPPFLAGS $OPENSSL_CPPFLAGS -DHAVE_SSL=1"
288         LDFLAGS="$LDFLAGS $OPENSSL_LDFLAGS $OPENSSL_LIBS"
289     fi
290 ])