[PATCH v7 23/23] selftests: Include tools Makefile.include

From: Leo Yan

Date: Tue Jun 02 2026 - 10:56:54 EST


selftests/lib.mk has its own compiler setup instead of using the common
tools build helpers. This duplicates part of the LLVM handling prepared
by tools/scripts/Makefile.include.

Include tools/scripts/Makefile.include from selftests/lib.mk so
selftests reuse the LLVM prefix/suffix validation and the provided
CLANG_CROSS_FLAGS. This improves Clang cross builds, especially when
CROSS_COMPILE is set it can derive --prefix, --sysroot and
--gcc-toolchain flags from the GCC cross toolchain.

Keep the $(CLANG_TARGET_FLAGS_$(ARCH)) target flag as a fallback when
CLANG_CROSS_FLAGS is empty, and append EXTRA_CFLAGS to the selftest.

Signed-off-by: Leo Yan <leo.yan@xxxxxxx>
---
tools/testing/selftests/lib.mk | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/tools/testing/selftests/lib.mk b/tools/testing/selftests/lib.mk
index f02cc8a2e4ae322d65a031178013270922611c42..e2e2d8aa57280ffd37bced18ecd43e7a5566abae 100644
--- a/tools/testing/selftests/lib.mk
+++ b/tools/testing/selftests/lib.mk
@@ -1,11 +1,17 @@
# This mimics the top-level Makefile. We do it explicitly here so that this
# Makefile can operate with or without the kbuild infrastructure.
-ifneq ($(LLVM),)
-ifneq ($(filter %/,$(LLVM)),)
-LLVM_PREFIX := $(LLVM)
-else ifneq ($(filter -%,$(LLVM)),)
-LLVM_SUFFIX := $(LLVM)
+ifeq (0,$(MAKELEVEL))
+ ifeq ($(OUTPUT),)
+ OUTPUT := $(shell pwd)
+ DEFAULT_INSTALL_HDR_PATH := 1
+ endif
endif
+selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
+top_srcdir = $(selfdir)/../../..
+
+include $(top_srcdir)/tools/scripts/Makefile.include
+
+ifneq ($(LLVM),)

CLANG := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)

@@ -35,7 +41,8 @@ else
CLANG_FLAGS += --target=$(CLANG_TARGET_FLAGS)
endif # CLANG_TARGET_FLAGS
else
-CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%))
+# CLANG_CROSS_FLAGS comes from Makefile.include
+CLANG_FLAGS += $(CLANG_CROSS_FLAGS)
endif # CROSS_COMPILE

# gcc defaults to silence (off) for the following warnings, but clang defaults
@@ -51,15 +58,6 @@ else
CC := $(CROSS_COMPILE)gcc
endif # LLVM

-ifeq (0,$(MAKELEVEL))
- ifeq ($(OUTPUT),)
- OUTPUT := $(shell pwd)
- DEFAULT_INSTALL_HDR_PATH := 1
- endif
-endif
-selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
-top_srcdir = $(selfdir)/../../..
-
# msg: emit succinct information message describing current building step
# $1 - generic step name (e.g., CC, LINK, etc);
# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
@@ -202,6 +200,8 @@ CFLAGS += -D_GNU_SOURCE=
# Additional include paths needed by kselftest.h and local headers
CFLAGS += -I${top_srcdir}/tools/testing/selftests

+CFLAGS += $(EXTRA_CFLAGS)
+
# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
# make USERCFLAGS=-Werror USERLDFLAGS=-static
CFLAGS += $(USERCFLAGS)

--
2.34.1