kernel: add missing config symbols for 5.15
[openwrt/openwrt.git] / include / bpf.mk
1 BPF_DEPENDS := @HAS_BPF_TOOLCHAIN
2 LLVM_VER:=
3
4 CLANG_MIN_VER:=12
5
6 ifneq ($(CONFIG_USE_LLVM_HOST),)
7 BPF_TOOLCHAIN_HOST_PATH:=$(call qstrip,$(CONFIG_BPF_TOOLCHAIN_HOST_PATH))
8 ifneq ($(BPF_TOOLCHAIN_HOST_PATH),)
9 BPF_PATH:=$(BPF_TOOLCHAIN_HOST_PATH)/bin:$(PATH)
10 else
11 BPF_PATH:=$(PATH)
12 endif
13 CLANG:=$(firstword $(shell PATH='$(BPF_PATH)' command -v clang clang-13 clang-12 clang-11))
14 LLVM_VER:=$(subst clang,,$(notdir $(CLANG)))
15 endif
16 ifneq ($(CONFIG_USE_LLVM_PREBUILT),)
17 CLANG:=$(TOPDIR)/llvm-bpf/bin/clang
18 endif
19 ifneq ($(CONFIG_USE_LLVM_BUILD),)
20 CLANG:=$(STAGING_DIR_HOST)/llvm-bpf/bin/clang
21 endif
22
23 LLVM_PATH:=$(dir $(CLANG))
24 LLVM_LLC:=$(LLVM_PATH)/llc$(LLVM_VER)
25 LLVM_DIS:=$(LLVM_PATH)/llvm-dis$(LLVM_VER)
26 LLVM_OPT:=$(LLVM_PATH)/opt$(LLVM_VER)
27 LLVM_STRIP:=$(LLVM_PATH)/llvm-strip$(LLVM_VER)
28
29 BPF_KARCH:=mips
30 BPF_ARCH:=mips$(if $(CONFIG_ARCH_64BIT),64)$(if $(CONFIG_BIG_ENDIAN),,el)
31 BPF_TARGET:=bpf$(if $(CONFIG_BIG_ENDIAN),eb,el)
32
33 BPF_HEADERS_DIR:=$(STAGING_DIR)/bpf-headers
34
35 BPF_KERNEL_INCLUDE := \
36 -nostdinc -isystem $(TOOLCHAIN_DIR)/include \
37 -I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include \
38 -I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include/asm/mach-generic \
39 -I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include/generated \
40 -I$(BPF_HEADERS_DIR)/include \
41 -I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include/uapi \
42 -I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include/generated/uapi \
43 -I$(BPF_HEADERS_DIR)/include/uapi \
44 -I$(BPF_HEADERS_DIR)/include/generated/uapi \
45 -I$(BPF_HEADERS_DIR)/tools/lib \
46 -I$(BPF_HEADERS_DIR)/tools/testing/selftests \
47 -I$(BPF_HEADERS_DIR)/samples/bpf \
48 -include linux/kconfig.h -include asm_goto_workaround.h
49
50 BPF_CFLAGS := \
51 $(BPF_KERNEL_INCLUDE) -I$(PKG_BUILD_DIR) \
52 -D__KERNEL__ -D__BPF_TRACING__ -DCONFIG_GENERIC_CSUM \
53 -D__TARGET_ARCH_${BPF_KARCH} \
54 -m$(if $(CONFIG_BIG_ENDIAN),big,little)-endian \
55 -fno-stack-protector -Wall \
56 -Wno-unused-value -Wno-pointer-sign \
57 -Wno-compare-distinct-pointer-types \
58 -Wno-gnu-variable-sized-type-not-at-end \
59 -Wno-address-of-packed-member -Wno-tautological-compare \
60 -Wno-unknown-warning-option \
61 -fno-asynchronous-unwind-tables \
62 -Wno-uninitialized -Wno-unused-variable \
63 -Wno-unused-label \
64 -O2 -emit-llvm -Xclang -disable-llvm-passes
65
66 ifeq ($(DUMP),)
67 CLANG_VER:=$(shell $(CLANG) -dM -E - < /dev/null | grep __clang_major__ | cut -d' ' -f3)
68 CLANG_VER_VALID:=$(shell [ "$(CLANG_VER)" -ge "$(CLANG_MIN_VER)" ] && echo 1 )
69 ifeq ($(CLANG_VER_VALID),)
70 $(error ERROR: LLVM/clang version too old. Minimum required: $(CLANG_MIN_VER), found: $(CLANG_VER))
71 endif
72 endif
73
74 define CompileBPF
75 $(CLANG) -g -target $(BPF_ARCH)-linux-gnu $(BPF_CFLAGS) $(2) \
76 -c $(1) -o $(patsubst %.c,%.bc,$(1))
77 $(LLVM_OPT) -O2 -mtriple=$(BPF_TARGET) < $(patsubst %.c,%.bc,$(1)) > $(patsubst %.c,%.opt,$(1))
78 $(LLVM_DIS) < $(patsubst %.c,%.opt,$(1)) > $(patsubst %.c,%.S,$(1))
79 $(LLVM_LLC) -march=$(BPF_TARGET) -mcpu=v3 -filetype=obj -o $(patsubst %.c,%.o,$(1)) < $(patsubst %.c,%.S,$(1))
80 $(CP) $(patsubst %.c,%.o,$(1)) $(patsubst %.c,%.debug.o,$(1))
81 $(LLVM_STRIP) --strip-debug $(patsubst %.c,%.o,$(1))
82 endef
83