-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
86 lines (65 loc) · 1.86 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
PROGRAM := yap
# dependencies
git = $(shell command -v git || /bin/false)
bpftool = $(shell command -v bpftool || /bin/false)
# general
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(patsubst %/,%,$(dir $(mkfile_path)))
OUTPUT := $(current_dir)/output
ARCH := $(subst x86_64,x86,$(shell uname -m))
GOARCH := $(subst x86,amd64,$(ARCH))
# ebpf
VMLINUXH := vmlinux.h
BTFFILE := /sys/kernel/btf/vmlinux
CFLAGS ?= -D__TARGET_ARCH_$(ARCH)
# libbpf
BPFTOOL := bpftool
BPFTOOL_GIT := https://github.com/libbpf/bpftool.git
# libbpfgo
LIBBPFGO_GIT := https://github.com/aquasecurity/libbpfgo.git
LIBBPFGO := libbpfgo
# frontend
CGO_CFLAGS = "-I $(current_dir)/$(LIBBPFGO)/output"
CGO_LDFLAGS = "-lelf -lz $(current_dir)/$(LIBBPFGO)/output/libbpf/libbpf.a"
.PHONY: $(PROGRAM)
$(PROGRAM): $(LIBBPFGO) | $(PROGRAM)/bpf
CC=gcc \
CGO_CFLAGS=$(CGO_CFLAGS) \
CGO_LDFLAGS=$(CGO_LDFLAGS) \
GOARCH=$(GOARCH) \
go build -v -o ${PROGRAM} .
.PHONY: $(PROGRAM)/bpf
$(PROGRAM)/bpf: $(VMLINUXH)
clang $(CFLAGS) -g -O2 -c -target bpf \
-o $(OUTPUT)/profile.bpf.o bpf/profile.bpf.c
.PHONY: $(LIBBPFGO)
$(LIBBPFGO):
$(git) clone $(LIBBPFGO_GIT) && \
make -C $(LIBBPFGO) libbpfgo-static
.PHONY: $(BPFTOOL)
$(BPFTOOL):
$(git) clone --recurse-submodules $(BPFTOOL_GIT) && \
make -C $(BPFTOOL)/src && \
sudo make -C $(BPFTOOL)/src install-bin
.PHONY: $(VMLINUXH)
$(VMLINUXH): $(OUTPUT)
ifeq ($(wildcard $(bpftool)),)
@echo "ERROR: could not find bpftool"
@exit 1
endif
@if [ ! -f $(BTFFILE) ]; then \
echo "ERROR: kernel does not seem to support BTF"; \
exit 1; \
fi
@if [ ! -f bpf/$(VMLINUXH) ]; then \
echo "INFO: generating $(VMLINUXH) from $(BTFFILE)"; \
$(bpftool) btf dump file $(BTFFILE) format c > bpf/$(VMLINUXH); \
fi
.PHONY: $(OUTPUT)
$(OUTPUT):
mkdir -p $(OUTPUT)
.PHONY: clean
clean:
rm -rf $(OUTPUT)
rm -rf $(LIBBPFGO)
rm bpf/$(VMLINUXH)