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 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_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) 339$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS) 340$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS) 341$(OUTPUT)/test_tag: $(TESTING_HELPERS) 342$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS) 343$(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS) 344$(OUTPUT)/test_maps: $(TESTING_HELPERS) 345$(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS) 346$(OUTPUT)/xsk.o: $(BPFOBJ) 347 348BPFTOOL ?= $(DEFAULT_BPFTOOL) 349$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ 350 $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool 351 $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \ 352 ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" \ 353 EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \ 354 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \ 355 OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \ 356 LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \ 357 LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/ \ 358 SKIP_LLVM=$(SKIP_LLVM) \ 359 SKIP_LIBBFD=$(SKIP_LIBBFD) \ 360 SKIP_CRYPTO=$(SKIP_CRYPTO) \ 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) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \ 369 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \ 370 OUTPUT=$(BUILD_DIR)/bpftool/ \ 371 LIBBPF_OUTPUT=$(BUILD_DIR)/libbpf/ \ 372 LIBBPF_DESTDIR=$(SCRATCH_DIR)/ \ 373 SKIP_LLVM=$(SKIP_LLVM) \ 374 SKIP_LIBBFD=$(SKIP_LIBBFD) \ 375 SKIP_CRYPTO=$(SKIP_CRYPTO) \ 376 prefix= DESTDIR=$(SCRATCH_DIR)/ install-bin 377endif 378 379ifneq ($(SKIP_DOCS),1) 380all: docs 381endif 382 383docs: 384 $(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras) \ 385 -f Makefile.docs \ 386 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@ 387 388docs-clean: 389 $(Q)$(MAKE) $(submake_extras) \ 390 -f Makefile.docs \ 391 prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@ 392 393$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ 394 $(APIDIR)/linux/bpf.h \ 395 | $(BUILD_DIR)/libbpf 396 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \ 397 EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \ 398 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \ 399 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers 400 401ifneq ($(BPFOBJ),$(HOST_BPFOBJ)) 402$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ 403 $(APIDIR)/linux/bpf.h \ 404 | $(HOST_BUILD_DIR)/libbpf 405 $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \ 406 ARCH= CROSS_COMPILE= \ 407 EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)' \ 408 EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' \ 409 OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \ 410 CC="$(HOSTCC)" LD="$(HOSTLD)" \ 411 DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers 412endif 413 414# vmlinux.h is first dumped to a temporary file and then compared to 415# the previous version. This helps to avoid unnecessary re-builds of 416# $(TRUNNER_BPF_OBJS) 417$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR) 418ifeq ($(VMLINUX_H),) 419 $(call msg,GEN,,$@) 420 $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $(INCLUDE_DIR)/.vmlinux.h.tmp 421 $(Q)cmp -s $(INCLUDE_DIR)/.vmlinux.h.tmp $@ || mv $(INCLUDE_DIR)/.vmlinux.h.tmp $@ 422else 423 $(call msg,CP,,$@) 424 $(Q)cp "$(VMLINUX_H)" $@ 425endif 426 427$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids \ 428 $(TOOLSDIR)/bpf/resolve_btfids/main.c \ 429 $(TOOLSDIR)/lib/rbtree.c \ 430 $(TOOLSDIR)/lib/zalloc.c \ 431 $(TOOLSDIR)/lib/string.c \ 432 $(TOOLSDIR)/lib/ctype.c \ 433 $(TOOLSDIR)/lib/str_error_r.c 434 $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \ 435 CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \ 436 LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \ 437 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \ 438 HOSTPKG_CONFIG='$(PKG_CONFIG)' \ 439 OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ) 440 441# Get Clang's default includes on this system, as opposed to those seen by 442# '--target=bpf'. This fixes "missing" files on some architectures/distros, 443# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc. 444# 445# Use '-idirafter': Don't interfere with include mechanics except where the 446# build would have failed anyways. 447define get_sys_includes 448$(shell $(1) $(2) -v -E - </dev/null 2>&1 \ 449 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ 450$(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}') \ 451$(shell $(1) $(2) -dM -E - </dev/null | grep '__loongarch_grlen ' | awk '{printf("-D__BITS_PER_LONG=%d", $$3)}') \ 452$(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)}') 453endef 454 455# Determine target endianness. 456IS_LITTLE_ENDIAN := $(shell $(CC) -dM -E - </dev/null | \ 457 grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__') 458MENDIAN:=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) 459BPF_TARGET_ENDIAN:=$(if $(IS_LITTLE_ENDIAN),--target=bpfel,--target=bpfeb) 460 461ifneq ($(CROSS_COMPILE),) 462CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%)) 463endif 464 465CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH)) 466BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ 467 -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ 468 -I$(CURDIR)/libarena/include \ 469 -I$(abspath $(OUTPUT)/../usr/include) \ 470 -std=gnu11 \ 471 -fno-strict-aliasing \ 472 -Wno-microsoft-anon-tag \ 473 -fms-extensions \ 474 -Wno-compare-distinct-pointer-types \ 475 -Wno-initializer-overrides \ 476 # 477# TODO: enable me -Wsign-compare 478 479CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) 480 481$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline 482$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline 483 484$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h 485$(OUTPUT)/cgroup_getset_retval_hooks.o: cgroup_getset_retval_hooks.h 486 487# Build BPF object using Clang 488# $1 - input .c file 489# $2 - output .o file 490# $3 - CFLAGS 491# $4 - binary name 492define CLANG_BPF_BUILD_RULE 493 $(call msg,CLNG-BPF,$4,$2) 494 $(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v3 -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 disabled alu32 498define CLANG_NOALU32_BPF_BUILD_RULE 499 $(call msg,CLNG-BPF,$4,$2) 500 $(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v2 -o $2 $(if $(PERMISSIVE),|| \ 501 ($(RM) $2; printf ' %-12s %s\n' 'SKIP-BPF' '$(notdir $2)' 1>&2)) 502endef 503# Similar to CLANG_BPF_BUILD_RULE, but with cpu-v4 504define CLANG_CPUV4_BPF_BUILD_RULE 505 $(call msg,CLNG-BPF,$4,$2) 506 $(Q)$(CLANG) $3 -O2 $(BPF_TARGET_ENDIAN) -c $1 -mcpu=v4 -o $2 $(if $(PERMISSIVE),|| \ 507 ($(RM) $2; printf ' %-12s %s\n' 'SKIP-BPF' '$(notdir $2)' 1>&2)) 508endef 509# Build BPF object using GCC 510define GCC_BPF_BUILD_RULE 511 $(call msg,GCC-BPF,$4,$2) 512 $(Q)$(BPF_GCC) $3 -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes -O2 -c $1 -o $2 $(if $(PERMISSIVE),|| \ 513 ($(RM) $2; printf ' %-12s %s\n' 'SKIP-BPF' '$(notdir $2)' 1>&2)) 514endef 515 516SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c 517 518LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ 519 linked_vars.skel.h linked_maps.skel.h \ 520 test_subskeleton.skel.h test_subskeleton_lib.skel.h \ 521 test_usdt.skel.h tracing_multi.skel.h \ 522 tracing_multi_module.skel.h \ 523 tracing_multi_intersect.skel.h \ 524 tracing_multi_session.skel.h 525 526LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \ 527 core_kern.c core_kern_overflow.c test_ringbuf.c \ 528 test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c \ 529 test_ringbuf_overwrite.c 530 531LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c 532 533# Generate both light skeleton and libbpf skeleton for these 534LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \ 535 kfunc_call_test_subprog.c 536SKEL_BLACKLIST += $$(LSKELS) $$(LSKELS_SIGNED) 537 538test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o 539linked_funcs.skel.h-deps := linked_funcs1.bpf.o linked_funcs2.bpf.o 540linked_vars.skel.h-deps := linked_vars1.bpf.o linked_vars2.bpf.o 541linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o 542# In the subskeleton case, we want the test_subskeleton_lib.subskel.h file 543# but that's created as a side-effect of the skel.h generation. 544test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o 545test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o 546test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o 547xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o 548xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o 549xdp_features.skel.h-deps := xdp_features.bpf.o 550tracing_multi.skel.h-deps := tracing_multi_attach.bpf.o tracing_multi_check.bpf.o 551tracing_multi_module.skel.h-deps := tracing_multi_attach_module.bpf.o tracing_multi_check.bpf.o 552tracing_multi_intersect.skel.h-deps := tracing_multi_intersect_attach.bpf.o tracing_multi_check.bpf.o 553tracing_multi_session.skel.h-deps := tracing_multi_session_attach.bpf.o tracing_multi_check.bpf.o 554 555LINKED_BPF_OBJS := $(foreach skel,$(LINKED_SKELS),$($(skel)-deps)) 556LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(LINKED_BPF_OBJS)) 557 558HEADERS_FOR_BPF_OBJS := $(wildcard $(BPFDIR)/*.bpf.h) \ 559 $(wildcard $(CURDIR)/libarena/include/*.[ch]) \ 560 $(addprefix $(BPFDIR)/, bpf_core_read.h \ 561 bpf_endian.h \ 562 bpf_helpers.h \ 563 bpf_tracing.h) 564 565# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on 566# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES. 567# Parameters: 568# $1 - test runner base binary name (e.g., test_progs) 569# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, bpf_gcc, etc) 570define DEFINE_TEST_RUNNER 571 572LSKEL_SIGN := -S -k $(PRIVATE_KEY) -i $(VERIFICATION_CERT) 573TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2 574TRUNNER_BINARY := $1$(if $2,-)$2 575TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \ 576 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c))) 577TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ 578 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES))) 579TRUNNER_LIB_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ 580 $$(filter %.c,$(TRUNNER_LIB_SOURCES))) 581TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES)) 582TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h 583TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c)) 584TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.bpf.o, $$(TRUNNER_BPF_SRCS)) 585TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \ 586 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\ 587 $$(TRUNNER_BPF_SRCS))) 588TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA)) 589TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS)) 590TRUNNER_BPF_LSKELS_SIGNED := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS_SIGNED)) 591TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS) 592 593# Evaluate rules now with extra TRUNNER_XXX variables above already defined 594$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2)) 595 596endef 597 598# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and 599# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with: 600# $1 - test runner base binary name (e.g., test_progs) 601# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, bpf_gcc, etc) 602define DEFINE_TEST_RUNNER_RULES 603 604# Permissive build behaviour (skip-on-failure compile, partial-link) only 605# applies to test_progs and its flavors; runners that use strong cross-object 606# references (e.g. test_maps) keep strict semantics even when permissive. 607# The check is inlined per-runner so $1 is substituted at $(call) time and 608# the result is baked into each rule's recipe. 609 610ifeq ($($(TRUNNER_OUTPUT)-dir),) 611$(TRUNNER_OUTPUT)-dir := y 612$(TRUNNER_OUTPUT): 613 $$(call msg,MKDIR,,$$@) 614 $(Q)mkdir -p $$@ 615endif 616 617# ensure we set up BPF objects generation rule just once for a given 618# input/output directory combination 619ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),) 620$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y 621$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.bpf.o: \ 622 $(TRUNNER_BPF_PROGS_DIR)/%.c \ 623 $(TRUNNER_BPF_PROGS_DIR)/*.h \ 624 $$(INCLUDE_DIR)/vmlinux.h \ 625 $(HEADERS_FOR_BPF_OBJS) \ 626 | $(TRUNNER_OUTPUT) $$(BPFOBJ) 627 $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \ 628 $(TRUNNER_BPF_CFLAGS) \ 629 $$($$<-CFLAGS) \ 630 $$($$<-$2-CFLAGS),$(TRUNNER_BINARY)) 631 632$(TRUNNER_BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT) 633 $(Q)$(if $(PERMISSIVE),if [ ! -f $$< ]; then \ 634 $$(RM) $$@ $$(@:.skel.h=.subskel.h); \ 635 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 636 exit 0; \ 637 fi;) \ 638 printf ' %-12s %s\n' 'GEN-SKEL' '[$(TRUNNER_BINARY)] $$(notdir $$@)' 1>&2; \ 639 $$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$< && \ 640 $$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o) && \ 641 $$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o) && \ 642 diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) && \ 643 $$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$@ && \ 644 $$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$(@:.skel.h=.subskel.h) $(if $(PERMISSIVE),|| { \ 645 $$(RM) $$@ $$(@:.skel.h=.subskel.h); \ 646 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 647 }) && \ 648 rm -f $$(<:.o=.linked1.o) $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) 649 650$(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT) 651 $(Q)$(if $(PERMISSIVE),if [ ! -f $$< ]; then \ 652 $$(RM) $$@; \ 653 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 654 exit 0; \ 655 fi;) \ 656 printf ' %-12s %s\n' 'GEN-SKEL' '[$(TRUNNER_BINARY)] $$(notdir $$@)' 1>&2; \ 657 $$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$< && \ 658 $$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o) && \ 659 $$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o) && \ 660 diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) && \ 661 $$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@ $(if $(PERMISSIVE),|| { \ 662 $$(RM) $$@; \ 663 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 664 }) && \ 665 rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) 666 667$(TRUNNER_BPF_LSKELS_SIGNED): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT) 668 $(Q)$(if $(PERMISSIVE),if [ ! -f $$< ]; then \ 669 $$(RM) $$@; \ 670 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 671 exit 0; \ 672 fi;) \ 673 printf ' %-12s %s\n' 'GEN-SKEL' '[$(TRUNNER_BINARY) (signed)] $$(notdir $$@)' 1>&2; \ 674 $$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$< && \ 675 $$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o) && \ 676 $$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o) && \ 677 diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) && \ 678 $$(BPFTOOL) gen skeleton $(LSKEL_SIGN) $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@ $(if $(PERMISSIVE),|| { \ 679 $$(RM) $$@; \ 680 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 681 }) && \ 682 rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) 683 684$(LINKED_BPF_OBJS): %: $(TRUNNER_OUTPUT)/% 685 686# .SECONDEXPANSION here allows to correctly expand %-deps variables as prerequisites 687.SECONDEXPANSION: 688$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_OUTPUT)/%: $$$$(%-deps) $(BPFTOOL) | $(TRUNNER_OUTPUT) 689 $(Q)$(if $(PERMISSIVE),for f in $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps)); do \ 690 if [ ! -f $$$$f ]; then \ 691 $$(RM) $$@ $$(@:.skel.h=.subskel.h); \ 692 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 693 exit 0; \ 694 fi; \ 695 done;) \ 696 printf ' %-12s %s\n' 'LINK-BPF' '[$(TRUNNER_BINARY)] $$(notdir $$(@:.skel.h=.bpf.o))' 1>&2; \ 697 $$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps)) && \ 698 $$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o) && \ 699 $$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o) && \ 700 diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o) && \ 701 printf ' %-12s %s\n' 'GEN-SKEL' '[$(TRUNNER_BINARY)] $$(notdir $$@)' 1>&2 && \ 702 $$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@ && \ 703 $$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h) $(if $(PERMISSIVE),|| { \ 704 $$(RM) $$@ $$(@:.skel.h=.subskel.h); \ 705 printf ' %-12s %s\n' 'SKIP-SKEL' '$$(notdir $$@)' 1>&2; \ 706 }) && \ 707 rm -f $$(@:.skel.h=.linked1.o) $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o) 708 709# When the compiler generates a %.d file, only skel basenames (not 710# full paths) are specified as prerequisites for corresponding %.o 711# file. vpath directives below instruct make to search for skel files 712# in TRUNNER_OUTPUT, if they are not present in the working directory. 713vpath %.skel.h $(TRUNNER_OUTPUT) 714vpath %.lskel.h $(TRUNNER_OUTPUT) 715vpath %.subskel.h $(TRUNNER_OUTPUT) 716 717endif 718 719# ensure we set up tests.h header generation rule just once 720ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),) 721$(TRUNNER_TESTS_DIR)-tests-hdr := y 722$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c 723 $$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@) 724 $$(shell (echo '/* Generated header, do not edit */'; \ 725 sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p' \ 726 $(TRUNNER_TESTS_DIR)/*.c | sort ; \ 727 ) > $$@) 728endif 729 730$(TRUNNER_OUTPUT)/resolve_btfids.test.o: $(RESOLVE_BTFIDS) $(TRUNNER_OUTPUT)/btf_data.bpf.o 731$(TRUNNER_OUTPUT)/resolve_btfids.test.o: private TEST_NEEDS_BTFIDS = 1 732 733# compile individual test files 734# Note: we cd into output directory to ensure embedded BPF object is found 735$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ 736 $(TRUNNER_TESTS_DIR)/%.c \ 737 | $(TRUNNER_OUTPUT)/%.test.d 738 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@) 739 $(Q)(cd $$(@D) && $$(CC) -I. $$(CFLAGS) -MMD -MT $$@ -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)) $(if $(filter test_progs%,$1),$(if $(PERMISSIVE),|| \ 740 ($(RM) $$@; printf ' %-12s %s\n' 'SKIP-TEST' '$$(notdir $$@)' 1>&2))) 741 $$(if $$(TEST_NEEDS_BTFIDS), \ 742 $(Q)if [ -f $$@ ]; then \ 743 $(if $(filter 1,$(V)),true,printf ' %-8s%s %s\n' "BTFIDS" " [$(TRUNNER_BINARY)]" "$$(notdir $$@)"); \ 744 $(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@; \ 745 $(RESOLVE_BTFIDS) --patch_btfids $$@.BTF_ids $$@; \ 746 fi) 747 748$(TRUNNER_TEST_OBJS:.o=.d): $(TRUNNER_OUTPUT)/%.test.d: \ 749 $(TRUNNER_TESTS_DIR)/%.c \ 750 $(TRUNNER_EXTRA_HDRS) \ 751 $$(BPFOBJ) | $(TRUNNER_OUTPUT) \ 752 $(TRUNNER_BPF_SKELS) \ 753 $(TRUNNER_BPF_LSKELS) \ 754 $(TRUNNER_BPF_LSKELS_SIGNED) \ 755 $(TRUNNER_BPF_SKELS_LINKED) 756 757ifeq ($(filter clean docs-clean emit_tests,$(MAKECMDGOALS)),) 758include $(wildcard $(TRUNNER_TEST_OBJS:.o=.d)) 759endif 760 761# add per extra obj CFGLAGS definitions 762$(foreach N,$(patsubst $(TRUNNER_OUTPUT)/%.o,%,$(TRUNNER_EXTRA_OBJS)), \ 763 $(eval $(TRUNNER_OUTPUT)/$(N).o: CFLAGS += $($(N).c-CFLAGS))) 764 765$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ 766 %.c \ 767 $(TRUNNER_EXTRA_HDRS) \ 768 $(VERIFY_SIG_HDR) \ 769 $(TRUNNER_TESTS_HDR) \ 770 $$(BPFOBJ) | $(TRUNNER_OUTPUT) 771 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@) 772 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ 773 774$(TRUNNER_LIB_OBJS): $(TRUNNER_OUTPUT)/%.o:$(TOOLSDIR)/lib/%.c 775 $$(call msg,LIB-OBJ,$(TRUNNER_BINARY),$$@) 776 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ 777 778# non-flavored in-srctree builds receive special treatment, in particular, we 779# do not need to copy extra resources (see e.g. test_btf_dump_case()) 780$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT) 781ifneq ($2:$(OUTPUT),:$(shell pwd)) 782 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES)) 783 $(Q)rsync -aq $(if $(PERMISSIVE),--ignore-missing-args) $$^ $(TRUNNER_OUTPUT)/ 784endif 785 786# some X.test.o files have runtime dependencies on Y.bpf.o files 787$(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS) 788 789$(OUTPUT)/$(TRUNNER_BINARY): $(if $(filter test_progs%,$1),$(if $(PERMISSIVE),$$(wildcard $(TRUNNER_TEST_OBJS)),$(TRUNNER_TEST_OBJS)),$(TRUNNER_TEST_OBJS)) \ 790 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \ 791 $(TRUNNER_LIB_OBJS) \ 792 $(TRUNNER_BPFTOOL) \ 793 $(OUTPUT)/veristat \ 794 | $(TRUNNER_BINARY)-extras \ 795 $(if $(filter test_progs%,$1),$(if $(PERMISSIVE),$(TRUNNER_TEST_OBJS))) 796 $$(call msg,BINARY,,$$@) 797 $(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 $$@ 798 $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/$(USE_BOOTSTRAP)bpftool \ 799 $(OUTPUT)/$(if $2,$2/)bpftool 800 801endef 802 803VERIFY_SIG_SETUP := $(CURDIR)/verify_sig_setup.sh 804VERIFY_SIG_HDR := verification_cert.h 805VERIFICATION_CERT := $(BUILD_DIR)/signing_key.der 806PRIVATE_KEY := $(BUILD_DIR)/signing_key.pem 807 808$(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP) 809 $(Q)mkdir -p $(BUILD_DIR) 810 $(Q)$(VERIFY_SIG_SETUP) genkey $(BUILD_DIR) 811 812# Generates a header with C array declaration, containing test_progs_verification_cert bytes 813$(VERIFY_SIG_HDR): $(VERIFICATION_CERT) 814 $(Q)(echo "unsigned char test_progs_verification_cert[] = {"; \ 815 od -v -t 'xC' -w12 $< | sed 's/ \(\S\+\)/ 0x\1,/g;s/^\S\+/ /;$$d'; \ 816 echo "};"; \ 817 echo "unsigned int test_progs_verification_cert_len = $$(wc -c < $<);") > $@ 818 819LIBARENA_MAKE_ARGS = \ 820 BPFTOOL="$(BPFTOOL)" \ 821 INCLUDE_DIR="$(INCLUDE_DIR)" \ 822 LIBBPF_INCLUDE="$(HOST_INCLUDE_DIR)" \ 823 BPFOBJ="$(BPFOBJ)" \ 824 LDLIBS="$(LDLIBS) -lzstd" \ 825 CLANG="$(CLANG)" \ 826 BPF_CFLAGS="$(BPF_CFLAGS) $(CLANG_CFLAGS)" \ 827 BPF_TARGET_ENDIAN="$(BPF_TARGET_ENDIAN)" \ 828 Q="$(Q)" 829 830LIBARENA_BPF_DEPS := $(wildcard libarena/Makefile \ 831 libarena/include/* \ 832 libarena/include/libarena/* \ 833 libarena/src/* \ 834 libarena/selftests/* \ 835 libarena/*.bpf.o) 836 837LIBARENA_SKEL := libarena/libarena.skel.h 838 839$(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS) 840 +$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS) 841 842ifneq ($(CLANG_HAS_ARENA_ASAN),) 843LIBARENA_ASAN_SKEL := libarena/libarena_asan.skel.h 844CFLAGS += -DHAS_BPF_ARENA_ASAN 845 846$(LIBARENA_ASAN_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS) 847 +$(MAKE) -C libarena libarena_asan.skel.h $(LIBARENA_MAKE_ARGS) 848endif 849 850# Define test_progs test runner. 851TRUNNER_TESTS_DIR := prog_tests 852TRUNNER_BPF_PROGS_DIR := progs 853TRUNNER_EXTRA_SOURCES := test_progs.c \ 854 cgroup_helpers.c \ 855 trace_helpers.c \ 856 network_helpers.c \ 857 testing_helpers.c \ 858 btf_helpers.c \ 859 cap_helpers.c \ 860 unpriv_helpers.c \ 861 sysctl_helpers.c \ 862 netlink_helpers.c \ 863 jit_disasm_helpers.c \ 864 io_helpers.c \ 865 test_loader.c \ 866 xsk.c \ 867 disasm.c \ 868 disasm_helpers.c \ 869 json_writer.c \ 870 $(VERIFY_SIG_HDR) \ 871 flow_dissector_load.h \ 872 ip_check_defrag_frags.h \ 873 bpftool_helpers.c \ 874 usdt_1.c usdt_2.c \ 875 $(LIBARENA_SKEL) \ 876 $(LIBARENA_ASAN_SKEL) 877TRUNNER_LIB_SOURCES := find_bit.c 878TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ 879 $(OUTPUT)/liburandom_read.so \ 880 $(OUTPUT)/xdp_synproxy \ 881 $(OUTPUT)/sign-file \ 882 $(OUTPUT)/uprobe_multi \ 883 $(TEST_KMOD_TARGETS) \ 884 ima_setup.sh \ 885 $(VERIFY_SIG_SETUP) \ 886 $(wildcard progs/btf_dump_test_case_*.c) \ 887 $(wildcard progs/*.bpf.o) 888TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE 889TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS 890$(eval $(call DEFINE_TEST_RUNNER,test_progs)) 891 892# Define test_progs-no_alu32 test runner. 893TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE 894TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) 895$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32)) 896 897# Define test_progs-cpuv4 test runner. 898ifneq ($(CLANG_CPUV4),) 899TRUNNER_BPF_BUILD_RULE := CLANG_CPUV4_BPF_BUILD_RULE 900TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS 901$(eval $(call DEFINE_TEST_RUNNER,test_progs,cpuv4)) 902endif 903 904# Define test_progs BPF-GCC-flavored test runner. 905ifneq ($(BPF_GCC),) 906TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE 907TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc,) 908$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc)) 909endif 910 911# Define test_maps test runner. 912TRUNNER_TESTS_DIR := map_tests 913TRUNNER_BPF_PROGS_DIR := progs 914TRUNNER_EXTRA_SOURCES := test_maps.c 915TRUNNER_LIB_SOURCES := 916TRUNNER_EXTRA_FILES := 917TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built) 918TRUNNER_BPF_CFLAGS := 919$(eval $(call DEFINE_TEST_RUNNER,test_maps)) 920 921# Define test_verifier test runner. 922# It is much simpler than test_maps/test_progs and sufficiently different from 923# them (e.g., test.h is using completely pattern), that it's worth just 924# explicitly defining all the rules explicitly. 925verifier/tests.h: verifier/*.c 926 $(shell ( cd verifier/; \ 927 echo '/* Generated header, do not edit */'; \ 928 echo '#ifdef FILL_ARRAY'; \ 929 ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \ 930 echo '#endif' \ 931 ) > verifier/tests.h) 932$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) 933 $(call msg,BINARY,,$@) 934 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 935 936# Include find_bit.c to compile xskxceiver. 937EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c prog_tests/test_xsk.c prog_tests/test_xsk.h 938$(OUTPUT)/xskxceiver: $(EXTRA_SRC) xskxceiver.c xskxceiver.h $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT) 939 $(call msg,BINARY,,$@) 940 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 941 942$(OUTPUT)/xdp_hw_metadata: xdp_hw_metadata.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xdp_hw_metadata.skel.h | $(OUTPUT) 943 $(call msg,BINARY,,$@) 944 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 945 946$(OUTPUT)/xdp_features: xdp_features.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xdp_features.skel.h | $(OUTPUT) 947 $(call msg,BINARY,,$@) 948 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 949 950# Make sure we are able to include and link libbpf against c++. 951CXXFLAGS += $(CFLAGS) 952CXXFLAGS := $(subst -D_GNU_SOURCE=,,$(CXXFLAGS)) 953CXXFLAGS := $(subst -std=gnu11,-std=gnu++11,$(CXXFLAGS)) 954$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ) 955 $(call msg,CXX,,$@) 956 $(Q)$(CXX) $(CXXFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@ 957 958# Benchmark runner 959$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ) 960 $(call msg,CC,,$@) 961 $(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@ $(if $(PERMISSIVE),|| \ 962 ($(RM) $@; printf ' %-12s %s\n' 'SKIP-BENCH' '$(notdir $@)' 1>&2)) 963$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h 964$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h 965$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \ 966 $(OUTPUT)/perfbuf_bench.skel.h 967$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h 968$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h 969$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h 970$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h 971$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h 972$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h 973$(OUTPUT)/bench_local_storage_create.o: $(OUTPUT)/bench_local_storage_create.skel.h 974$(OUTPUT)/bench_bpf_hashmap_lookup.o: $(OUTPUT)/bpf_hashmap_lookup.skel.h 975$(OUTPUT)/bench_htab_mem.o: $(OUTPUT)/htab_mem_bench.skel.h 976$(OUTPUT)/bench_bpf_crypto.o: $(OUTPUT)/crypto_bench.skel.h 977$(OUTPUT)/bench_sockmap.o: $(OUTPUT)/bench_sockmap_prog.skel.h 978$(OUTPUT)/bench_lpm_trie_map.o: $(OUTPUT)/lpm_trie_bench.skel.h $(OUTPUT)/lpm_trie_map.skel.h 979$(OUTPUT)/bench_bpf_nop.o: $(OUTPUT)/bpf_nop_bench.skel.h bench_bpf_timing.h 980$(OUTPUT)/bench_xdp_lb.o: $(OUTPUT)/xdp_lb_bench.skel.h bench_bpf_timing.h 981$(OUTPUT)/bench_bpf_timing.o: bench_bpf_timing.h 982$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) 983$(OUTPUT)/bench: LDLIBS += -lm 984$(OUTPUT)/bench: $(OUTPUT)/bench.o \ 985 $(TESTING_HELPERS) \ 986 $(TRACE_HELPERS) \ 987 $(CGROUP_HELPERS) \ 988 $(OUTPUT)/bench_count.o \ 989 $(OUTPUT)/bench_rename.o \ 990 $(OUTPUT)/bench_trigger.o \ 991 $(OUTPUT)/bench_ringbufs.o \ 992 $(OUTPUT)/bench_bloom_filter_map.o \ 993 $(OUTPUT)/bench_bpf_loop.o \ 994 $(OUTPUT)/bench_strncmp.o \ 995 $(OUTPUT)/bench_bpf_hashmap_full_update.o \ 996 $(OUTPUT)/bench_local_storage.o \ 997 $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o \ 998 $(OUTPUT)/bench_bpf_hashmap_lookup.o \ 999 $(OUTPUT)/bench_local_storage_create.o \ 1000 $(OUTPUT)/bench_htab_mem.o \ 1001 $(OUTPUT)/bench_bpf_crypto.o \ 1002 $(OUTPUT)/bench_sockmap.o \ 1003 $(OUTPUT)/bench_lpm_trie_map.o \ 1004 $(OUTPUT)/bench_bpf_timing.o \ 1005 $(OUTPUT)/bench_bpf_nop.o \ 1006 $(OUTPUT)/bench_xdp_lb.o \ 1007 $(OUTPUT)/usdt_1.o \ 1008 $(OUTPUT)/usdt_2.o \ 1009 # 1010 $(call msg,BINARY,,$@) 1011 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ $(if $(PERMISSIVE),|| \ 1012 ($(RM) $@; printf ' %-12s %s\n' 'SKIP-LINK' '$(notdir $@) (some benchmarks may have been skipped)' 1>&2)) 1013 1014# This works around GCC warning about snprintf truncating strings like: 1015# 1016# char a[PATH_MAX], b[PATH_MAX]; 1017# snprintf(a, "%s/foo", b); // triggers -Wformat-truncation 1018$(OUTPUT)/veristat.o: CFLAGS += -Wno-format-truncation 1019$(OUTPUT)/veristat.o: $(BPFOBJ) 1020$(OUTPUT)/veristat: $(OUTPUT)/veristat.o 1021 $(call msg,BINARY,,$@) 1022 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ 1023 1024# Linking uprobe_multi can fail due to relocation overflows on mips. 1025$(OUTPUT)/uprobe_multi: CFLAGS += $(if $(filter mips, $(ARCH)),-mxgot) 1026$(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld 1027 $(call msg,BINARY,,$@) 1028 $(Q)$(CC) $(CFLAGS) -Wl,-T,uprobe_multi.ld -O0 $(LDFLAGS) \ 1029 $(filter-out %.ld,$^) $(LDLIBS) -o $@ 1030 1031EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ 1032 prog_tests/tests.h map_tests/tests.h verifier/tests.h \ 1033 feature bpftool $(TEST_KMOD_TARGETS) \ 1034 $(addprefix $(OUTPUT)/,*.o *.d *.skel.h *.lskel.h *.subskel.h \ 1035 *.BTF *.BTF_ids *.BTF.base \ 1036 no_alu32 cpuv4 bpf_gcc \ 1037 liburandom_read.so) \ 1038 $(OUTPUT)/FEATURE-DUMP.selftests 1039 1040.PHONY: docs docs-clean 1041 1042# Delete partially updated (corrupted) files on error 1043.DELETE_ON_ERROR: 1044 1045# When permissive, tell rsync to ignore missing source arguments so that 1046# partial builds do not abort installation. 1047ifneq ($(PERMISSIVE),) 1048override define INSTALL_SINGLE_RULE 1049 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)) 1050 $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links --ignore-missing-args $(INSTALL_LIST) $(INSTALL_PATH)/) 1051endef 1052endif 1053 1054DEFAULT_INSTALL_RULE := $(INSTALL_RULE) 1055override define INSTALL_RULE 1056 $(DEFAULT_INSTALL_RULE) 1057 @for DIR in $(TEST_INST_SUBDIRS); do \ 1058 mkdir -p $(INSTALL_PATH)/$$DIR; \ 1059 rsync -a $(if $(PERMISSIVE),--ignore-missing-args) \ 1060 $(OUTPUT)/$$DIR/*.bpf.o \ 1061 $(INSTALL_PATH)/$$DIR; \ 1062 done 1063endef 1064 1065libarena: $(LIBARENA_SKEL) 1066 1067ifneq ($(CLANG_HAS_ARENA_ASAN),) 1068libarena_asan: $(LIBARENA_ASAN_SKEL) 1069endif 1070