Skip to content

Commit 6e60860

Browse files
refactor: allow multiple bpf files in Makefile compilation
1 parent d3a3924 commit 6e60860

2 files changed

Lines changed: 76 additions & 50 deletions

File tree

Makefile

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
1-
# Compiler Settings
2-
CC = gcc
3-
# Incluye includes y cflags de pkg-config en CPPFLAGS (no en CFLAGS)
4-
CPPFLAGS += -Iinclude $(shell pkg-config --cflags libbpf libcurl)
5-
CFLAGS += -Wall -MMD -Wunused-but-set-variable
6-
LDLIBS += $(shell pkg-config --libs libbpf libcurl) -ldl
7-
8-
SRCDIR=src
9-
OBJDIR=build
10-
BINDIR=bin
11-
EXE=$(BINDIR)/rootisnaked
12-
TESTS_DIR=tests
13-
INCLUDE_DIR=include
14-
15-
# Source Files
16-
SOURCES=$(wildcard $(SRCDIR)/*.c)
17-
OBJ=$(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SOURCES))
18-
DEPFILES=$(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.d, $(SOURCES))
19-
20-
# eBPF Program
21-
BPF_SOURCE=$(SRCDIR)/kernel/rootisnaked.bpf.c
22-
BPF_OBJ=$(OBJDIR)/rootisnaked.bpf.o
23-
24-
# Compiler settings
25-
LINTER := clang-tidy
26-
FORMATTER := clang-format
27-
28-
# Detect architecture automatically
29-
ARCH := $(shell uname -m)
30-
31-
# Map architecture to BPF target names
1+
# ----------------------------------------------------------------------
2+
# Compiler configuration
3+
# ----------------------------------------------------------------------
4+
CC := gcc # user‑space compiler (can be clang)
5+
CPPFLAGS += -Iinclude $(shell pkg-config --cflags libbpf libcurl)
6+
CFLAGS += -Wall -MMD -Wunused-but-set-variable
7+
LDLIBS += $(shell pkg-config --libs libbpf libcurl) -ldl
8+
9+
# ----------------------------------------------------------------------
10+
# Directory layout
11+
# ----------------------------------------------------------------------
12+
SRCDIR := src
13+
KERNDIR := $(SRCDIR)/kernel # eBPF source directory
14+
OBJDIR := build
15+
BINDIR := bin
16+
INCLUDE_DIR := include
17+
18+
# ----------------------------------------------------------------------
19+
# Architecture detection (for BPF target)
20+
# ----------------------------------------------------------------------
21+
ARCH := $(shell uname -m)
3222
ifeq ($(ARCH),x86_64)
3323
ARCH_BPF = x86
3424
else ifeq ($(ARCH),aarch64)
@@ -39,41 +29,77 @@ else
3929
$(error Unsupported architecture: $(ARCH))
4030
endif
4131

42-
# Targets
43-
all: format $(BPF_OBJ) $(EXE)
32+
# ----------------------------------------------------------------------
33+
# Source / object lists
34+
# ----------------------------------------------------------------------
35+
# User‑space (normal) sources
36+
SOURCES := $(wildcard $(SRCDIR)/*.c)
37+
OBJ := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES))
38+
DEPFILES := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.d,$(SOURCES))
39+
40+
# eBPF sources (any *.bpf.c under $(KERNDIR))
41+
BPF_SRCS := $(wildcard $(KERNDIR)/*.bpf.c)
42+
BPF_OBJS := $(patsubst $(KERNDIR)/%.bpf.c,$(OBJDIR)/%.bpf.o,$(BPF_SRCS))
43+
44+
# Final executable
45+
EXE := $(BINDIR)/rootisnaked
46+
47+
# ----------------------------------------------------------------------
48+
# Tools for lint/format
49+
# ----------------------------------------------------------------------
50+
LINTER := clang-tidy
51+
FORMATTER := clang-format
52+
53+
# ----------------------------------------------------------------------
54+
# Primary targets
55+
# ----------------------------------------------------------------------
56+
.PHONY: all format lint gen-vmlinux clean build
57+
58+
all: $(EXE) # default target builds everything
4459

45-
# Compile Executable (link)
46-
$(EXE): $(OBJ)
60+
# Build the user‑space binary; order‑only prerequisite ensures eBPF objects are fresh
61+
$(EXE): $(OBJ) | $(BPF_OBJS)
4762
@mkdir -p $(BINDIR)
4863
$(CC) $(OBJ) $(LDLIBS) -o $@
4964

50-
# Compile Object Files
65+
# ----------------------------------------------------------------------
66+
# Pattern rule for normal C objects (user‑space)
67+
# ----------------------------------------------------------------------
5168
$(OBJDIR)/%.o: $(SRCDIR)/%.c
5269
@mkdir -p $(OBJDIR)
5370
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
5471

55-
# Compile eBPF Object File
56-
$(BPF_OBJ): $(BPF_SOURCE)
72+
# ----------------------------------------------------------------------
73+
# Pattern rule for eBPF objects (one per *.bpf.c)
74+
# ----------------------------------------------------------------------
75+
$(OBJDIR)/%.bpf.o: $(KERNDIR)/%.bpf.c $(INCLUDE_DIR)/vmlinux.h
5776
@mkdir -p $(OBJDIR)
58-
clang -O2 -g -target bpf -D__TARGET_ARCH_$(ARCH_BPF) -I$(INCLUDE_DIR)/ -c $< -o $@
59-
60-
# Include dependency files
61-
-include $(DEPFILES)
62-
63-
# Run formatter on source and header files
77+
$(CC) -O2 -g -target bpf \
78+
-D__TARGET_ARCH_$(ARCH_BPF) \
79+
-I$(INCLUDE_DIR) \
80+
-c $< -o $@
81+
82+
# ----------------------------------------------------------------------
83+
# Helper / maintenance targets
84+
# ----------------------------------------------------------------------
6485
format:
6586
@$(FORMATTER) -i $(SRCDIR)/*.c $(INCLUDE_DIR)/*.h
6687

67-
# Run linter on source and header files
6888
lint:
6989
@$(LINTER) --config-file=.clang-tidy $(SRCDIR)/*.c $(INCLUDE_DIR)/*.h -- $(CPPFLAGS) $(CFLAGS)
7090

71-
gen-vmlinux: ## Generate vmlinux.h
72-
bpftool btf dump file /sys/kernel/btf/vmlinux format c > include/vmlinux.h
91+
# Generate vmlinux.h (BTF) – optional, but many eBPF programs need it
92+
gen-vmlinux:
93+
@mkdir -p $(INCLUDE_DIR)
94+
bpftool btf dump file /sys/kernel/btf/vmlinux format c > $(INCLUDE_DIR)/vmlinux.h
7395

74-
# Clean up
7596
clean:
7697
rm -rf $(OBJDIR) $(BINDIR)
7798

78-
build: $(BPF_OBJ) $(EXE) ## Build all
99+
# Alias that forces a full rebuild (useful for CI)
100+
build: clean all
79101

102+
# ----------------------------------------------------------------------
103+
# Include generated dependency files for normal C objects
104+
# ----------------------------------------------------------------------
105+
-include $(DEPFILES)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ sudo apt install -y linux-headers-$(uname -r) vim gcc make clang libbpf-dev curl
5555
## Compile and run
5656

5757
```bash
58-
make
58+
make -j$(nproc)
5959
export TELEGRAM_TOKEN="xxxxx:xxxxx"; export DEBUG=false; export CHAT_ID="xxxxx"; sudo -E ./bin/rootisnaked
6060
```
6161

0 commit comments

Comments
 (0)