testsuite: Handle KDIR=any

I update my kernel frequently and may not be running a kernel with
headers installed. That always leads me to:

	1) run `meson test -C build`
	2) Oh, crap, it fails: no kernel headers
	3) Find whatever kernel header is installed and point KDIR to it

We can do better and tell KDIR=any to use whatever it finds.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/384
This commit is contained in:
Lucas De Marchi 2025-06-27 12:01:15 -05:00
parent 9c1a363e3e
commit ea67bad278
2 changed files with 27 additions and 7 deletions

View File

@ -65,6 +65,18 @@ When working on kmod, use the included `build-dev.ini` file, as:
meson setup --native-file build-dev.ini builddir/
The testsuite can be executed with:
meson test -C builddir
It builds test kernel modules, so kernel headers need to be pre-installed. By
default it tries to use the kernel header for the currently running kernel.
`KDIR=any` environment variable can be used to tell it to use any installed
kernel header or `KDIR=/path/to/specific/headers` when a specific one is
needed. Example:
KDIR=any meson test -C builddir
Make sure to read [our contributing guide](CONTRIBUTING.md) and the other
READMEs: [libkmod](libkmod/README) and [testsuite](testsuite/README).

View File

@ -1,14 +1,22 @@
# When KDIR is not manually set, use the MODULE_DIRECTORY for the pre-existent
# kmod, not the one being built, to figure out how to get to the kernel
# sources/headers for building our own dummy kernel modules. The final
# location of the modules may change later by scripts/setup-rootfs.sh that
# assembles a per-test-rootfs.
ifndef KDIR
MODULE_DIRECTORY := $(shell pkg-config --variable module_directory kmod)
ifeq ($(MODULE_DIRECTORY),)
# sources/headers of the currently running kernel for building our own dummy
# kernel modules. The final location of the modules may change later by
# scripts/setup-rootfs.sh that assembles a per-test-rootfs.
#
# KDIR=any can also be used to use any installed headers, not necessarily the
# currently running one. This makes it easier to handle distros that
# install modules in a different dir even for minor updates.
MODULE_DIRECTORY := $(shell pkg-config --variable module_directory kmod)
ifeq ($(MODULE_DIRECTORY),)
MODULE_DIRECTORY := /lib/modules
endif
endif
ifndef KDIR
KDIR := $(MODULE_DIRECTORY)/$$(uname -r)/build
else ifeq ($(KDIR),any)
KDIR := $(shell find $(MODULE_DIRECTORY) -maxdepth 1 -mindepth 1 -type d | sort -V | tail -n1)/build
endif
ARCH_SPECIFIC_MODULES := mod-simple-x86_64.ko mod-simple-i386.ko mod-simple-sparc64.ko