Makefile 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Define compiler and options
  2. CC = /usr/bin/clang
  3. LLC = /usr/bin/llc
  4. LLVM_STRIP = /usr/bin/llvm-strip
  5. # Define source and output directories
  6. SRC = ./ebpf/ebpf.c
  7. ifeq ($(debug),1)
  8. DEBUG_MODE=-D_DEBUG_MODE
  9. endif
  10. ARCH ?= $(shell uname -m | sed -e s/x86_64/x86_64/ \
  11. -e s/aarch64.\*/arm64/)
  12. MACHINE_ARCH := $(ARCH)
  13. GO_ARCH := $(MACHINE_ARCH)
  14. ARCH_TAG=-D__TARGET_ARCH_x86 -D__x86_64__
  15. ifeq ($(MACHINE_ARCH),x86_64)
  16. MACHINE_ARCH := amd64
  17. GO_ARCH := amd64
  18. endif
  19. ifeq ($(MACHINE_ARCH),amd64)
  20. MACHINE_ARCH := amd64
  21. GO_ARCH := amd64
  22. endif
  23. ifeq ($(MACHINE_ARCH),aarch64)
  24. MACHINE_ARCH := arm64
  25. GO_ARCH := arm64
  26. ARCH_TAG=-D__TARGET_ARCH_arm64 -D__aarch64__ -D__AARCH64EB__
  27. endif
  28. ifeq ($(MACHINE_ARCH),arm64)
  29. MACHINE_ARCH := arm64
  30. GO_ARCH := arm64
  31. ARCH_TAG=-D__TARGET_ARCH_arm64 -D__aarch64__ -D__AARCH64EB__
  32. endif
  33. OBJ_DIR = ebpf/bin/$(MACHINE_ARCH)
  34. # Define flags
  35. CFLAGS = -I. -Ivmlinux -Iinclude -Iebpf/include -Iebpf/utrace/go/include -Iebpf/utrace/java/include -Iebpf/utrace/netcore/include \
  36. -D__BPF_TRACING__ -D GROUP_LEADER_OFFSET_OVERRIDE=0 -DSTART_BOOTTIME_OFFSET_OVERRIDE=0 \
  37. -DSTART_BOOTTIME_VARNAME=real_start_time -std=gnu99 -Wimplicit-function-declaration \
  38. -ffreestanding -fno-builtin -Wall -Wno-deprecated-declarations \
  39. -Wno-gnu-variable-sized-type-not-at-end -Wno-pragma-once-outside-header \
  40. -Wno-address-of-packed-member -Wno-unknown-warning-option \
  41. -fno-color-diagnostics -fno-unwind-tables -fno-stack-protector \
  42. -Wno-unused-variable \
  43. $(ARCH_TAG) \
  44. $(DEBUG_MODE) \
  45. -fno-asynchronous-unwind-tables -g -O2 -emit-llvm
  46. # Define kernel versions and corresponding object files
  47. KERNEL_VERSIONS = 512
  48. #KERNEL_VERSIONS = 512 506 420 416
  49. define MAP_VERSION
  50. $(if $(filter $1,$(VERSION_512)),v5.12,$(if $(filter $1,$(VERSION_506)),v5.6,$(if $(filter $1,$(VERSION_420)),v4.20,$(if $(filter $1,$(VERSION_416)),v4.16,unknown))))
  51. endef
  52. # 定义版本值
  53. VERSION_512 = 512
  54. VERSION_506 = 506
  55. VERSION_420 = 420
  56. VERSION_416 = 416
  57. # Create necessary directories
  58. $(OBJ_DIR):
  59. mkdir -p $(OBJ_DIR)
  60. all: $(KERNEL_VERSIONS:%=$(OBJ_DIR)/%_$(MACHINE_ARCH).o)
  61. # Rule to compile and process eBPF for each kernel version
  62. $(OBJ_DIR)/%_$(MACHINE_ARCH).o: $(OBJ_DIR)/%_$(MACHINE_ARCH).ll
  63. $(LLC) -march=bpf -filetype=obj -mcpu=v2 -o $(OBJ_DIR)/$(call MAP_VERSION,$*)_$(MACHINE_ARCH).o $<
  64. $(LLVM_STRIP) -g $(OBJ_DIR)/$(call MAP_VERSION,$*)_$(MACHINE_ARCH).o
  65. # Rule to generate LLVM IR from eBPF source
  66. $(OBJ_DIR)/%_$(MACHINE_ARCH).ll: $(SRC) | $(OBJ_DIR)
  67. $(CC) $(CFLAGS) -D__KERNEL_FROM=$* -o $@ -c $(SRC)
  68. clean:
  69. rm -f $(OBJ_DIR)/*_$(MACHINE_ARCH).ll $(OBJ_DIR)/*_$(MACHINE_ARCH).o
  70. .PHONY: all clean