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