-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
139 lines (100 loc) · 2.85 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Copyright 2021 Authors of kubearmor/libbpf
# SPDX-License-Identifier: Apache-2.0
#
# This Makefile was inspired by three libbpf related Makefiles:
#
# libbpf-bootstrap
# https://github.com/libbpf/libbpf-bootstrap/blob/a4169a2108fe1ab812e33471e0fcab214fde73a9/examples/c/Makefile
#
# libbpfgo
# https://github.com/aquasecurity/libbpfgo/blob/a68363581fcf8cb11039cec6f9d34338ef4393a9/Makefile
#
# linux kernel
# https://github.com/torvalds/linux/blob/d5ad8ec3cfb56a017de6a784835666475b4be349/tools/testing/selftests/bpf/Makefile
#
ifeq ($(V), 1)
Q =
else
Q = @
endif
INCDIR = ./include
.PHONY: all
all: libbpf vmlinuxh bpfobj tests
# C libbpf
CC = gcc
CFLAGS = -g -O2 -Werror -Wall -fpie
GIT = $(shell which git)
LIBBPFDIR = $(abspath ./libbpf)
LIBBPFOBJ = $(INCDIR)/libbpf.a
LIBBPFSRCDIR = $(LIBBPFDIR)/src
LIBBPFSRCFILES = $(wildcard $(LIBBPFSRCDIR)/*.[ch])
LIBBPFDESTDIR = $(abspath $(INCDIR))
LIBBPFOBJDIR = $(LIBBPFDESTDIR)/libbpf
.PHONY: libbpf
libbpf: $(LIBBPFOBJ)
$(LIBBPFOBJ): $(LIBBPFSRCDIR) $(LIBBPFSRCFILES) | $(INCDIR)
$(info INFO: compiling $@)
$(Q)CC="$(CC)" CFLAGS="$(CFLAGS)" \
$(MAKE) -C $(LIBBPFSRCDIR) \
BUILD_STATIC_ONLY=1 \
OBJDIR=$(LIBBPFOBJDIR) \
DESTDIR=$(LIBBPFDESTDIR) \
INCLUDEDIR= LIBDIR= UAPIDIR= \
install
$(LIBBPFSRCDIR):
ifeq ($(wildcard $@), )
$(info INFO: updating submodule 'libbpf')
$(Q)$(GIT) submodule update --init --recursive
endif
# vmlinux header file
BPFTOOL = $(shell which bpftool)
BTFFILE = /sys/kernel/btf/vmlinux
VMLINUXH = $(INCDIR)/vmlinux.h
.PHONY: vmlinuxh
vmlinuxh: $(VMLINUXH)
$(VMLINUXH): $(BTFFILE) | $(INCDIR)
$(info INFO: generating $@ from $<)
$(Q)$(BPFTOOL) btf dump file $< format c > $@;
$(BTFFILE):
ifeq ($(wildcard $@), )
$(error ERROR: kernel does not seem to support BTF)
endif
# bpf objects
CLANG = clang
CLANGFLAGS = -g -O2 -c -target bpf -MMD -MP
CLANGINC = $(INCDIR)
BPFOBJDIR = $(abspath ./tests)
BPFS_C = $(wildcard $(BPFOBJDIR)/*.bpf.c)
BPFS_O = $(BPFS_C:.c=.o)
BPFS_D = $(patsubst %.o,%.d,$(BPFS_O))
.PHONY: bpfobj
bpfobj: libbpf vmlinuxh $(BPFS_O)
-include $(BPFS_D)
$(BPFOBJDIR)/%.o: $(BPFOBJDIR)/%.c
$(info INFO: compiling bpf object $@)
$(Q)$(CLANG) $(CLANGFLAGS) -I $(CLANGINC) -o $@ $<
# tests
TESTSDIR = $(abspath ./tests)
TESTS_GO = $(wildcard $(TESTSDIR)/*.go)
TESTS = $(TESTS_GO:.go=)
CGOLDFLAGS = $(abspath $(LIBBPFOBJ))
CGOCFLAGS = $(abspath $(INCDIR))
.PHONY: tests
tests: bpfobj $(TESTS)
$(TESTS): % : %.go | bpfobj
$(info INFO: compiling test $@)
$(Q)CGO_LDFLAGS=$(CGOLDFLAGS) CGO_CFLAGS="-I $(CGOCFLAGS)" \
go build -o $@ $^
# run tests
.PHONY: run-tests
run-tests: $(TESTS)
$(Q)for test in $^; do \
echo -e "\nINFO: running test $${test}"; \
cd $(TESTSDIR); sudo $${test}; \
done
# intermediary output
$(INCDIR):
$(Q)mkdir -p $@
# cleanup
clean:
$(Q)rm -rf $(INCDIR) $(BPFS_O) $(BPFS_D) $(TESTS)