xref: /linux/tools/testing/selftests/bpf/Makefile (revision 7b26bc6582b13a52a42a4a9765e8f30d58a81198)
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
13TOOLSARCHINCDIR := $(TOOLSDIR)/arch/$(SRCARCH)/include
14BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
15APIDIR := $(TOOLSINCDIR)/uapi
16ifneq ($(O),)
17GENDIR := $(O)/include/generated
18else
19GENDIR := $(abspath ../../../../include/generated)
20endif
21GENHDR := $(GENDIR)/autoconf.h
22PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
23
24ifneq ($(wildcard $(GENHDR)),)
25  GENFLAGS := -DHAVE_GENHDR
26endif
27
28BPF_GCC		?= $(shell command -v bpf-gcc;)
29SAN_CFLAGS	?=
30SAN_LDFLAGS	?= $(SAN_CFLAGS)
31RELEASE		?=
32OPT_FLAGS	?= $(if $(RELEASE),-O2,-O0)
33
34LIBELF_CFLAGS	:= $(shell $(PKG_CONFIG) libelf --cflags 2>/dev/null)
35LIBELF_LIBS	:= $(shell $(PKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
36
37ifeq ($(srctree),)
38srctree := $(patsubst %/,%,$(dir $(CURDIR)))
39srctree := $(patsubst %/,%,$(dir $(srctree)))
40srctree := $(patsubst %/,%,$(dir $(srctree)))
41srctree := $(patsubst %/,%,$(dir $(srctree)))
42endif
43
44CFLAGS += -g $(OPT_FLAGS) -rdynamic					\
45	  -Wall -Werror -fno-omit-frame-pointer				\
46	  $(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS)			\
47	  -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)		\
48	  -I$(TOOLSINCDIR) -I$(TOOLSARCHINCDIR) -I$(APIDIR) -I$(OUTPUT)
49LDFLAGS += $(SAN_LDFLAGS)
50LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread
51
52PCAP_CFLAGS	:= $(shell $(PKG_CONFIG) --cflags libpcap 2>/dev/null && echo "-DTRAFFIC_MONITOR=1")
53PCAP_LIBS	:= $(shell $(PKG_CONFIG) --libs libpcap 2>/dev/null)
54LDLIBS += $(PCAP_LIBS)
55CFLAGS += $(PCAP_CFLAGS)
56
57# The following tests perform type punning and they may break strict
58# aliasing rules, which are exploited by both GCC and clang by default
59# while optimizing.  This can lead to broken programs.
60progs/bind4_prog.c-CFLAGS := -fno-strict-aliasing
61progs/bind6_prog.c-CFLAGS := -fno-strict-aliasing
62progs/dynptr_fail.c-CFLAGS := -fno-strict-aliasing
63progs/linked_list_fail.c-CFLAGS := -fno-strict-aliasing
64progs/map_kptr_fail.c-CFLAGS := -fno-strict-aliasing
65progs/syscall.c-CFLAGS := -fno-strict-aliasing
66progs/test_pkt_md_access.c-CFLAGS := -fno-strict-aliasing
67progs/test_sk_lookup.c-CFLAGS := -fno-strict-aliasing
68progs/timer_crash.c-CFLAGS := -fno-strict-aliasing
69progs/test_global_func9.c-CFLAGS := -fno-strict-aliasing
70progs/verifier_nocsr.c-CFLAGS := -fno-strict-aliasing
71
72# Some utility functions use LLVM libraries
73jit_disasm_helpers.c-CFLAGS = $(LLVM_CFLAGS)
74
75ifneq ($(LLVM),)
76# Silence some warnings when compiled with clang
77CFLAGS += -Wno-unused-command-line-argument
78endif
79
80# Check whether bpf cpu=v4 is supported or not by clang
81ifneq ($(shell $(CLANG) --target=bpf -mcpu=help 2>&1 | grep 'v4'),)
82CLANG_CPUV4 := 1
83endif
84
85# Order correspond to 'make run_tests' order
86TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_progs \
87	test_sockmap \
88	test_tcpnotify_user test_sysctl \
89	test_progs-no_alu32
90TEST_INST_SUBDIRS := no_alu32
91
92# Also test bpf-gcc, if present
93ifneq ($(BPF_GCC),)
94TEST_GEN_PROGS += test_progs-bpf_gcc
95TEST_INST_SUBDIRS += bpf_gcc
96
97# The following tests contain C code that, although technically legal,
98# triggers GCC warnings that cannot be disabled: declaration of
99# anonymous struct types in function parameter lists.
100progs/btf_dump_test_case_bitfields.c-bpf_gcc-CFLAGS := -Wno-error
101progs/btf_dump_test_case_namespacing.c-bpf_gcc-CFLAGS := -Wno-error
102progs/btf_dump_test_case_packing.c-bpf_gcc-CFLAGS := -Wno-error
103progs/btf_dump_test_case_padding.c-bpf_gcc-CFLAGS := -Wno-error
104progs/btf_dump_test_case_syntax.c-bpf_gcc-CFLAGS := -Wno-error
105
106# The following tests do type-punning, via the __imm_insn macro, from
107# `struct bpf_insn' to long and then uses the value.  This triggers an
108# "is used uninitialized" warning in GCC due to strict-aliasing
109# rules.
110progs/verifier_ref_tracking.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
111progs/verifier_unpriv.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
112progs/verifier_cgroup_storage.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
113progs/verifier_ld_ind.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
114progs/verifier_map_ret_val.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
115progs/verifier_spill_fill.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
116progs/verifier_subprog_precision.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
117progs/verifier_uninit.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
118endif
119
120ifneq ($(CLANG_CPUV4),)
121TEST_GEN_PROGS += test_progs-cpuv4
122TEST_INST_SUBDIRS += cpuv4
123endif
124
125TEST_GEN_FILES = test_lwt_ip_encap.bpf.o test_tc_edt.bpf.o
126TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)
127
128# Order correspond to 'make run_tests' order
129TEST_PROGS := test_kmod.sh \
130	test_xdp_redirect.sh \
131	test_xdp_redirect_multi.sh \
132	test_xdp_meta.sh \
133	test_tunnel.sh \
134	test_lwt_seg6local.sh \
135	test_lirc_mode2.sh \
136	test_flow_dissector.sh \
137	test_xdp_vlan_mode_generic.sh \
138	test_xdp_vlan_mode_native.sh \
139	test_lwt_ip_encap.sh \
140	test_tc_tunnel.sh \
141	test_tc_edt.sh \
142	test_xdping.sh \
143	test_bpftool_build.sh \
144	test_bpftool.sh \
145	test_bpftool_metadata.sh \
146	test_doc_build.sh \
147	test_xsk.sh \
148	test_xdp_features.sh
149
150TEST_PROGS_EXTENDED := with_addr.sh \
151	with_tunnels.sh ima_setup.sh verify_sig_setup.sh \
152	test_xdp_vlan.sh test_bpftool.py
153
154# Compile but not part of 'make run_tests'
155TEST_GEN_PROGS_EXTENDED = \
156	bench \
157	bpf_testmod.ko \
158	bpf_test_modorder_x.ko \
159	bpf_test_modorder_y.ko \
160	bpf_test_no_cfi.ko \
161	flow_dissector_load \
162	runqslower \
163	test_cpp \
164	test_flow_dissector \
165	test_lirc_mode2_user \
166	veristat \
167	xdp_features \
168	xdp_hw_metadata \
169	xdp_redirect_multi \
170	xdp_synproxy \
171	xdping \
172	xskxceiver
173
174TEST_GEN_FILES += liburandom_read.so urandom_read sign-file uprobe_multi
175
176ifneq ($(V),1)
177submake_extras := feature_display=0
178endif
179
180# override lib.mk's default rules
181OVERRIDE_TARGETS := 1
182override define CLEAN
183	$(call msg,CLEAN)
184	$(Q)$(RM) -r $(TEST_GEN_PROGS)
185	$(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED)
186	$(Q)$(RM) -r $(TEST_GEN_FILES)
187	$(Q)$(RM) -r $(EXTRA_CLEAN)
188	$(Q)$(MAKE) -C bpf_testmod clean
189	$(Q)$(MAKE) docs-clean
190endef
191
192include ../lib.mk
193
194NON_CHECK_FEAT_TARGETS := clean docs-clean
195CHECK_FEAT := $(filter-out $(NON_CHECK_FEAT_TARGETS),$(or $(MAKECMDGOALS), "none"))
196ifneq ($(CHECK_FEAT),)
197FEATURE_USER := .selftests
198FEATURE_TESTS := llvm
199FEATURE_DISPLAY := $(FEATURE_TESTS)
200
201# Makefile.feature expects OUTPUT to end with a slash
202ifeq ($(shell expr $(MAKE_VERSION) \>= 4.4), 1)
203$(let OUTPUT,$(OUTPUT)/,\
204	$(eval include ../../../build/Makefile.feature))
205else
206OUTPUT := $(OUTPUT)/
207$(eval include ../../../build/Makefile.feature)
208OUTPUT := $(patsubst %/,%,$(OUTPUT))
209endif
210endif
211
212ifeq ($(feature-llvm),1)
213  LLVM_CFLAGS  += -DHAVE_LLVM_SUPPORT
214  LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets
215  # both llvm-config and lib.mk add -D_GNU_SOURCE, which ends up as conflict
216  LLVM_CFLAGS  += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags))
217  LLVM_LDLIBS  += $(shell $(LLVM_CONFIG) --link-static --libs $(LLVM_CONFIG_LIB_COMPONENTS))
218  LLVM_LDLIBS  += $(shell $(LLVM_CONFIG) --link-static --system-libs $(LLVM_CONFIG_LIB_COMPONENTS))
219  LLVM_LDLIBS  += -lstdc++
220  LLVM_LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags)
221endif
222
223SCRATCH_DIR := $(OUTPUT)/tools
224BUILD_DIR := $(SCRATCH_DIR)/build
225INCLUDE_DIR := $(SCRATCH_DIR)/include
226BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
227ifneq ($(CROSS_COMPILE),)
228HOST_BUILD_DIR		:= $(BUILD_DIR)/host
229HOST_SCRATCH_DIR	:= $(OUTPUT)/host-tools
230HOST_INCLUDE_DIR	:= $(HOST_SCRATCH_DIR)/include
231else
232HOST_BUILD_DIR		:= $(BUILD_DIR)
233HOST_SCRATCH_DIR	:= $(SCRATCH_DIR)
234HOST_INCLUDE_DIR	:= $(INCLUDE_DIR)
235endif
236HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
237RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
238RUNQSLOWER_OUTPUT := $(BUILD_DIR)/runqslower/
239
240VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
241		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
242		     ../../../../vmlinux				\
243		     /sys/kernel/btf/vmlinux				\
244		     /boot/vmlinux-$(shell uname -r)
245VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
246ifeq ($(VMLINUX_BTF),)
247$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
248endif
249
250# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
251# to build individual tests.
252# NOTE: Semicolon at the end is critical to override lib.mk's default static
253# rule for binaries.
254$(notdir $(TEST_GEN_PROGS)						\
255	 $(TEST_GEN_PROGS_EXTENDED)): %: $(OUTPUT)/% ;
256
257# sort removes libbpf duplicates when not cross-building
258MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf	\
259	       $(BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/bpftool		\
260	       $(HOST_BUILD_DIR)/resolve_btfids				\
261	       $(RUNQSLOWER_OUTPUT) $(INCLUDE_DIR))
262$(MAKE_DIRS):
263	$(call msg,MKDIR,,$@)
264	$(Q)mkdir -p $@
265
266$(OUTPUT)/%.o: %.c
267	$(call msg,CC,,$@)
268	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
269
270$(OUTPUT)/%:%.c
271	$(call msg,BINARY,,$@)
272	$(Q)$(LINK.c) $^ $(LDLIBS) -o $@
273
274# LLVM's ld.lld doesn't support all the architectures, so use it only on x86
275ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 riscv))
276LLD := lld
277else
278LLD := $(shell command -v $(LD))
279endif
280
281# Filter out -static for liburandom_read.so and its dependent targets so that static builds
282# do not fail. Static builds leave urandom_read relying on system-wide shared libraries.
283$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
284	$(call msg,LIB,,$@)
285	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
286		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
287		     $(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \
288		     -Wno-unused-command-line-argument \
289		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
290		     -Wl,--version-script=liburandom_read.map \
291		     -fPIC -shared -o $@
292
293$(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
294	$(call msg,BINARY,,$@)
295	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
296		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
297		     -Wno-unused-command-line-argument \
298		     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT) \
299		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
300		     -Wl,-rpath=. -o $@
301
302$(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
303	$(call msg,SIGN-FILE,,$@)
304	$(Q)$(CC) $(shell $(PKG_CONFIG) --cflags libcrypto 2> /dev/null) \
305		  $< -o $@ \
306		  $(shell $(PKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)
307
308$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
309	$(call msg,MOD,,$@)
310	$(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
311	$(Q)$(MAKE) $(submake_extras) -C bpf_testmod \
312		RESOLVE_BTFIDS=$(RESOLVE_BTFIDS)     \
313		EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
314	$(Q)cp bpf_testmod/bpf_testmod.ko $@
315
316$(OUTPUT)/bpf_test_no_cfi.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_no_cfi/Makefile bpf_test_no_cfi/*.[ch])
317	$(call msg,MOD,,$@)
318	$(Q)$(RM) bpf_test_no_cfi/bpf_test_no_cfi.ko # force re-compilation
319	$(Q)$(MAKE) $(submake_extras) -C bpf_test_no_cfi \
320		RESOLVE_BTFIDS=$(RESOLVE_BTFIDS)	 \
321		EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
322	$(Q)cp bpf_test_no_cfi/bpf_test_no_cfi.ko $@
323
324$(OUTPUT)/bpf_test_modorder_x.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_modorder_x/Makefile bpf_test_modorder_x/*.[ch])
325	$(call msg,MOD,,$@)
326	$(Q)$(RM) bpf_test_modorder_x/bpf_test_modorder_x.ko # force re-compilation
327	$(Q)$(MAKE) $(submake_extras) -C bpf_test_modorder_x \
328		RESOLVE_BTFIDS=$(RESOLVE_BTFIDS)	     \
329		EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
330	$(Q)cp bpf_test_modorder_x/bpf_test_modorder_x.ko $@
331
332$(OUTPUT)/bpf_test_modorder_y.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_modorder_y/Makefile bpf_test_modorder_y/*.[ch])
333	$(call msg,MOD,,$@)
334	$(Q)$(RM) bpf_test_modorder_y/bpf_test_modorder_y.ko # force re-compilation
335	$(Q)$(MAKE) $(submake_extras) -C bpf_test_modorder_y \
336		RESOLVE_BTFIDS=$(RESOLVE_BTFIDS)	     \
337		EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
338	$(Q)cp bpf_test_modorder_y/bpf_test_modorder_y.ko $@
339
340
341DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
342ifneq ($(CROSS_COMPILE),)
343CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
344TRUNNER_BPFTOOL := $(CROSS_BPFTOOL)
345USE_BOOTSTRAP := ""
346else
347TRUNNER_BPFTOOL := $(DEFAULT_BPFTOOL)
348USE_BOOTSTRAP := "bootstrap/"
349endif
350
351$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
352	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower	       \
353		    OUTPUT=$(RUNQSLOWER_OUTPUT) VMLINUX_BTF=$(VMLINUX_BTF)     \
354		    BPFTOOL_OUTPUT=$(HOST_BUILD_DIR)/bpftool/		       \
355		    BPFOBJ_OUTPUT=$(BUILD_DIR)/libbpf/			       \
356		    BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR)		       \
357		    EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \
358		    EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' &&	       \
359		    cp $(RUNQSLOWER_OUTPUT)runqslower $@
360
361TEST_GEN_PROGS_EXTENDED += $(TRUNNER_BPFTOOL)
362
363$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
364
365TESTING_HELPERS	:= $(OUTPUT)/testing_helpers.o
366CGROUP_HELPERS	:= $(OUTPUT)/cgroup_helpers.o
367UNPRIV_HELPERS  := $(OUTPUT)/unpriv_helpers.o
368TRACE_HELPERS	:= $(OUTPUT)/trace_helpers.o
369JSON_WRITER		:= $(OUTPUT)/json_writer.o
370CAP_HELPERS	:= $(OUTPUT)/cap_helpers.o
371NETWORK_HELPERS := $(OUTPUT)/network_helpers.o
372
373$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
374$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
375$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
376$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
377$(OUTPUT)/test_tag: $(TESTING_HELPERS)
378$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS)
379$(OUTPUT)/xdping: $(TESTING_HELPERS)
380$(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS)
381$(OUTPUT)/test_maps: $(TESTING_HELPERS)
382$(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS)
383$(OUTPUT)/xsk.o: $(BPFOBJ)
384
385BPFTOOL ?= $(DEFAULT_BPFTOOL)
386$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
387		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
388	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
389		    ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" 	       \
390		    EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)'	       \
391		    EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)'			       \
392		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/			       \
393		    LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/		       \
394		    LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/			       \
395		    prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin
396
397ifneq ($(CROSS_COMPILE),)
398$(CROSS_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)	\
399		    $(BPFOBJ) | $(BUILD_DIR)/bpftool
400	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)				\
401		    ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE)			\
402		    EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)'		\
403		    EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)'				\
404		    OUTPUT=$(BUILD_DIR)/bpftool/				\
405		    LIBBPF_OUTPUT=$(BUILD_DIR)/libbpf/				\
406		    LIBBPF_DESTDIR=$(SCRATCH_DIR)/				\
407		    prefix= DESTDIR=$(SCRATCH_DIR)/ install-bin
408endif
409
410all: docs
411
412docs:
413	$(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras)	\
414	            -f Makefile.docs					\
415	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
416
417docs-clean:
418	$(Q)$(MAKE) $(submake_extras)					\
419	            -f Makefile.docs					\
420	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
421
422$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
423	   $(APIDIR)/linux/bpf.h					       \
424	   | $(BUILD_DIR)/libbpf
425	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
426		    EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \
427		    EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)'	       \
428		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
429
430ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
431$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
432		$(APIDIR)/linux/bpf.h					       \
433		| $(HOST_BUILD_DIR)/libbpf
434	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR)                             \
435		    ARCH= CROSS_COMPILE=				       \
436		    EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)'	       \
437		    EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)'			       \
438		    OUTPUT=$(HOST_BUILD_DIR)/libbpf/			       \
439		    CC="$(HOSTCC)" LD="$(HOSTLD)"			       \
440		    DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
441endif
442
443# vmlinux.h is first dumped to a temprorary file and then compared to
444# the previous version. This helps to avoid unnecessary re-builds of
445# $(TRUNNER_BPF_OBJS)
446$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
447ifeq ($(VMLINUX_H),)
448	$(call msg,GEN,,$@)
449	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $(INCLUDE_DIR)/.vmlinux.h.tmp
450	$(Q)cmp -s $(INCLUDE_DIR)/.vmlinux.h.tmp $@ || mv $(INCLUDE_DIR)/.vmlinux.h.tmp $@
451else
452	$(call msg,CP,,$@)
453	$(Q)cp "$(VMLINUX_H)" $@
454endif
455
456$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids	\
457		       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
458		       $(TOOLSDIR)/lib/rbtree.c			\
459		       $(TOOLSDIR)/lib/zalloc.c			\
460		       $(TOOLSDIR)/lib/string.c			\
461		       $(TOOLSDIR)/lib/ctype.c			\
462		       $(TOOLSDIR)/lib/str_error_r.c
463	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
464		CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \
465		LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
466		OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
467
468# Get Clang's default includes on this system, as opposed to those seen by
469# '--target=bpf'. This fixes "missing" files on some architectures/distros,
470# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
471#
472# Use '-idirafter': Don't interfere with include mechanics except where the
473# build would have failed anyways.
474define get_sys_includes
475$(shell $(1) $(2) -v -E - </dev/null 2>&1 \
476	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
477$(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}') \
478$(shell $(1) $(2) -dM -E - </dev/null | grep '__loongarch_grlen ' | awk '{printf("-D__BITS_PER_LONG=%d", $$3)}') \
479$(shell $(1) $(2) -dM -E - </dev/null | grep -E 'MIPS(EL|EB)|_MIPS_SZ(PTR|LONG) |_MIPS_SIM |_ABI(O32|N32|64) ' | awk '{printf("-D%s=%s ", $$2, $$3)}')
480endef
481
482# Determine target endianness.
483IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
484			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
485MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
486BPF_TARGET_ENDIAN=$(if $(IS_LITTLE_ENDIAN),--target=bpfel,--target=bpfeb)
487
488ifneq ($(CROSS_COMPILE),)
489CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
490endif
491
492CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
493BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN)	\
494	     -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)			\
495	     -I$(abspath $(OUTPUT)/../usr/include)			\
496	     -Wno-compare-distinct-pointer-types
497# TODO: enable me -Wsign-compare
498
499CLANG_CFLAGS = $(CLANG_SYS_INCLUDES)
500
501$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
502$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
503
504$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
505$(OUTPUT)/cgroup_getset_retval_hooks.o: cgroup_getset_retval_hooks.h
506
507# Build BPF object using Clang
508# $1 - input .c file
509# $2 - output .o file
510# $3 - CFLAGS
511# $4 - binary name
512define CLANG_BPF_BUILD_RULE
513	$(call msg,CLNG-BPF,$4,$2)
514	$(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v3 -o $2
515endef
516# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
517define CLANG_NOALU32_BPF_BUILD_RULE
518	$(call msg,CLNG-BPF,$4,$2)
519	$(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v2 -o $2
520endef
521# Similar to CLANG_BPF_BUILD_RULE, but with cpu-v4
522define CLANG_CPUV4_BPF_BUILD_RULE
523	$(call msg,CLNG-BPF,$4,$2)
524	$(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v4 -o $2
525endef
526# Build BPF object using GCC
527define GCC_BPF_BUILD_RULE
528	$(call msg,GCC-BPF,$4,$2)
529	$(Q)$(BPF_GCC) $3 -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes -O2 -c $1 -o $2
530endef
531
532SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
533
534LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
535		linked_vars.skel.h linked_maps.skel.h 			\
536		test_subskeleton.skel.h test_subskeleton_lib.skel.h	\
537		test_usdt.skel.h
538
539LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c 		\
540	trace_printk.c trace_vprintk.c map_ptr_kern.c 			\
541	core_kern.c core_kern_overflow.c test_ringbuf.c			\
542	test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c
543
544# Generate both light skeleton and libbpf skeleton for these
545LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
546	kfunc_call_test_subprog.c
547SKEL_BLACKLIST += $$(LSKELS)
548
549test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o
550linked_funcs.skel.h-deps := linked_funcs1.bpf.o linked_funcs2.bpf.o
551linked_vars.skel.h-deps := linked_vars1.bpf.o linked_vars2.bpf.o
552linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o
553# In the subskeleton case, we want the test_subskeleton_lib.subskel.h file
554# but that's created as a side-effect of the skel.h generation.
555test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o
556test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o
557test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o
558xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o
559xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o
560xdp_features.skel.h-deps := xdp_features.bpf.o
561
562LINKED_BPF_OBJS := $(foreach skel,$(LINKED_SKELS),$($(skel)-deps))
563LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(LINKED_BPF_OBJS))
564
565HEADERS_FOR_BPF_OBJS := $(wildcard $(BPFDIR)/*.bpf.h)		\
566			$(addprefix $(BPFDIR)/,	bpf_core_read.h	\
567			                        bpf_endian.h	\
568						bpf_helpers.h	\
569			                        bpf_tracing.h)
570
571# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
572# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
573# Parameters:
574# $1 - test runner base binary name (e.g., test_progs)
575# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, bpf_gcc, etc)
576define DEFINE_TEST_RUNNER
577
578TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
579TRUNNER_BINARY := $1$(if $2,-)$2
580TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
581				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
582TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
583				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
584TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
585TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
586TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
587TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.bpf.o, $$(TRUNNER_BPF_SRCS))
588TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h,	\
589				 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\
590					       $$(TRUNNER_BPF_SRCS)))
591TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA))
592TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
593TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
594
595# Evaluate rules now with extra TRUNNER_XXX variables above already defined
596$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
597
598endef
599
600# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
601# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
602# $1 - test runner base binary name (e.g., test_progs)
603# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, bpf_gcc, etc)
604define DEFINE_TEST_RUNNER_RULES
605
606ifeq ($($(TRUNNER_OUTPUT)-dir),)
607$(TRUNNER_OUTPUT)-dir := y
608$(TRUNNER_OUTPUT):
609	$$(call msg,MKDIR,,$$@)
610	$(Q)mkdir -p $$@
611endif
612
613# ensure we set up BPF objects generation rule just once for a given
614# input/output directory combination
615ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
616$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
617$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.bpf.o:				\
618		     $(TRUNNER_BPF_PROGS_DIR)/%.c			\
619		     $(TRUNNER_BPF_PROGS_DIR)/*.h			\
620		     $$(INCLUDE_DIR)/vmlinux.h				\
621		     $(HEADERS_FOR_BPF_OBJS)				\
622		     | $(TRUNNER_OUTPUT) $$(BPFOBJ)
623	$$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@,			\
624					  $(TRUNNER_BPF_CFLAGS)         \
625					  $$($$<-CFLAGS)		\
626					  $$($$<-$2-CFLAGS),$(TRUNNER_BINARY))
627
628$(TRUNNER_BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
629	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
630	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
631	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
632	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
633	$(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
634	$(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$@
635	$(Q)$$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$(@:.skel.h=.subskel.h)
636	$(Q)rm -f $$(<:.o=.linked1.o) $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
637
638$(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
639	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
640	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$<
641	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o)
642	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o)
643	$(Q)diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
644	$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@
645	$(Q)rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
646
647$(LINKED_BPF_OBJS): %: $(TRUNNER_OUTPUT)/%
648
649# .SECONDEXPANSION here allows to correctly expand %-deps variables as prerequisites
650.SECONDEXPANSION:
651$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_OUTPUT)/%: $$$$(%-deps) $(BPFTOOL) | $(TRUNNER_OUTPUT)
652	$$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.bpf.o))
653	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
654	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
655	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o)
656	$(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
657	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
658	$(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
659	$(Q)$$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h)
660	$(Q)rm -f $$(@:.skel.h=.linked1.o) $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
661
662# When the compiler generates a %.d file, only skel basenames (not
663# full paths) are specified as prerequisites for corresponding %.o
664# file. vpath directives below instruct make to search for skel files
665# in TRUNNER_OUTPUT, if they are not present in the working directory.
666vpath %.skel.h $(TRUNNER_OUTPUT)
667vpath %.lskel.h $(TRUNNER_OUTPUT)
668vpath %.subskel.h $(TRUNNER_OUTPUT)
669
670endif
671
672# ensure we set up tests.h header generation rule just once
673ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
674$(TRUNNER_TESTS_DIR)-tests-hdr := y
675$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
676	$$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
677	$$(shell (echo '/* Generated header, do not edit */';					\
678		  sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p'	\
679			$(TRUNNER_TESTS_DIR)/*.c | sort ;	\
680		 ) > $$@)
681endif
682
683# compile individual test files
684# Note: we cd into output directory to ensure embedded BPF object is found
685$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
686		      $(TRUNNER_TESTS_DIR)/%.c				\
687		      | $(TRUNNER_OUTPUT)/%.test.d
688	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
689	$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -MMD -MT $$@ -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
690
691$(TRUNNER_TEST_OBJS:.o=.d): $(TRUNNER_OUTPUT)/%.test.d:			\
692			    $(TRUNNER_TESTS_DIR)/%.c			\
693			    $(TRUNNER_EXTRA_HDRS)			\
694			    $(TRUNNER_BPF_SKELS)			\
695			    $(TRUNNER_BPF_LSKELS)			\
696			    $(TRUNNER_BPF_SKELS_LINKED)			\
697			    $$(BPFOBJ) | $(TRUNNER_OUTPUT)
698
699ifeq ($(filter clean docs-clean,$(MAKECMDGOALS)),)
700include $(wildcard $(TRUNNER_TEST_OBJS:.o=.d))
701endif
702
703# add per extra obj CFGLAGS definitions
704$(foreach N,$(patsubst $(TRUNNER_OUTPUT)/%.o,%,$(TRUNNER_EXTRA_OBJS)),	\
705	$(eval $(TRUNNER_OUTPUT)/$(N).o: CFLAGS += $($(N).c-CFLAGS)))
706
707$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
708		       %.c						\
709		       $(TRUNNER_EXTRA_HDRS)				\
710		       $(TRUNNER_TESTS_HDR)				\
711		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
712	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
713	$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
714
715# non-flavored in-srctree builds receive special treatment, in particular, we
716# do not need to copy extra resources (see e.g. test_btf_dump_case())
717$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
718ifneq ($2:$(OUTPUT),:$(shell pwd))
719	$$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
720	$(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
721endif
722
723$(OUTPUT)/$(TRUNNER_BINARY): LDLIBS += $$(LLVM_LDLIBS)
724$(OUTPUT)/$(TRUNNER_BINARY): LDFLAGS += $$(LLVM_LDFLAGS)
725
726# some X.test.o files have runtime dependencies on Y.bpf.o files
727$(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS)
728
729$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
730			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
731			     $(RESOLVE_BTFIDS)				\
732			     $(TRUNNER_BPFTOOL)				\
733			     | $(TRUNNER_BINARY)-extras
734	$$(call msg,BINARY,,$$@)
735	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) $$(LDFLAGS) -o $$@
736	$(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@
737	$(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/$(USE_BOOTSTRAP)bpftool \
738		   $(OUTPUT)/$(if $2,$2/)bpftool
739
740endef
741
742# Define test_progs test runner.
743TRUNNER_TESTS_DIR := prog_tests
744TRUNNER_BPF_PROGS_DIR := progs
745TRUNNER_EXTRA_SOURCES := test_progs.c		\
746			 cgroup_helpers.c	\
747			 trace_helpers.c	\
748			 network_helpers.c	\
749			 testing_helpers.c	\
750			 btf_helpers.c		\
751			 cap_helpers.c		\
752			 unpriv_helpers.c 	\
753			 netlink_helpers.c	\
754			 jit_disasm_helpers.c	\
755			 io_helpers.c		\
756			 test_loader.c		\
757			 xsk.c			\
758			 disasm.c		\
759			 disasm_helpers.c	\
760			 json_writer.c 		\
761			 flow_dissector_load.h	\
762			 ip_check_defrag_frags.h
763TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
764		       $(OUTPUT)/bpf_test_no_cfi.ko			\
765		       $(OUTPUT)/bpf_test_modorder_x.ko		\
766		       $(OUTPUT)/bpf_test_modorder_y.ko		\
767		       $(OUTPUT)/liburandom_read.so			\
768		       $(OUTPUT)/xdp_synproxy				\
769		       $(OUTPUT)/sign-file				\
770		       $(OUTPUT)/uprobe_multi				\
771		       ima_setup.sh 					\
772		       verify_sig_setup.sh				\
773		       $(wildcard progs/btf_dump_test_case_*.c)		\
774		       $(wildcard progs/*.bpf.o)
775TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
776TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
777$(eval $(call DEFINE_TEST_RUNNER,test_progs))
778
779# Define test_progs-no_alu32 test runner.
780TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
781TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
782$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
783
784# Define test_progs-cpuv4 test runner.
785ifneq ($(CLANG_CPUV4),)
786TRUNNER_BPF_BUILD_RULE := CLANG_CPUV4_BPF_BUILD_RULE
787TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
788$(eval $(call DEFINE_TEST_RUNNER,test_progs,cpuv4))
789endif
790
791# Define test_progs BPF-GCC-flavored test runner.
792ifneq ($(BPF_GCC),)
793TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
794TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc,)
795$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
796endif
797
798# Define test_maps test runner.
799TRUNNER_TESTS_DIR := map_tests
800TRUNNER_BPF_PROGS_DIR := progs
801TRUNNER_EXTRA_SOURCES := test_maps.c
802TRUNNER_EXTRA_FILES :=
803TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
804TRUNNER_BPF_CFLAGS :=
805$(eval $(call DEFINE_TEST_RUNNER,test_maps))
806
807# Define test_verifier test runner.
808# It is much simpler than test_maps/test_progs and sufficiently different from
809# them (e.g., test.h is using completely pattern), that it's worth just
810# explicitly defining all the rules explicitly.
811verifier/tests.h: verifier/*.c
812	$(shell ( cd verifier/; \
813		  echo '/* Generated header, do not edit */'; \
814		  echo '#ifdef FILL_ARRAY'; \
815		  ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
816		  echo '#endif' \
817		) > verifier/tests.h)
818$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
819	$(call msg,BINARY,,$@)
820	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
821
822# Include find_bit.c to compile xskxceiver.
823EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c
824$(OUTPUT)/xskxceiver: $(EXTRA_SRC) xskxceiver.c xskxceiver.h $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT)
825	$(call msg,BINARY,,$@)
826	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
827
828$(OUTPUT)/xdp_hw_metadata: xdp_hw_metadata.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xdp_hw_metadata.skel.h | $(OUTPUT)
829	$(call msg,BINARY,,$@)
830	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
831
832$(OUTPUT)/xdp_features: xdp_features.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xdp_features.skel.h | $(OUTPUT)
833	$(call msg,BINARY,,$@)
834	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
835
836# Make sure we are able to include and link libbpf against c++.
837$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
838	$(call msg,CXX,,$@)
839	$(Q)$(CXX) $(subst -D_GNU_SOURCE=,,$(CFLAGS)) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
840
841# Benchmark runner
842$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ)
843	$(call msg,CC,,$@)
844	$(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@
845$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
846$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
847$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
848			    $(OUTPUT)/perfbuf_bench.skel.h
849$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h
850$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h
851$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h
852$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h
853$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h
854$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h
855$(OUTPUT)/bench_local_storage_create.o: $(OUTPUT)/bench_local_storage_create.skel.h
856$(OUTPUT)/bench_bpf_hashmap_lookup.o: $(OUTPUT)/bpf_hashmap_lookup.skel.h
857$(OUTPUT)/bench_htab_mem.o: $(OUTPUT)/htab_mem_bench.skel.h
858$(OUTPUT)/bench_bpf_crypto.o: $(OUTPUT)/crypto_bench.skel.h
859$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ)
860$(OUTPUT)/bench: LDLIBS += -lm
861$(OUTPUT)/bench: $(OUTPUT)/bench.o \
862		 $(TESTING_HELPERS) \
863		 $(TRACE_HELPERS) \
864		 $(CGROUP_HELPERS) \
865		 $(OUTPUT)/bench_count.o \
866		 $(OUTPUT)/bench_rename.o \
867		 $(OUTPUT)/bench_trigger.o \
868		 $(OUTPUT)/bench_ringbufs.o \
869		 $(OUTPUT)/bench_bloom_filter_map.o \
870		 $(OUTPUT)/bench_bpf_loop.o \
871		 $(OUTPUT)/bench_strncmp.o \
872		 $(OUTPUT)/bench_bpf_hashmap_full_update.o \
873		 $(OUTPUT)/bench_local_storage.o \
874		 $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o \
875		 $(OUTPUT)/bench_bpf_hashmap_lookup.o \
876		 $(OUTPUT)/bench_local_storage_create.o \
877		 $(OUTPUT)/bench_htab_mem.o \
878		 $(OUTPUT)/bench_bpf_crypto.o \
879		 #
880	$(call msg,BINARY,,$@)
881	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
882
883$(OUTPUT)/veristat.o: $(BPFOBJ)
884$(OUTPUT)/veristat: $(OUTPUT)/veristat.o
885	$(call msg,BINARY,,$@)
886	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
887
888# Linking uprobe_multi can fail due to relocation overflows on mips.
889$(OUTPUT)/uprobe_multi: CFLAGS += $(if $(filter mips, $(ARCH)),-mxgot)
890$(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld
891	$(call msg,BINARY,,$@)
892	$(Q)$(CC) $(CFLAGS) -Wl,-T,uprobe_multi.ld -O0 $(LDFLAGS) 	\
893		$(filter-out %.ld,$^) $(LDLIBS) -o $@
894
895EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)			\
896	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
897	feature bpftool 						\
898	$(addprefix $(OUTPUT)/,*.o *.d *.skel.h *.lskel.h *.subskel.h	\
899			       no_alu32 cpuv4 bpf_gcc bpf_testmod.ko	\
900			       bpf_test_no_cfi.ko			\
901			       bpf_test_modorder_x.ko			\
902			       bpf_test_modorder_y.ko			\
903			       liburandom_read.so)			\
904	$(OUTPUT)/FEATURE-DUMP.selftests
905
906.PHONY: docs docs-clean
907
908# Delete partially updated (corrupted) files on error
909.DELETE_ON_ERROR:
910
911DEFAULT_INSTALL_RULE := $(INSTALL_RULE)
912override define INSTALL_RULE
913	$(DEFAULT_INSTALL_RULE)
914	@for DIR in $(TEST_INST_SUBDIRS); do		  \
915		mkdir -p $(INSTALL_PATH)/$$DIR;   \
916		rsync -a $(OUTPUT)/$$DIR/*.bpf.o $(INSTALL_PATH)/$$DIR;\
917	done
918endef
919