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