xref: /linux/tools/testing/selftests/bpf/Makefile.runner (revision ef3744f49d98fab34c33d916e7f59b439c02da95)
1# SPDX-License-Identifier: GPL-2.0
2#
3# Build one test_progs flavor or test_maps.
4# Invoked by the main Makefile after shared build inputs are ready.
5# Flavored outputs live in $(OUTPUT)/$(FLAVOR); unflavored outputs
6# share $(OUTPUT) with the main Makefile.
7#
8# Command-line parameters:
9#   OUTPUT         selftests output directory, resolved by lib.mk
10#   RUNNER         test_progs | test_maps
11#   FLAVOR         empty | no_alu32 | cpuv4 | bpf_gcc
12#   TESTS_DIR      prog_tests | map_tests
13#   BPF_*          see Makefile.skel; BPF_CC is empty for test_maps
14#   CC, CLANG      compilers resolved by the main Makefile
15#   CFLAGS, LDFLAGS assembled flags, including lib.mk's additions
16#
17# Exported by the main Makefile:
18#   LLVM_LDLIBS, LLVM_LDFLAGS: LLVM feature-probe results
19
20include ../../../build/Build.include
21include ../../../scripts/Makefile.arch
22include ../../../scripts/Makefile.include
23
24# Same message helpers as ../lib.mk, which only the top level includes.
25ifeq ($(V),1)
26Q =
27msg =
28else
29Q = @
30msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
31MAKEFLAGS += --no-print-directory
32endif
33
34include Makefile.buildvars
35
36BINARY := $(RUNNER)$(if $(FLAVOR),-$(FLAVOR))
37RDIR   := $(OUTPUT)$(if $(FLAVOR),/$(FLAVOR))
38
39all: $(OUTPUT)/$(BINARY)
40.PHONY: all
41
42# Delete partially updated (corrupted) files on error
43.DELETE_ON_ERROR:
44
45# No built-in rules: every file built here has an explicit rule, and the
46# objects that come from the main Makefile have to be an error when
47# missing rather than a silent rebuild by the built-in %.o recipes.
48MAKEFLAGS += -r
49
50# Permissive build behaviour (skip-on-failure compile, partial-link) only
51# applies to test_progs and its flavors; runners that use strong cross-object
52# references (e.g. test_maps) keep strict semantics even when permissive.
53PERMISSIVE_TESTS := $(if $(filter test_progs,$(RUNNER)),$(PERMISSIVE))
54
55$(RDIR):
56	$(call msg,MKDIR,,$@)
57	$(Q)mkdir -p $@
58
59# Per-runner sources
60
61ifeq ($(RUNNER),test_progs)
62GENERATED_HDRS := $(LIBARENA_SKEL) $(LIBARENA_ASAN_SKEL)
63endif
64
65TEST_SRCS  := $(notdir $(wildcard $(TESTS_DIR)/*.c))
66TEST_OBJS  := $(patsubst %.c,$(RDIR)/%.test.o,$(TEST_SRCS))
67TEST_DEPS  := $(TEST_OBJS:.o=.d)
68
69# BPF objects and skeletons - rules shared with the main Makefile
70
71include Makefile.skel
72
73# When the compiler generates a %.d file, only skel basenames (not
74# full paths) are specified as prerequisites for corresponding %.o
75# file. vpath directives below instruct make to search for skel files
76# in $(RDIR), if they are not present in the working directory.
77vpath %.skel.h $(RDIR)
78vpath %.lskel.h $(RDIR)
79vpath %.subskel.h $(RDIR)
80
81# Test objects and runner binary
82
83$(RDIR)/resolve_btfids.test.o: $(RESOLVE_BTFIDS) $(RDIR)/btf_data.bpf.o
84$(RDIR)/resolve_btfids.test.o: private TEST_NEEDS_BTFIDS = 1
85
86# compile individual test files
87# Note: we cd into output directory to ensure embedded BPF object is found
88# The %.test.d dependency files are a side effect of the -MMD below; the
89# separate no-recipe rule for them orders the first compilation after
90# skeleton generation and re-orders test object compilation when headers
91# change.
92$(TEST_OBJS): $(RDIR)/%.test.o: $(TESTS_DIR)/%.c | $(RDIR)/%.test.d
93	$(call msg,TEST-OBJ,$(BINARY),$@)
94	$(Q)(cd $(@D) && $(CC) -I. $(CFLAGS) -MMD -MT $@ -c $(CURDIR)/$< $(LDLIBS) -o $(@F)) $(if \
95		$(PERMISSIVE_TESTS),$(call skip_on_fail,TEST))
96	$(if $(TEST_NEEDS_BTFIDS),					\
97		$(Q)if [ -f $@ ]; then					\
98		$(if $(filter 1,$(V)),true,printf '  %-8s%s %s\n' "BTFIDS" " [$(BINARY)]" "$(notdir $@)"); \
99		$(RESOLVE_BTFIDS) --btf $(RDIR)/btf_data.bpf.o $@;	\
100		$(RESOLVE_BTFIDS) --patch_btfids $@.BTF_ids $@;		\
101		fi)
102
103$(TEST_DEPS): $(RDIR)/%.test.d: $(TESTS_DIR)/%.c $(GENERATED_HDRS) $(BPFOBJ) \
104	      | $(RDIR) $(ALL_SKELS)
105
106include $(wildcard $(TEST_DEPS))
107
108cmd_runner_link = $(CC) $(CFLAGS) \
109	$(if $(PERMISSIVE_TESTS),$(wildcard $(TEST_OBJS)) \
110		$(filter-out $(TEST_OBJS),$(filter %.a %.o,$^)), \
111		$(filter %.a %.o,$^)) \
112	$(LDLIBS) $(LLVM_LDLIBS) $(LDFLAGS) $(LLVM_LDFLAGS) -o $@
113
114# BPF objects are also needed at runtime.
115# In permissive mode, existing test objects are normal prerequisites
116# so edits trigger relinking. Order-only prerequisites attempt all
117# tests; the recipe links the objects that exist afterward.
118$(OUTPUT)/$(BINARY): $(if $(PERMISSIVE_TESTS),$(wildcard $(TEST_OBJS)),$(TEST_OBJS)) \
119		     $(RUNNER_OBJS-$(RUNNER)) $(BPFOBJ)			\
120		     $(TRUNNER_BPFTOOL)					\
121		     | $(BPF_OBJS)					\
122		     $(if $(PERMISSIVE_TESTS),$(TEST_OBJS))
123	$(call msg,BINARY,,$@)
124	$(Q)$(cmd_runner_link)
125	$(Q)ln -sf $(if $(FLAVOR),..,.)/tools/build/bpftool/$(USE_BOOTSTRAP)bpftool $(RDIR)/bpftool
126