Pkg-config fails to find libraries when building host tools from makefile hosted within a package

Hello -

I am working through a package that requires first building host tools from source that are subsequently used by the target cross-platform build. The tools are built from an existing Makefile that we currently build from the command line. When I build the same Makefile from within an OpenWrt Host/Compile section, the calls to pkg-config from that Makefile fail. Specifically the required link library (--libs) request doesn't return anything.

Specifically, the command to return libraries required by gtk+-3.0 fails when called within the OpenWrt package build, but succeeds when the same Makefile is built outside the OpenWrt package build. From the command line shell:

brianfriedkin@ubuntu:~/openwrt$ pkg-config --libs gtk+-3.0
-lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

Called from the Makefile built outside OpenWrt, using @echo to output the same:

PKGCONFIG = $(shell which pkg-config)
LINK_OPTIONS = $(shell $(PKGCONFIG) --libs gtk+-3.0)
	@echo "# gtk+-3.0 libraries: " $(LINK_OPTIONS)

gtk+-3.0 libraries:  -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

Note the results are the same when calling pkg-config from the command line and from within my Makefile.

The failure occurs building the same Makefile but called from the OpenWrt package Host/Compile section:

define Host/Compile
	@echo "Host/Compile"
	cd $(MODDABLE)/build/makefiles/lin; $(MAKE)
endef
brianfriedkin@ubuntu:~/openwrt$ make package/hosttest/{clean,compile} V=s
Collecting package info: done
make[1]: Entering directory '/home/brianfriedkin/openwrt'
...
make[4]: Entering directory '/home/brianfriedkin/Projects/moddable-openwrt/build/makefiles/lin'
Package gtk+-3.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-3.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-3.0' found
# simulator debug : cc main.c
/home/brianfriedkin/Projects/moddable-openwrt/build/simulator/lin/main.c:29:10: fatal error: glib.h: No such file or directory

I should note that the Makefile calls make a number of times sequentially to build different targets. It isn't clear to me if this affects the problem at hand:

debug:
	make -f serial2xsbug.mk
	make -f simulator.mk
	make -f tools.mk

Anyway, after some trial and error and exploring various workarounds, I found that the package build succeeds If I add an empty Host/Exports section and pass the pkg-config binary path to $(MAKE). Here are the workarounds to the OpenWrt package Makefile:

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/host-build.mk
...
define Host/Exports
endef
...
define Host/Compile
	@echo "Host/Compile"
	cd $(MODDABLE)/build/makefiles/lin; $(MAKE) PKGCONFIG=/usr/bin/pkg-config
endef

And then the corresponding change to the external Makefile:

PKGCONFIG ?= $(shell which pkg-config)

I believe that the empty Host/Exports section overrides the same in host-build.mk, which itself exports a number of variables, including PKG_CONFIG_PATH and PKG_LIB_DIR. That said, the build still fails unless I also pass the PKGCONFIG path to $(MAKE). Both changes to the OpenWrt Makefile are required.

Looking deeper at the problem, without the workarounds, I see that $(PKGCONFIG) is different when hosted from the OpenWrt Makefile. Tracing $(PKGCONFIG) from my external Makefile without the workarounds:

@echo "# PKGCONFIG: " $(PKGCONFIG)
@echo "# PKG_CONFIG_PATH: " $(PKG_CONFIG_PATH)
@echo "# PKG_CONFIG_LIBDIR: " $(PKG_CONFIG_LIBDIR)
# PKGCONFIG:  /home/brianfriedkin/openwrt/staging_dir/host/bin/pkg-config
# PKG_CONFIG_PATH:  /home/brianfriedkin/openwrt/staging_dir/host/lib/pkgconfig:/home/brianfriedkin/openwrt/staging_dir/hostpkg/lib/pkgconfig
# PKG_CONFIG_LIBDIR:  /home/brianfriedkin/openwrt/staging_dir/hostpkg/lib/pkgconfig

Outside of the OpenWrt build, both PKG_CONFIG_PATH and PKG_CONFIG_LIBDIR are empty:

brianfriedkin@ubuntu:~/openwrt$ echo $PKG_CONFIG_PATH

brianfriedkin@ubuntu:~/openwrt$ echo $PKG_CONFIG_LIBDIR

I am looking for a way to modify my OpenWrt Makefile that avoids having to pass PKGCONFIG=/usr/bin/pkg-config to $(MAKE). That is just a workaround that won't work if pkg-config is at a different location on the host. I hope I've provided enough detail to explain the problem and I am very much looking forward to a solution.

Regards,
Brian