Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: net: extract BPF building logic from the Makefile

The BPF sample building code looks a little bit spaghetti-ish
so move it out to its own Makefile snippet. Similar in the spirit
to how we include lib.mk. libynl will soon get a similar snippet.

There is a small change hiding in the move, the relative
paths (../../.., ../.. etc) are replaced with variables
from lib.mk such as top_srcdir and selfdir.

Link: https://lore.kernel.org/r/20240423183542.3807234-3-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+54 -52
+1 -52
tools/testing/selftests/net/Makefile
··· 108 108 $(OUTPUT)/bind_bhash: LDLIBS += -lpthread 109 109 $(OUTPUT)/io_uring_zerocopy_tx: CFLAGS += -I../../../include/ 110 110 111 - # Rules to generate bpf objs 112 - CLANG ?= clang 113 - SCRATCH_DIR := $(OUTPUT)/tools 114 - BUILD_DIR := $(SCRATCH_DIR)/build 115 - BPFDIR := $(abspath ../../../lib/bpf) 116 - APIDIR := $(abspath ../../../include/uapi) 117 - 118 - CCINCLUDE += -I../bpf 119 - CCINCLUDE += -I../../../../usr/include/ 120 - CCINCLUDE += -I$(SCRATCH_DIR)/include 121 - 122 - BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a 123 - 124 - MAKE_DIRS := $(BUILD_DIR)/libbpf 125 - $(MAKE_DIRS): 126 - $(call msg,MKDIR,,$@) 127 - $(Q)mkdir -p $@ 128 - 129 - # Get Clang's default includes on this system, as opposed to those seen by 130 - # '--target=bpf'. This fixes "missing" files on some architectures/distros, 131 - # such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc. 132 - # 133 - # Use '-idirafter': Don't interfere with include mechanics except where the 134 - # build would have failed anyways. 135 - define get_sys_includes 136 - $(shell $(1) $(2) -v -E - </dev/null 2>&1 \ 137 - | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ 138 - $(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}') 139 - endef 140 - 141 - ifneq ($(CROSS_COMPILE),) 142 - CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%)) 143 - endif 144 - 145 - CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH)) 146 - 147 - BPF_PROG_OBJS := $(patsubst %.c,$(OUTPUT)/%.o,$(wildcard *.bpf.c)) 148 - 149 - $(BPF_PROG_OBJS): $(OUTPUT)/%.o : %.c $(BPFOBJ) | $(MAKE_DIRS) 150 - $(call msg,BPF_PROG,,$@) 151 - $(Q)$(CLANG) -O2 -g --target=bpf $(CCINCLUDE) $(CLANG_SYS_INCLUDES) \ 152 - -c $< -o $@ 153 - 154 - $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ 155 - $(APIDIR)/linux/bpf.h \ 156 - | $(BUILD_DIR)/libbpf 157 - $(call msg,MAKE,,$@) 158 - $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \ 159 - EXTRA_CFLAGS='-g -O0' \ 160 - DESTDIR=$(SCRATCH_DIR) prefix= all install_headers 161 - 162 - EXTRA_CLEAN := $(SCRATCH_DIR) 111 + include bpf.mk
+53
tools/testing/selftests/net/bpf.mk
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + # Rules to generate bpf objs 3 + CLANG ?= clang 4 + SCRATCH_DIR := $(OUTPUT)/tools 5 + BUILD_DIR := $(SCRATCH_DIR)/build 6 + BPFDIR := $(top_srcdir)/tools/lib/bpf 7 + APIDIR := $(top_srcdir)/tools/include/uapi 8 + 9 + CCINCLUDE += -I$(selfdir)/bpf 10 + CCINCLUDE += -I$(top_srcdir)/usr/include/ 11 + CCINCLUDE += -I$(SCRATCH_DIR)/include 12 + 13 + BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a 14 + 15 + MAKE_DIRS := $(BUILD_DIR)/libbpf 16 + $(MAKE_DIRS): 17 + $(call msg,MKDIR,,$@) 18 + $(Q)mkdir -p $@ 19 + 20 + # Get Clang's default includes on this system, as opposed to those seen by 21 + # '--target=bpf'. This fixes "missing" files on some architectures/distros, 22 + # such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc. 23 + # 24 + # Use '-idirafter': Don't interfere with include mechanics except where the 25 + # build would have failed anyways. 26 + define get_sys_includes 27 + $(shell $(1) $(2) -v -E - </dev/null 2>&1 \ 28 + | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ 29 + $(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}') 30 + endef 31 + 32 + ifneq ($(CROSS_COMPILE),) 33 + CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%)) 34 + endif 35 + 36 + CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH)) 37 + 38 + BPF_PROG_OBJS := $(patsubst %.c,$(OUTPUT)/%.o,$(wildcard *.bpf.c)) 39 + 40 + $(BPF_PROG_OBJS): $(OUTPUT)/%.o : %.c $(BPFOBJ) | $(MAKE_DIRS) 41 + $(call msg,BPF_PROG,,$@) 42 + $(Q)$(CLANG) -O2 -g --target=bpf $(CCINCLUDE) $(CLANG_SYS_INCLUDES) \ 43 + -c $< -o $@ 44 + 45 + $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ 46 + $(APIDIR)/linux/bpf.h \ 47 + | $(BUILD_DIR)/libbpf 48 + $(call msg,MAKE,,$@) 49 + $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \ 50 + EXTRA_CFLAGS='-g -O0' \ 51 + DESTDIR=$(SCRATCH_DIR) prefix= all install_headers 52 + 53 + EXTRA_CLEAN += $(SCRATCH_DIR)