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