Skip to content

Commit

Permalink
Consolidate common parts of Makefiles into common.mk
Browse files Browse the repository at this point in the history
There's a lot of repetition in the Makefiles which is not really
needed (and leads to small deviations over timer). Move all the common
stuff into common.mk and just include that.

Make a few adjustments along the way to make sure things keep working.

Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
  • Loading branch information
tohojo committed Mar 8, 2019
1 parent 5d865fa commit 71ca34a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 390 deletions.
83 changes: 7 additions & 76 deletions basic01-xdp-pass/Makefile
Original file line number Diff line number Diff line change
@@ -1,85 +1,16 @@
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)

TARGETS := xdp_pass
XDP_TARGETS := xdp_pass_kern
USER_TARGETS := xdp_pass
USER_C := xdp_pass_user.c


LLC ?= llc
CLANG ?= clang
CC := gcc

LIBBPF_DIR = ../libbpf/src/
COMMON_DIR = ../common/
COMMON_OBJS := $(COMMON_DIR)/common_params.o

XDP_C = ${TARGETS:=_kern.c}
XDP_OBJ = ${XDP_C:.c=.o}
USER_C = ${TARGETS:=_user.c}
USER_OBJ = ${USER_C:.c=.o}
OBJECT_LIBBPF = $(LIBBPF_DIR)/libbpf.a

COMMON_OBJS := ../common/common_params.o
# Supports adding more COMMON_OBJS
# COMMON_OBJS += ../common/common_bpf.o

# Create expansions for dependencies
COMMON_H := ${COMMON_OBJS:.o=.h}

CFLAGS ?= -I$(LIBBPF_DIR)/root/usr/include/
CFLAGS += -I../headers/
LDFLAGS ?= -L$(LIBBPF_DIR)

LIBS = -lbpf -lelf

all: llvm-check $(TARGETS) $(XDP_OBJ)

.PHONY: clean $(CLANG) $(LLC)

clean:
cd $(LIBBPF_DIR) && $(MAKE) clean;
make -C ../common/ clean
rm -f $(TARGETS)
rm -f $(XDP_OBJ)
rm -f $(USER_OBJ)
rm -f *.ll
rm -f *~

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

$(OBJECT_LIBBPF):
@if [ ! -d $(LIBBPF_DIR) ]; then \
echo "Error: Need libbpf submodule"; \
echo "May need to run git submodule update --init"; \
exit 1; \
else \
cd $(LIBBPF_DIR) && $(MAKE) all; \
mkdir -p root; DESTDIR=root $(MAKE) install_headers; \
fi

# 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/

$(TARGETS): %: %_user.c $(OBJECT_LIBBPF) Makefile $(COMMON_OBJS)
$(CC) -Wall $(CFLAGS) $(LDFLAGS) -o $@ $(COMMON_OBJS) \
$< $(LIBS)

$(XDP_OBJ): %.o: %.c Makefile
$(CLANG) -S \
-target bpf \
-D __BPF_TRACING__ \
$(CFLAGS) \
-Wall \
-Wno-unused-value \
-Wno-pointer-sign \
-Wno-compare-distinct-pointer-types \
-Werror \
-O2 -emit-llvm -c -g $<
$(LLC) -march=bpf -filetype=obj -o $@ ${@:.o=.ll}
include $(COMMON_DIR)/common.mk
80 changes: 2 additions & 78 deletions basic02-prog-by-name/Makefile
Original file line number Diff line number Diff line change
@@ -1,86 +1,10 @@
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)

# Departing from the implicit _user.c scheme
XDP_TARGETS := xdp_prog_kern
USER_TARGETS := xdp_loader

LLC ?= llc
CLANG ?= clang
CC := gcc

LIBBPF_DIR = ../libbpf/src/
COMMON_DIR = ../common/

XDP_C = ${XDP_TARGETS:=.c}
XDP_OBJ = ${XDP_C:.c=.o}
USER_C = ${USER_TARGETS:=.c}
USER_OBJ = ${USER_C:.c=.o}

OBJECT_LIBBPF = $(LIBBPF_DIR)/libbpf.a

COMMON_OBJS := ../common/common_params.o
COMMON_OBJS += ../common/common_user_bpf_xdp.o
# Create expansions for dependencies
COMMON_H := ${COMMON_OBJS:.o=.h}

CFLAGS ?= -I$(LIBBPF_DIR)/root/usr/include/
CFLAGS += -I../headers/
LDFLAGS ?= -L$(LIBBPF_DIR)

LIBS = -lbpf -lelf

all: llvm-check $(USER_TARGETS) $(XDP_OBJ)

.PHONY: clean $(CLANG) $(LLC)

clean:
cd $(LIBBPF_DIR) && $(MAKE) clean;
make -C ../common/ clean
rm -f $(USER_TARGETS)
rm -f $(XDP_OBJ)
rm -f $(USER_OBJ)
rm -f *.ll
rm -f *~

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

$(OBJECT_LIBBPF):
@if [ ! -d $(LIBBPF_DIR) ]; then \
echo "Error: Need libbpf submodule"; \
echo "May need to run: git submodule update --init"; \
exit 1; \
else \
cd $(LIBBPF_DIR) && $(MAKE) all; \
mkdir -p root; DESTDIR=root $(MAKE) install_headers; \
fi

# 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/

$(USER_TARGETS): %: %.c $(OBJECT_LIBBPF) Makefile $(COMMON_OBJS)
$(CC) -Wall $(CFLAGS) $(LDFLAGS) -o $@ $(COMMON_OBJS) \
$< $(LIBS)
include $(COMMON_DIR)/common.mk

$(XDP_OBJ): %.o: %.c Makefile
$(CLANG) -S \
-target bpf \
-D __BPF_TRACING__ \
$(CFLAGS) \
-Wall \
-Wno-unused-value \
-Wno-pointer-sign \
-Wno-compare-distinct-pointer-types \
-Werror \
-O2 -emit-llvm -c -g $<
$(LLC) -march=bpf -filetype=obj -o $@ ${@:.o=.ll}
79 changes: 2 additions & 77 deletions basic03-map-counter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,8 @@
XDP_TARGETS := xdp_prog_kern
USER_TARGETS := xdp_load_and_stats

LLC ?= llc
CLANG ?= clang
CC := gcc

LIBBPF_DIR = ../libbpf/src/
COMMON_DIR = ../common/

XDP_C = ${XDP_TARGETS:=.c}
XDP_OBJ = ${XDP_C:.c=.o}
USER_C = ${USER_TARGETS:=.c}
USER_OBJ = ${USER_C:.c=.o}

OBJECT_LIBBPF = $(LIBBPF_DIR)/libbpf.a

COMMON_OBJS := ../common/common_params.o
COMMON_OBJS += ../common/common_user_bpf_xdp.o
# Create expansions for dependencies
COMMON_H := ${COMMON_OBJS:.o=.h}

CFLAGS ?= -I$(LIBBPF_DIR)/root/usr/include/
CFLAGS += -I../headers/
LDFLAGS ?= -L$(LIBBPF_DIR)

LIBS = -lbpf -lelf

all: llvm-check $(USER_TARGETS) $(XDP_OBJ)

.PHONY: clean $(CLANG) $(LLC)

clean:
cd $(LIBBPF_DIR) && $(MAKE) clean;
make -C ../common/ clean
rm -f $(USER_TARGETS)
rm -f $(XDP_OBJ)
rm -f $(USER_OBJ)
rm -f *.ll
rm -f *~

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

$(OBJECT_LIBBPF):
@if [ ! -d $(LIBBPF_DIR) ]; then \
echo "Error: Need libbpf submodule"; \
echo "May need to run: git submodule update --init"; \
exit 1; \
else \
cd $(LIBBPF_DIR) && $(MAKE) all; \
mkdir -p root; DESTDIR=root $(MAKE) install_headers; \
fi

# 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/

$(USER_TARGETS): %: %.c $(OBJECT_LIBBPF) Makefile $(COMMON_OBJS) common_kern_user.h
$(CC) -Wall $(CFLAGS) $(LDFLAGS) -o $@ $(COMMON_OBJS) \
$< $(LIBS)
include $(COMMON_DIR)/common.mk

$(XDP_OBJ): %.o: %.c common_kern_user.h Makefile
$(CLANG) -S \
-target bpf \
-D __BPF_TRACING__ \
$(CFLAGS) \
-Wall \
-Wno-unused-value \
-Wno-pointer-sign \
-Wno-compare-distinct-pointer-types \
-Werror \
-O2 -emit-llvm -c -g $<
$(LLC) -march=bpf -filetype=obj -o $@ ${@:.o=.ll}
83 changes: 3 additions & 80 deletions basic04-pinning-maps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,9 @@

# Departing from the implicit _user.c scheme
XDP_TARGETS := xdp_prog_kern
USER_TARGETS := xdp_loader
USER_TARGETS += xdp_stats

LLC ?= llc
CLANG ?= clang
CC := gcc
USER_TARGETS := xdp_loader xdp_stats

LIBBPF_DIR = ../libbpf/src/
COMMON_DIR = ../common/

XDP_C = ${XDP_TARGETS:=.c}
XDP_OBJ = ${XDP_C:.c=.o}
USER_C = ${USER_TARGETS:=.c}
USER_OBJ = ${USER_C:.c=.o}

OBJECT_LIBBPF = $(LIBBPF_DIR)/libbpf.a

COMMON_OBJS := ../common/common_params.o
COMMON_OBJS += ../common/common_user_bpf_xdp.o
# Create expansions for dependencies
COMMON_H := ${COMMON_OBJS:.o=.h}

CFLAGS ?= -I$(LIBBPF_DIR)/root/usr/include/
CFLAGS += -I../headers/
LDFLAGS ?= -L$(LIBBPF_DIR)

LIBS = -lbpf -lelf

all: llvm-check $(USER_TARGETS) $(XDP_OBJ)

.PHONY: clean $(CLANG) $(LLC)

clean:
cd $(LIBBPF_DIR) && $(MAKE) clean;
make -C ../common/ clean
rm -f $(USER_TARGETS)
rm -f $(XDP_OBJ)
rm -f $(USER_OBJ)
rm -f *.ll
rm -f *~

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

$(OBJECT_LIBBPF):
@if [ ! -d $(LIBBPF_DIR) ]; then \
echo "Error: Need libbpf submodule"; \
echo "May need to run: git submodule update --init"; \
exit 1; \
else \
cd $(LIBBPF_DIR) && $(MAKE) all; \
mkdir -p root; DESTDIR=root $(MAKE) install_headers; \
fi

# 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/

$(USER_TARGETS): %: %.c $(OBJECT_LIBBPF) Makefile $(COMMON_OBJS) common_kern_user.h
$(CC) -Wall $(CFLAGS) $(LDFLAGS) -o $@ $(COMMON_OBJS) \
$< $(LIBS)

$(XDP_OBJ): %.o: %.c common_kern_user.h Makefile
$(CLANG) -S \
-target bpf \
-D __BPF_TRACING__ \
$(CFLAGS) \
-Wall \
-Wno-unused-value \
-Wno-pointer-sign \
-Wno-compare-distinct-pointer-types \
-Werror \
-O2 -emit-llvm -c -g $<
$(LLC) -march=bpf -filetype=obj -o $@ ${@:.o=.ll}
include $(COMMON_DIR)/common.mk
Loading

0 comments on commit 71ca34a

Please sign in to comment.