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

    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.