1# SPDX-License-Identifier: GPL-2.0
2include ../../../build/Build.include
3include ../../../scripts/Makefile.arch
4include ../../../scripts/Makefile.include
5
6CXX ?= $(CROSS_COMPILE)g++
7
8CURDIR := $(abspath .)
9TOOLSDIR := $(abspath ../../..)
10LIBDIR := $(TOOLSDIR)/lib
11BPFDIR := $(LIBDIR)/bpf
12TOOLSINCDIR := $(TOOLSDIR)/include
13BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
14APIDIR := $(TOOLSINCDIR)/uapi
15GENDIR := $(abspath ../../../../include/generated)
16GENHDR := $(GENDIR)/autoconf.h
17
18ifneq ($(wildcard $(GENHDR)),)
19  GENFLAGS := -DHAVE_GENHDR
20endif
21
22BPF_GCC		?= $(shell command -v bpf-gcc;)
23SAN_CFLAGS	?=
24CFLAGS += -g -O0 -rdynamic -Wall $(GENFLAGS) $(SAN_CFLAGS)		\
25	  -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)		\
26	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)			\
27	  -Dbpf_prog_load=bpf_prog_test_load				\
28	  -Dbpf_load_program=bpf_test_load_program
29LDLIBS += -lcap -lelf -lz -lrt -lpthread
30
31# Silence some warnings when compiled with clang
32ifneq ($(LLVM),)
33CFLAGS += -Wno-unused-command-line-argument
34endif
35
36# Order correspond to 'make run_tests' order
37TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
38	test_verifier_log test_dev_cgroup \
39	test_sock test_sockmap get_cgroup_id_user \
40	test_cgroup_storage \
41	test_netcnt test_tcpnotify_user test_sysctl \
42	test_progs-no_alu32
43
44# Also test bpf-gcc, if present
45ifneq ($(BPF_GCC),)
46TEST_GEN_PROGS += test_progs-bpf_gcc
47endif
48
49TEST_GEN_FILES = test_lwt_ip_encap.o \
50	test_tc_edt.o
51TEST_FILES = xsk_prereqs.sh \
52	$(wildcard progs/btf_dump_test_case_*.c)
53
54# Order correspond to 'make run_tests' order
55TEST_PROGS := test_kmod.sh \
56	test_xdp_redirect.sh \
57	test_xdp_meta.sh \
58	test_xdp_veth.sh \
59	test_offload.py \
60	test_sock_addr.sh \
61	test_tunnel.sh \
62	test_lwt_seg6local.sh \
63	test_lirc_mode2.sh \
64	test_skb_cgroup_id.sh \
65	test_flow_dissector.sh \
66	test_xdp_vlan_mode_generic.sh \
67	test_xdp_vlan_mode_native.sh \
68	test_lwt_ip_encap.sh \
69	test_tcp_check_syncookie.sh \
70	test_tc_tunnel.sh \
71	test_tc_edt.sh \
72	test_xdping.sh \
73	test_bpftool_build.sh \
74	test_bpftool.sh \
75	test_bpftool_metadata.sh \
76	test_doc_build.sh \
77	test_xsk.sh
78
79TEST_PROGS_EXTENDED := with_addr.sh \
80	with_tunnels.sh \
81	test_xdp_vlan.sh
82
83# Compile but not part of 'make run_tests'
84TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
85	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
86	test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
87	xdpxceiver
88
89TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
90
91# Emit succinct information message describing current building step
92# $1 - generic step name (e.g., CC, LINK, etc);
93# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
94# $3 - target (assumed to be file); only file name will be emitted;
95# $4 - optional extra arg, emitted as-is, if provided.
96ifeq ($(V),1)
97Q =
98msg =
99else
100Q = @
101msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
102MAKEFLAGS += --no-print-directory
103submake_extras := feature_display=0
104endif
105
106# override lib.mk's default rules
107OVERRIDE_TARGETS := 1
108override define CLEAN
109	$(call msg,CLEAN)
110	$(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
111	$(Q)$(MAKE) -C bpf_testmod clean
112	$(Q)$(MAKE) docs-clean
113endef
114
115include ../lib.mk
116
117SCRATCH_DIR := $(OUTPUT)/tools
118BUILD_DIR := $(SCRATCH_DIR)/build
119INCLUDE_DIR := $(SCRATCH_DIR)/include
120BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
121ifneq ($(CROSS_COMPILE),)
122HOST_BUILD_DIR		:= $(BUILD_DIR)/host
123HOST_SCRATCH_DIR	:= $(OUTPUT)/host-tools
124else
125HOST_BUILD_DIR		:= $(BUILD_DIR)
126HOST_SCRATCH_DIR	:= $(SCRATCH_DIR)
127endif
128HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
129RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
130
131VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
132		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
133		     ../../../../vmlinux				\
134		     /sys/kernel/btf/vmlinux				\
135		     /boot/vmlinux-$(shell uname -r)
136VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
137ifeq ($(VMLINUX_BTF),)
138$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
139endif
140
141# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
142# to build individual tests.
143# NOTE: Semicolon at the end is critical to override lib.mk's default static
144# rule for binaries.
145$(notdir $(TEST_GEN_PROGS)						\
146	 $(TEST_PROGS)							\
147	 $(TEST_PROGS_EXTENDED)						\
148	 $(TEST_GEN_PROGS_EXTENDED)					\
149	 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
150
151# sort removes libbpf duplicates when not cross-building
152MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf	       \
153	       $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids      \
154	       $(INCLUDE_DIR))
155$(MAKE_DIRS):
156	$(call msg,MKDIR,,$@)
157	$(Q)mkdir -p $@
158
159$(OUTPUT)/%.o: %.c
160	$(call msg,CC,,$@)
161	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
162
163$(OUTPUT)/%:%.c
164	$(call msg,BINARY,,$@)
165	$(Q)$(LINK.c) $^ $(LDLIBS) -o $@
166
167$(OUTPUT)/urandom_read: urandom_read.c
168	$(call msg,BINARY,,$@)
169	$(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1
170
171$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
172	$(call msg,MOD,,$@)
173	$(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
174	$(Q)$(MAKE) $(submake_extras) -C bpf_testmod
175	$(Q)cp bpf_testmod/bpf_testmod.ko $@
176
177$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
178	$(call msg,CC,,$@)
179	$(Q)$(CC) -c $(CFLAGS) -o $@ $<
180
181DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
182
183$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL)
184	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower	\
185		    OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF)   \
186		    BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) &&	\
187		    cp $(SCRATCH_DIR)/runqslower $@
188
189$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
190
191$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
192$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
193$(OUTPUT)/test_sock: cgroup_helpers.c
194$(OUTPUT)/test_sock_addr: cgroup_helpers.c
195$(OUTPUT)/test_sockmap: cgroup_helpers.c
196$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
197$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
198$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
199$(OUTPUT)/test_netcnt: cgroup_helpers.c
200$(OUTPUT)/test_sock_fields: cgroup_helpers.c
201$(OUTPUT)/test_sysctl: cgroup_helpers.c
202
203BPFTOOL ?= $(DEFAULT_BPFTOOL)
204$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
205		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
206	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
207		    CC=$(HOSTCC) LD=$(HOSTLD)				       \
208		    EXTRA_CFLAGS='-g -O0'				       \
209		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/			       \
210		    prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install
211
212all: docs
213
214docs:
215	$(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras)	\
216	            -f Makefile.docs					\
217	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
218
219docs-clean:
220	$(Q)$(MAKE) $(submake_extras)					\
221	            -f Makefile.docs					\
222	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
223
224$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
225	   ../../../include/uapi/linux/bpf.h                                   \
226	   | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
227	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
228		    EXTRA_CFLAGS='-g -O0'				       \
229		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
230
231ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
232$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)                \
233	   ../../../include/uapi/linux/bpf.h                                   \
234	   | $(INCLUDE_DIR) $(HOST_BUILD_DIR)/libbpf
235	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR)                             \
236		    EXTRA_CFLAGS='-g -O0'				       \
237		    OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \
238		    DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
239endif
240
241$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
242ifeq ($(VMLINUX_H),)
243	$(call msg,GEN,,$@)
244	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
245else
246	$(call msg,CP,,$@)
247	$(Q)cp "$(VMLINUX_H)" $@
248endif
249
250$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids	\
251		       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
252		       $(TOOLSDIR)/lib/rbtree.c			\
253		       $(TOOLSDIR)/lib/zalloc.c			\
254		       $(TOOLSDIR)/lib/string.c			\
255		       $(TOOLSDIR)/lib/ctype.c			\
256		       $(TOOLSDIR)/lib/str_error_r.c
257	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
258		CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) \
259		OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
260
261# Get Clang's default includes on this system, as opposed to those seen by
262# '-target bpf'. This fixes "missing" files on some architectures/distros,
263# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
264#
265# Use '-idirafter': Don't interfere with include mechanics except where the
266# build would have failed anyways.
267define get_sys_includes
268$(shell $(1) -v -E - </dev/null 2>&1 \
269	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
270$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
271endef
272
273# Determine target endianness.
274IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
275			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
276MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
277
278CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
279BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) 			\
280	     -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)			\
281	     -I$(abspath $(OUTPUT)/../usr/include)
282
283CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
284	       -Wno-compare-distinct-pointer-types
285
286$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
287$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
288
289$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
290
291# Build BPF object using Clang
292# $1 - input .c file
293# $2 - output .o file
294# $3 - CFLAGS
295define CLANG_BPF_BUILD_RULE
296	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
297	$(Q)$(CLANG) $3 -O2 -target bpf -c $1 -o $2 -mcpu=v3
298endef
299# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
300define CLANG_NOALU32_BPF_BUILD_RULE
301	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
302	$(Q)$(CLANG) $3 -O2 -target bpf -c $1 -o $2 -mcpu=v2
303endef
304# Build BPF object using GCC
305define GCC_BPF_BUILD_RULE
306	$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
307	$(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
308endef
309
310SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
311
312LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
313		linked_vars.skel.h linked_maps.skel.h
314
315test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
316linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o
317linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o
318linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o
319
320LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
321
322# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
323# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
324# Parameters:
325# $1 - test runner base binary name (e.g., test_progs)
326# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
327define DEFINE_TEST_RUNNER
328
329TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
330TRUNNER_BINARY := $1$(if $2,-)$2
331TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
332				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
333TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
334				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
335TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
336TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
337TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
338TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
339TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h,	\
340				 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\
341					       $$(TRUNNER_BPF_SRCS)))
342TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
343TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
344
345# Evaluate rules now with extra TRUNNER_XXX variables above already defined
346$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
347
348endef
349
350# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
351# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
352# $1 - test runner base binary name (e.g., test_progs)
353# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
354define DEFINE_TEST_RUNNER_RULES
355
356ifeq ($($(TRUNNER_OUTPUT)-dir),)
357$(TRUNNER_OUTPUT)-dir := y
358$(TRUNNER_OUTPUT):
359	$$(call msg,MKDIR,,$$@)
360	$(Q)mkdir -p $$@
361endif
362
363# ensure we set up BPF objects generation rule just once for a given
364# input/output directory combination
365ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
366$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
367$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
368		     $(TRUNNER_BPF_PROGS_DIR)/%.c			\
369		     $(TRUNNER_BPF_PROGS_DIR)/*.h			\
370		     $$(INCLUDE_DIR)/vmlinux.h				\
371		     $(wildcard $(BPFDIR)/bpf_*.h) | $(TRUNNER_OUTPUT)
372	$$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@,			\
373					  $(TRUNNER_BPF_CFLAGS))
374
375$(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
376	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
377	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
378	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
379	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
380	$(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
381	$(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@
382
383$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
384	$$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.o))
385	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
386	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
387	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o)
388	$(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
389	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
390	$(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
391endif
392
393# ensure we set up tests.h header generation rule just once
394ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
395$(TRUNNER_TESTS_DIR)-tests-hdr := y
396$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
397	$$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
398	$$(shell ( cd $(TRUNNER_TESTS_DIR);				\
399		  echo '/* Generated header, do not edit */';		\
400		  ls *.c 2> /dev/null |					\
401			sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@';	\
402		 ) > $$@)
403endif
404
405# compile individual test files
406# Note: we cd into output directory to ensure embedded BPF object is found
407$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
408		      $(TRUNNER_TESTS_DIR)/%.c				\
409		      $(TRUNNER_EXTRA_HDRS)				\
410		      $(TRUNNER_BPF_OBJS)				\
411		      $(TRUNNER_BPF_SKELS)				\
412		      $(TRUNNER_BPF_SKELS_LINKED)			\
413		      $$(BPFOBJ) | $(TRUNNER_OUTPUT)
414	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
415	$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
416
417$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
418		       %.c						\
419		       $(TRUNNER_EXTRA_HDRS)				\
420		       $(TRUNNER_TESTS_HDR)				\
421		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
422	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
423	$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
424
425# non-flavored in-srctree builds receive special treatment, in particular, we
426# do not need to copy extra resources (see e.g. test_btf_dump_case())
427$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
428ifneq ($2:$(OUTPUT),:$(shell pwd))
429	$$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
430	$(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
431endif
432
433$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
434			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
435			     $(RESOLVE_BTFIDS)				\
436			     | $(TRUNNER_BINARY)-extras
437	$$(call msg,BINARY,,$$@)
438	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
439	$(Q)$(RESOLVE_BTFIDS) --no-fail --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
440
441endef
442
443# Define test_progs test runner.
444TRUNNER_TESTS_DIR := prog_tests
445TRUNNER_BPF_PROGS_DIR := progs
446TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
447			 network_helpers.c testing_helpers.c		\
448			 btf_helpers.c	flow_dissector_load.h
449TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
450		       ima_setup.sh					\
451		       $(wildcard progs/btf_dump_test_case_*.c)
452TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
453TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
454TRUNNER_BPF_CFLAGS += -DENABLE_ATOMICS_TESTS
455$(eval $(call DEFINE_TEST_RUNNER,test_progs))
456
457# Define test_progs-no_alu32 test runner.
458TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
459TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
460$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
461
462# Define test_progs BPF-GCC-flavored test runner.
463ifneq ($(BPF_GCC),)
464TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
465TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
466$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
467endif
468
469# Define test_maps test runner.
470TRUNNER_TESTS_DIR := map_tests
471TRUNNER_BPF_PROGS_DIR := progs
472TRUNNER_EXTRA_SOURCES := test_maps.c
473TRUNNER_EXTRA_FILES :=
474TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
475TRUNNER_BPF_CFLAGS :=
476$(eval $(call DEFINE_TEST_RUNNER,test_maps))
477
478# Define test_verifier test runner.
479# It is much simpler than test_maps/test_progs and sufficiently different from
480# them (e.g., test.h is using completely pattern), that it's worth just
481# explicitly defining all the rules explicitly.
482verifier/tests.h: verifier/*.c
483	$(shell ( cd verifier/; \
484		  echo '/* Generated header, do not edit */'; \
485		  echo '#ifdef FILL_ARRAY'; \
486		  ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
487		  echo '#endif' \
488		) > verifier/tests.h)
489$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
490	$(call msg,BINARY,,$@)
491	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
492
493# Make sure we are able to include and link libbpf against c++.
494$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
495	$(call msg,CXX,,$@)
496	$(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
497
498# Benchmark runner
499$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
500	$(call msg,CC,,$@)
501	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
502$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
503$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
504$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
505			    $(OUTPUT)/perfbuf_bench.skel.h
506$(OUTPUT)/bench.o: bench.h testing_helpers.h
507$(OUTPUT)/bench: LDLIBS += -lm
508$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
509		 $(OUTPUT)/bench_count.o \
510		 $(OUTPUT)/bench_rename.o \
511		 $(OUTPUT)/bench_trigger.o \
512		 $(OUTPUT)/bench_ringbufs.o
513	$(call msg,BINARY,,$@)
514	$(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
515
516EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)	\
517	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
518	feature								\
519	$(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc bpf_testmod.ko)
520
521.PHONY: docs docs-clean
522