1
0

bpf.mk 3.1 KB

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