Skip to content

Commit 4c5098f

Browse files
committed
Use kernel build system to build EBPF prog
1 parent 970f8c3 commit 4c5098f

1 file changed

Lines changed: 38 additions & 59 deletions

File tree

ebpf_prog/Makefile

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,52 @@
33
# On Debian based distros we need the following 2 directories.
44
# Otherwise, just use the kernel headers from the kernel sources.
55
#
6-
KERNEL_VER ?= $(shell ls -d /lib/modules/*/source | sort | tail -1 | cut -d/ -f4)
7-
KERNEL_DIR ?= /lib/modules/$(KERNEL_VER)/source
8-
KERNEL_HEADERS ?= /usr/src/linux-headers-$(KERNEL_VER)/
6+
7+
KERNEL_VER = $(shell ls -d /lib/modules/*/source | sort | tail -1 | cut -d/ -f4)
8+
KERNEL_DIR = /lib/modules/$(KERNEL_VER)/source
99
CC = clang
10-
LLC ?= llc
11-
ARCH ?= $(shell uname -m)
10+
LLC = llc
1211

13-
# as in /usr/src/linux-headers-*/arch/
14-
# TODO: extract correctly the archs, and add more if needed.
15-
ifeq ($(ARCH),x86_64)
16-
ARCH := x86
17-
else ifeq ($(ARCH),i686)
18-
ARCH := x86
19-
else ifeq ($(ARCH),armv7l)
20-
ARCH := arm
21-
else ifeq ($(ARCH),armv8l)
22-
ARCH := arm
23-
else ifeq ($(ARCH),aarch64)
24-
ARCH := arm64
25-
else ifeq ($(ARCH),loongarch64)
26-
ARCH := loongarch
27-
else ifeq ($(ARCH),riscv64)
28-
ARCH := riscv
29-
else ifeq ($(ARCH),s390x)
30-
ARCH := s390
31-
endif
12+
# Find all .c files and create a list of corresponding .o targets.
13+
SRC_FILES := $(wildcard *.c)
14+
BPF_TARGETS := $(SRC_FILES:.c=.o)
3215

33-
ifeq ($(ARCH),arm)
34-
# on previous archs, it fails with "SMP not supported on pre-ARMv6"
35-
EXTRA_FLAGS = "-D__LINUX_ARM_ARCH__=7"
36-
endif
16+
# 'always-y' tells kbuild to compile our BPF targets.
17+
always-y := $(BPF_TARGETS)
3718

38-
SRC := $(wildcard *.c)
39-
BIN := $(SRC:.c=.o)
40-
CFLAGS = -I. \
41-
-I$(KERNEL_HEADERS)/arch/$(ARCH)/include/generated/ \
42-
-I$(KERNEL_HEADERS)/include \
43-
-include $(KERNEL_DIR)/include/linux/kconfig.h \
44-
-I$(KERNEL_DIR)/include \
45-
-I$(KERNEL_DIR)/include/uapi \
46-
-I$(KERNEL_DIR)/include/generated/uapi \
47-
-I$(KERNEL_DIR)/arch/$(ARCH)/include \
48-
-I$(KERNEL_DIR)/arch/$(ARCH)/include/generated \
49-
-I$(KERNEL_DIR)/arch/$(ARCH)/include/uapi \
50-
-I$(KERNEL_DIR)/arch/$(ARCH)/include/generated/uapi \
51-
-I$(KERNEL_DIR)/tools/testing/selftests/bpf/ \
52-
-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
53-
-D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \
54-
$(EXTRA_FLAGS) \
55-
-Wunused \
19+
# Add extra compiler flags. We add flags to suppress warnings that
20+
# originate from the kernel headers themselves or are common style issues.
21+
ccflags-y := \
22+
-g -O2 \
5623
-Wno-unused-value \
24+
-Wno-pointer-sign \
25+
-Wno-compare-distinct-pointer-types \
5726
-Wno-gnu-variable-sized-type-not-at-end \
5827
-Wno-address-of-packed-member \
5928
-Wno-tautological-compare \
60-
-Wno-unknown-warning-option \
61-
-fno-stack-protector \
62-
-g -O2 -emit-llvm
63-
64-
all: $(BIN)
65-
66-
%.bc: %.c
67-
$(CC) $(CFLAGS) -c $<
68-
69-
%.o: %.bc
70-
$(LLC) -march=bpf -mcpu=generic -filetype=obj -o $@ $<
29+
-Wno-unknown-warning-option \
30+
-Wno-duplicate-decl-specifier \
31+
-Wno-enum-enum-conversion \
32+
-Wno-missing-prototypes
33+
34+
# List of gcc-specific flags from your log that clang 20 does not understand.
35+
GCC_ONLY_FLAGS := \
36+
-mpreferred-stack-boundary=3 \
37+
-mindirect-branch=thunk-extern \
38+
-mindirect-branch-register \
39+
-mrecord-mcount \
40+
-fno-allow-store-data-races \
41+
-fmin-function-alignment=16 \
42+
-fconserve-stack
43+
44+
# For each of our BPF object files, remove the incompatible gcc flags.
45+
$(foreach bpf_obj,$(BPF_TARGETS),$(eval CFLAGS_REMOVE_$(bpf_obj) = $(GCC_ONLY_FLAGS)))
46+
47+
# Standard targets for an external module Makefile.
48+
all:
49+
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) CC=$(CC)
7150

7251
clean:
73-
rm -f $(BIN)
52+
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
7453

75-
.SUFFIXES:
54+
.PHONY: all clean

0 commit comments

Comments
 (0)