11# Optional build flags (can be overridden via command line)
2- STATIC ?= # Default is not static
3- CORE ?= 1 # Default to CORE=1, can be disabled via CORE=0
2+ STATIC ?=
3+ MODE ?= core
4+
5+ # Validate MODE value
6+ VALID_MODES := core nocore nocoreuni
7+ ifeq ($(filter $(MODE ) ,$(VALID_MODES ) ) ,)
8+ $(error Invalid MODE value: $(MODE). Valid values are: $(VALID_MODES))
9+ endif
410
511# System and architecture detection
612ARCH := $(shell uname -m)
713BPFTOOL := bpftool
814
915# Detect architecture for eBPF
1016ifeq ($(ARCH ) , x86_64)
11- TARGET_ARCH := __TARGET_ARCH_x86
17+ TARGET_ARCH := __TARGET_ARCH_x86
1218else ifeq ($(ARCH), aarch64)
13- TARGET_ARCH := __TARGET_ARCH_arm64
19+ TARGET_ARCH := __TARGET_ARCH_arm64
1420else
15- $(error Unsupported architecture : $(ARCH ) )
21+ $(error Unsupported architecture: $(ARCH))
1622endif
1723
1824# Define compiler and flags
19- CLANG := clang
20- CFLAGS := -Wall -O2 -D$(TARGET_ARCH )
25+ CLANG := clang
26+ CFLAGS := -Wall -O2 -D$(TARGET_ARCH )
2127LDFLAGS := -lbpf -lelf -lz -lzstd -lcap
2228
23- # Conditional flags
24- ifeq ($(CORE ) , 1)
25- CFLAGS += -DCORE
26- BPF_CORE_FLAG := -DCORE
29+ # MODE-based flags
30+ ifeq ($(MODE ) , core)
31+ CFLAGS += -DCORE
32+ BPF_FLAGS := -DCORE
33+ NEEDS_VMLINUX := 1
34+ else ifeq ($(MODE), nocore)
35+ CFLAGS += -DNOCORE
36+ BPF_FLAGS := -DNOCORE
37+ NEEDS_VMLINUX := 0
38+ else ifeq ($(MODE), nocoreuni)
39+ CFLAGS += -DNOCOREUNI
40+ BPF_FLAGS := -DNOCOREUNI
41+ NEEDS_VMLINUX := 0
2742endif
2843
44+ # Static linking flag
2945ifeq ($(STATIC ) , 1)
30- LDFLAGS += -static
46+ LDFLAGS += -static
3147endif
3248
3349# Files
3450LOADER_SRCS := lemon.c cpu_stealer.c mem.c dump.c disk.c net.c capabilities.c
35- LOADER_BIN := lemon.$(ARCH )
36- BPF_SRC := ebpf/mem.ebpf.c
37- BPF_OBJ := ebpf/mem.ebpf.o
38- BPF_SKEL := ebpf/mem.ebpf.skel.h
51+ LOADER_BIN := lemon. $( MODE ) .$(ARCH )
52+ BPF_SRC := ebpf/mem.ebpf.c
53+ BPF_OBJ := ebpf/mem.ebpf.o
54+ BPF_SKEL := ebpf/mem.ebpf.skel.h
3955
40- # Default target: If CORE is enabled, make vmlinux first, then compile eBPF and loader
41- all : clean $(if $(filter 1,$(CORE ) ) , vmlinux) $(BPF_OBJ ) $(LOADER_BIN )
56+ # Default target: If core mode is enabled, make vmlinux first, then compile eBPF and loader
57+ all : clean $(if $(filter 1,$(NEEDS_VMLINUX ) ) , vmlinux) $(BPF_OBJ ) $(LOADER_BIN )
4258
4359# Build eBPF object and generate skeleton
4460$(BPF_OBJ ) : $(BPF_SRC )
45- $(CLANG ) -target bpf -D$(TARGET_ARCH ) $(BPF_CORE_FLAG ) -I/usr/include/linux -I/usr/include/$(ARCH ) -linux-gnu \
46- -Wall -O2 -g -c $< -o $@
61+ $(CLANG ) -target bpf -D$(TARGET_ARCH ) $(BPF_FLAGS ) -I/usr/include/linux -I/usr/include/$(ARCH ) -linux-gnu \
62+ -Wall -O2 -g -c $< -o $@
4763 llvm-strip -g $(BPF_OBJ )
4864 $(BPFTOOL ) gen skeleton $(BPF_OBJ ) > $(BPF_SKEL )
4965
@@ -53,16 +69,46 @@ $(LOADER_BIN): $(LOADER_SRCS)
5369 objcopy --strip-all --keep-symbol=read_kernel_memory $@ $@ _strip
5470 mv $@ _strip $@
5571
56- # Dump vmlinux BTF as C header (only if CORE is enabled)
72+ # Dump vmlinux BTF as C header (only if core mode is enabled)
5773vmlinux :
5874 $(BPFTOOL ) btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
5975
60- # Static target for convenience
61- static :
62- $(MAKE ) STATIC=1
63-
6476# Clean
6577clean :
6678 rm -f $(LOADER_BIN ) $(BPF_OBJ ) $(BPF_SKEL ) vmlinux.h
6779
68- .PHONY : all static clean vmlinux
80+ # Help target
81+ help :
82+ @echo " Available targets:"
83+ @echo " all - Build the project (default, core, dynamic linked)"
84+ @echo " static - Build with static linking"
85+ @echo " clean - Clean build artifacts"
86+ @echo " vmlinux - Generate vmlinux.h (for core mode)"
87+ @echo " help - Show this help"
88+ @echo " "
89+ @echo " Variables:"
90+ @echo " MODE - Build mode (default: core)"
91+ @echo " core: Use eBPF CO-RE with vmlinux.h"
92+ @echo " nocore: Compile in legacy no CO-RE mode using installed Linux headers"
93+ @echo " nocoreuni: Compile in no CO-RE mode using universal header included"
94+ @echo " STATIC - Enable static linking (default: disabled)"
95+ @echo " "
96+ @echo " Usage examples:"
97+ @echo " make # Build with core mode"
98+ @echo " make MODE=nocore # Build in legacy no CO-RE mode using installed Linux headers"
99+ @echo " make MODE=nocoreuni # Build in no CO-RE mode using universal header included"
100+ @echo " make STATIC=1 MODE=nocore # Static build legacy NO CO-RE"
101+
102+ # Show current configuration
103+ config :
104+ @echo " Current configuration:"
105+ @echo " MODE: $( MODE) "
106+ @echo " STATIC: $( if $( STATIC) ,enabled,disabled) "
107+ @echo " ARCH: $( ARCH) "
108+ @echo " TARGET_ARCH: $( TARGET_ARCH) "
109+ @echo " NEEDS_VMLINUX: $( NEEDS_VMLINUX) "
110+ @echo " CFLAGS: $( CFLAGS) "
111+ @echo " BPF_FLAGS: $( BPF_FLAGS) "
112+ @echo " LDFLAGS: $( LDFLAGS) "
113+
114+ .PHONY : all static clean vmlinux help config
0 commit comments