When building against a distribution kernel there may be no in-tree build directory. Prefer explicit O= first, then KBUILD_OUTPUT from the environment, then an in-tree source tree if Module.symvers is present, and finally /lib/modules/$(uname -r)/build.
Also skip test_kmods all/clean when the selected KDIR does not exist, so selftests clean/install flows do not fail on systems without that host module build directory. Signed-off-by: Ricardo B. Marliere <[email protected]> --- tools/testing/selftests/bpf/test_kmods/Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/test_kmods/Makefile b/tools/testing/selftests/bpf/test_kmods/Makefile index 63c4d3f6a12f..a5216815d989 100644 --- a/tools/testing/selftests/bpf/test_kmods/Makefile +++ b/tools/testing/selftests/bpf/test_kmods/Makefile @@ -1,5 +1,6 @@ TEST_KMOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) -KDIR ?= $(abspath $(TEST_KMOD_DIR)/../../../../..) +SRCTREE_KDIR := $(abspath $(TEST_KMOD_DIR)/../../../../..) +KDIR ?= $(if $(O),$(O),$(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT),$(if $(wildcard $(SRCTREE_KDIR)/Module.symvers),$(SRCTREE_KDIR),/lib/modules/$(shell uname -r)/build))) ifeq ($(V),1) Q = @@ -14,8 +15,13 @@ $(foreach m,$(MODULES),$(eval obj-m += $(m:.ko=.o))) CFLAGS_bpf_testmod.o = -I$(src) +# Ensure KDIR exists, otherwise skip module build and clean. all: +ifneq ("$(wildcard $(KDIR))", "") $(Q)$(MAKE) -C $(KDIR) M=$(TEST_KMOD_DIR) modules +endif clean: +ifneq ("$(wildcard $(KDIR))", "") $(Q)$(MAKE) -C $(KDIR) M=$(TEST_KMOD_DIR) clean +endif -- 2.53.0

