-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
55 lines (42 loc) · 1.96 KB
/
Copy pathMakefile
File metadata and controls
55 lines (42 loc) · 1.96 KB
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
# bisample - eBPF generate / build / test. Run inside the nix devShell.
#
# nix develop --command make generate # generate vmlinux.h + bpf2go (Go types)
# nix develop --command make build # go build
# nix develop --command make test # go test ./...
CURDIR := $(shell pwd)
LIBBPF_CFLAGS := $(shell pkg-config --cflags libbpf)
# Nix's cc-wrapper injects hardening flags and --gcc-toolchain unsupported by
# the BPF target, breaking BPF compilation. Pass the bare clang (no wrapper) to
# bpf2go. The wrapper's nix-support/orig-cc points at the real clang store path.
CLANG_WRAPPER_DIR := $(shell dirname $(shell dirname $(realpath $(shell command -v clang))))
export BPF_CC := $(shell cat $(CLANG_WRAPPER_DIR)/nix-support/orig-cc)/bin/clang
# cflags bpf2go passes to clang; expanded as $BPF_CFLAGS in go:generate.
# -Wno-missing-declarations: avoid -Werror rejecting the forward-declaration-only
# empty declarations in the bpftool-generated vmlinux.h.
export BPF_CFLAGS := -O2 -g -Wall -Werror -Wno-missing-declarations -I$(CURDIR)/bpf $(LIBBPF_CFLAGS)
.PHONY: generate build bin test test-bpf clean
generate: bpf/vmlinux.h
go generate ./internal/bpf/...
# Generate kernel type definitions from the host BTF (for CO-RE).
bpf/vmlinux.h:
bpftool btf dump file /sys/kernel/btf/vmlinux format c > $@
build:
go build ./...
# Build binaries into bin/. Requires bpf2go output (*.o is go:embed-ed), so
# run make generate first.
bin: bin/bisampled bin/bisample
bin/bisampled:
go build -o $@ ./cmd/bisampled
bin/bisample:
go build -o $@ ./cmd/bisample
test:
go test ./...
# Tests that actually load eBPF into the kernel (loader test_run / attach) need
# CAP_BPF. Build the test binary first and run it as root (don't sudo go test
# itself, to keep the devShell PATH/env).
test-bpf:
go test -c -o /tmp/bisample-loader.test ./internal/loader/
sudo /tmp/bisample-loader.test -test.v
clean:
rm -f bpf/vmlinux.h internal/bpf/bisample_bpf*.go internal/bpf/bisample_bpf*.o
rm -rf bin/