When building with O= the test-runner enumeration headers (prog_tests/tests.h, map_tests/tests.h), the verifier test list (verifier/tests.h) and the signing certificate header (verification_cert.h) are written into the source tree instead of the out-of-tree build directory. Fix TRUNNER_TESTS_HDR to resolve under $(OUTPUT) and add an order-only prerequisite so the subdirectory exists before the recipe runs. The mkdir must precede the shell redirect in the same command, so use '&&' rather than nesting mkdir inside the parenthesised group (the redirect opens the file before the subshell starts). Fix VERIFY_SIG_HDR likewise; the recipe already writes to $@ so no further changes are needed there. For verifier/tests.h, also resolve the target under $(OUTPUT) and generate absolute paths in the '#include' directives, since the relative '"file.c"' form would resolve against the output directory rather than the source tree. Signed-off-by: Ricardo B. Marlière (SUSE) --- tools/testing/selftests/bpf/Makefile | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index b642ee489ea6..b289183475d4 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -580,7 +580,7 @@ TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ TRUNNER_LIB_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ $$(filter %.c,$(TRUNNER_LIB_SOURCES))) TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES)) -TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h +TRUNNER_TESTS_HDR := $(OUTPUT)/$(TRUNNER_TESTS_DIR)/tests.h TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c)) TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.bpf.o, $$(TRUNNER_BPF_SRCS)) TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \ @@ -720,9 +720,9 @@ endif # ensure we set up tests.h header generation rule just once ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),) $(TRUNNER_TESTS_DIR)-tests-hdr := y -$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c +$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c | $(TRUNNER_OUTPUT) $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@) - $$(shell (echo '/* Generated header, do not edit */'; \ + $$(shell mkdir -p $$(dir $$@) && (echo '/* Generated header, do not edit */'; \ sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p' \ $(TRUNNER_TESTS_DIR)/*.c | sort ; \ ) > $$@) @@ -802,7 +802,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(if $(filter test_progs%,$1),$(if $(PERMISSIVE),$$ endef VERIFY_SIG_SETUP := $(CURDIR)/verify_sig_setup.sh -VERIFY_SIG_HDR := verification_cert.h +VERIFY_SIG_HDR := $(OUTPUT)/verification_cert.h VERIFICATION_CERT := $(BUILD_DIR)/signing_key.der PRIVATE_KEY := $(BUILD_DIR)/signing_key.pem @@ -923,14 +923,14 @@ $(eval $(call DEFINE_TEST_RUNNER,test_maps)) # It is much simpler than test_maps/test_progs and sufficiently different from # them (e.g., test.h is using completely pattern), that it's worth just # explicitly defining all the rules explicitly. -verifier/tests.h: verifier/*.c - $(shell ( cd verifier/; \ +$(OUTPUT)/verifier/tests.h: verifier/*.c | $(OUTPUT) + $(shell mkdir -p $(OUTPUT)/verifier && ( \ echo '/* Generated header, do not edit */'; \ echo '#ifdef FILL_ARRAY'; \ - ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \ + ls verifier/*.c 2> /dev/null | sed -e 's@\(.*\)@#include "$(CURDIR)/\1"@'; \ echo '#endif' \ - ) > verifier/tests.h) -$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) + ) > $(OUTPUT)/verifier/tests.h) +$(OUTPUT)/test_verifier: test_verifier.c $(OUTPUT)/verifier/tests.h $(BPFOBJ) | $(OUTPUT) $(call msg,BINARY,,$@) $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ -- 2.55.0 libarena's BPF objects, linked objects, and skeleton headers are always written into the source tree regardless of whether an out-of-tree build directory was specified via O=. Add OUTPUT support to libarena/Makefile: default OUTPUT to '.' for standalone builds, prefix all generated file targets with $(OUTPUT)/, and use addprefix to propagate the directory into the per-object lists. Create $(OUTPUT) at Makefile-load time with a $(shell mkdir -p) so all rules have a valid destination. In the parent bpf/Makefile, pass OUTPUT="$(OUTPUT)/libarena" in LIBARENA_MAKE_ARGS and update LIBARENA_SKEL/LIBARENA_ASAN_SKEL to reflect the new location. Pass the absolute target path as the make goal so the sub-make goal matches the $(OUTPUT)/... rule in libarena/Makefile regardless of the invocation directory. Signed-off-by: Ricardo B. Marlière (SUSE) --- tools/testing/selftests/bpf/Makefile | 11 ++++++----- tools/testing/selftests/bpf/libarena/Makefile | 24 ++++++++++++++---------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index b289183475d4..7d42632f9d42 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -826,7 +826,8 @@ LIBARENA_MAKE_ARGS = \ CLANG="$(CLANG)" \ BPF_CFLAGS="$(BPF_CFLAGS) $(CLANG_CFLAGS)" \ BPF_TARGET_ENDIAN="$(BPF_TARGET_ENDIAN)" \ - Q="$(Q)" + Q="$(Q)" \ + OUTPUT="$(OUTPUT)/libarena" LIBARENA_BPF_DEPS := $(wildcard libarena/Makefile \ libarena/include/* \ @@ -835,17 +836,17 @@ LIBARENA_BPF_DEPS := $(wildcard libarena/Makefile \ libarena/selftests/* \ libarena/*.bpf.o) -LIBARENA_SKEL := libarena/libarena.skel.h +LIBARENA_SKEL := $(OUTPUT)/libarena/libarena.skel.h $(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS) - +$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS) + +$(MAKE) -C libarena $(LIBARENA_SKEL) $(LIBARENA_MAKE_ARGS) ifneq ($(CLANG_HAS_ARENA_ASAN),) -LIBARENA_ASAN_SKEL := libarena/libarena_asan.skel.h +LIBARENA_ASAN_SKEL := $(OUTPUT)/libarena/libarena_asan.skel.h CFLAGS += -DHAS_BPF_ARENA_ASAN $(LIBARENA_ASAN_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS) - +$(MAKE) -C libarena libarena_asan.skel.h $(LIBARENA_MAKE_ARGS) + +$(MAKE) -C libarena $(LIBARENA_ASAN_SKEL) $(LIBARENA_MAKE_ARGS) endif # Define test_progs test runner. diff --git a/tools/testing/selftests/bpf/libarena/Makefile b/tools/testing/selftests/bpf/libarena/Makefile index 5e2ab514805e..195d5f833186 100644 --- a/tools/testing/selftests/bpf/libarena/Makefile +++ b/tools/testing/selftests/bpf/libarena/Makefile @@ -27,10 +27,14 @@ BPFDIR=$(abspath $(LIBARENA)/..) INCLUDE_DIR ?= $(BPFDIR)/tools/include LIBBPF_INCLUDE ?= $(INCLUDE_DIR) +# Build output directory; defaults to in-tree for standalone builds. +OUTPUT ?= . +$(shell mkdir -p $(OUTPUT)) + # Scan src/ and selftests/ to generate the final binaries LIBARENA_SOURCES = $(wildcard $(LIBARENA)/src/*.bpf.c) $(wildcard $(LIBARENA)/selftests/*.bpf.c) -LIBARENA_OBJECTS = $(notdir $(LIBARENA_SOURCES:.bpf.c=.bpf.o)) -LIBARENA_OBJECTS_ASAN = $(notdir $(LIBARENA_SOURCES:.bpf.c=_asan.bpf.o)) +LIBARENA_OBJECTS = $(addprefix $(OUTPUT)/,$(notdir $(LIBARENA_SOURCES:.bpf.c=.bpf.o))) +LIBARENA_OBJECTS_ASAN = $(addprefix $(OUTPUT)/,$(notdir $(LIBARENA_SOURCES:.bpf.c=_asan.bpf.o))) INCLUDES = -I$(LIBARENA)/include -I$(BPFDIR) ifneq ($(INCLUDE_DIR),) @@ -61,32 +65,32 @@ CFLAGS += $(INCLUDES) vpath %.bpf.c $(LIBARENA)/src $(LIBARENA)/selftests vpath %.c $(LIBARENA)/src $(LIBARENA)/selftests -skeletons: libarena.skel.h libarena_asan.skel.h +skeletons: $(OUTPUT)/libarena.skel.h $(OUTPUT)/libarena_asan.skel.h .PHONY: skeletons -libarena_asan.skel.h: libarena_asan.bpf.o +$(OUTPUT)/libarena_asan.skel.h: $(OUTPUT)/libarena_asan.bpf.o $(call msg,GEN-SKEL,libarena,$@) $(Q)$(BPFTOOL) gen skeleton $< name "libarena_asan" > $@ -libarena.skel.h: libarena.bpf.o +$(OUTPUT)/libarena.skel.h: $(OUTPUT)/libarena.bpf.o $(call msg,GEN-SKEL,libarena,$@) $(Q)$(BPFTOOL) gen skeleton $< name "libarena" > $@ -libarena_asan.bpf.o: $(LIBARENA_OBJECTS_ASAN) +$(OUTPUT)/libarena_asan.bpf.o: $(LIBARENA_OBJECTS_ASAN) $(call msg,GEN-OBJ,libarena,$@) $(Q)$(BPFTOOL) gen object $@ $^ -libarena.bpf.o: $(LIBARENA_OBJECTS) +$(OUTPUT)/libarena.bpf.o: $(LIBARENA_OBJECTS) $(call msg,GEN-OBJ,libarena,$@) $(Q)$(BPFTOOL) gen object $@ $^ -%_asan.bpf.o: %.bpf.c +$(OUTPUT)/%_asan.bpf.o: %.bpf.c $(call msg,CLNG-BPF,libarena,$@) $(Q)$(CLANG) $(BPF_CFLAGS) $(ASAN_FLAGS) -DBPF_ARENA_ASAN $(BPF_TARGET_ENDIAN) -c $< -o $@ -%.bpf.o: %.bpf.c +$(OUTPUT)/%.bpf.o: %.bpf.c $(call msg,CLNG-BPF,libarena,$@) $(Q)$(CLANG) $(BPF_CFLAGS) $(BPF_TARGET_ENDIAN) -c $< -o $@ clean: - $(Q)rm -f *.skel.h *.bpf.o *.linked*.o + $(Q)rm -f $(OUTPUT)/*.skel.h $(OUTPUT)/*.bpf.o $(OUTPUT)/*.linked*.o -- 2.55.0 When building bpf selftests with O=, the kernel module intermediates (.ko, .o, .mod.*, .cmd files, Module.symvers, modules.order) produced by the test_kmods sub-make are placed in the source tree instead of the out-of-tree directory, because kbuild external-module builds always write output to the M= source directory by default. Introduce TEST_KMODS_OUTDIR in bpf/Makefile that resolves to $(OUTPUT)/test_kmods when OUTPUT is set, and fall back to the in-tree test_kmods/ directory for standalone builds. Update the pattern rule, the RM invocation, and the copy rule to use this variable, and pass OUTPUT=$(OUTPUT)/test_kmods to the test_kmods sub-make. In test_kmods/Makefile, forward the received OUTPUT value as MO= to the kernel Makefile. The top-level kernel Makefile maps MO= to KBUILD_EXTMOD_OUTPUT, which redirects all external-module build output to the specified directory. The kernel also creates that directory automatically, so no explicit mkdir is needed. Also update the CLEAN target in bpf/Makefile to propagate OUTPUT to both the test_kmods and libarena sub-makes so that clean removes from the correct location. Signed-off-by: Ricardo B. Marlière (SUSE) --- tools/testing/selftests/bpf/Makefile | 16 ++++++++++------ tools/testing/selftests/bpf/test_kmods/Makefile | 3 +++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 7d42632f9d42..b150a66f3c25 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -135,6 +135,9 @@ TEST_PROGS_EXTENDED := \ TEST_KMODS := bpf_testmod.ko bpf_test_no_cfi.ko bpf_test_modorder_x.ko \ bpf_test_modorder_y.ko bpf_test_rqspinlock.ko TEST_KMOD_TARGETS = $(addprefix $(OUTPUT)/,$(TEST_KMODS)) +# Build directory for kernel modules: routed to $(OUTPUT)/test_kmods when +# OUTPUT is set so that kbuild intermediates stay out of the source tree. +TEST_KMODS_OUTDIR := $(if $(OUTPUT),$(OUTPUT)/test_kmods,test_kmods) # Compile but not part of 'make run_tests' TEST_GEN_PROGS_EXTENDED = \ @@ -163,8 +166,8 @@ override define CLEAN $(Q)$(RM) -r $(TEST_GEN_FILES) $(Q)$(RM) -r $(TEST_KMODS) $(Q)$(RM) -r $(EXTRA_CLEAN) - $(Q)$(MAKE) -C test_kmods clean - $(Q)$(MAKE) -C libarena clean + $(Q)$(MAKE) -C test_kmods $(if $(OUTPUT),OUTPUT=$(OUTPUT)/test_kmods) clean + $(Q)$(MAKE) -C libarena $(if $(OUTPUT),OUTPUT=$(OUTPUT)/libarena) clean $(Q)$(MAKE) docs-clean endef @@ -301,17 +304,18 @@ $(OUTPUT)/sign-file: ../../../../scripts/sign-file.c # support that for regular rules. However, pattern matching rules are implicitly # treated as grouped even with older versions of make, so as a workaround, the # subst() turns the rule into a pattern matching rule -$(addprefix test_kmods/,$(subst .ko,%ko,$(TEST_KMODS))): $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard test_kmods/Makefile test_kmods/*.[ch]) - $(Q)$(RM) test_kmods/*.ko test_kmods/*.mod.o # force re-compilation +$(addprefix $(TEST_KMODS_OUTDIR)/,$(subst .ko,%ko,$(TEST_KMODS))): $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard test_kmods/Makefile test_kmods/*.[ch]) + $(Q)$(RM) $(TEST_KMODS_OUTDIR)/*.ko $(TEST_KMODS_OUTDIR)/*.mod.o # force re-compilation $(Q)$(MAKE) $(submake_extras) -C test_kmods \ $(if $(O),O=$(abspath $(O))) \ $(if $(KBUILD_OUTPUT),KBUILD_OUTPUT=$(abspath $(KBUILD_OUTPUT)))\ + $(if $(OUTPUT),OUTPUT=$(OUTPUT)/test_kmods) \ RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \ EXTRA_CFLAGS='' EXTRA_LDFLAGS='' -$(TEST_KMOD_TARGETS): $(addprefix test_kmods/,$(TEST_KMODS)) +$(TEST_KMOD_TARGETS): $(addprefix $(TEST_KMODS_OUTDIR)/,$(TEST_KMODS)) $(call msg,MOD,,$@) - $(Q)$(if $(PERMISSIVE),if [ -f test_kmods/$(@F) ]; then )cp test_kmods/$(@F) $@$(if $(PERMISSIVE),; fi) + $(Q)$(if $(PERMISSIVE),if [ -f $(TEST_KMODS_OUTDIR)/$(@F) ]; then )cp $(TEST_KMODS_OUTDIR)/$(@F) $@$(if $(PERMISSIVE),; fi) DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool diff --git a/tools/testing/selftests/bpf/test_kmods/Makefile b/tools/testing/selftests/bpf/test_kmods/Makefile index 031c7454ce65..53e47e715ec4 100644 --- a/tools/testing/selftests/bpf/test_kmods/Makefile +++ b/tools/testing/selftests/bpf/test_kmods/Makefile @@ -32,14 +32,17 @@ PERMISSIVE := $(filter 0,$(BPF_STRICT_BUILD)) all: ifeq ($(PERMISSIVE),) $(Q)$(MAKE) -C $(KDIR) $(if $(KMOD_O_VALID),O=$(KMOD_O_VALID) KBUILD_OUTPUT=$(KMOD_O_VALID),KBUILD_OUTPUT=) \ + $(if $(OUTPUT),MO=$(OUTPUT)) \ M=$(TEST_KMOD_DIR) modules else ifneq ("$(wildcard $(KDIR))", "") $(Q)$(MAKE) -C $(KDIR) $(if $(KMOD_O_VALID),O=$(KMOD_O_VALID) KBUILD_OUTPUT=$(KMOD_O_VALID),KBUILD_OUTPUT=) \ + $(if $(OUTPUT),MO=$(OUTPUT)) \ M=$(TEST_KMOD_DIR) modules endif clean: ifneq ("$(wildcard $(KDIR))", "") $(Q)$(MAKE) -C $(KDIR) $(if $(KMOD_O_VALID),O=$(KMOD_O_VALID) KBUILD_OUTPUT=$(KMOD_O_VALID),KBUILD_OUTPUT=) \ + $(if $(OUTPUT),MO=$(OUTPUT)) \ M=$(TEST_KMOD_DIR) clean endif -- 2.55.0