From dade612e22030db666e12a964dfc4428ecfdecaf Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Sun, 22 Feb 2015 17:10:17 +0000 Subject: [PATCH] pkg: Add PKG_CHECK_MODULES_SUBST macro This is a tweaked version of PKG_CHECK_MODULES which supports separating out the private and public dependencies and substituting them into the .pc file using autoconf magic. See the documentation for the macro for more information. https://bugs.freedesktop.org/show_bug.cgi?id=87154 --- README | 19 +++++++++++++++++- pkg-config-guide.html | 19 ++++++++++++++++-- pkg-config.1 | 55 ++++++++++++++++++++++++++++++++++++++++----------- pkg.m4 | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 14 deletions(-) diff --git a/README b/README index 5a53aeb..3f0c2fb 100644 --- a/README +++ b/README @@ -3,7 +3,24 @@ flags when compiling/linking a lot easier. Report bugs at http://bugzilla.freedesktop.org/ -To use pkg-config, do something like the following in your configure.ac +To use pkg-config from a library, do something like the following in your +configure.ac + + PKG_CHECK_MODULES_SUBST([GLIB], [gio-2.0 gthread-2.0], [glib-2.0 >= 2.40.0]) + +This puts the necessary include flags to compile/link something against GIO, +GLib, GThread and all their dependencies in $(GLIB_CFLAGS), and the -L/-l flags +for linking in $(GLIB_LIBS). It also substitutes @PKG_REQUIRES_PRIVATE@ and +@PKG_REQUIRES@ in autoconf outputted files, so by putting: + + Requires: @PKG_REQUIRES@ + Requires.private: @PKG_REQUIRES_PRIVATE@ + +in your library’s .pc.in file, its private and public dependencies are +automatically advertised correctly for programs to link again. + +To use pkg-config from a program, do something like the following in your +configure.ac: PKG_CHECK_MODULES([GNOME], [gtk > 1.2.8 gnomeui >= 1.2.0]) diff --git a/pkg-config-guide.html b/pkg-config-guide.html index 16c43e2..4bc9382 100644 --- a/pkg-config-guide.html +++ b/pkg-config-guide.html @@ -354,6 +354,14 @@ $ pkg-config --modversion hello <VARIABLE-PREFIX>_CFLAGS and <VARIABLE-PREFIX>_LIBS according to the output from pkg-config --cflags and pkg-config --libs. + +
  • PKG_CHECK_MODULES_SUBST(VARIABLE-PREFIX, PRIVATE-MODULES, PUBLIC-MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND], [PRIVATE-VARIABLE], [PUBLIC-VARIABLE]): + A wrapper around PKG_CHECK_MODULES which split the list of modules + into public and private dependencies, and produces two variables listing the + dependencies across all invocations of PKG_CHECK_MODULES_SUBST. These + two variables are exposed via AC_SUBST and should be used in a + pkg-config file as the substituted variables for Requires and + Requires.private.
  • @@ -371,8 +379,8 @@ $ pkg-config --modversion hello

    The integration can be more robust when used with autoconf and automake. By using the - supplied PKG_CHECK_MODULES macro, the metadata is easily accessed - in the build process.

    + supplied PKG_CHECK_MODULES_SUBST and PKG_CHECK_MODULES + macros, the metadata is easily accessed in the build process.

    configure.ac:
     PKG_CHECK_MODULES([X], [x])
    @@ -396,6 +404,13 @@ myapp_LDADD = $(X_LIBS)
    libx headers. In either case, pkg-config will output the compiler flags when --static is used or not.

    +

    The PKG_CHECK_MODULES_SUBST macro can help with this: use it + instead of PKG_CHECK_MODULES and list your private dependencies + separately from your public ones — so the x library should be + listed in the second argument to PKG_CHECK_MODULES_SUBST. Put + Requires.private: @PKG_REQUIRES_PRIVATE@ and + Requires: @PKG_REQUIRES@ in your z.pc file.

    +
  • My library z uses libx internally, but does not expose libx data types in its public API. What do I put in my z.pc file?
  • diff --git a/pkg-config.1 b/pkg-config.1 index 74bb70a..891cf3c 100644 --- a/pkg-config.1 +++ b/pkg-config.1 @@ -396,25 +396,46 @@ variables described above. .\" .SH AUTOCONF MACROS .TP -.I "PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES [,ACTION-IF-FOUND [,ACTION-IF-NOT-FOUND]])" +.I "PKG_CHECK_MODULES_SUBST(VARIABLE-PREFIX, PRIVATE-MODULES, PUBLIC-MODULES [,ACTION-IF-FOUND [,ACTION-IF-NOT-FOUND [,PRIVATE-VARIABLE [,PUBLIC-VARIABLE]]]])" -The macro PKG_CHECK_MODULES can be used in \fIconfigure.ac\fP to +The macro PKG_CHECK_MODULES_SUBST can be used in \fIconfigure.ac\fP to check whether modules exist. A typical usage would be: .nf - PKG_CHECK_MODULES([MYSTUFF], [gtk+-2.0 >= 1.3.5 libxml = 1.8.4]) + PKG_CHECK_MODULES_SUBST([MYSTUFF], [gio-2.0 >= 2.40.1 gthread-2.0], [glib-2.0 >= 2.38.3]) + PKG_CHECK_MODULES_SUBST([MYSTUFF_UI], [gtk+-3.0], []) .fi This would result in MYSTUFF_LIBS and MYSTUFF_CFLAGS substitution -variables, set to the libs and cflags for the given module list. -If a module is missing or has the wrong version, by default configure -will abort with a message. To replace the default action, -specify an \%ACTION-IF-NOT-FOUND. \%PKG_CHECK_MODULES will not print any -error messages if you specify your own ACTION-IF-NOT-FOUND. -However, it will set the variable MYSTUFF_PKG_ERRORS, which you can -use to display what went wrong. +variables, set to the libs and cflags for the given module lists (both +public and private). If a module is missing or has the wrong version, by +default configure will abort with a message. To replace the default action, +specify an \%ACTION-IF-NOT-FOUND. \%PKG_CHECK_MODULES_SUBST will not print +any error messages if you specify your own ACTION-IF-NOT-FOUND. However, it +will set the variable MYSTUFF_PKG_ERRORS, which you can use to display what +went wrong. + +\%PKG_CHECK_MODULES_SUBST will produce two lists of modules, concatenated +across all invocations of the macro. These two variables are exposed via +\%AC_SUBST, and should be used in a pkg-config file as the substituted +variables for \%Requires and \%Requires.private: +.nf + Requires.private: @PKG_REQUIRES_PRIVATE@ + Requires: @PKG_REQUIRES@ +.fi + +For the example above, the variables would be set to: +.nf + PKG_REQUIRES_PRIVATE="gio-2.0 >= 2.40.1 gthread-2.0 gtk+-3.0" + PKG_REQUIRES="glib-2.0 >= 2.38.3" +.fi + +The names of the \%PKG_REQUIRES_PRIVATE and \%PKG_REQUIRES variables can +be set using the PRIVATE-VARIABLE and PUBLIC-VARIABLE arguments, which +default to PKG_REQUIRES and PKG_REQUIRES_PRIVATE. Both variables are +AC_SUBST-ed by this macro. Note that if there is a possibility the first call to -PKG_CHECK_MODULES might not happen, you should be sure to include an +PKG_CHECK_MODULES_SUBST might not happen, you should be sure to include an explicit call to PKG_PROG_PKG_CONFIG in your configure.ac. Also note that repeated usage of VARIABLE-PREFIX is not recommended. @@ -423,6 +444,18 @@ VARIABLE-PREFIX will simply use the _LIBS and _CFLAGS variables set from the previous usage without calling \fIpkg-config\fP again. .\" .TP +.I "PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES [,ACTION-IF-FOUND [,ACTION-IF-NOT-FOUND]])" + +A simpler version of \%PKG_CHECK_MODULES_SUBST, suitable for use in +applications which do not need to differentiate between public and private +dependencies. + +It behaves like \%PKG_CHECK_MODULES_SUBST, but does not build the +PKG_REQUIRES and PKG_REQUIRES_PRIVATE lists, and takes a single list of +modules to check for. + +.\" +.TP .I "PKG_PROG_PKG_CONFIG([MIN-VERSION])" Defines the PKG_CONFIG variable to the best pkg-config available, diff --git a/pkg.m4 b/pkg.m4 index 62995f0..95bda5d 100644 --- a/pkg.m4 +++ b/pkg.m4 @@ -178,6 +178,59 @@ PKG_CONFIG=$_save_PKG_CONFIG[]dnl ]) +# PKG_CHECK_MODULES_SUBST(PREFIX, PRIVATE-MODULES, PUBLIC-MODULES, +# [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND], +# [PRIVATE-VARIABLE], [PUBLIC-VARIABLE]) +# --------------------------------------------------------------------- +# +# A wrapper around PKG_CHECK_MODULES which splits the list of modules into +# public and private dependencies, and produces two variables listing the +# dependencies across all invocations of PKG_CHECK_MODULES_SUBST. These two +# variables are exposed via AC_SUBST, and should be used in a pkg-config +# file as the substituted values for Requires and Requires.private. +# +# The PREFIX, PRIVATE-MODULES and PUBLIC-MODULES arguments should be +# specified as for PKG_CHECK_MODULES, with the concatenation of +# PUBLIC-MODULES and PRIVATE-MODULES equaling the LIST-OF-MODULES from +# PKG_CHECK_MODULES. The ACTION-IF-FOUND and ACTION-IF-NOT-FOUND arguments +# are optional, and should also be specified as for PKG_CHECK_MODULES. +# ACTION-IF-FOUND is evaluated if the full LIST-OF-MODULES is found; +# ACTION-IF-NOT-FOUND similarly. +# +# PUBLIC-VARIABLE defaults to PKG_REQUIRES, and PRIVATE-VARIABLE defaults to +# PKG_REQUIRES_PRIVATE. Both variables are AC_SUBST-ed by this macro. +# +# For example: +# +# PKG_CHECK_MODULES_SUBST([GLIB],[gthread-2.0],[glib-2.0 gio-2.0]) +# PKG_CHECK_MODULES_SUBST([DBUS],[dbus-glib-1 >= 0.98 dbus-1],[]) +# +# results in the substitutions: +# +# PKG_REQUIRES_PRIVATE="gthread-2.0 dbus-glib-1 >= 0.98 dbus-1" +# PKG_REQUIRES="glib-2.0 gio-2.0" +# +# and can be used with a template pkg-config file (.pc.in) using: +# +# Requires: @PKG_REQUIRES@ +# Requires.private: @PKG_REQUIRES_PRIVATE@ +AC_DEFUN([PKG_CHECK_MODULES_SUBST],[ + m4_define([pkg_requires_private], + [m4_default_quoted([$6],[PKG_REQUIRES_PRIVATE])]) + m4_define([pkg_requires], + [m4_default_quoted([$7],[PKG_REQUIRES])]) + + pkg_requires_private="$[]pkg_requires_private $2" + pkg_requires="$[]pkg_requires $3" + + PKG_CHECK_MODULES([$1],[$2 $3],[$4],[$5]) + + # Substitute output. + AC_SUBST(pkg_requires_private) + AC_SUBST(pkg_requires) +]) + + # PKG_INSTALLDIR(DIRECTORY) # ------------------------- # Substitutes the variable pkgconfigdir as the location where a module -- 1.9.3