1# pkg.m4 - Macros to locate and utilise pkg-config.   -*- Autoconf -*-
2# serial 12 (pkg-config-0.29.2)
3
4dnl Copyright © 2004 Scott James Remnant <scott@netsplit.com>.
5dnl Copyright © 2012-2015 Dan Nicholson <dbn.lists@gmail.com>
6dnl
7dnl This program is free software; you can redistribute it and/or modify
8dnl it under the terms of the GNU General Public License as published by
9dnl the Free Software Foundation; either version 2 of the License, or
10dnl (at your option) any later version.
11dnl
12dnl This program is distributed in the hope that it will be useful, but
13dnl WITHOUT ANY WARRANTY; without even the implied warranty of
14dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15dnl General Public License for more details.
16dnl
17dnl You should have received a copy of the GNU General Public License
18dnl along with this program; if not, write to the Free Software
19dnl Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20dnl 02111-1307, USA.
21dnl
22dnl As a special exception to the GNU General Public License, if you
23dnl distribute this file as part of a program that contains a
24dnl configuration script generated by Autoconf, you may include it under
25dnl the same distribution terms that you use for the rest of that
26dnl program.
27
28dnl PKG_PREREQ(MIN-VERSION)
29dnl -----------------------
30dnl Since: 0.29
31dnl
32dnl Verify that the version of the pkg-config macros are at least
33dnl MIN-VERSION. Unlike PKG_PROG_PKG_CONFIG, which checks the user's
34dnl installed version of pkg-config, this checks the developer's version
35dnl of pkg.m4 when generating configure.
36dnl
37dnl To ensure that this macro is defined, also add:
38dnl m4_ifndef([PKG_PREREQ],
39dnl     [m4_fatal([must install pkg-config 0.29 or later before running autoconf/autogen])])
40dnl
41dnl See the "Since" comment for each macro you use to see what version
42dnl of the macros you require.
43m4_defun([PKG_PREREQ],
44[m4_define([PKG_MACROS_VERSION], [0.29.2])
45m4_if(m4_version_compare(PKG_MACROS_VERSION, [$1]), -1,
46    [m4_fatal([pkg.m4 version $1 or higher is required but ]PKG_MACROS_VERSION[ found])])
47])dnl PKG_PREREQ
48
49dnl PKG_PROG_PKG_CONFIG([MIN-VERSION])
50dnl ----------------------------------
51dnl Since: 0.16
52dnl
53dnl Search for the pkg-config tool and set the PKG_CONFIG variable to
54dnl first found in the path. Checks that the version of pkg-config found
55dnl is at least MIN-VERSION. If MIN-VERSION is not specified, 0.9.0 is
56dnl used since that's the first version where most current features of
57dnl pkg-config existed.
58AC_DEFUN([PKG_PROG_PKG_CONFIG],
59[m4_pattern_forbid([^_?PKG_[A-Z_]+$])
60m4_pattern_allow([^PKG_CONFIG(_(PATH|LIBDIR|SYSROOT_DIR|ALLOW_SYSTEM_(CFLAGS|LIBS)))?$])
61m4_pattern_allow([^PKG_CONFIG_(DISABLE_UNINSTALLED|TOP_BUILD_DIR|DEBUG_SPEW)$])
62AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility])
63AC_ARG_VAR([PKG_CONFIG_PATH], [directories to add to pkg-config's search path])
64AC_ARG_VAR([PKG_CONFIG_LIBDIR], [path overriding pkg-config's built-in search path])
65
66if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
67	AC_PATH_TOOL([PKG_CONFIG], [pkg-config])
68fi
69if test -n "$PKG_CONFIG"; then
70	_pkg_min_version=m4_default([$1], [0.9.0])
71	AC_MSG_CHECKING([pkg-config is at least version $_pkg_min_version])
72	if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then
73		AC_MSG_RESULT([yes])
74	else
75		AC_MSG_RESULT([no])
76		PKG_CONFIG=""
77	fi
78fi[]dnl
79])dnl PKG_PROG_PKG_CONFIG
80
81dnl PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
82dnl -------------------------------------------------------------------
83dnl Since: 0.18
84dnl
85dnl Check to see whether a particular set of modules exists. Similar to
86dnl PKG_CHECK_MODULES(), but does not set variables or print errors.
87dnl
88dnl Please remember that m4 expands AC_REQUIRE([PKG_PROG_PKG_CONFIG])
89dnl only at the first occurence in configure.ac, so if the first place
90dnl it's called might be skipped (such as if it is within an "if", you
91dnl have to call PKG_CHECK_EXISTS manually
92AC_DEFUN([PKG_CHECK_EXISTS],
93[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
94if test -n "$PKG_CONFIG" && \
95    AC_RUN_LOG([$PKG_CONFIG --exists --print-errors "$1"]); then
96  m4_default([$2], [:])
97m4_ifvaln([$3], [else
98  $3])dnl
99fi])
100
101dnl _PKG_CONFIG([VARIABLE], [COMMAND], [MODULES])
102dnl ---------------------------------------------
103dnl Internal wrapper calling pkg-config via PKG_CONFIG and setting
104dnl pkg_failed based on the result.
105m4_define([_PKG_CONFIG],
106[if test -n "$$1"; then
107    pkg_cv_[]$1="$$1"
108 elif test -n "$PKG_CONFIG"; then
109    PKG_CHECK_EXISTS([$3],
110                     [pkg_cv_[]$1=`$PKG_CONFIG --[]$2 "$3" 2>/dev/null`
111		      test "x$?" != "x0" && pkg_failed=yes ],
112		     [pkg_failed=yes])
113 else
114    pkg_failed=untried
115fi[]dnl
116])dnl _PKG_CONFIG
117
118dnl _PKG_SHORT_ERRORS_SUPPORTED
119dnl ---------------------------
120dnl Internal check to see if pkg-config supports short errors.
121AC_DEFUN([_PKG_SHORT_ERRORS_SUPPORTED],
122[AC_REQUIRE([PKG_PROG_PKG_CONFIG])
123if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
124        _pkg_short_errors_supported=yes
125else
126        _pkg_short_errors_supported=no
127fi[]dnl
128])dnl _PKG_SHORT_ERRORS_SUPPORTED
129
130
131dnl PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],
132dnl   [ACTION-IF-NOT-FOUND])
133dnl --------------------------------------------------------------
134dnl Since: 0.4.0
135dnl
136dnl Note that if there is a possibility the first call to
137dnl PKG_CHECK_MODULES might not happen, you should be sure to include an
138dnl explicit call to PKG_PROG_PKG_CONFIG in your configure.ac
139AC_DEFUN([PKG_CHECK_MODULES],
140[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
141AC_ARG_VAR([$1][_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl
142AC_ARG_VAR([$1][_LIBS], [linker flags for $1, overriding pkg-config])dnl
143
144pkg_failed=no
145AC_MSG_CHECKING([for $2])
146
147_PKG_CONFIG([$1][_CFLAGS], [cflags], [$2])
148_PKG_CONFIG([$1][_LIBS], [libs], [$2])
149
150dnl Check whether $pkg_cv_[]$1[]_LIBS works.
151if test $pkg_failed = no; then
152  pkg_save_LDFLAGS="$LDFLAGS"
153  LDFLAGS="$LDFLAGS $pkg_cv_[]$1[]_LIBS"
154  AC_TRY_LINK([],[], [], [pkg_failed=yes])
155  LDFLAGS=$pkg_save_LDFLAGS
156fi
157
158m4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS
159and $1[]_LIBS to avoid the need to call pkg-config.
160See the pkg-config man page for more details.])
161
162if test $pkg_failed = yes; then
163        AC_MSG_RESULT([no])
164        _PKG_SHORT_ERRORS_SUPPORTED
165        if test $_pkg_short_errors_supported = yes; then
166	        $1[]_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "$2" 2>&1`
167        else
168	        $1[]_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "$2" 2>&1`
169        fi
170	# Put the nasty error message in config.log where it belongs
171	echo "$$1[]_PKG_ERRORS" >&AS_MESSAGE_LOG_FD
172
173	m4_default([$4], [AC_MSG_ERROR(
174[Package requirements ($2) were not met:
175
176$$1_PKG_ERRORS
177
178Consider adjusting the PKG_CONFIG_PATH environment variable if you
179installed software in a non-standard prefix.
180
181_PKG_TEXT])[]dnl
182        ])
183elif test $pkg_failed = untried; then
184        AC_MSG_RESULT([no])
185	m4_default([$4], [AC_MSG_FAILURE(
186[The pkg-config script could not be found or is too old.  Make sure it
187is in your PATH or set the PKG_CONFIG environment variable to the full
188path to pkg-config.
189
190_PKG_TEXT
191
192To get pkg-config, see <http://pkg-config.freedesktop.org/>.])[]dnl
193        ])
194else
195	$1[]_CFLAGS=$pkg_cv_[]$1[]_CFLAGS
196	$1[]_LIBS=$pkg_cv_[]$1[]_LIBS
197        AC_MSG_RESULT([yes])
198	$3
199fi[]dnl
200])dnl PKG_CHECK_MODULES
201
202
203dnl PKG_CHECK_MODULES_STATIC(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],
204dnl   [ACTION-IF-NOT-FOUND])
205dnl ---------------------------------------------------------------------
206dnl Since: 0.29
207dnl
208dnl Checks for existence of MODULES and gathers its build flags with
209dnl static libraries enabled. Sets VARIABLE-PREFIX_CFLAGS from --cflags
210dnl and VARIABLE-PREFIX_LIBS from --libs.
211dnl
212dnl Note that if there is a possibility the first call to
213dnl PKG_CHECK_MODULES_STATIC might not happen, you should be sure to
214dnl include an explicit call to PKG_PROG_PKG_CONFIG in your
215dnl configure.ac.
216AC_DEFUN([PKG_CHECK_MODULES_STATIC],
217[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
218_save_PKG_CONFIG=$PKG_CONFIG
219PKG_CONFIG="$PKG_CONFIG --static"
220PKG_CHECK_MODULES($@)
221PKG_CONFIG=$_save_PKG_CONFIG[]dnl
222])dnl PKG_CHECK_MODULES_STATIC
223
224
225dnl PKG_INSTALLDIR([DIRECTORY])
226dnl -------------------------
227dnl Since: 0.27
228dnl
229dnl Substitutes the variable pkgconfigdir as the location where a module
230dnl should install pkg-config .pc files. By default the directory is
231dnl $libdir/pkgconfig, but the default can be changed by passing
232dnl DIRECTORY. The user can override through the --with-pkgconfigdir
233dnl parameter.
234AC_DEFUN([PKG_INSTALLDIR],
235[m4_pushdef([pkg_default], [m4_default([$1], ['${libdir}/pkgconfig'])])
236m4_pushdef([pkg_description],
237    [pkg-config installation directory @<:@]pkg_default[@:>@])
238AC_ARG_WITH([pkgconfigdir],
239    [AS_HELP_STRING([--with-pkgconfigdir], pkg_description)],,
240    [with_pkgconfigdir=]pkg_default)
241AC_SUBST([pkgconfigdir], [$with_pkgconfigdir])
242m4_popdef([pkg_default])
243m4_popdef([pkg_description])
244])dnl PKG_INSTALLDIR
245
246
247dnl PKG_NOARCH_INSTALLDIR([DIRECTORY])
248dnl --------------------------------
249dnl Since: 0.27
250dnl
251dnl Substitutes the variable noarch_pkgconfigdir as the location where a
252dnl module should install arch-independent pkg-config .pc files. By
253dnl default the directory is $datadir/pkgconfig, but the default can be
254dnl changed by passing DIRECTORY. The user can override through the
255dnl --with-noarch-pkgconfigdir parameter.
256AC_DEFUN([PKG_NOARCH_INSTALLDIR],
257[m4_pushdef([pkg_default], [m4_default([$1], ['${datadir}/pkgconfig'])])
258m4_pushdef([pkg_description],
259    [pkg-config arch-independent installation directory @<:@]pkg_default[@:>@])
260AC_ARG_WITH([noarch-pkgconfigdir],
261    [AS_HELP_STRING([--with-noarch-pkgconfigdir], pkg_description)],,
262    [with_noarch_pkgconfigdir=]pkg_default)
263AC_SUBST([noarch_pkgconfigdir], [$with_noarch_pkgconfigdir])
264m4_popdef([pkg_default])
265m4_popdef([pkg_description])
266])dnl PKG_NOARCH_INSTALLDIR
267
268
269dnl PKG_CHECK_VAR(VARIABLE, MODULE, CONFIG-VARIABLE,
270dnl [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
271dnl -------------------------------------------
272dnl Since: 0.28
273dnl
274dnl Retrieves the value of the pkg-config variable for the given module.
275AC_DEFUN([PKG_CHECK_VAR],
276[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
277AC_ARG_VAR([$1], [value of $3 for $2, overriding pkg-config])dnl
278
279_PKG_CONFIG([$1], [variable="][$3]["], [$2])
280AS_VAR_COPY([$1], [pkg_cv_][$1])
281
282AS_VAR_IF([$1], [""], [$5], [$4])dnl
283])dnl PKG_CHECK_VAR
284