-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
103 lines (85 loc) · 4.27 KB
/
Copy pathMakefile
File metadata and controls
103 lines (85 loc) · 4.27 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
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
MODULE := github.com/boyvinall/trafficmon
BINARY := trafficmon
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
LDFLAGS := -s -w -X main.version=$(VERSION)
MODULE_DIRS := . cmd/trafficmon receiver cmd/otel-collector
OCB_VERSION := v0.119.0
# arch selecting which compose.yaml service release-linux-docker runs --
# release-linux-$(LINUX_ARCH) must exist there. Override on the command line
# for a different arch, e.g. `make release-linux-docker LINUX_ARCH=arm64`.
LINUX_ARCH ?= amd64
# goreleaser refuses to build at all without a reachable tag. Only
# release.yml's checkout (running against a tag ref) omits this flag; every
# other build, including local ones, passes --snapshot.
GORELEASER_SNAPSHOT_FLAG := $(if $(filter tag,$(GITHUB_REF_TYPE)),,--snapshot)
.PHONY: help all build lint test clean run bpf-generate generate-otel-collector build-otel-collector release-build release-build-linux release-linux-docker
define PROMPT
@echo
@echo "**********************************************************"
@echo "*"
@echo "* $(1)"
@echo "*"
@echo "**********************************************************"
@echo
endef
#: build, lint, and test (default)
all: build build-otel-collector lint test
#: compile for the current platform
build:
$(call PROMPT, $@)
cd cmd/trafficmon && go build -ldflags "$(LDFLAGS)" -o ../../bin/$(BINARY) .
#: build then run under sudo (packet capture needs root)
run: build
$(call PROMPT, $@)
sudo ./bin/$(BINARY)
#: regenerate the ocb collector distribution sources (go.mod/go.sum, components.go, main*.go) from cmd/otel-collector/builder-config.yaml -- these are gitignored, not committed, so lint/test/build-otel-collector need this first on a fresh checkout
generate-otel-collector:
$(call PROMPT, $@)
cd cmd/otel-collector && go run go.opentelemetry.io/collector/cmd/builder@$(OCB_VERSION) --config builder-config.yaml --skip-compilation && go mod tidy
#: regenerate and compile the custom OpenTelemetry Collector distribution
build-otel-collector: generate-otel-collector
$(call PROMPT, $@)
cd cmd/otel-collector && go build -o ../../bin/trafficmon-otelcol .
#: run all linters, across all modules
lint: generate-otel-collector
$(call PROMPT, $@)
for d in $(MODULE_DIRS); do (cd $$d && golangci-lint run ./...) || exit 1; done
#: run all tests, across all modules
test: generate-otel-collector
$(call PROMPT, $@)
for d in $(MODULE_DIRS); do (cd $$d && go test ./...) || exit 1; done
#: cross-build the current platform's release variant via goreleaser (the darwin/windows builds in .goreleaser.yaml); needs goreleaser on PATH
release-build:
$(call PROMPT, $@)
goreleaser build --single-target --clean $(GORELEASER_SNAPSHOT_FLAG)
#: statically-linked linux release variant (netgo + static libpcap) via goreleaser; run inside an Alpine/musl container so the binary carries no glibc symbol-versioning floor -- regenerates eBPF bindings first
release-build-linux: bpf-generate
$(call PROMPT, $@)
goreleaser build --single-target --clean --id trafficmon-linux $(GORELEASER_SNAPSHOT_FLAG)
#: run release-build-linux inside the compose.yaml Alpine/musl container instead of on the host, for LINUX_ARCH (default amd64; e.g. `make release-linux-docker LINUX_ARCH=arm64`) -- each arch is its own compose service, pinned to a matching `platform:`, so the container's gcc always matches the arch it's targeting
release-linux-docker:
$(call PROMPT, $@)
docker compose run --rm release-linux-$(LINUX_ARCH)
#: remove build artifacts
clean:
$(call PROMPT, $@)
rm -rf bin/ dist/ out/
#: regenerate procinfo/bpf's bpf2go bindings + compiled BPF objects (Linux + BTF + clang/llvm/libbpf-dev/bpftool only; not part of `build`/`all` since the toolchain isn't available on a normal macOS dev machine -- CI runs this explicitly before building on the Linux leg)
bpf-generate:
$(call PROMPT, $@)
go generate ./procinfo/bpf/...
#: print Makefile targets and short descriptions
help:
@echo "make targets:\n"
@awk '/^#:[[:space:]]/ { sub(/^#:[[:space:]]*/, ""); desc=$$0; next } \
/^[[:space:]]*$$/ { next } \
/^#/ { next } \
/^[a-zA-Z][a-zA-Z0-9_.-]*:/ { \
if (desc != "") { \
split($$0, a, ":"); \
tgt=a[1]; \
gsub(/^[[:space:]]+|[[:space:]]+$$/, "", tgt); \
printf " %-18s %s\n", tgt, desc; \
desc="" \
} \
}' $(firstword $(MAKEFILE_LIST))