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