kbuild: let the environment set HOSTPKG_CONFIG
Subsystems (2)
PATCH 1/1 kbuild: let the environment set HOSTPKG_CONFIG
HOSTPKG_CONFIG was added so that "tooling that builds the kernel" could choose which pkg-config the host tools ask about their dependencies, but it is assigned with '=', so only a make command line assignment takes effect. Exporting it does nothing, silently.
That is surprising, because the other inputs to host program builds do honour the environment: HOSTCFLAGS, HOSTCXXFLAGS, HOSTLDFLAGS and HOSTLDLIBS are all read from it and documented in kbuild.rst as such.
It also matters. A cross development environment sets PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH so that pkg-config answers for the target rather than the build machine. Those settings are correct for everything that will run on the target and wrong for a program that has to run on the build machine. objtool asks pkg-config where libelf is, gets the target's include directory, and compiles a host tool against the target's C library headers. Because the directory arrives as -I it is not treated as a system header directory, -Wno-system-headers no longer covers it, and the build stops on warnings that would otherwise be suppressed:
usr/include/sys/cdefs.h:486: error: "__attribute_const__" redefined [-Werror] tools/include/linux/compiler.h:123: note: previous definition usr/include/stdio.h:449: error: redundant redeclaration of 'fscanf'
An environment that knows it is cross building can then say
export HOSTPKG_CONFIG="env -u PKG_CONFIG_SYSROOT_DIR \
-u PKG_CONFIG_PATH pkg-config"once, in the shell it hands to the user, instead of every make command line growing an override. Build systems that already pass it on the command line are unaffected.
--- Documentation/kbuild/kbuild.rst | 5 +++++ Makefile | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
Documentation/kbuild/kbuild.rst +5 -0
@@ -110,6 +110,11 @@ HOSTLDLIBS ---------- Additional libraries to link against when building host programs. +HOSTPKG_CONFIG +-------------- +The pkg-config to use when building host programs. Set this when cross +building, where the default would otherwise answer for the target. + .. _userkbuildflags: USERCFLAGS
Makefile +1 -1
@@ -461,7 +461,7 @@ HOSTCC = gcc HOSTCXX = g++ endif HOSTRUSTC = rustc -HOSTPKG_CONFIG = pkg-config +HOSTPKG_CONFIG ?= pkg-config # the KERNELDOC macro needs to be exported, as scripts/Makefile.build # has a logic to call it
Signature · 1 line
2.51.0
Nathan Chancellor
maintainerQuoted 4 lines
HOSTPKG_CONFIGwas added so that "tooling that builds the kernel" could choose which pkg-config the host tools ask about their dependencies, but it is assigned with '=', so only a make command line assignment takes effect. Exporting it does nothing, silently.
I found this paragraph a little hard to read and parse. I might write it like:
HOSTPKG_CONFIG was added so that builders could choose which pkg-config is used to query host dependencies. However, it does not respect values exported from the builder's environment because its assigment uses '=', requiring a make command line variable to override it.
Quoted 3 lines
That is surprising, because the other inputs to host program builds do honour the environment: HOSTCFLAGS, HOSTCXXFLAGS, HOSTLDFLAGS and HOSTLDLIBS are all read from it and documented in
kbuild.rstas such.
I am not sure HOSTPKG_CONFIG is entirely comparable to these variables, which are purely for compiler flags. I would compare it more to HOSTCC, HOSTCXX, and HOSTRUSTC, which all use '=' and do not honor an environment value. I am not sure we want to deviate from that here but I am open to opinions from the other Kbuild folks.
Quoted 15 lines
It also matters. A cross development environment sets
PKG_CONFIG_SYSROOT_DIRandPKG_CONFIG_PATHso that pkg-config answers for the target rather than the build machine. Those settings are correct for everything that will run on the target and wrong for a program that has to run on the build machine. objtool asks pkg-config where libelf is, gets the target's include directory, and compiles a host tool against the target's C library headers. Because the directory arrives as -I it is not treated as a system header directory, -Wno-system-headers no longer covers it, and the build stops on warnings that would otherwise be suppressed:
usr/include/sys/cdefs.h:486: error: "__attribute_const__" redefined [-Werror]tools/include/linux/compiler.h:123: note: previous definitionusr/include/stdio.h:449: error: redundant redeclaration of 'fscanf'
Quoted 8 lines
An environment that knows it is cross building can then say
export HOSTPKG_CONFIG="env -u PKG_CONFIG_SYSROOT_DIR \ -u PKG_CONFIG_PATH pkg-config"once, in the shell it hands to the user, instead of every make command line growing an override. Build systems that already pass it on the command line are unaffected.
It would be nice if we could try and handle this situation directly inside Kbuild somehow (like just unsetting these PKG_CONFIG variables) but I guess that could mess with cross building tools/perf via the tools/% Kbuild target... maybe that doesn't matter though. I am not sure I find the argument of avoiding make command overrides convincing, as I would expect a build wrapper to already need to support a unified set of variables but maybe I am just naive. Another option would be passing this value via MAKEFLAGS.
Signature · 2 lines
Cheers, Nathan
Nicolas Schier
maintainer[resending from 2026-08-05, as my mail client broke the reply recipient
list]
Quoted 24 lines · Trevor Woerner
HOSTPKG_CONFIGwas added so that "tooling that builds the kernel" could choose which pkg-config the host tools ask about their dependencies, but it is assigned with '=', so only a make command line assignment takes effect. Exporting it does nothing, silently.That is surprising, because the other inputs to host program builds do honour the environment: HOSTCFLAGS, HOSTCXXFLAGS, HOSTLDFLAGS and HOSTLDLIBS are all read from it and documented in
kbuild.rstas such.It also matters. A cross development environment sets
PKG_CONFIG_SYSROOT_DIRandPKG_CONFIG_PATHso that pkg-config answers for the target rather than the build machine. Those settings are correct for everything that will run on the target and wrong for a program that has to run on the build machine. objtool asks pkg-config where libelf is, gets the target's include directory, and compiles a host tool against the target's C library headers. Because the directory arrives as -I it is not treated as a system header directory, -Wno-system-headers no longer covers it, and the build stops on warnings that would otherwise be suppressed:
usr/include/sys/cdefs.h:486: error: "__attribute_const__" redefined [-Werror]tools/include/linux/compiler.h:123: note: previous definitionusr/include/stdio.h:449: error: redundant redeclaration of 'fscanf'
Quoted 7 lines
An environment that knows it is cross building can then say
export HOSTPKG_CONFIG="env -u PKG_CONFIG_SYSROOT_DIR \ -u PKG_CONFIG_PATH pkg-config"once, in the shell it hands to the user, instead of every make command line growing an override.
Thank for the patch with the detailed description. This sounds to be consistent, but looking at the other variables that effectively can be set via environment it looks differently to me: Environment variables are honoured especially for _flags_, while most variables for _tooling paths_ (e.g. HOSTCC) are to be overriden via make command line (w/ exceptions e.g. CROSS_COMPILE, LLVM). Right now, I am not aware of the reason for this distinction, but before opening the gate for tooling overrides via environment, we should think it through (aka Chesterton's Fence).
Another simple way of not piling up overrides could be something like:
cat <<-eof >Makefile.cross
override HOSTPKG_CONFIG := env -u PKG_CONFIG_SYSROOT_DIR -u PKG_CONFIG_PATH pkg-config include Makefile
eof
make -f Makefile.cross
So, I suggest to wait a bit and see if further arguments come up.
Kind regards,
Nicolas