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