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