When building against a distribution kernel there is no in-tree build
directory. Detect this by checking for .config in the source tree root and
fall back to /lib/modules/$(uname -r)/build, which is the standard location
for distribution kernel module build directories.
An explicit O= value still takes highest precedence.
Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
---
tools/testing/selftests/bpf/test_kmods/Makefile | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/test_kmods/Makefile b/tools/testing/selftests/bpf/test_kmods/Makefile
index 63c4d3f6a12f..8babf9b98684 100644
--- a/tools/testing/selftests/bpf/test_kmods/Makefile
+++ b/tools/testing/selftests/bpf/test_kmods/Makefile
@@ -1,5 +1,9 @@
TEST_KMOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
-KDIR ?= $(abspath $(TEST_KMOD_DIR)/../../../../..)
+SRCTREE_KDIR := $(abspath $(TEST_KMOD_DIR)/../../../../..)
+# Prefer an explicit O= build output directory if specified. Otherwise use the
+# in-tree kernel build when it has been configured, and fall back to the
+# running kernel's module build directory for distribution kernel builds.
+KDIR ?= $(if $(O),$(O),$(if $(wildcard $(SRCTREE_KDIR)/.config),$(SRCTREE_KDIR),/lib/modules/$(shell uname -r)/build))
ifeq ($(V),1)
Q =
--
2.53.0