Skip to content

Commit

Permalink
selftests: error out if kernel header files are not yet built
Browse files Browse the repository at this point in the history
As per a discussion with Muhammad Usama Anjum [1], the following is how
one is supposed to build selftests:

    make headers && make -C tools/testing/selftests/mm

Change the selftest build system's lib.mk to fail out with a helpful
message if that prerequisite "make headers" has not been done yet.

[1] https://lore.kernel.org/all/[email protected]/

[[email protected]: abort the make process the first time headers aren't detected]
  Link: https://lkml.kernel.org/r/[email protected]
[[email protected]: fix out-of-tree builds]
  Link: https://lkml.kernel.org/r/[email protected]
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: John Hubbard <[email protected]>
Signed-off-by: Anders Roxell <[email protected]>
Reviewed-by: Muhammad Usama Anjum <[email protected]>
Tested-by: Muhammad Usama Anjum <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Shuah Khan <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
johnhubbard authored and akpm00 committed Jun 19, 2023
1 parent 01d6c48 commit 9fc96c7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
21 changes: 20 additions & 1 deletion tools/testing/selftests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ ifneq ($(KBUILD_OUTPUT),)
abs_objtree := $(realpath $(abs_objtree))
BUILD := $(abs_objtree)/kselftest
KHDR_INCLUDES := -isystem ${abs_objtree}/usr/include
KHDR_DIR := ${abs_objtree}/usr/include
else
BUILD := $(CURDIR)
abs_srctree := $(shell cd $(top_srcdir) && pwd)
KHDR_INCLUDES := -isystem ${abs_srctree}/usr/include
KHDR_DIR := ${abs_srctree}/usr/include
DEFAULT_INSTALL_HDR_PATH := 1
endif

Expand All @@ -162,7 +164,7 @@ export KHDR_INCLUDES
# all isn't the first target in the file.
.DEFAULT_GOAL := all

all:
all: kernel_header_files
@ret=1; \
for TARGET in $(TARGETS); do \
BUILD_TARGET=$$BUILD/$$TARGET; \
Expand All @@ -173,6 +175,23 @@ all:
ret=$$((ret * $$?)); \
done; exit $$ret;

kernel_header_files:
@ls $(KHDR_DIR)/linux/*.h >/dev/null 2>/dev/null; \
if [ $$? -ne 0 ]; then \
RED='\033[1;31m'; \
NOCOLOR='\033[0m'; \
echo; \
echo -e "$${RED}error$${NOCOLOR}: missing kernel header files."; \
echo "Please run this and try again:"; \
echo; \
echo " cd $(top_srcdir)"; \
echo " make headers"; \
echo; \
exit 1; \
fi

.PHONY: kernel_header_files

run_tests: all
@for TARGET in $(TARGETS); do \
BUILD_TARGET=$$BUILD/$$TARGET; \
Expand Down
40 changes: 37 additions & 3 deletions tools/testing/selftests/lib.mk
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,26 @@ endif
selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
top_srcdir = $(selfdir)/../../..

ifeq ($(KHDR_INCLUDES),)
KHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
ifeq ("$(origin O)", "command line")
KBUILD_OUTPUT := $(O)
endif

ifneq ($(KBUILD_OUTPUT),)
# Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
# expand a shell special character '~'. We use a somewhat tedious way here.
abs_objtree := $(shell cd $(top_srcdir) && mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
$(if $(abs_objtree),, \
$(error failed to create output directory "$(KBUILD_OUTPUT)"))
# $(realpath ...) resolves symlinks
abs_objtree := $(realpath $(abs_objtree))
KHDR_DIR := ${abs_objtree}/usr/include
else
abs_srctree := $(shell cd $(top_srcdir) && pwd)
KHDR_DIR := ${abs_srctree}/usr/include
endif

KHDR_INCLUDES := -isystem $(KHDR_DIR)

# The following are built by lib.mk common compile rules.
# TEST_CUSTOM_PROGS should be used by tests that require
# custom build rule and prevent common build rule use.
Expand All @@ -58,7 +74,25 @@ TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))

all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
all: kernel_header_files $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) \
$(TEST_GEN_FILES)

kernel_header_files:
@ls $(KHDR_DIR)/linux/*.h >/dev/null 2>/dev/null; \
if [ $$? -ne 0 ]; then \
RED='\033[1;31m'; \
NOCOLOR='\033[0m'; \
echo; \
echo -e "$${RED}error$${NOCOLOR}: missing kernel header files."; \
echo "Please run this and try again:"; \
echo; \
echo " cd $(top_srcdir)"; \
echo " make headers"; \
echo; \
exit 1; \
fi

.PHONY: kernel_header_files

define RUN_TESTS
BASE_DIR="$(selfdir)"; \
Expand Down

0 comments on commit 9fc96c7

Please sign in to comment.