summaryrefslogtreecommitdiff
path: root/src/xdp/common/common.mk
diff options
context:
space:
mode:
Diffstat (limited to 'src/xdp/common/common.mk')
-rw-r--r--src/xdp/common/common.mk103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/xdp/common/common.mk b/src/xdp/common/common.mk
new file mode 100644
index 000000000..89aac68ef
--- /dev/null
+++ b/src/xdp/common/common.mk
@@ -0,0 +1,103 @@
+# Common Makefile parts for BPF-building with libbpf
+# --------------------------------------------------
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+#
+# This file should be included from your Makefile like:
+# COMMON_DIR = ../common/
+# include $(COMMON_DIR)/common.mk
+#
+# It is expected that you define the variables:
+# XDP_TARGETS and USER_TARGETS
+# as a space-separated list
+#
+LLC ?= llc
+CLANG ?= clang
+CC ?= gcc
+
+XDP_C = ${XDP_TARGETS:=.c}
+XDP_OBJ = ${XDP_C:.c=.o}
+USER_C := ${USER_TARGETS:=.c}
+USER_OBJ := ${USER_C:.c=.o}
+
+# Expect this is defined by including Makefile, but define if not
+COMMON_DIR ?= ../common/
+
+COPY_LOADER ?=
+LOADER_DIR ?= $(COMMON_DIR)/../utils
+
+# Extend if including Makefile already added some
+COMMON_OBJS += $(COMMON_DIR)/common_params.o $(COMMON_DIR)/common_user_bpf_xdp.o
+
+# Create expansions for dependencies
+COMMON_H := ${COMMON_OBJS:.o=.h}
+
+EXTRA_DEPS +=
+
+# BPF-prog kern and userspace shares struct via header file:
+KERN_USER_H ?= $(wildcard common_kern_user.h)
+
+CFLAGS ?= -g -I../include/
+BPF_CFLAGS ?= -I../include/
+
+LIBS = -l:libbpf.a -lelf -lz $(USER_LIBS)
+
+all: llvm-check $(USER_TARGETS) $(XDP_OBJ) $(COPY_LOADER) $(COPY_STATS)
+
+.PHONY: clean $(CLANG) $(LLC)
+
+clean:
+ $(MAKE) -C $(COMMON_DIR) clean
+ rm -f $(USER_TARGETS) $(XDP_OBJ) $(USER_OBJ) $(COPY_LOADER) $(COPY_STATS)
+ rm -f *.ll
+ rm -f *~
+
+ifdef COPY_LOADER
+$(COPY_LOADER): $(LOADER_DIR)/${COPY_LOADER:=.c} $(COMMON_H)
+ make -C $(LOADER_DIR) $(COPY_LOADER)
+ cp $(LOADER_DIR)/$(COPY_LOADER) $(COPY_LOADER)
+endif
+
+ifdef COPY_STATS
+$(COPY_STATS): $(LOADER_DIR)/${COPY_STATS:=.c} $(COMMON_H)
+ make -C $(LOADER_DIR) $(COPY_STATS)
+ cp $(LOADER_DIR)/$(COPY_STATS) $(COPY_STATS)
+# Needing xdp_stats imply depending on header files:
+EXTRA_DEPS += $(COMMON_DIR)/xdp_stats_kern.h $(COMMON_DIR)/xdp_stats_kern_user.h
+endif
+
+# For build dependency on this file, if it gets updated
+COMMON_MK = $(COMMON_DIR)/common.mk
+
+llvm-check: $(CLANG) $(LLC)
+ @for TOOL in $^ ; do \
+ if [ ! $$(command -v $${TOOL} 2>/dev/null) ]; then \
+ echo "*** ERROR: Cannot find tool $${TOOL}" ;\
+ exit 1; \
+ else true; fi; \
+ done
+
+# Create dependency: detect if C-file change and touch H-file, to trigger
+# target $(COMMON_OBJS)
+$(COMMON_H): %.h: %.c
+ touch $@
+
+# Detect if any of common obj changed and create dependency on .h-files
+$(COMMON_OBJS): %.o: %.h
+ make -C $(COMMON_DIR)
+
+$(USER_TARGETS): %: %.c Makefile $(COMMON_MK) $(COMMON_OBJS) $(KERN_USER_H) $(EXTRA_DEPS)
+ $(CC) -Wall $(CFLAGS) $(LDFLAGS) -o $@ $(COMMON_OBJS) \
+ $< $(LIBS)
+
+$(XDP_OBJ): %.o: %.c Makefile $(COMMON_MK) $(KERN_USER_H) $(EXTRA_DEPS)
+ $(CLANG) -S \
+ -target bpf \
+ -D __BPF_TRACING__ \
+ $(BPF_CFLAGS) \
+ -Wall \
+ -Wno-unused-value \
+ -Wno-pointer-sign \
+ -Wno-compare-distinct-pointer-types \
+ -Werror \
+ -O2 -emit-llvm -c -g -o ${@:.o=.ll} $<
+ $(LLC) -march=bpf -filetype=obj -o $@ ${@:.o=.ll}