From 7889fea1dc4aedc8d1a8f9ce30d8c0d19b05436a Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 31 Oct 2024 08:54:13 -0700 Subject: [PATCH] Update Makefile to only generate what is needed (#1213) * Update Makefile to only generate what is needed Use depinfo files and make targets ensure that generate is only run when needed. * Update generate cmd to use env vars * Ignore the depfiles * Add generate/all target * Pattern match generate targets * Remove go-mod-tidy as dep of generate --- .gitignore | 1 + Makefile | 35 +++++++++++++------ .../instrumentation/bpf/database/sql/probe.go | 2 +- .../segmentio/kafka-go/consumer/probe.go | 2 +- .../segmentio/kafka-go/producer/probe.go | 2 +- .../bpf/go.opentelemetry.io/auto/sdk/probe.go | 2 +- .../otel/traceglobal/probe.go | 2 +- .../google.golang.org/grpc/client/probe.go | 2 +- .../google.golang.org/grpc/server/probe.go | 2 +- .../bpf/net/http/client/probe.go | 4 +-- .../bpf/net/http/server/probe.go | 2 +- 11 files changed, 36 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 64aecb99c..e7c8b6498 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.so *.dylib **/*.o +**/*.go.d .tools/ **/internal/test/e2e/*/main diff --git a/Makefile b/Makefile index 5031f8913..ac18e4137 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,19 @@ # Assume the Makefile is in the root of the repository. REPODIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) +# Used by bpf2go to generate make compatible depinfo files. +export BPF2GO_MAKEBASE := $(REPODIR) + TOOLS_MOD_DIR := ./internal/tools TOOLS = $(CURDIR)/.tools ALL_GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' ! -path './LICENSES/*' -exec dirname {} \; | sort) ALL_GO_MODS := $(shell find . -type f -name 'go.mod' ! -path '$(TOOLS_MOD_DIR)/*' ! -path './LICENSES/*' | sort) -# Build the list of include directories to compile the bpf program -BPF_INCLUDE += -I${REPODIR}/internal/include/libbpf -BPF_INCLUDE += -I${REPODIR}/internal/include +# BPF compile time dependencies. +BPF2GO_CFLAGS += -I${REPODIR}/internal/include/libbpf +BPF2GO_CFLAGS += -I${REPODIR}/internal/include +export BPF2GO_CFLAGS # Go default variables GOCMD?= go @@ -54,14 +58,25 @@ TEST_TARGETS := test-verbose test-ebpf test-ebpf: ARGS = -tags=ebpf_test -run ^TestEBPF # These need to be run with sudo. test-verbose: ARGS = -v $(TEST_TARGETS): test -test: generate $(ALL_GO_MODS:%=test/%) +test: go-mod-tidy generate $(ALL_GO_MODS:%=test/%) test/%/go.mod: @cd $* && $(GOCMD) test $(ARGS) ./... -.PHONY: generate -generate: export CFLAGS := $(BPF_INCLUDE) -generate: go-mod-tidy -generate: +PROBE_ROOT = internal/pkg/instrumentation/bpf/ +PROBE_GEN_GO := $(shell find $(PROBE_ROOT) -type f -name 'bpf_*_bpfe[lb].go') +PROBE_GEN_OBJ := $(PROBE_GEN_GO:.go=.o) + +# Include all depinfo files to ensure we only re-generate when needed. +-include $(shell find $(PROBE_ROOT) -type f -name 'bpf_*_bpfel.go.d') + +.PHONY: generate generate/all +generate: $(PROBE_GEN_GO) +$(PROBE_GEN_GO): %.go: %.o + +$(PROBE_GEN_OBJ): + $(GOCMD) generate ./$(dir $@)... + +generate/all: $(GOCMD) generate ./... .PHONY: docker-generate @@ -85,7 +100,7 @@ go-mod-tidy/%: .PHONY: golangci-lint golangci-lint-fix golangci-lint-fix: ARGS=--fix golangci-lint-fix: golangci-lint -golangci-lint: generate $(ALL_GO_MOD_DIRS:%=golangci-lint/%) +golangci-lint: go-mod-tidy generate $(ALL_GO_MOD_DIRS:%=golangci-lint/%) golangci-lint/%: DIR=$* golangci-lint/%: | $(GOLANGCI_LINT) @echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \ @@ -93,7 +108,7 @@ golangci-lint/%: | $(GOLANGCI_LINT) && $(GOLANGCI_LINT) run --allow-serial-runners --timeout=2m0s $(ARGS) .PHONY: build -build: generate +build: go-mod-tidy generate CGO_ENABLED=$(CGO_ENABLED) $(GOCMD) build -o otel-go-instrumentation ./cli/... .PHONY: docker-build diff --git a/internal/pkg/instrumentation/bpf/database/sql/probe.go b/internal/pkg/instrumentation/bpf/database/sql/probe.go index c70a2796b..7392d5cbd 100644 --- a/internal/pkg/instrumentation/bpf/database/sql/probe.go +++ b/internal/pkg/instrumentation/bpf/database/sql/probe.go @@ -18,7 +18,7 @@ import ( "go.opentelemetry.io/auto/internal/pkg/instrumentation/utils" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c const ( // pkg is the package being instrumented. diff --git a/internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/consumer/probe.go b/internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/consumer/probe.go index fe2998fa7..5b3c89256 100644 --- a/internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/consumer/probe.go +++ b/internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/consumer/probe.go @@ -19,7 +19,7 @@ import ( "go.opentelemetry.io/auto/internal/pkg/structfield" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c const ( // pkg is the package being instrumented. diff --git a/internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/producer/probe.go b/internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/producer/probe.go index 528071bcf..d08b09bdd 100644 --- a/internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/producer/probe.go +++ b/internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/producer/probe.go @@ -18,7 +18,7 @@ import ( "go.opentelemetry.io/auto/internal/pkg/structfield" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c const ( // pkg is the package being instrumented. diff --git a/internal/pkg/instrumentation/bpf/go.opentelemetry.io/auto/sdk/probe.go b/internal/pkg/instrumentation/bpf/go.opentelemetry.io/auto/sdk/probe.go index 1347ea1e8..8d25a372f 100644 --- a/internal/pkg/instrumentation/bpf/go.opentelemetry.io/auto/sdk/probe.go +++ b/internal/pkg/instrumentation/bpf/go.opentelemetry.io/auto/sdk/probe.go @@ -21,7 +21,7 @@ import ( "go.opentelemetry.io/otel/trace" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c // New returns a new [probe.Probe]. func New(logger *slog.Logger) probe.Probe { diff --git a/internal/pkg/instrumentation/bpf/go.opentelemetry.io/otel/traceglobal/probe.go b/internal/pkg/instrumentation/bpf/go.opentelemetry.io/otel/traceglobal/probe.go index 56a90c411..399f16c7a 100644 --- a/internal/pkg/instrumentation/bpf/go.opentelemetry.io/otel/traceglobal/probe.go +++ b/internal/pkg/instrumentation/bpf/go.opentelemetry.io/otel/traceglobal/probe.go @@ -25,7 +25,7 @@ import ( "go.opentelemetry.io/auto/internal/pkg/instrumentation/context" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c const ( // pkg is the package being instrumented. diff --git a/internal/pkg/instrumentation/bpf/google.golang.org/grpc/client/probe.go b/internal/pkg/instrumentation/bpf/google.golang.org/grpc/client/probe.go index 90adee925..e0493509f 100644 --- a/internal/pkg/instrumentation/bpf/google.golang.org/grpc/client/probe.go +++ b/internal/pkg/instrumentation/bpf/google.golang.org/grpc/client/probe.go @@ -22,7 +22,7 @@ import ( "go.opentelemetry.io/auto/internal/pkg/structfield" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c const ( // pkg is the package being instrumented. diff --git a/internal/pkg/instrumentation/bpf/google.golang.org/grpc/server/probe.go b/internal/pkg/instrumentation/bpf/google.golang.org/grpc/server/probe.go index 0fec3fae5..7630ae9dd 100644 --- a/internal/pkg/instrumentation/bpf/google.golang.org/grpc/server/probe.go +++ b/internal/pkg/instrumentation/bpf/google.golang.org/grpc/server/probe.go @@ -24,7 +24,7 @@ import ( "go.opentelemetry.io/auto/internal/pkg/structfield" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c const ( // pkg is the package being instrumented. diff --git a/internal/pkg/instrumentation/bpf/net/http/client/probe.go b/internal/pkg/instrumentation/bpf/net/http/client/probe.go index 7fd04c55d..81df309b3 100644 --- a/internal/pkg/instrumentation/bpf/net/http/client/probe.go +++ b/internal/pkg/instrumentation/bpf/net/http/client/probe.go @@ -24,8 +24,8 @@ import ( "go.opentelemetry.io/auto/internal/pkg/structfield" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf_no_tp ./bpf/probe.bpf.c -- -DNO_HEADER_PROPAGATION +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf_no_tp ./bpf/probe.bpf.c -- -DNO_HEADER_PROPAGATION const ( // pkg is the package being instrumented. diff --git a/internal/pkg/instrumentation/bpf/net/http/server/probe.go b/internal/pkg/instrumentation/bpf/net/http/server/probe.go index 2b305148d..59d46f8d7 100644 --- a/internal/pkg/instrumentation/bpf/net/http/server/probe.go +++ b/internal/pkg/instrumentation/bpf/net/http/server/probe.go @@ -23,7 +23,7 @@ import ( "go.opentelemetry.io/auto/internal/pkg/structfield" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c const ( // pkg is the package being instrumented.