Add shared pkg.m4 file with convenience macros for using pkg-config.
[amitay/samba.git] / m4 / pkg.m4
1 # pkg.m4 - Macros to locate and utilise pkg-config.            -*- Autoconf -*-
2
3 # Copyright © 2004 Scott James Remnant <scott@netsplit.com>.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #
18 # As a special exception to the GNU General Public License, if you
19 # distribute this file as part of a program that contains a
20 # configuration script generated by Autoconf, you may include it under
21 # the same distribution terms that you use for the rest of that program.
22
23 # PKG_PROG_PKG_CONFIG([MIN-VERSION])
24 # ----------------------------------
25 AC_DEFUN([PKG_PROG_PKG_CONFIG],
26 [m4_pattern_forbid([^_?PKG_[A-Z_]+$])
27 m4_pattern_allow([^PKG_CONFIG(_PATH)?$])
28 AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility])dnl
29 if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
30         AC_PATH_TOOL([PKG_CONFIG], [pkg-config])
31 fi
32 if test -n "$PKG_CONFIG"; then
33         _pkg_min_version=m4_default([$1], [0.9.0])
34         AC_MSG_CHECKING([pkg-config is at least version $_pkg_min_version])
35         if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then
36                 AC_MSG_RESULT([yes])
37         else
38                 AC_MSG_RESULT([no])
39                 PKG_CONFIG=""
40         fi
41                 
42 fi[]dnl
43 ])# PKG_PROG_PKG_CONFIG
44
45 # PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
46 #
47 # Check to see whether a particular set of modules exists.  Similar
48 # to PKG_CHECK_MODULES(), but does not set variables or print errors.
49 #
50 #
51 # Similar to PKG_CHECK_MODULES, make sure that the first instance of
52 # this or PKG_CHECK_MODULES is called, or make sure to call
53 # PKG_CHECK_EXISTS manually
54 # --------------------------------------------------------------
55 AC_DEFUN([PKG_CHECK_EXISTS],
56 [AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
57 if test -n "$PKG_CONFIG" && \
58     AC_RUN_LOG([$PKG_CONFIG --exists --print-errors "$1"]); then
59   m4_ifval([$2], [$2], [:])
60 m4_ifvaln([$3], [else
61   $3])dnl
62 fi])
63
64
65 # _PKG_CONFIG([VARIABLE], [COMMAND], [MODULES])
66 # ---------------------------------------------
67 m4_define([_PKG_CONFIG],
68 [if test -n "$PKG_CONFIG"; then
69     if test -n "$$1"; then
70         pkg_cv_[]$1="$$1"
71     else
72         PKG_CHECK_EXISTS([$3],
73                          [pkg_cv_[]$1=`$PKG_CONFIG --[]$2 "$3" 2>/dev/null`],
74                          [pkg_failed=yes])
75     fi
76 else
77         pkg_failed=untried
78 fi[]dnl
79 ])# _PKG_CONFIG
80
81 # _PKG_SHORT_ERRORS_SUPPORTED
82 # -----------------------------
83 AC_DEFUN([_PKG_SHORT_ERRORS_SUPPORTED],
84 [AC_REQUIRE([PKG_PROG_PKG_CONFIG])
85 if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
86         _pkg_short_errors_supported=yes
87 else
88         _pkg_short_errors_supported=no
89 fi[]dnl
90 ])# _PKG_SHORT_ERRORS_SUPPORTED
91
92
93 # PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],
94 # [ACTION-IF-NOT-FOUND])
95 #
96 #
97 # Note that if there is a possibility the first call to
98 # PKG_CHECK_MODULES might not happen, you should be sure to include an
99 # explicit call to PKG_PROG_PKG_CONFIG in your configure.ac
100 #
101 #
102 # --------------------------------------------------------------
103 AC_DEFUN([PKG_CHECK_MODULES],
104 [AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
105 AC_ARG_VAR([$1][_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl
106 AC_ARG_VAR([$1][_LIBS], [linker flags for $1, overriding pkg-config])dnl
107
108 pkg_failed=no
109 AC_MSG_CHECKING([for $1])
110
111 _PKG_CONFIG([$1][_CFLAGS], [cflags], [$2])
112 _PKG_CONFIG([$1][_LIBS], [libs], [$2])
113
114 m4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS
115 and $1[]_LIBS to avoid the need to call pkg-config.
116 See the pkg-config man page for more details.])
117
118 if test $pkg_failed = yes; then
119         _PKG_SHORT_ERRORS_SUPPORTED
120         if test $_pkg_short_errors_supported = yes; then
121                 $1[]_PKG_ERRORS=`$PKG_CONFIG --short-errors --errors-to-stdout --print-errors "$2"`
122         else 
123                 $1[]_PKG_ERRORS=`$PKG_CONFIG --errors-to-stdout --print-errors "$2"`
124         fi
125         # Put the nasty error message in config.log where it belongs
126         echo "$$1[]_PKG_ERRORS" >&AS_MESSAGE_LOG_FD
127
128         ifelse([$4], , [AC_MSG_ERROR(dnl
129 [Package requirements ($2) were not met:
130
131 $$1_PKG_ERRORS
132
133 Consider adjusting the PKG_CONFIG_PATH environment variable if you
134 installed software in a non-standard prefix.
135
136 _PKG_TEXT
137 ])],
138                 [AC_MSG_RESULT([no])
139                 $4])
140 elif test $pkg_failed = untried; then
141         ifelse([$4], , [AC_MSG_FAILURE(dnl
142 [The pkg-config script could not be found or is too old.  Make sure it
143 is in your PATH or set the PKG_CONFIG environment variable to the full
144 path to pkg-config.
145
146 _PKG_TEXT
147
148 To get pkg-config, see <http://www.freedesktop.org/software/pkgconfig>.])],
149                 [$4])
150 else
151         $1[]_CFLAGS=$pkg_cv_[]$1[]_CFLAGS
152         $1[]_LIBS=$pkg_cv_[]$1[]_LIBS
153         AC_MSG_RESULT([yes])
154         ifelse([$3], , :, [$3])
155 fi[]dnl
156 ])# PKG_CHECK_MODULES