-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
460 lines (398 loc) · 11.3 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
SHELL= /bin/bash
PWD= $(shell pwd)
# Normalize ARCH
ifeq ($(shell uname -m), x86_64)
ARCH?= amd64
else ifeq ($(shell uname -m), aarch64)
ARCH?= arm64
endif
ifeq ($(ARCH), amd64)
ARCH_ALT?= x86_64
ARCH_BPF_TARGET?= x86
else ifeq ($(ARCH), arm64)
ARCH_ALT?= aarch64
ARCH_BPF_TARGET?= arm64
else
$(error unsupported architecture $(ARCH))
endif
ifeq ($(V),1)
Q =
msg =
QDOCKER =
else
Q = @
msg = @printf ' %-8s %s%s\n' "$(1)" "$(2)" "$(if $(3), $(3))";
QREDIR = > /dev/null
QDOCKER = -q
endif
define assert_no_syslib
$(if $(SYSLIB), $(error cant build target $@ with SYSLIB))
endef
CFLAGS?= -g -O2 -fno-strict-aliasing -fPIC
ifdef CENTOS7
CFLAGS+= -std=gnu99 -DNO_PUSH_PRAGMA
endif
CPPFLAGS?= -D_GNU_SOURCE
ifndef SYSLIB
CPPFLAGS+= -Iinclude/usr/include
endif
CDIAGFLAGS+= -Wall
CDIAGFLAGS+= -Wextra
CDIAGFLAGS+= -Werror
CDIAGFLAGS+= -Wchar-subscripts
CDIAGFLAGS+= -Wcomment
CDIAGFLAGS+= -Wformat
CDIAGFLAGS+= -Wformat-security
CDIAGFLAGS+= -Wimplicit
ifndef CENTOS7
CDIAGFLAGS+= -Wimplicit-fallthrough
endif
CDIAGFLAGS+= -Winline
CDIAGFLAGS+= -Wmissing-declarations
CDIAGFLAGS+= -Wmissing-prototypes
CDIAGFLAGS+= -Wparentheses
CDIAGFLAGS+= -Wpointer-arith
CDIAGFLAGS+= -Wreturn-type
CDIAGFLAGS+= -Wshadow
CDIAGFLAGS+= -Wsign-compare
CDIAGFLAGS+= -Wstrict-prototypes
CDIAGFLAGS+= -Wswitch
CDIAGFLAGS+= -Wtrigraphs
CDIAGFLAGS+= -Wuninitialized
CDIAGFLAGS+= -Wunused
CDIAGFLAGS+= -Wno-unused-parameter
ifdef CENTOS7
CDIAGFLAGS+= -Wno-inline
endif
CLANG?= clang
BPFTOOL?= bpftool
DOCKER?= docker
# All EEBPF files we track for dependency
EEBPF_FILES:= $(shell find elastic-ebpf)
EEBPF_INCLUDES:= -Ielastic-ebpf/GPL/Events -Ielastic-ebpf/contrib/vmlinux/$(ARCH_ALT)
# LIBQUARK
LIBQUARK_DEPS:= $(wildcard *.h) bpf_prog_skel.h
ifndef SYSLIB
LIBQUARK_DEPS+= $(EEBPF_FILES) include
endif
LIBQUARK_DEPS:= $(filter-out manpages.h, $(LIBQUARK_DEPS))
LIBQUARK_SRCS:= \
bpf_queue.c \
btf.c \
btfhub.c \
compat.c \
kprobe_queue.c \
quark.c \
qutil.c
LIBQUARK_OBJS:= $(patsubst %.c,%.o,$(LIBQUARK_SRCS))
LIBQUARK_STATIC:= libquark.a
LIBQUARK_STATIC_BIG:= libquark_big.a
# If we are _not_ using system libraries, we link everything with
# _big, so that's our target.
ifndef SYSLIB
LIBQUARK_TARGET=$(LIBQUARK_STATIC_BIG)
else
LIBQUARK_TARGET=$(LIBQUARK_STATIC)
EXTRA_LDFLAGS:= -lbpf
endif
# ZLIB
ZLIB_SRC:= zlib
ZLIB_FILES:= $(shell find $(ZLIB_SRC) \(\
-name '*.[ch]' -o \
-name Makefile -o \
-name configure \
\))
ZLIB_STATIC:= $(ZLIB_SRC)/libz.a
# BSD elftoolchain
ELFTOOLCHAIN_SRC:= elftoolchain
ELFTOOLCHAIN_FILES:= $(shell find elftoolchain/{common,libelf} -name '*.[ch]')
ELFTOOLCHAIN_FILES:= $(filter-out elftoolchain/libelftc/elftc_version.c, $(ELFTOOLCHAIN_FILES))
ELFTOOLCHAIN_STATIC:= $(ELFTOOLCHAIN_SRC)/libelf/libelf_pic.a
# Embedded LIBBPF
LIBBPF_SRC:= libbpf/src
LIBBPF_STATIC:= $(LIBBPF_SRC)/libbpf.a
LIBBPF_DEPS:= $(wildcard libbpf/src/*.[ch]) \
$(wildcard libbpf/include/*.[ch])
ifndef SYSLIB
LIBBPF_DEPS+= $(ELFTOOLCHAIN_FILES) \
$(ELFTOOLCHAIN_STATIC) \
$(ZLIB_STATIC)
endif
LIBBPF_EXTRA_CFLAGS:= -DQUARK
LIBBPF_EXTRA_CFLAGS+= -fPIC
LIBBPF_EXTRA_CFLAGS+= -I../../elftoolchain/libelf
LIBBPF_EXTRA_CFLAGS+= -I../../elftoolchain/common
LIBBPF_EXTRA_CFLAGS+= -I../../zlib
ifdef CENTOS7
LIBBPF_EXTRA_CFLAGS+= -Wno-address
endif
# BPFPROG (kernel side)
BPFPROG_OBJ:= bpf_prog.o
BPFPROG_DEPS:= bpf_prog.c
ifndef SYSLIB
BPFPROG_DEPS+= $(LIBBPF_DEPS) $(EEBPF_FILES) include
endif
# SVGS
SVGS:= $(patsubst %.dot,%.svg,$(wildcard *.dot))
# DOCS_HTML, matches docs/%.html
DOCS:= $(wildcard *.[378])
DOCS_HTML:= $(patsubst %.3,docs/%.3.html,$(wildcard *.3))
DOCS_HTML+= $(patsubst %.7,docs/%.7.html,$(wildcard *.7))
DOCS_HTML+= $(patsubst %.8,docs/%.8.html,$(wildcard *.8))
all: $(LIBQUARK_TARGET) \
quark-mon \
quark-btf \
quark-test
$(ZLIB_STATIC): $(ZLIB_FILES)
$(call assert_no_syslib)
@cd zlib && CFLAGS="-O3 -fPIC" ./configure --static $(QREDIR)
@make -C zlib libz.a
$(ELFTOOLCHAIN_STATIC): $(ELFTOOLCHAIN_FILES)
$(call assert_no_syslib)
$(Q)make -C elftoolchain/libelf
$(LIBBPF_STATIC): $(LIBBPF_DEPS)
$(Q)make -C $(LIBBPF_SRC) \
BUILD_STATIC_ONLY=y \
NO_PKG_CONFIG=y \
EXTRA_CFLAGS="$(LIBBPF_EXTRA_CFLAGS)"
$(LIBQUARK_STATIC): $(LIBQUARK_OBJS)
$(call msg,AR,$@)
$(Q)$(AR) rcs $@ $^
$(LIBQUARK_STATIC_BIG): $(LIBQUARK_STATIC) $(LIBBPF_STATIC) $(ELFTOOLCHAIN_STATIC) $(ZLIB_STATIC)
$(call msg,AR,$@)
$(call assert_no_syslib)
$(Q)printf "\
create libquark_big.a\n\
addlib libquark.a\n\
addlib libbpf/src/libbpf.a\n\
addlib elftoolchain/libelf/libelf_pic.a\n\
addlib zlib/libz.a\n\
save\n\
end\n" | ar -M
$(LIBQUARK_OBJS): %.o: %.c $(LIBQUARK_DEPS)
$(call msg,CC,$@)
$(Q)$(CC) -c $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) $<
bpf_prog_skel.h: $(BPFPROG_OBJ)
$(call msg,BPFTOOL,bpf_prog_skel.h)
$(Q)$(BPFTOOL) gen skeleton $(BPFPROG_OBJ) > bpf_prog_skel.h
$(BPFPROG_OBJ): $(BPFPROG_DEPS)
$(call msg,CLANG,bpf_prog.tmp.o)
$(Q)$(CLANG) \
-g -O2 \
-target bpf \
-D__KERNEL__ \
-D__TARGET_ARCH_$(ARCH_BPF_TARGET) \
$(CPPFLAGS) \
$(EEBPF_INCLUDES) \
-c bpf_prog.c \
-o bpf_prog.tmp.o
$(call msg,BPFTOOL,$@)
$(Q)$(BPFTOOL) gen object $@ bpf_prog.tmp.o
$(Q)rm bpf_prog.tmp.o
DOCKER_RUN_ARGS=$(QDOCKER) \
-v $(PWD):$(PWD) \
-w $(PWD) \
-u $(shell id -u):$(shell id -g) \
quark-builder
docker: docker-image clean-all
$(call msg,DOCKER-RUN,Dockerfile)
$(Q)$(DOCKER) run $(DOCKER_RUN_ARGS) /bin/bash -c "make -C $(PWD)"
docker-cross-arm64: clean-all docker-image manpages.h
$(call msg,DOCKER-RUN,Dockerfile)
$(Q)$(DOCKER) run \
-e ARCH=arm64 \
-e CC=aarch64-linux-gnu-gcc \
-e LD=aarch64-linux-gnu-ld \
-e AR=aarch64-linux-gnu-ar \
$(DOCKER_RUN_ARGS) \
/bin/bash -c "make -C $(PWD) all initramfs.gz"
docker-image: clean-all
$(call msg,DOCKER-IMAGE,Dockerfile)
$(Q)$(DOCKER) build \
$(QDOCKER) \
-f Dockerfile \
-t quark-builder \
.
docker-shell:
$(DOCKER) run -it $(DOCKER_RUN_ARGS) /bin/bash
CENTOS7_RUN_ARGS=$(QDOCKER) \
-v $(PWD):$(PWD) \
-w $(PWD) \
-u $(shell id -u):$(shell id -g) \
-e CENTOS7=y \
centos7-quark-builder
centos7: clean-all docker-image centos7-image
# We first make only bpf_prog.o and bpf_prog_skel.h in the
# modern Ubuntu image, we can't make those on centos7
$(DOCKER) run \
$(DOCKER_RUN_ARGS) \
/bin/bash -c "make -C $(PWD) bpf_prog.o bpf_prog_skel.h"
# Now we build the rest of the suite as it won't try to rebuild
# bpf_prog.o and bpf_prog_skel.h
$(DOCKER) run \
$(CENTOS7_RUN_ARGS) \
/bin/bash -c "make -j1 -C $(PWD)"
centos7-image: clean-all
$(call msg,DOCKER-IMAGE,Dockerfile.centos7)
$(DOCKER) build \
$(QDOCKER) \
-f Dockerfile.centos7 \
-t centos7-quark-builder \
.
centos7-shell:
$(DOCKER) run -it $(CENTOS7_RUN_ARGS) /bin/bash
include: $(LIBBPF_DEPS)
$(Q)make -C $(LIBBPF_SRC) \
NO_PKG_CONFIG=y \
install_headers DESTDIR=../../include $(QREDIR)
$(Q)make -C $(LIBBPF_SRC) \
NO_PKG_CONFIG=y \
install_uapi_headers DESTDIR=../../include $(QREDIR)
%.svg: %.dot
$(call msg,DOT,$@)
$(Q)dot -Tsvg $< -o $@
svg: $(SVGS)
test: quark-test
sudo ./quark-test
test-kernel: initramfs.gz
./ktest-all.sh
test-all: test test-kernel
initramfs:
mkdir initramfs
initramfs.gz: init quark-mon-static quark-btf-static quark-test-static initramfs
$(call assert_no_syslib)
cp init initramfs/
cp quark-mon-static initramfs/quark-mon
cp quark-btf-static initramfs/quark-btf
cp quark-test-static initramfs/quark-test
cd initramfs && find . -print0 | cpio -0 -ov --format=newc | gzip -9 > ../$@
init: init.c
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -static -o $@ $^
quark-mon: quark-mon.c manpages.h $(LIBQUARK_TARGET)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
-o $@ $< $(LIBQUARK_TARGET) $(EXTRA_LDFLAGS)
quark-btf: quark-btf.c manpages.h $(LIBQUARK_TARGET)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
-o $@ $< $(LIBQUARK_TARGET) $(EXTRA_LDFLAGS)
quark-test: quark-test.c manpages.h $(LIBQUARK_TARGET)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
-o $@ $< $(LIBQUARK_TARGET) $(EXTRA_LDFLAGS)
quark-mon-static: quark-mon.c manpages.h $(LIBQUARK_STATIC_BIG)
$(call msg,CC,$@)
$(call assert_no_syslib)
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -DNO_PRIVDROP $(CDIAGFLAGS) \
-static -o $@ $< $(LIBQUARK_STATIC_BIG)
quark-btf-static: quark-btf.c manpages.h $(LIBQUARK_STATIC_BIG)
$(call msg,CC,$@)
$(call assert_no_syslib)
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
-static -o $@ $< $(LIBQUARK_STATIC_BIG)
quark-test-static: quark-test.c manpages.h $(LIBQUARK_STATIC_BIG)
$(call msg,CC,$@)
$(call assert_no_syslib)
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
-static -o $@ $< $(LIBQUARK_STATIC_BIG)
man-embedder: man-embedder.c
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -o $@ $^
manpages.h: man-embedder display_man.c quark-btf.8 quark-mon.8 quark-test.8
$(Q)echo '// SPDX-License-Identifier: Apache-2.0' > $@
$(Q)echo '/* Copyright (c) 2024 Elastic NV */' >> $@
$(Q)echo '' >> $@
$(call msg,MAN-EMB,quark-btf.8)
$(Q)./man-embedder quark-btf.8 MAN_QUARK_BTF >> $@
$(call msg,MAN-EMB,quark-mon.8)
$(Q)./man-embedder quark-mon.8 MAN_QUARK_MON >> $@
$(call msg,MAN-EMB,quark-test.8)
$(Q)./man-embedder quark-test.8 MAN_QUARK_TEST >> $@
$(call msg,MAN-EMB,display_man.c)
$(Q)cat display_man.c >> $@
docs/index.html: docs/quark.7.html
$(call msg,CP,index.html)
$(Q)cp $< $@
docs/%.html: %
$(call msg,MANDOC,$<)
$(Q)mandoc -Tlint $< || exit 1
$(Q)mandoc -Thtml -I os=$(shell uname -s) \
-Otoc,style=mandoc.css,man=%N.%S.html $< > $@
$(Q)sed -i 's/fork\.2\.html/https:\/\/linux.die.net\/man\/2\/fork/g' $@
$(Q)sed -i 's/exec\.3\.html/https:\/\/linux.die.net\/man\/3\/exec/g' $@
$(Q)sed -i 's/exit\.3\.html/https:\/\/linux.die.net\/man\/3\/exit/g' $@
docs: $(DOCS_HTML) docs/index.html
btfhub:
$(Q)test $(BTFHUB_ARCHIVE_PATH) || \
(echo "usage: make btfhub.c BTFHUB_ARCHIVE_PATH=/btfhub-archive-path"; exit 1)
$(call msg,SHELL,./genbtf.sh $(BTFHUB_ARCHIVE_PATH))
$(Q)./genbtf.sh $(BTFHUB_ARCHIVE_PATH) > btfhub.new
$(call msg,MV,btfhub.c)
$(Q)mv btfhub.new btfhub.c
eebpf-sync:
$(Q)test $(EEBPF_PATH) || \
(echo "usage: make eebpf-sync EEBPF_PATH=/elastic-ebpf-path"; exit 1)
$(call msg,SHELL,./elastic-ebpf/sync.sh $(EEBPF_PATH))
$(Q)./elastic-ebpf/sync.sh $(EEBPF_PATH)
clean:
$(call msg,CLEAN)
$(Q)rm -f \
*.o \
*.a \
man-embedder \
manpages.h \
quark-mon \
quark-mon-static \
quark-btf \
quark-btf-static \
quark-test \
quark-test-static \
btf_prog_skel.h \
init
$(Q)rm -rf initramfs
clean-all: clean
$(call msg,CLEAN-ALL)
$(Q)rm -f $(SVGS)
$(Q)rm -rf include
$(Q)rm -f initramfs.gz
$(Q)make -C $(LIBBPF_SRC) clean NO_PKG_CONFIG=y
$(Q)make -C $(ELFTOOLCHAIN_SRC)/libelf clean
$(Q)make -C $(ZLIB_SRC) clean || true
$(Q)rm -f $(ZLIB_SRC)/{Makefile,zconf.h,configure.log}
clean-docs:
$(call msg,CLEAN,docs)
$(Q)rm -f docs/*.html
.PHONY: \
all \
btfhub \
centos7 \
centos7-image \
centos7-shell \
clean \
clean-all \
clean-docs \
docs \
docker \
docker-cross-arm64 \
docker-image \
docker-shell \
eebpf-sync \
test \
test-all \
test-kernel
.NOTPARALLEL: \
clean \
clean-all \
centos7 \
centos7-image \
centos7-shell \
docker \
docker-cross-arm64 \
docker-image \
docker-shell \
initramfs.gz \
test \
test-all \
test-kernel
.SUFFIXES: