Re: [PATCH v4 0/7] kbuild: build the userspace tools in tools/ with the kernel
From: Sasha Levin
Date: Tue Dec 01 2026 - 12:51:01 EST
On Mon, Nov 30, 2026 at 07:35:12PM -0700, Nathan Chancellor wrote:
>I took this for a spin on next-20261130 on an arm64 test box. Everything
>built successfully, although I did have some warnings with clang-23 (I
>think you have sent patches for some of them but I didn't check too
>closely).
Thanks for trying it out!
The rseq fixes I sent earlier are in -next already. The warnings you got
are new: clang 23's -Wunused-but-set-global in spidev_fdx.c,
taskstats_fill_stats_tgid.c and param_test.c, and a function pointer
comparison in a proc selftest that went in after rc1. I have fixes for
those and will send them to their maintainers.
One question while you're here: param_test.c marks loop_cnt_1..6 as
"used" and only reads them from inline asm on x86, and clang 23 still
says "set but not used". Is that intended? "used" does silence
-Wunused-variable.
>1. Some of the tests do not respect '-s', resulting in a decent amount
> of additional output for allmodconfig (see recording below).
That's the selftests' lib.mk, it prints its "CC" lines no matter what.
I have a patch for it, and one for the version perf prints on stderr.
>2. I see some issues with warnings being garbled, maybe from too much
> parallel building?
Yes. rseq builds param_test.c six times at once and each clang writes
its warnings straight to the terminal. A plain "make -j -C
tools/testing/selftests/rseq" does the same.
>3. I worry about warnings coming from the tools build not being as easy
> to investigate because the warning does not always have the absolute
> or srctree relative file name, requiring an additional command to
> search the tree for the file in tools before being able to fix it. It
> is just unfortunate ergonomics.
Agreed. With the change below, each tool's make runs with -Otarget and
its stderr goes to a file. Once the tools are built, the files are shown
one after the other, each under a line that names the tool's directory:
tools_build: tools/spi:
spidev_fdx.c:16:12: warning: variable 'verbose' set but not used [-Wunused-but-set-global]
16 | static int verbose;
| ^
1 warning generated.
-Otarget is only passed with GNU make 4.4 and later. Older versions lock
stdout for it and can hang when that's /dev/null.
With that and the patches above, your build (arm64, clang 23.1.6,
next-20261130, cross-compiled here) prints nothing with -s.
This is what I have on top of v4:
diff --git a/Makefile b/Makefile
index 2a69f4c73a641..abd8ddb2ded50 100644
--- a/Makefile
+++ b/Makefile
@@ -1648,7 +1648,8 @@ tools/%: FORCE
# The userspace programs selected in tools/Kconfig, built for the target
PHONY += tools_build
tools_build: headers
- $(Q)$(MAKE) -f $(srctree)/tools/Makefile.kbuild
+ $(Q)$(MAKE) -f $(srctree)/tools/Makefile.kbuild; ret=$$?; \
+ $(MAKE) -f $(srctree)/tools/Makefile.kbuild __tools_report && exit $$ret
ifdef CONFIG_TOOLS_BUILD
all: tools_build
diff --git a/tools/Makefile.kbuild b/tools/Makefile.kbuild
index 8fcaef8c147fc..a4988bec2e738 100644
--- a/tools/Makefile.kbuild
+++ b/tools/Makefile.kbuild
@@ -44,28 +44,54 @@ endif
squote := '
escsq = $(subst $(squote),'\$(squote)',$1)
-# $(call build,<output directory>,<make arguments>,<files to check>,<CONFIG_ symbol>[,<on failure>])
+# $(call build,<directory in tools/>,<output directory>,<make arguments>,<files to check>,<CONFIG_ symbol>[,<on failure>])
# Builds in an empty directory when the make arguments, LDFLAGS or the
# compiler changed, or when one of the files, or a file in one of the
# directories, is newer than the last build. A failure fails the build,
# unless commands to run instead are given. "make -n" only shows the build.
-build-key = $(call escsq,$(2) LDFLAGS=$(USERLDFLAGS) $(CONFIG_CC_VERSION_TEXT))
+#
+# The tools name files relative to their own directory, and compilers that
+# run in parallel write into each other's messages. What a tool prints on
+# stderr goes to a file next to its output directory, below the name of the
+# tool's directory, and __tools_report shows these files one after the
+# other when the tools are built. A file is kept and added to until it was
+# shown, so that an interrupted build does not lose what it holds.
+build-key = $(call escsq,-C $(toolsdir)/$(1) $(3) LDFLAGS=$(USERLDFLAGS) $(CONFIG_CC_VERSION_TEXT))
dry-run := $(findstring n,$(firstword -$(MAKEFLAGS)))
+# GNU make before 4.4 locks stdout to synchronize its output, and can hang
+# when that is /dev/null.
+output-sync := $(if $(filter notintermediate,$(.FEATURES)),-Otarget)
# Headers the kernel build generates. When the kernel is built in the source
# tree, they are written after the tools were built.
generated-headers := -path '$(abs_srctree)/include/generated' -o -path '$(abs_srctree)/include/config' \
-o -path '$(abs_srctree)/arch/*/include/generated'
define build
- +$(Q)$(if $(dry-run),,if [ "$$(cat $(1)/.tools_build 2>/dev/null)" != '$(build-key)' ] $(if $(3),|| \
- [ -n "$$(find $(3) \( $(generated-headers) \) -prune -o -newer $(1)/.tools_build -type f -print -quit 2>/dev/null)" ]); then \
- rm -rf $(1); \
+ +$(Q)$(if $(dry-run),,if [ "$$(cat $(2)/.tools_build 2>/dev/null)" != '$(build-key)' ] $(if $(4),|| \
+ [ -n "$$(find $(4) \( $(generated-headers) \) -prune -o -newer $(2)/.tools_build -type f -print -quit 2>/dev/null)" ]); then \
+ rm -rf $(2); \
fi;) \
- mkdir -p $(1) && $(MAKE) $(2) && \
- $(if $(dry-run),:,printf '%s' '$(build-key)' > $(1)/.tools_build) || \
- { echo >&2 "tools_build: $(patsubst $(outdir)/%,%,$(1)) failed to build; set CONFIG_$(4)=n to skip it"; $(or $(5),false); }
+ mkdir -p $(2) && \
+ $(if $(dry-run),,{ [ -s $(2).tools_build.log ] || echo 'tools_build: tools/$(1):' > $(2).tools_build.log; } && \
+ exec 2>> $(2).tools_build.log &&) \
+ $(MAKE) $(output-sync) -C $(toolsdir)/$(1) $(3) && \
+ $(if $(dry-run),:,printf '%s' '$(build-key)' > $(2)/.tools_build) || \
+ { echo >&2 "tools_build: tools/$(1) failed to build; set CONFIG_$(5)=n to skip it"; $(or $(6),false); }
endef
+# A file with nothing after its first line has nothing to show. A file is
+# removed once it was read and shown; failing to do that fails the build.
+PHONY += __tools_report
+__tools_report:
+ $(Q)[ -d $(outdir) ] || exit 0; set -f; \
+ logs=$$(find $(outdir) -name '*.tools_build.log') && \
+ logs=$$(printf '%s\n' $$logs | sort) || exit 1; ret=0; \
+ for log in $$logs; do \
+ size=$$(wc -c < "$$log") && header=$$(head -n 1 "$$log") || { ret=1; continue; }; \
+ [ $$((size)) -le $$(($${#header} + 1)) ] || cat "$$log" >&2 || { ret=1; continue; }; \
+ rm -f "$$log" || ret=1; \
+ done; exit $$ret
+
# Programs in tools/<name>, which track their own header dependencies
tools-$(CONFIG_TOOLS_BUILD_BOOTCONFIG) += bootconfig
tools-$(CONFIG_TOOLS_BUILD_COUNTER) += counter
@@ -82,7 +108,7 @@ __tools_build: $(tool-targets)
PHONY += $(tool-targets)
$(tool-targets): tool-%: FORCE
- $(call build,$(outdir)/$*,-C $(toolsdir)/$* O=$(outdir)/$* CC='$(call escsq,$(tools-cc))',,TOOLS_BUILD_$(shell echo $* | tr a-z A-Z))
+ $(call build,$*,$(outdir)/$*,O=$(outdir)/$* CC='$(call escsq,$(tools-cc))',,TOOLS_BUILD_$(shell echo $* | tr a-z A-Z))
# perf, without the feature table in the kernel build log
perf-flags := CC='$(call escsq,$(tools-cc))' FEATURE_DISPLAY= WERROR=$(if $(CONFIG_WERROR),1,0)
@@ -92,7 +118,7 @@ __tools_build: $(if $(CONFIG_TOOLS_BUILD_PERF),perf)
PHONY += perf
perf: FORCE
- $(call build,$(outdir)/perf,-C $(toolsdir)/perf -f Makefile.perf O=$(outdir)/perf $(perf-flags) $(perf-flags-y),,TOOLS_BUILD_PERF)
+ $(call build,perf,$(outdir)/perf,-f Makefile.perf O=$(outdir)/perf $(perf-flags) $(perf-flags-y),,TOOLS_BUILD_PERF)
# Selftests in tools/testing/selftests/<name>
selftests-$(CONFIG_TOOLS_BUILD_SELFTESTS_ACCT) += acct
@@ -211,7 +237,7 @@ __tools_build: $(selftest-targets)
PHONY += $(selftest-targets)
$(selftest-targets): selftest-%: $(selftest-shared) FORCE
- $(call build,$(outdir)/selftests/$*,-C $(toolsdir)/testing/selftests/$* OUTPUT=$(outdir)/selftests/$* $(selftest-flags),$(selftest-shared) $(toolsdir)/testing/selftests/$* $(selftest-extra-$*),TOOLS_BUILD_SELFTESTS_$(shell echo $* | tr a-z/- A-Z__),$(call selftest-failed,$*))
+ $(call build,testing/selftests/$*,$(outdir)/selftests/$*,OUTPUT=$(outdir)/selftests/$* $(selftest-flags),$(selftest-shared) $(toolsdir)/testing/selftests/$* $(selftest-extra-$*),TOOLS_BUILD_SELFTESTS_$(shell echo $* | tr a-z/- A-Z__),$(call selftest-failed,$*))
PHONY += FORCE
FORCE:
--
Thanks,
Sasha