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
315LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
316	test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c
317SKEL_BLACKLIST += $$(LSKELS)
318
319test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
320linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o
321linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o
322linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o
323
324LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
325
326# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
327# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
328# Parameters:
329# $1 - test runner base binary name (e.g., test_progs)
330# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
331define DEFINE_TEST_RUNNER
332
333TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
334TRUNNER_BINARY := $1$(if $2,-)$2
335TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
336				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
337TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
338				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
339TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
340TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
341TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
342TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
343TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h,	\
344				 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\
345					       $$(TRUNNER_BPF_SRCS)))
346TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS))
347TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
348TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
349
350# Evaluate rules now with extra TRUNNER_XXX variables above already defined
351$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
352
353endef
354
355# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
356# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
357# $1 - test runner base binary name (e.g., test_progs)
358# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
359define DEFINE_TEST_RUNNER_RULES
360
361ifeq ($($(TRUNNER_OUTPUT)-dir),)
362$(TRUNNER_OUTPUT)-dir := y
363$(TRUNNER_OUTPUT):
364	$$(call msg,MKDIR,,$$@)
365	$(Q)mkdir -p $$@
366endif
367
368# ensure we set up BPF objects generation rule just once for a given
369# input/output directory combination
370ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
371$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
372$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
373		     $(TRUNNER_BPF_PROGS_DIR)/%.c			\
374		     $(TRUNNER_BPF_PROGS_DIR)/*.h			\
375		     $$(INCLUDE_DIR)/vmlinux.h				\
376		     $(wildcard $(BPFDIR)/bpf_*.h) | $(TRUNNER_OUTPUT)
377	$$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@,			\
378					  $(TRUNNER_BPF_CFLAGS))
379
380$(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
381	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
382	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
383	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
384	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
385	$(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
386	$(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@
387
388$(TRUNNER_BPF_LSKELS): %.lskel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
389	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
390	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
391	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
392	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
393	$(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
394	$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@
395
396$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
397	$$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.o))
398	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
399	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
400	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o)
401	$(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
402	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
403	$(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
404endif
405
406# ensure we set up tests.h header generation rule just once
407ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
408$(TRUNNER_TESTS_DIR)-tests-hdr := y
409$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
410	$$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
411	$$(shell ( cd $(TRUNNER_TESTS_DIR);				\
412		  echo '/* Generated header, do not edit */';		\
413		  ls *.c 2> /dev/null |					\
414			sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@';	\
415		 ) > $$@)
416endif
417
418# compile individual test files
419# Note: we cd into output directory to ensure embedded BPF object is found
420$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
421		      $(TRUNNER_TESTS_DIR)/%.c				\
422		      $(TRUNNER_EXTRA_HDRS)				\
423		      $(TRUNNER_BPF_OBJS)				\
424		      $(TRUNNER_BPF_SKELS)				\
425		      $(TRUNNER_BPF_LSKELS)				\
426		      $(TRUNNER_BPF_SKELS_LINKED)			\
427		      $$(BPFOBJ) | $(TRUNNER_OUTPUT)
428	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
429	$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
430
431$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
432		       %.c						\
433		       $(TRUNNER_EXTRA_HDRS)				\
434		       $(TRUNNER_TESTS_HDR)				\
435		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
436	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
437	$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
438
439# non-flavored in-srctree builds receive special treatment, in particular, we
440# do not need to copy extra resources (see e.g. test_btf_dump_case())
441$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
442ifneq ($2:$(OUTPUT),:$(shell pwd))
443	$$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
444	$(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
445endif
446
447$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
448			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
449			     $(RESOLVE_BTFIDS)				\
450			     | $(TRUNNER_BINARY)-extras
451	$$(call msg,BINARY,,$$@)
452	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
453	$(Q)$(RESOLVE_BTFIDS) --no-fail --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
454
455endef
456
457# Define test_progs test runner.
458TRUNNER_TESTS_DIR := prog_tests
459TRUNNER_BPF_PROGS_DIR := progs
460TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
461			 network_helpers.c testing_helpers.c		\
462			 btf_helpers.c	flow_dissector_load.h
463TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
464		       ima_setup.sh					\
465		       $(wildcard progs/btf_dump_test_case_*.c)
466TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
467TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
468TRUNNER_BPF_CFLAGS += -DENABLE_ATOMICS_TESTS
469$(eval $(call DEFINE_TEST_RUNNER,test_progs))
470
471# Define test_progs-no_alu32 test runner.
472TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
473TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
474$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
475
476# Define test_progs BPF-GCC-flavored test runner.
477ifneq ($(BPF_GCC),)
478TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
479TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
480$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
481endif
482
483# Define test_maps test runner.
484TRUNNER_TESTS_DIR := map_tests
485TRUNNER_BPF_PROGS_DIR := progs
486TRUNNER_EXTRA_SOURCES := test_maps.c
487TRUNNER_EXTRA_FILES :=
488TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
489TRUNNER_BPF_CFLAGS :=
490$(eval $(call DEFINE_TEST_RUNNER,test_maps))
491
492# Define test_verifier test runner.
493# It is much simpler than test_maps/test_progs and sufficiently different from
494# them (e.g., test.h is using completely pattern), that it's worth just
495# explicitly defining all the rules explicitly.
496verifier/tests.h: verifier/*.c
497	$(shell ( cd verifier/; \
498		  echo '/* Generated header, do not edit */'; \
499		  echo '#ifdef FILL_ARRAY'; \
500		  ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
501		  echo '#endif' \
502		) > verifier/tests.h)
503$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
504	$(call msg,BINARY,,$@)
505	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
506
507# Make sure we are able to include and link libbpf against c++.
508$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
509	$(call msg,CXX,,$@)
510	$(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
511
512# Benchmark runner
513$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
514	$(call msg,CC,,$@)
515	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
516$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
517$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
518$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
519			    $(OUTPUT)/perfbuf_bench.skel.h
520$(OUTPUT)/bench.o: bench.h testing_helpers.h
521$(OUTPUT)/bench: LDLIBS += -lm
522$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
523		 $(OUTPUT)/bench_count.o \
524		 $(OUTPUT)/bench_rename.o \
525		 $(OUTPUT)/bench_trigger.o \
526		 $(OUTPUT)/bench_ringbufs.o
527	$(call msg,BINARY,,$@)
528	$(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
529
530EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)	\
531	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
532	feature								\
533	$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h no_alu32 bpf_gcc bpf_testmod.ko)
534
535.PHONY: docs docs-clean
536