projects/linux·blob master · refs

1081 lines · 42.9 KiB

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