xref: /linux/tools/testing/selftests/bpf/Makefile (revision 61b7369483efb5e0a9f3b48e75fac00d46d661e0)
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
17HOSTPKG_CONFIG := pkg-config
18
19ifneq ($(wildcard $(GENHDR)),)
20  GENFLAGS := -DHAVE_GENHDR
21endif
22
23BPF_GCC		?= $(shell command -v bpf-gcc;)
24SAN_CFLAGS	?=
25SAN_LDFLAGS	?= $(SAN_CFLAGS)
26CFLAGS += -g -O0 -rdynamic -Wall -Werror $(GENFLAGS) $(SAN_CFLAGS)	\
27	  -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)		\
28	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
29LDFLAGS += $(SAN_LDFLAGS)
30LDLIBS += -lelf -lz -lrt -lpthread
31
32# Silence some warnings when compiled with clang
33ifneq ($(LLVM),)
34CFLAGS += -Wno-unused-command-line-argument
35endif
36
37# Order correspond to 'make run_tests' order
38TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
39	test_dev_cgroup \
40	test_sock test_sockmap get_cgroup_id_user \
41	test_cgroup_storage \
42	test_tcpnotify_user test_sysctl \
43	test_progs-no_alu32
44
45# Also test bpf-gcc, if present
46ifneq ($(BPF_GCC),)
47TEST_GEN_PROGS += test_progs-bpf_gcc
48endif
49
50TEST_GEN_FILES = test_lwt_ip_encap.bpf.o test_tc_edt.bpf.o
51TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)
52
53# Order correspond to 'make run_tests' order
54TEST_PROGS := test_kmod.sh \
55	test_xdp_redirect.sh \
56	test_xdp_redirect_multi.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	test_xdp_features.sh
79
80TEST_PROGS_EXTENDED := with_addr.sh \
81	with_tunnels.sh ima_setup.sh verify_sig_setup.sh \
82	test_xdp_vlan.sh test_bpftool.py
83
84# Compile but not part of 'make run_tests'
85TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
86	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
87	test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
88	xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \
89	xdp_features
90
91TEST_GEN_FILES += liburandom_read.so urandom_read sign-file
92
93# Emit succinct information message describing current building step
94# $1 - generic step name (e.g., CC, LINK, etc);
95# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
96# $3 - target (assumed to be file); only file name will be emitted;
97# $4 - optional extra arg, emitted as-is, if provided.
98ifeq ($(V),1)
99Q =
100msg =
101else
102Q = @
103msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
104MAKEFLAGS += --no-print-directory
105submake_extras := feature_display=0
106endif
107
108# override lib.mk's default rules
109OVERRIDE_TARGETS := 1
110override define CLEAN
111	$(call msg,CLEAN)
112	$(Q)$(RM) -r $(TEST_GEN_PROGS)
113	$(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED)
114	$(Q)$(RM) -r $(TEST_GEN_FILES)
115	$(Q)$(RM) -r $(EXTRA_CLEAN)
116	$(Q)$(MAKE) -C bpf_testmod clean
117	$(Q)$(MAKE) docs-clean
118endef
119
120include ../lib.mk
121
122SCRATCH_DIR := $(OUTPUT)/tools
123BUILD_DIR := $(SCRATCH_DIR)/build
124INCLUDE_DIR := $(SCRATCH_DIR)/include
125BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
126ifneq ($(CROSS_COMPILE),)
127HOST_BUILD_DIR		:= $(BUILD_DIR)/host
128HOST_SCRATCH_DIR	:= $(OUTPUT)/host-tools
129HOST_INCLUDE_DIR	:= $(HOST_SCRATCH_DIR)/include
130else
131HOST_BUILD_DIR		:= $(BUILD_DIR)
132HOST_SCRATCH_DIR	:= $(SCRATCH_DIR)
133HOST_INCLUDE_DIR	:= $(INCLUDE_DIR)
134endif
135HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
136RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
137RUNQSLOWER_OUTPUT := $(BUILD_DIR)/runqslower/
138
139VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
140		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
141		     ../../../../vmlinux				\
142		     /sys/kernel/btf/vmlinux				\
143		     /boot/vmlinux-$(shell uname -r)
144VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
145ifeq ($(VMLINUX_BTF),)
146$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
147endif
148
149# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
150# to build individual tests.
151# NOTE: Semicolon at the end is critical to override lib.mk's default static
152# rule for binaries.
153$(notdir $(TEST_GEN_PROGS)						\
154	 $(TEST_GEN_PROGS_EXTENDED)					\
155	 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
156
157# sort removes libbpf duplicates when not cross-building
158MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf	\
159	       $(BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/bpftool		\
160	       $(HOST_BUILD_DIR)/resolve_btfids				\
161	       $(RUNQSLOWER_OUTPUT) $(INCLUDE_DIR))
162$(MAKE_DIRS):
163	$(call msg,MKDIR,,$@)
164	$(Q)mkdir -p $@
165
166$(OUTPUT)/%.o: %.c
167	$(call msg,CC,,$@)
168	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
169
170$(OUTPUT)/%:%.c
171	$(call msg,BINARY,,$@)
172	$(Q)$(LINK.c) $^ $(LDLIBS) -o $@
173
174# LLVM's ld.lld doesn't support all the architectures, so use it only on x86
175ifeq ($(SRCARCH),x86)
176LLD := lld
177else
178LLD := ld
179endif
180
181# Filter out -static for liburandom_read.so and its dependent targets so that static builds
182# do not fail. Static builds leave urandom_read relying on system-wide shared libraries.
183$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c
184	$(call msg,LIB,,$@)
185	$(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS))   \
186		     $^ $(filter-out -static,$(LDLIBS))	     \
187		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
188		     -fPIC -shared -o $@
189
190$(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
191	$(call msg,BINARY,,$@)
192	$(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
193		     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT)  \
194		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
195		     -Wl,-rpath=. -o $@
196
197$(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
198	$(call msg,SIGN-FILE,,$@)
199	$(Q)$(CC) $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null) \
200		  $< -o $@ \
201		  $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)
202
203$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
204	$(call msg,MOD,,$@)
205	$(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
206	$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_testmod
207	$(Q)cp bpf_testmod/bpf_testmod.ko $@
208
209DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
210ifneq ($(CROSS_COMPILE),)
211CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
212TRUNNER_BPFTOOL := $(CROSS_BPFTOOL)
213USE_BOOTSTRAP := ""
214else
215TRUNNER_BPFTOOL := $(DEFAULT_BPFTOOL)
216USE_BOOTSTRAP := "bootstrap/"
217endif
218
219$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
220	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower	       \
221		    OUTPUT=$(RUNQSLOWER_OUTPUT) VMLINUX_BTF=$(VMLINUX_BTF)     \
222		    BPFTOOL_OUTPUT=$(HOST_BUILD_DIR)/bpftool/		       \
223		    BPFOBJ_OUTPUT=$(BUILD_DIR)/libbpf			       \
224		    BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR)		       \
225		    EXTRA_CFLAGS='-g -O0 $(SAN_CFLAGS)'			       \
226		    EXTRA_LDFLAGS='$(SAN_LDFLAGS)' &&			       \
227		    cp $(RUNQSLOWER_OUTPUT)runqslower $@
228
229TEST_GEN_PROGS_EXTENDED += $(TRUNNER_BPFTOOL)
230
231$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
232
233TESTING_HELPERS	:= $(OUTPUT)/testing_helpers.o
234CGROUP_HELPERS	:= $(OUTPUT)/cgroup_helpers.o
235UNPRIV_HELPERS  := $(OUTPUT)/unpriv_helpers.o
236TRACE_HELPERS	:= $(OUTPUT)/trace_helpers.o
237JSON_WRITER		:= $(OUTPUT)/json_writer.o
238CAP_HELPERS	:= $(OUTPUT)/cap_helpers.o
239
240$(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS)
241$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
242$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
243$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS)
244$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
245$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
246$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
247$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS)
248$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
249$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
250$(OUTPUT)/test_tag: $(TESTING_HELPERS)
251$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS)
252$(OUTPUT)/xdping: $(TESTING_HELPERS)
253$(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS)
254$(OUTPUT)/test_maps: $(TESTING_HELPERS)
255$(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS)
256$(OUTPUT)/xsk.o: $(BPFOBJ)
257
258BPFTOOL ?= $(DEFAULT_BPFTOOL)
259$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
260		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
261	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
262		    ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" 	       \
263		    EXTRA_CFLAGS='-g -O0'				       \
264		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/			       \
265		    LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/		       \
266		    LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/			       \
267		    prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin
268
269ifneq ($(CROSS_COMPILE),)
270$(CROSS_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)	\
271		    $(BPFOBJ) | $(BUILD_DIR)/bpftool
272	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)				\
273		    ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE)			\
274		    EXTRA_CFLAGS='-g -O0'					\
275		    OUTPUT=$(BUILD_DIR)/bpftool/				\
276		    LIBBPF_OUTPUT=$(BUILD_DIR)/libbpf/				\
277		    LIBBPF_DESTDIR=$(SCRATCH_DIR)/				\
278		    prefix= DESTDIR=$(SCRATCH_DIR)/ install-bin
279endif
280
281all: docs
282
283docs:
284	$(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras)	\
285	            -f Makefile.docs					\
286	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
287
288docs-clean:
289	$(Q)$(MAKE) $(submake_extras)					\
290	            -f Makefile.docs					\
291	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
292
293$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
294	   $(APIDIR)/linux/bpf.h					       \
295	   | $(BUILD_DIR)/libbpf
296	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
297		    EXTRA_CFLAGS='-g -O0 $(SAN_CFLAGS)'			       \
298		    EXTRA_LDFLAGS='$(SAN_LDFLAGS)'			       \
299		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
300
301ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
302$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
303		$(APIDIR)/linux/bpf.h					       \
304		| $(HOST_BUILD_DIR)/libbpf
305	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR)                             \
306		    EXTRA_CFLAGS='-g -O0' ARCH= CROSS_COMPILE=		       \
307		    OUTPUT=$(HOST_BUILD_DIR)/libbpf/			       \
308		    CC="$(HOSTCC)" LD="$(HOSTLD)"			       \
309		    DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
310endif
311
312$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
313ifeq ($(VMLINUX_H),)
314	$(call msg,GEN,,$@)
315	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
316else
317	$(call msg,CP,,$@)
318	$(Q)cp "$(VMLINUX_H)" $@
319endif
320
321$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids	\
322		       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
323		       $(TOOLSDIR)/lib/rbtree.c			\
324		       $(TOOLSDIR)/lib/zalloc.c			\
325		       $(TOOLSDIR)/lib/string.c			\
326		       $(TOOLSDIR)/lib/ctype.c			\
327		       $(TOOLSDIR)/lib/str_error_r.c
328	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
329		CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \
330		LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
331		OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
332
333# Get Clang's default includes on this system, as opposed to those seen by
334# '-target bpf'. This fixes "missing" files on some architectures/distros,
335# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
336#
337# Use '-idirafter': Don't interfere with include mechanics except where the
338# build would have failed anyways.
339define get_sys_includes
340$(shell $(1) $(2) -v -E - </dev/null 2>&1 \
341	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
342$(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}') \
343$(shell $(1) $(2) -dM -E - </dev/null | grep '__loongarch_grlen ' | awk '{printf("-D__BITS_PER_LONG=%d", $$3)}')
344endef
345
346# Determine target endianness.
347IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
348			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
349MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
350
351ifneq ($(CROSS_COMPILE),)
352CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
353endif
354
355CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
356BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN)	\
357	     -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)			\
358	     -I$(abspath $(OUTPUT)/../usr/include)
359
360CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
361	       -Wno-compare-distinct-pointer-types
362
363$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
364$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
365
366$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
367$(OUTPUT)/cgroup_getset_retval_hooks.o: cgroup_getset_retval_hooks.h
368
369# Build BPF object using Clang
370# $1 - input .c file
371# $2 - output .o file
372# $3 - CFLAGS
373define CLANG_BPF_BUILD_RULE
374	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
375	$(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v3 -o $2
376endef
377# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
378define CLANG_NOALU32_BPF_BUILD_RULE
379	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
380	$(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v2 -o $2
381endef
382# Build BPF object using GCC
383define GCC_BPF_BUILD_RULE
384	$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
385	$(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
386endef
387
388SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
389
390LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
391		linked_vars.skel.h linked_maps.skel.h 			\
392		test_subskeleton.skel.h test_subskeleton_lib.skel.h	\
393		test_usdt.skel.h
394
395LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c 		\
396	trace_printk.c trace_vprintk.c map_ptr_kern.c 			\
397	core_kern.c core_kern_overflow.c test_ringbuf.c			\
398	test_ringbuf_map_key.c
399
400# Generate both light skeleton and libbpf skeleton for these
401LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
402	kfunc_call_test_subprog.c
403SKEL_BLACKLIST += $$(LSKELS)
404
405test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o
406linked_funcs.skel.h-deps := linked_funcs1.bpf.o linked_funcs2.bpf.o
407linked_vars.skel.h-deps := linked_vars1.bpf.o linked_vars2.bpf.o
408linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o
409# In the subskeleton case, we want the test_subskeleton_lib.subskel.h file
410# but that's created as a side-effect of the skel.h generation.
411test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o
412test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o
413test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o
414xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o
415xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o
416xdp_features.skel.h-deps := xdp_features.bpf.o
417
418LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
419
420# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
421# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
422# Parameters:
423# $1 - test runner base binary name (e.g., test_progs)
424# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
425define DEFINE_TEST_RUNNER
426
427TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
428TRUNNER_BINARY := $1$(if $2,-)$2
429TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
430				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
431TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
432				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
433TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
434TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
435TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
436TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.bpf.o, $$(TRUNNER_BPF_SRCS))
437TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h,	\
438				 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\
439					       $$(TRUNNER_BPF_SRCS)))
440TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA))
441TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
442TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
443
444# Evaluate rules now with extra TRUNNER_XXX variables above already defined
445$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
446
447endef
448
449# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
450# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
451# $1 - test runner base binary name (e.g., test_progs)
452# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
453define DEFINE_TEST_RUNNER_RULES
454
455ifeq ($($(TRUNNER_OUTPUT)-dir),)
456$(TRUNNER_OUTPUT)-dir := y
457$(TRUNNER_OUTPUT):
458	$$(call msg,MKDIR,,$$@)
459	$(Q)mkdir -p $$@
460endif
461
462# ensure we set up BPF objects generation rule just once for a given
463# input/output directory combination
464ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
465$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
466$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.bpf.o:				\
467		     $(TRUNNER_BPF_PROGS_DIR)/%.c			\
468		     $(TRUNNER_BPF_PROGS_DIR)/*.h			\
469		     $$(INCLUDE_DIR)/vmlinux.h				\
470		     $(wildcard $(BPFDIR)/bpf_*.h)			\
471		     $(wildcard $(BPFDIR)/*.bpf.h)			\
472		     | $(TRUNNER_OUTPUT) $$(BPFOBJ)
473	$$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@,			\
474					  $(TRUNNER_BPF_CFLAGS))
475
476$(TRUNNER_BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
477	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
478	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
479	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
480	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
481	$(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
482	$(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$@
483	$(Q)$$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$(@:.skel.h=.subskel.h)
484
485$(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
486	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
487	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$<
488	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o)
489	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o)
490	$(Q)diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
491	$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@
492
493$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
494	$$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.bpf.o))
495	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
496	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
497	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o)
498	$(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
499	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
500	$(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
501	$(Q)$$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h)
502endif
503
504# ensure we set up tests.h header generation rule just once
505ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
506$(TRUNNER_TESTS_DIR)-tests-hdr := y
507$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
508	$$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
509	$$(shell (echo '/* Generated header, do not edit */';					\
510		  sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p'	\
511			$(TRUNNER_TESTS_DIR)/*.c | sort ;	\
512		 ) > $$@)
513endif
514
515# compile individual test files
516# Note: we cd into output directory to ensure embedded BPF object is found
517$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
518		      $(TRUNNER_TESTS_DIR)/%.c				\
519		      $(TRUNNER_EXTRA_HDRS)				\
520		      $(TRUNNER_BPF_OBJS)				\
521		      $(TRUNNER_BPF_SKELS)				\
522		      $(TRUNNER_BPF_LSKELS)				\
523		      $(TRUNNER_BPF_SKELS_LINKED)			\
524		      $$(BPFOBJ) | $(TRUNNER_OUTPUT)
525	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
526	$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
527
528$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
529		       %.c						\
530		       $(TRUNNER_EXTRA_HDRS)				\
531		       $(TRUNNER_TESTS_HDR)				\
532		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
533	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
534	$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
535
536# non-flavored in-srctree builds receive special treatment, in particular, we
537# do not need to copy extra resources (see e.g. test_btf_dump_case())
538$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
539ifneq ($2:$(OUTPUT),:$(shell pwd))
540	$$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
541	$(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
542endif
543
544$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
545			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
546			     $(RESOLVE_BTFIDS)				\
547			     $(TRUNNER_BPFTOOL)				\
548			     | $(TRUNNER_BINARY)-extras
549	$$(call msg,BINARY,,$$@)
550	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
551	$(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@
552	$(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/$(USE_BOOTSTRAP)bpftool \
553		   $(OUTPUT)/$(if $2,$2/)bpftool
554
555endef
556
557# Define test_progs test runner.
558TRUNNER_TESTS_DIR := prog_tests
559TRUNNER_BPF_PROGS_DIR := progs
560TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
561			 network_helpers.c testing_helpers.c		\
562			 btf_helpers.c flow_dissector_load.h		\
563			 cap_helpers.c test_loader.c xsk.c disasm.c	\
564			 json_writer.c unpriv_helpers.c
565
566TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
567		       $(OUTPUT)/liburandom_read.so			\
568		       $(OUTPUT)/xdp_synproxy				\
569		       $(OUTPUT)/sign-file				\
570		       ima_setup.sh 					\
571		       verify_sig_setup.sh				\
572		       $(wildcard progs/btf_dump_test_case_*.c)		\
573		       $(wildcard progs/*.bpf.o)
574TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
575TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
576$(eval $(call DEFINE_TEST_RUNNER,test_progs))
577
578# Define test_progs-no_alu32 test runner.
579TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
580TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
581$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
582
583# Define test_progs BPF-GCC-flavored test runner.
584ifneq ($(BPF_GCC),)
585TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
586TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc,)
587$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
588endif
589
590# Define test_maps test runner.
591TRUNNER_TESTS_DIR := map_tests
592TRUNNER_BPF_PROGS_DIR := progs
593TRUNNER_EXTRA_SOURCES := test_maps.c
594TRUNNER_EXTRA_FILES :=
595TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
596TRUNNER_BPF_CFLAGS :=
597$(eval $(call DEFINE_TEST_RUNNER,test_maps))
598
599# Define test_verifier test runner.
600# It is much simpler than test_maps/test_progs and sufficiently different from
601# them (e.g., test.h is using completely pattern), that it's worth just
602# explicitly defining all the rules explicitly.
603verifier/tests.h: verifier/*.c
604	$(shell ( cd verifier/; \
605		  echo '/* Generated header, do not edit */'; \
606		  echo '#ifdef FILL_ARRAY'; \
607		  ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
608		  echo '#endif' \
609		) > verifier/tests.h)
610$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
611	$(call msg,BINARY,,$@)
612	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
613
614$(OUTPUT)/xskxceiver: xskxceiver.c xskxceiver.h $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT)
615	$(call msg,BINARY,,$@)
616	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
617
618$(OUTPUT)/xdp_hw_metadata: xdp_hw_metadata.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xdp_hw_metadata.skel.h | $(OUTPUT)
619	$(call msg,BINARY,,$@)
620	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
621
622$(OUTPUT)/xdp_features: xdp_features.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xdp_features.skel.h | $(OUTPUT)
623	$(call msg,BINARY,,$@)
624	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
625
626# Make sure we are able to include and link libbpf against c++.
627$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
628	$(call msg,CXX,,$@)
629	$(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
630
631# Benchmark runner
632$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ)
633	$(call msg,CC,,$@)
634	$(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@
635$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
636$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
637$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
638			    $(OUTPUT)/perfbuf_bench.skel.h
639$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h
640$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h
641$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h
642$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h
643$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h
644$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h
645$(OUTPUT)/bench_local_storage_create.o: $(OUTPUT)/bench_local_storage_create.skel.h
646$(OUTPUT)/bench_bpf_hashmap_lookup.o: $(OUTPUT)/bpf_hashmap_lookup.skel.h
647$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ)
648$(OUTPUT)/bench: LDLIBS += -lm
649$(OUTPUT)/bench: $(OUTPUT)/bench.o \
650		 $(TESTING_HELPERS) \
651		 $(TRACE_HELPERS) \
652		 $(OUTPUT)/bench_count.o \
653		 $(OUTPUT)/bench_rename.o \
654		 $(OUTPUT)/bench_trigger.o \
655		 $(OUTPUT)/bench_ringbufs.o \
656		 $(OUTPUT)/bench_bloom_filter_map.o \
657		 $(OUTPUT)/bench_bpf_loop.o \
658		 $(OUTPUT)/bench_strncmp.o \
659		 $(OUTPUT)/bench_bpf_hashmap_full_update.o \
660		 $(OUTPUT)/bench_local_storage.o \
661		 $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o \
662		 $(OUTPUT)/bench_bpf_hashmap_lookup.o \
663		 $(OUTPUT)/bench_local_storage_create.o \
664		 #
665	$(call msg,BINARY,,$@)
666	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
667
668$(OUTPUT)/veristat.o: $(BPFOBJ)
669$(OUTPUT)/veristat: $(OUTPUT)/veristat.o
670	$(call msg,BINARY,,$@)
671	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
672
673EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)	\
674	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
675	feature bpftool							\
676	$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h	\
677			       no_alu32 bpf_gcc bpf_testmod.ko		\
678			       liburandom_read.so)
679
680.PHONY: docs docs-clean
681
682# Delete partially updated (corrupted) files on error
683.DELETE_ON_ERROR:
684