summaryrefslogtreecommitdiff
path: root/m4/ax_check_openssl.m4
blob: 8ae39cae61b617b82a7e96207616b052bd3d2e91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# ===========================================================================
#     http://www.gnu.org/software/autoconf-archive/ax_check_openssl.html
# ===========================================================================
#
# SYNOPSIS
#
#   AX_CHECK_OPENSSL([action-if-found[, action-if-not-found]])
#
# DESCRIPTION
#
#   Look for OpenSSL in a number of default spots, or in a user-selected
#   spot (via --with-openssl).  Sets
#
#     OPENSSL_INCLUDES to the include directives required
#     OPENSSL_LIBS to the -l directives required
#     OPENSSL_LDFLAGS to the -L or -R flags required
#
#   and calls ACTION-IF-FOUND or ACTION-IF-NOT-FOUND appropriately
#
#   This macro sets OPENSSL_INCLUDES such that source files should use the
#   openssl/ directory in include directives:
#
#     #include <openssl/hmac.h>
#
# LICENSE
#
#   Copyright (c) 2009,2010 Zmanda Inc. <http://www.zmanda.com/>
#   Copyright (c) 2009,2010 Dustin J. Mitchell <dustin@zmanda.com>
#
#   Copying and distribution of this file, with or without modification, are
#   permitted in any medium without royalty provided the copyright notice
#   and this notice are preserved. This file is offered as-is, without any
#   warranty.

#serial 9

AU_ALIAS([CHECK_SSL], [AX_CHECK_OPENSSL])
AC_DEFUN([AX_CHECK_OPENSSL], [
    found=false
    AC_ARG_WITH([openssl],
        [AS_HELP_STRING([--with-openssl=DIR],
            [With openssl support, see http://www.openssl.org])],
        [
            case "$withval" in
            "" | y | ye | yes )
                ssldirs="/usr/local/ssl /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /usr"
              ;;
            n | no )
              ;;
            *) ssldirs="$withval"
              ;;
            esac
        ], [
            ssldirs="/usr/local/ssl /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /usr"
        ])

    AS_IF([test "${with_openssl}" != "no"], [
        OPENSSL_INCLUDES=
        for ssldir in $ssldirs; do
            AC_MSG_CHECKING([for openssl/ssl.h in $ssldir])
            AS_IF([ test -f "$ssldir/include/openssl/ssl.h" ], [
                OPENSSL_INCLUDES="-I$ssldir/include"
                OPENSSL_LDFLAGS="-L$ssldir/lib"
                OPENSSL_LIBS="-lssl -lcrypto"
                found=true
                AC_MSG_RESULT([yes])
                break
            ], [
                AC_MSG_RESULT([no])
            ])
        done])
     
    AS_IF([test "${with_openssl}" != "no" && test ! ${found}], [ 
        # if pkg-config is installed and openssl has installed a .pc file,
        # then use that information and don't search ssldirs
        AC_PATH_PROG([PKG_CONFIG], [pkg-config])
        if test x"$PKG_CONFIG" != x""; then
            OPENSSL_LDFLAGS=`$PKG_CONFIG openssl --libs-only-L 2>/dev/null`
            if test $? = 0; then
                OPENSSL_LIBS=`$PKG_CONFIG openssl --libs-only-l 2>/dev/null`
                OPENSSL_INCLUDES=`$PKG_CONFIG openssl --cflags-only-I 2>/dev/null`
                found=true
            fi
        fi
    ])

    AS_IF([test "${with_openssl}" != "no" && test ${found}], [

        # try the preprocessor and linker with our new flags,
        # being careful not to pollute the global LIBS, LDFLAGS, and CPPFLAGS

        AC_MSG_CHECKING([whether compiling and linking against OpenSSL works])
        echo "Trying link with OPENSSL_LDFLAGS=$OPENSSL_LDFLAGS;" \
            "OPENSSL_LIBS=$OPENSSL_LIBS; OPENSSL_INCLUDES=$OPENSSL_INCLUDES" >&AS_MESSAGE_LOG_FD

        save_LIBS="$LIBS"
        save_LDFLAGS="$LDFLAGS"
        save_CPPFLAGS="$CPPFLAGS"
        LDFLAGS="$LDFLAGS $OPENSSL_LDFLAGS"
        LIBS="$OPENSSL_LIBS $LIBS"
        CPPFLAGS="$OPENSSL_INCLUDES $CPPFLAGS"
        AC_LINK_IFELSE(
            [AC_LANG_PROGRAM([#include <openssl/ssl.h>], [SSL_new(NULL)])],
            [
                AC_MSG_RESULT([yes])
                $1
            ], [
                AC_MSG_RESULT([no])
                $2
            ])
        CPPFLAGS="$save_CPPFLAGS"
        LDFLAGS="$save_LDFLAGS"
        LIBS="$save_LIBS"

        AC_SUBST([OPENSSL_INCLUDES])
        AC_SUBST([OPENSSL_LIBS])
        AC_SUBST([OPENSSL_LDFLAGS])
    ], [ $2 ])
])