xref: /freebsd/contrib/openpam/m4/ax_pkg_config.m4 (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1dnl -*- autoconf -*-
2dnl
3dnl Copyright (c) 2017 The University of Oslo
4dnl All rights reserved.
5dnl
6dnl Redistribution and use in source and binary forms, with or without
7dnl modification, are permitted provided that the following conditions
8dnl are met:
9dnl 1. Redistributions of source code must retain the above copyright
10dnl    notice, this list of conditions and the following disclaimer.
11dnl 2. Redistributions in binary form must reproduce the above copyright
12dnl    notice, this list of conditions and the following disclaimer in the
13dnl    documentation and/or other materials provided with the distribution.
14dnl 3. The name of the author may not be used to endorse or promote
15dnl    products derived from this software without specific prior written
16dnl    permission.
17dnl
18dnl THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19dnl ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20dnl IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21dnl ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22dnl FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23dnl DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24dnl OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25dnl HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26dnl LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27dnl OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28dnl SUCH DAMAGE.
29dnl
30
31m4_define([AX_PKG_CONFIG_MACROS_VERSION], [0.20170404])
32
33dnl
34dnl AX_PROG_PKG_CONFIG([min-version])
35dnl ---------------------------------
36dnl
37dnl Verify that pkgconf or pkg-config are present.
38dnl
39AC_DEFUN([AX_PROG_PKG_CONFIG], [
40    m4_pattern_forbid([^AX_PKG_CONFIG_[A-Z_]+$])
41    AC_ARG_VAR([PKG_CONFIG], [path to pkg-config binary])
42    AC_ARG_VAR([PKG_CONFIG_PATH], [list of directories to prepend to default search path])
43    AC_ARG_VAR([PKG_CONFIG_LIBDIR], [list of directories to search instead of default search path])
44    if test x"${PKG_CONFIG}" = x"" ; then
45        AC_PATH_PROGS([PKG_CONFIG], [pkgconf pkg-config]) >/dev/null
46    else
47        AC_PATH_PROG([PKG_CONFIG], [${PKG_CONFIG}])
48    fi
49    AC_MSG_CHECKING([for pkg-config or pkgconf])
50    if test -x "${PKG_CONFIG}" ; then
51        AC_MSG_RESULT([${PKG_CONFIG}])
52        case "${PKG_CONFIG}" in
53        *pkgconf)
54            _min_version="m4_default([$1], [1.3.0])"
55            ;;
56        *pkg-config)
57            _min_version="m4_default([$1], [0.23])"
58            ;;
59        *)
60            _min_version="9.9.error"
61            ;;
62        esac
63        AC_MSG_CHECKING([that ${PKG_CONFIG} is at least version ${_min_version}])
64        _act_version=`"${PKG_CONFIG}" --version`
65        if ! "${PKG_CONFIG}" --atleast-pkgconfig-version="${_min_version}" ; then
66            PKG_CONFIG=""
67        fi
68        AC_MSG_RESULT([${_act_version}])
69    else
70        AC_MSG_RESULT([no])
71        PKG_CONFIG=""
72    fi
73    if test x"${PKG_CONFIG}" = x"" ; then
74        AC_MSG_ERROR([pkg-config was not found or is too old])
75    fi
76    AC_ARG_WITH([pkgconfigdir],
77        AS_HELP_STRING([--with-pkgconfigdir],
78	    [installation directory for .pc files @<:@LIBDIR/pkgconfig@:>@]),
79	[pkgconfigdir=$withval], [pkgconfigdir='${libdir}/pkgconfig'])
80    AC_SUBST([pkgconfigdir], [$pkgconfigdir])
81])
82
83dnl
84dnl AX_PKG_CONFIG_VAR(package-name, var-name)
85dnl -----------------------------------------
86dnl
87dnl Retrieve specific pkg-config variables for the specified package.
88dnl
89AC_DEFUN([AX_PKG_CONFIG_VAR], [
90    AC_REQUIRE([AX_PROG_PKG_CONFIG])
91    m4_define([_p], AS_TR_SH([m4_tolower([$1])]))
92    m4_case([$2],
93        [version], [ax_pc_cv_[]_p[]_version=`"${PKG_CONFIG}" --modversion [$1]`],
94        [cflags], [ax_pc_cv_[]_p[]_cflags=`"${PKG_CONFIG}" --cflags [$1]`],
95        [libs], [ax_pc_cv_[]_p[]_libs=`"${PKG_CONFIG}" --libs [$1]`],
96        [ax_pc_cv_[]_p[]_[$2]=`"${PKG_CONFIG}" --variable=[$2] [$1]`])
97])
98
99dnl
100dnl AX_PKG_CONFIG_CHECK(package-name,
101dnl     [action-if-found], [action-if-not-found])
102dnl -------------------------------------------
103dnl
104dnl Check if the specified package is installed.  If it is, define
105dnl HAVE_PACKAGE, PACKAGE_VERSION, PACKAGE_CFLAGS and PACKAGE_LIBS.
106dnl The specified actions are performed in addition to the standard
107dnl actions.
108dnl
109AC_DEFUN([AX_PKG_CONFIG_CHECK], [
110    AC_REQUIRE([AX_PROG_PKG_CONFIG])
111    m4_define([_P], AS_TR_SH([m4_toupper([$1])]))
112    m4_define([_p], AS_TR_SH([m4_tolower([$1])]))
113    AC_ARG_VAR(_P[_CFLAGS], [C compiler flags for $1])
114    AC_ARG_VAR(_P[_LIBS], [linker flags for $1])
115    AC_MSG_CHECKING([if $1 is installed])
116    if AC_RUN_LOG(["${PKG_CONFIG}" --exists --print-errors "$1"]) ; then
117        AC_MSG_RESULT([yes])
118        [ax_pc_cv_have_]_p=yes
119        AC_DEFINE([HAVE_]_P, [1], [Define to 1 if you have $1])
120dnl
121        AC_MSG_CHECKING([$1 version])
122        AX_PKG_CONFIG_VAR([$1], [version])
123        AC_SUBST(_P[_VERSION], [$ax_pc_cv_]_p[_version])
124        AC_MSG_RESULT([${ax_pc_cv_]_p[_version:-unknown}])
125dnl
126        AC_MSG_CHECKING([$1 compiler flags])
127        AX_PKG_CONFIG_VAR([$1], [cflags])
128        AC_SUBST(_P[_CFLAGS], [$ax_pc_cv_]_p[_cflags])
129        AC_MSG_RESULT([${ax_pc_cv_]_p[_cflags:-none}])
130dnl
131        AC_MSG_CHECKING([$1 linker flags])
132        AX_PKG_CONFIG_VAR([$1], [libs])
133        AC_SUBST(_P[_LIBS], [$ax_pc_cv_]_p[_libs])
134        AC_MSG_RESULT([${ax_pc_cv_]_p[_libs:-none}])
135dnl
136        m4_default([$2], [:])
137    else
138        AC_MSG_RESULT([no])
139        [ax_pc_cv_have_]_p=no
140        m4_default([$3], [:])
141    fi
142    m4_ifdef([AM_CONDITIONAL], [
143        AM_CONDITIONAL([HAVE_]_P, [test x"$ax_pc_cv_have_]_p[" = x"yes"])
144    ])
145])
146
147dnl
148dnl AX_PKG_CONFIG_REQUIRE(package-name)
149dnl -----------------------------------
150dnl
151dnl As above, but fail if the package is not installed.
152dnl
153AC_DEFUN([AX_PKG_CONFIG_REQUIRE], [
154    AX_PKG_CONFIG_CHECK([$1], [], [
155        AC_MSG_ERROR([cannot proceed without $1])
156    ])
157])
158