forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
176 lines (133 loc) · 3.8 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
BASEDIR = $(abspath ./)
OUTPUT = ./output
SELFTEST = ./selftest
CC = gcc
CLANG = clang
ARCH := $(shell uname -m)
ARCH := $(subst x86_64,amd64,$(ARCH))
BTFFILE = /sys/kernel/btf/vmlinux
BPFTOOL = $(shell which bpftool || /bin/false)
GIT = $(shell which git || /bin/false)
VMLINUXH = $(OUTPUT)/vmlinux.h
# libbpf
LIBBPF_SRC = $(abspath ./libbpf/src)
LIBBPF_OBJ = $(abspath ./$(OUTPUT)/libbpf.a)
LIBBPF_OBJDIR = $(abspath ./$(OUTPUT)/libbpf)
LIBBPF_DESTDIR = $(abspath ./$(OUTPUT))
CFLAGS = -g -O2 -Wall -fpie
LDFLAGS =
# golang
CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))"
CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)"
CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"'
CGO_CFGLAGS_DYN = "-I. -I/usr/include/"
CGO_LDFLAGS_DYN = "-lelf -lz -lbpf"
# default == shared lib from OS package
all: libbpfgo-dynamic
test: libbpfgo-dynamic-test
# libbpfgo test object
libbpfgo-test-bpf-static: libbpfgo-static # needed for serialization
$(MAKE) -C $(SELFTEST)/build
libbpfgo-test-bpf-dynamic: libbpfgo-dynamic # needed for serialization
$(MAKE) -C $(SELFTEST)/build
libbpfgo-test-bpf-clean:
$(MAKE) -C $(SELFTEST)/build clean
# libbpf: shared
libbpfgo-dynamic: $(OUTPUT)/libbpf
CC=$(CLANG) \
CGO_CFLAGS=$(CGO_CFLAGS_DYN) \
CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \
go build .
libbpfgo-dynamic-test: libbpfgo-test-bpf-dynamic
CC=$(CLANG) \
CGO_CFLAGS=$(CGO_CFLAGS_DYN) \
CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \
sudo -E go test .
# libbpf: static
libbpfgo-static: $(VMLINUXH) | $(LIBBPF_OBJ)
CC=$(CLANG) \
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \
GOOS=linux GOARCH=$(ARCH) \
go build \
-tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \
.
libbpfgo-static-test: libbpfgo-test-bpf-static
sudo env PATH=$(PATH) \
CC=$(CLANG) \
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \
GOOS=linux GOARCH=$(ARCH) \
go test \
-v -tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \
.
# vmlinux header file
.PHONY: vmlinuxh
vmlinuxh: $(VMLINUXH)
$(VMLINUXH): $(OUTPUT)
@if [ ! -f $(BTFFILE) ]; then \
echo "ERROR: kernel does not seem to support BTF"; \
exit 1; \
fi
@if [ ! -f $(VMLINUXH) ]; then \
if [ ! $(BPFTOOL) ]; then \
echo "ERROR: could not find bpftool"; \
exit 1; \
fi; \
echo "INFO: generating $(VMLINUXH) from $(BTFFILE)"; \
$(BPFTOOL) btf dump file $(BTFFILE) format c > $(VMLINUXH); \
fi
# static libbpf generation for the git submodule
.PHONY: libbpf-static
libbpf-static: $(LIBBPF_OBJ)
$(LIBBPF_OBJ): $(LIBBPF_SRC) $(wildcard $(LIBBPF_SRC)/*.[ch]) | $(OUTPUT)/libbpf
CC="$(CC)" CFLAGS="$(CFLAGS)" LD_FLAGS="$(LDFLAGS)" \
$(MAKE) -C $(LIBBPF_SRC) \
BUILD_STATIC_ONLY=1 \
OBJDIR=$(LIBBPF_OBJDIR) \
DESTDIR=$(LIBBPF_DESTDIR) \
INCLUDEDIR= LIBDIR= UAPIDIR= install
$(LIBBPF_SRC):
ifeq ($(wildcard $@), )
echo "INFO: updating submodule 'libbpf'"
$(GIT) submodule update --init --recursive
endif
# selftests
SELFTESTS = $(shell find $(SELFTEST) -mindepth 1 -maxdepth 1 -type d ! -name 'common' ! -name 'build')
define FOREACH
SELFTESTERR=0; \
for DIR in $(SELFTESTS); do \
echo "INFO: entering $$DIR..."; \
$(MAKE) -j8 -C $$DIR $(1) || SELFTESTERR=1; \
done; \
if [ $$SELFTESTERR -eq 1 ]; then \
exit 1; \
fi
endef
.PHONY: selftest
.PHONY: selftest-static
.PHONY: selftest-dynamic
.PHONY: selftest-run
.PHONY: selftest-static-run
.PHONY: selftest-dynamic-run
.PHONY: selftest-clean
selftest: selftest-static
selftest-static:
$(call FOREACH, main-static)
selftest-dynamic:
$(call FOREACH, main-dynamic)
selftest-run: selftest-static-run
selftest-static-run:
$(call FOREACH, run-static)
selftest-dynamic-run:
$(call FOREACH, run-dynamic)
selftest-clean:
$(call FOREACH, clean)
# output
$(OUTPUT):
mkdir -p $(OUTPUT)
$(OUTPUT)/libbpf:
mkdir -p $(OUTPUT)/libbpf
# cleanup
clean: selftest-clean libbpfgo-test-bpf-clean
rm -rf $(OUTPUT)