Makefile 3.1 KB

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