Search code examples
linuxautotoolsautoconfpkg-config

autoconf: `PKG_CONFIG_PATH` not working in `configure.ac` when using `PKG_CHECK_EXISTS`


I want to check whether gmodule exists in my custom PKG_CONFIG_PATH

// configure.ac
AC_SUBST([PKG_CONFIG_PATH],"./glib/lib/x86_64-linux-gnu/pkgconfig/")
PKG_PROG_PKG_CONFIG
PKG_CHECK_EXISTS([gmodule-2.0],[],[
    AC_MSG_ERROR(can't find gmodule in glib-2.0)
])

But I have the following error:

checking for libunwind.h... yes
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
configure: error: can't find gmodule in glib-2.0

I'm 100 percent sure that gmodule-2.0.pc is in my custom path:

> ls ./glib/lib/x86_64-linux-gnu/pkgconfig/                            
gio-2.0.pc  gio-unix-2.0.pc  glib-2.0.pc  gmodule-2.0.pc  gmodule-export-2.0.pc  gmodule-no-export-2.0.pc  gobject-2.0.pc  gthread-2.0.pc

And I can also use pkg-config to find gmodule-2.0:

> PKG_CONFIG_PATH="./glib/lib/x86_64-linux-gnu/pkgconfig/" pkg-config gmodule-2.0 --cflags 
-pthread -I/home/xxx/fuzz/test/StochFuzz/glib/include -I/home/xxx/fuzz/test/StochFuzz/glib/include/glib-2.0 -I/home/xxx/fuzz/test/StochFuzz/glib/lib/x86_64-linux-gnu/glib-2.0/include

Do I miss something?


Solution

  • Do I miss something?

    It looks like you are expecting this ...

    AC_SUBST([PKG_CONFIG_PATH],"./glib/lib/x86_64-linux-gnu/pkgconfig/")
    

    ... to cause configure to use ./glib/lib/x86_64-linux-gnu/pkgconfig/ as the PKG_CONFIG_PATH when it runs pkg-config. In that case, you are at least missing that

    • the purpose of AC_SUBST() is to create an output variable. You want an output variable if you want to convey the pkg-config path to your Makefiles, but that is not directly relevant to your configure script.

    • although AC_SUBST does set the value of the specified shell variable if you designate a value for it,

      1. It does not necessarily export that variable.
      2. The assignment does not necessarily appear in the configure script at a place corresponding to the macro's location in configure.ac.
    • if you are trying to use components that are bundled with your project (and surely that's what you are doing if the wanted details are provided by pkg-config data from within your own source tree) then pkg-config is way overkill. Just put the needed paths and flags in your Makefile.am file(s), or if you're not using Automake then directly in your Makefile.in file(s).

    If you insist on doing it with pkg-config anyway, then this variation will likely induce the behavior you want from the PKG_CHECK_EXISTS macro:

    # configure.ac
    PKG_CONFIG_PATH=./glib/lib/x86_64-linux-gnu/pkgconfig/
    export PKG_CONFIG_PATH
    PKG_PROG_PKG_CONFIG
    PKG_CHECK_EXISTS([gmodule-2.0],[],[
        AC_MSG_ERROR(can't find gmodule in glib-2.0)
    ])
    

    If you also need to convey your custom PKG_CONFIG_PATH to your makefiles then you can add ...

    AC_SUBST([PKG_CONFIG_PATH])
    

    ... but you shouldn't. Notwithstanding the fact that you shouldn't be using pkg-config at all in this case (see above), when you do use pkg-config in an Autotools build system, the best way to use it is entirely within configure. Extract the needed paths and flags there, and convey those to your makefiles via output variables.