1# SPDX-License-Identifier: FSFAP
2#
3# ===========================================================================
4#     https://www.gnu.org/software/autoconf-archive/ax_check_openssl.html
5# ===========================================================================
6#
7# SYNOPSIS
8#
9#   AX_CHECK_OPENSSL([action-if-found[, action-if-not-found]])
10#
11# DESCRIPTION
12#
13#   Look for OpenSSL in a number of default spots, or in a user-selected
14#   spot (via --with-openssl).  Sets
15#
16#     OPENSSL_CFLAGS to the include directives required
17#     OPENSSL_LIBS to the -l directives required
18#     OPENSSL_LDFLAGS to the -L or -R flags required
19#
20#   and calls ACTION-IF-FOUND or ACTION-IF-NOT-FOUND appropriately
21#
22#   This macro sets OPENSSL_CFLAGS such that source files should use the
23#   openssl/ directory in include directives:
24#
25#     #include <openssl/hmac.h>
26#
27# LICENSE
28#
29#   Copyright (c) 2009,2010 Zmanda Inc. <http://www.zmanda.com/>
30#   Copyright (c) 2009,2010 Dustin J. Mitchell <dustin@zmanda.com>
31#
32#   Copying and distribution of this file, with or without modification, are
33#   permitted in any medium without royalty provided the copyright notice
34#   and this notice are preserved. This file is offered as-is, without any
35#   warranty.
36
37#serial 11
38
39AU_ALIAS([CHECK_SSL], [AX_CHECK_OPENSSL])
40AC_DEFUN([AX_CHECK_OPENSSL], [
41    found=false
42    AC_ARG_WITH([openssl],
43        [AS_HELP_STRING([--with-openssl=DIR],
44            [root of the OpenSSL directory])],
45        [
46            case "$withval" in
47            "" | y | ye | yes | n | no)
48            AC_MSG_ERROR([Invalid --with-openssl value])
49              ;;
50            *) ssldirs="$withval"
51              ;;
52            esac
53        ], [
54            # if pkg-config is installed and openssl has installed a .pc file,
55            # then use that information and don't search ssldirs
56            AC_CHECK_TOOL([PKG_CONFIG], [pkg-config])
57            if test x"$PKG_CONFIG" != x""; then
58                OPENSSL_LDFLAGS=`$PKG_CONFIG openssl --libs-only-L 2>/dev/null`
59                if test $? = 0; then
60                    OPENSSL_LIBS=`$PKG_CONFIG openssl --libs-only-l 2>/dev/null`
61                    OPENSSL_CFLAGS=`$PKG_CONFIG openssl --cflags-only-I 2>/dev/null`
62                    found=true
63                fi
64            fi
65
66            # no such luck; use some default ssldirs
67            if ! $found; then
68                ssldirs="/usr/local/ssl /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /usr"
69            fi
70        ]
71        )
72
73
74    # note that we #include <openssl/foo.h>, so the OpenSSL headers have to be in
75    # an 'openssl' subdirectory
76
77    if ! $found; then
78        OPENSSL_CFLAGS=
79        for ssldir in $ssldirs; do
80            AC_MSG_CHECKING([for include/openssl/ssl.h in $ssldir])
81            if test -f "$ssldir/include/openssl/ssl.h"; then
82                OPENSSL_CFLAGS="-I$ssldir/include"
83                OPENSSL_LDFLAGS="-L$ssldir/lib"
84                OPENSSL_LIBS="-lssl -lcrypto"
85                found=true
86                AC_MSG_RESULT([yes])
87                break
88            else
89                AC_MSG_RESULT([no])
90            fi
91        done
92
93        # if the file wasn't found, well, go ahead and try the link anyway -- maybe
94        # it will just work!
95    fi
96
97    # try the preprocessor and linker with our new flags,
98    # being careful not to pollute the global LIBS, LDFLAGS, and CPPFLAGS
99
100    AC_MSG_CHECKING([whether compiling and linking against OpenSSL works])
101    echo "Trying link with OPENSSL_LDFLAGS=$OPENSSL_LDFLAGS;" \
102        "OPENSSL_LIBS=$OPENSSL_LIBS; OPENSSL_CFLAGS=$OPENSSL_CFLAGS" >&AS_MESSAGE_LOG_FD
103
104    save_LIBS="$LIBS"
105    save_LDFLAGS="$LDFLAGS"
106    save_CPPFLAGS="$CPPFLAGS"
107    LDFLAGS="$LDFLAGS $OPENSSL_LDFLAGS"
108    LIBS="$OPENSSL_LIBS $LIBS"
109    CPPFLAGS="$OPENSSL_CFLAGS $CPPFLAGS"
110    AC_LINK_IFELSE(
111        [AC_LANG_PROGRAM([#include <openssl/ssl.h>], [SSL_new(NULL)])],
112        [
113            AC_MSG_RESULT([yes])
114            $1
115        ], [
116            AC_MSG_RESULT([no])
117            $2
118        ])
119    CPPFLAGS="$save_CPPFLAGS"
120    LDFLAGS="$save_LDFLAGS"
121    LIBS="$save_LIBS"
122
123    AC_SUBST([OPENSSL_CFLAGS])
124    AC_SUBST([OPENSSL_LIBS])
125    AC_SUBST([OPENSSL_LDFLAGS])
126])
127