Skip to content

Commit 88bda5a

Browse files
msunejoestringer
authored andcommitted
bpf: unify CFLAGS handling in Makefile.clang
This commit unifies CFLAGS deduction into a common Makefile.clang for both BPF unit tests and main 'all' target (used by lint and verifier tests). Signed-off-by: Marc Suñé <marc.sune@isovalent.com>
1 parent 521921f commit 88bda5a

5 files changed

Lines changed: 27 additions & 24 deletions

File tree

bpf/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ compile_commands.json
77
# ignore CLANG intermediate compilation tmp files
88
*.o.tmp
99

10+
# ignore generated CFLAGS
11+
**.clang_flags.mk
12+
1013
# ignore scapy autgen and python cache
1114
tests/scapy/__pycache__/*
1215
tests/output/*

bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ clean:
243243
$(QUIET) $(foreach TARGET,$(SUBDIRS), \
244244
$(MAKE) $(SUBMAKEOPTS) -C $(TARGET) clean;)
245245
$(QUIET)rm -fr *.o *.ll *.i *.s
246+
$(QUIET)rm -f $(CLANG_FLAGS_FILE)
246247
$(QUIET)rm -f $(TARGET)
247248

248-
249249
BEAR_CLI = $(shell which bear 2> /dev/null)
250250
gen_compile_commands:
251251
ifeq (, $(BEAR_CLI))

bpf/Makefile.bpf

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,13 @@
33

44
FLAGS := -I$(ROOT_DIR)/bpf -I$(ROOT_DIR)/bpf/include -O2 -g
55

6-
CLANG_FLAGS := ${FLAGS} --target=bpf -std=gnu99 -nostdinc
7-
# eBPF verifier enforces unaligned access checks where necessary, so don't
8-
# let clang complain too early.
9-
CLANG_FLAGS += -ftrap-function=__undefined_trap
10-
CLANG_FLAGS += -Wall -Wextra -Werror -Wshadow
11-
CLANG_FLAGS += -Wno-address-of-packed-member
12-
CLANG_FLAGS += -Wno-unknown-warning-option
13-
CLANG_FLAGS += -Wno-gnu-variable-sized-type-not-at-end
14-
CLANG_FLAGS += -Wimplicit-int-conversion -Wenum-conversion
15-
CLANG_FLAGS += -Wimplicit-fallthrough
16-
CLANG_FLAGS += -mcpu=v3
6+
# Note, FLAGS needs to be defined before inclusion
7+
include $(ROOT_DIR)/bpf/Makefile.clang
178

189
LIB := $(shell find $(ROOT_DIR)/bpf -name '*.h')
1910
BPF_C := $(patsubst %.o,%.c,$(BPF))
2011
BPF_ASM := $(patsubst %.o,%.s,$(BPF))
2112

22-
CLANG ?= clang
23-
HOST_CC ?= gcc
24-
HOST_STRIP ?= strip
25-
2613
ifeq ($(CROSS_ARCH),arm64)
2714
HOST_CC = aarch64-linux-gnu-gcc
2815
HOST_STRIP = aarch64-linux-gnu-strip

bpf/Makefile.clang

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CLANG ?= clang
2+
HOST_CC ?= gcc
3+
HOST_STRIP ?= strip
4+
5+
# Base CFLAGS (CLANG_FLAGS) are read from pkg/datapath/loader/compile.go
6+
# to keep them synced with the loader
7+
8+
CLANG_FLAGS_FILE := $(shell realpath --relative-to=$(CURDIR) $(ROOT_DIR)/bpf/.clang_flags.mk)
9+
CLANG_CFLAGS_GO := $(ROOT_DIR)/pkg/datapath/loader/tools/clang_cflags.go
10+
LOADER_GO_FILES := $(shell find $(ROOT_DIR)/pkg/datapath/loader -type f -name '*.go')
11+
12+
# Rebuild only when go files are modified in the loader
13+
$(CLANG_FLAGS_FILE): $(LOADER_GO_FILES)
14+
$(QUIET) echo 'CLANG_FLAGS := $(FLAGS) '`$(GO) run $(CLANG_CFLAGS_GO)` > $@
15+
16+
-include $(CLANG_FLAGS_FILE)
17+
18+
# Mimics the mcpu values set by cilium-agent. See GetBPFCPU().
19+
CLANG_FLAGS += -mcpu=v3

bpf/tests/Makefile

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ include ../../Makefile.defs
55

66
MAKEFLAGS += -r
77

8-
CLANG ?= clang
9-
108
FLAGS := -I$(ROOT_DIR)/bpf -I$(ROOT_DIR)/bpf/include -g
119

12-
# Base CFLAGS (CLANG_FLAGS) are read from pkg/datapath/loader/compile.go
13-
# to keep them synced with the loader
14-
CLANG_FLAGS := $(FLAGS) $(shell $(GO) run $(ROOT_DIR)/pkg/datapath/loader/tools/clang_cflags.go)
10+
# Note, FLAGS needs to be defined before inclusion
11+
include ../Makefile.clang
1512

1613
# Create dependency files for each .o file.
1714
CLANG_FLAGS += -MD
1815

19-
# Mimics the mcpu values set by cilium-agent. See GetBPFCPU().
20-
CLANG_FLAGS += -mcpu=v3
21-
2216
# Autogenerated scapy pkts
2317
SCAPY_HDR := $(ROOT_DIR)/bpf/tests/output/gen_pkts.h
2418
SCAPY_HDR_TS := $(ROOT_DIR)/bpf/tests/output/gen_pkts.h.stamp

0 commit comments

Comments
 (0)