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