blob: 4fa423ca02d2c1303fcb81556aafef417c1896ce [file] [log] [blame]
Joey Armstronga6af1522023-01-17 16:06:16 -05001# Copyright The OpenTelemetry Authors
Abhay Kumara61c5222025-11-10 07:32:50 +00002# SPDX-License-Identifier: Apache-2.0
Joey Armstronga6af1522023-01-17 16:06:16 -05003
Joey Armstronga6af1522023-01-17 16:06:16 -05004TOOLS_MOD_DIR := ./internal/tools
5
Joey Armstronga6af1522023-01-17 16:06:16 -05006ALL_DOCS := $(shell find . -name '*.md' -type f | sort)
Abhay Kumara61c5222025-11-10 07:32:50 +00007ALL_GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort)
8OTEL_GO_MOD_DIRS := $(filter-out $(TOOLS_MOD_DIR), $(ALL_GO_MOD_DIRS))
9ALL_COVERAGE_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | grep -E -v '^./example|^$(TOOLS_MOD_DIR)' | sort)
Joey Armstronga6af1522023-01-17 16:06:16 -050010
Abhay Kumara61c5222025-11-10 07:32:50 +000011GO = go
12TIMEOUT = 60
Joey Armstronga6af1522023-01-17 16:06:16 -050013
Abhay Kumara61c5222025-11-10 07:32:50 +000014# User to run as in docker images.
15DOCKER_USER=$(shell id -u):$(shell id -g)
16DEPENDENCIES_DOCKERFILE=./dependencies.Dockerfile
Joey Armstronga6af1522023-01-17 16:06:16 -050017
18.DEFAULT_GOAL := precommit
19
Abhay Kumara61c5222025-11-10 07:32:50 +000020.PHONY: precommit ci
21precommit: generate toolchain-check license-check misspell go-mod-tidy golangci-lint-fix verify-readmes verify-mods test-default
22ci: generate toolchain-check license-check lint vanity-import-check verify-readmes verify-mods build test-default check-clean-work-tree test-coverage
Joey Armstronga6af1522023-01-17 16:06:16 -050023
Abhay Kumara61c5222025-11-10 07:32:50 +000024# Tools
Joey Armstronga6af1522023-01-17 16:06:16 -050025
Abhay Kumara61c5222025-11-10 07:32:50 +000026TOOLS = $(CURDIR)/.tools
27
28$(TOOLS):
29 @mkdir -p $@
30$(TOOLS)/%: $(TOOLS_MOD_DIR)/go.mod | $(TOOLS)
Joey Armstronga6af1522023-01-17 16:06:16 -050031 cd $(TOOLS_MOD_DIR) && \
Abhay Kumara61c5222025-11-10 07:32:50 +000032 $(GO) build -o $@ $(PACKAGE)
Joey Armstronga6af1522023-01-17 16:06:16 -050033
Abhay Kumara61c5222025-11-10 07:32:50 +000034MULTIMOD = $(TOOLS)/multimod
35$(TOOLS)/multimod: PACKAGE=go.opentelemetry.io/build-tools/multimod
Joey Armstronga6af1522023-01-17 16:06:16 -050036
Abhay Kumara61c5222025-11-10 07:32:50 +000037SEMCONVGEN = $(TOOLS)/semconvgen
38$(TOOLS)/semconvgen: PACKAGE=go.opentelemetry.io/build-tools/semconvgen
Joey Armstronga6af1522023-01-17 16:06:16 -050039
Abhay Kumara61c5222025-11-10 07:32:50 +000040CROSSLINK = $(TOOLS)/crosslink
41$(TOOLS)/crosslink: PACKAGE=go.opentelemetry.io/build-tools/crosslink
Joey Armstronga6af1522023-01-17 16:06:16 -050042
Abhay Kumara61c5222025-11-10 07:32:50 +000043SEMCONVKIT = $(TOOLS)/semconvkit
44$(TOOLS)/semconvkit: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/semconvkit
Joey Armstronga6af1522023-01-17 16:06:16 -050045
Abhay Kumara61c5222025-11-10 07:32:50 +000046VERIFYREADMES = $(TOOLS)/verifyreadmes
47$(TOOLS)/verifyreadmes: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/verifyreadmes
48
49GOLANGCI_LINT = $(TOOLS)/golangci-lint
50$(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/v2/cmd/golangci-lint
51
52MISSPELL = $(TOOLS)/misspell
53$(TOOLS)/misspell: PACKAGE=github.com/client9/misspell/cmd/misspell
54
55GOCOVMERGE = $(TOOLS)/gocovmerge
56$(TOOLS)/gocovmerge: PACKAGE=github.com/wadey/gocovmerge
57
58STRINGER = $(TOOLS)/stringer
59$(TOOLS)/stringer: PACKAGE=golang.org/x/tools/cmd/stringer
60
61PORTO = $(TOOLS)/porto
62$(TOOLS)/porto: PACKAGE=github.com/jcchavezs/porto/cmd/porto
63
64GOTMPL = $(TOOLS)/gotmpl
65$(GOTMPL): PACKAGE=go.opentelemetry.io/build-tools/gotmpl
66
67GORELEASE = $(TOOLS)/gorelease
68$(GORELEASE): PACKAGE=golang.org/x/exp/cmd/gorelease
69
70GOVULNCHECK = $(TOOLS)/govulncheck
71$(TOOLS)/govulncheck: PACKAGE=golang.org/x/vuln/cmd/govulncheck
72
73.PHONY: tools
74tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(SEMCONVGEN) $(VERIFYREADMES) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)
75
76# Virtualized python tools via docker
77
78# The directory where the virtual environment is created.
79VENVDIR := venv
80
81# The directory where the python tools are installed.
82PYTOOLS := $(VENVDIR)/bin
83
84# The pip executable in the virtual environment.
85PIP := $(PYTOOLS)/pip
86
87# The directory in the docker image where the current directory is mounted.
88WORKDIR := /workdir
89
90# The python image to use for the virtual environment.
91PYTHONIMAGE := $(shell awk '$$4=="python" {print $$2}' $(DEPENDENCIES_DOCKERFILE))
92
93# Run the python image with the current directory mounted.
94DOCKERPY := docker run --rm -u $(DOCKER_USER) -v "$(CURDIR):$(WORKDIR)" -w $(WORKDIR) $(PYTHONIMAGE)
95
96# Create a virtual environment for Python tools.
97$(PYTOOLS):
98# The `--upgrade` flag is needed to ensure that the virtual environment is
99# created with the latest pip version.
100 @$(DOCKERPY) bash -c "python3 -m venv $(VENVDIR) && $(PIP) install --upgrade --cache-dir=$(WORKDIR)/.cache/pip pip"
101
102# Install python packages into the virtual environment.
103$(PYTOOLS)/%: $(PYTOOLS)
104 @$(DOCKERPY) $(PIP) install --cache-dir=$(WORKDIR)/.cache/pip -r requirements.txt
105
106CODESPELL = $(PYTOOLS)/codespell
107$(CODESPELL): PACKAGE=codespell
108
109# Generate
110
111.PHONY: generate
112generate: go-generate vanity-import-fix
113
114.PHONY: go-generate
115go-generate: $(OTEL_GO_MOD_DIRS:%=go-generate/%)
116go-generate/%: DIR=$*
117go-generate/%: $(STRINGER) $(GOTMPL)
118 @echo "$(GO) generate $(DIR)/..." \
119 && cd $(DIR) \
120 && PATH="$(TOOLS):$${PATH}" $(GO) generate ./...
121
122.PHONY: vanity-import-fix
123vanity-import-fix: $(PORTO)
124 @$(PORTO) --include-internal -w .
125
126# Generate go.work file for local development.
127.PHONY: go-work
128go-work: $(CROSSLINK)
129 $(CROSSLINK) work --root=$(shell pwd) --go=1.22.7
130
131# Build
132
133.PHONY: build
134
135build: $(OTEL_GO_MOD_DIRS:%=build/%) $(OTEL_GO_MOD_DIRS:%=build-tests/%)
136build/%: DIR=$*
137build/%:
138 @echo "$(GO) build $(DIR)/..." \
139 && cd $(DIR) \
140 && $(GO) build ./...
141
142build-tests/%: DIR=$*
143build-tests/%:
144 @echo "$(GO) build tests $(DIR)/..." \
145 && cd $(DIR) \
146 && $(GO) list ./... \
147 | grep -v third_party \
148 | xargs $(GO) test -vet=off -run xxxxxMatchNothingxxxxx >/dev/null
149
150# Tests
151
152TEST_TARGETS := test-default test-bench test-short test-verbose test-race test-concurrent-safe
153.PHONY: $(TEST_TARGETS) test
154test-default test-race: ARGS=-race
155test-bench: ARGS=-run=xxxxxMatchNothingxxxxx -test.benchtime=1ms -bench=.
156test-short: ARGS=-short
157test-verbose: ARGS=-v -race
158test-concurrent-safe: ARGS=-run=ConcurrentSafe -count=100 -race
159test-concurrent-safe: TIMEOUT=120
160$(TEST_TARGETS): test
161test: $(OTEL_GO_MOD_DIRS:%=test/%)
162test/%: DIR=$*
163test/%:
164 @echo "$(GO) test -timeout $(TIMEOUT)s $(ARGS) $(DIR)/..." \
165 && cd $(DIR) \
166 && $(GO) list ./... \
167 | grep -v third_party \
168 | xargs $(GO) test -timeout $(TIMEOUT)s $(ARGS)
169
170COVERAGE_MODE = atomic
171COVERAGE_PROFILE = coverage.out
172.PHONY: test-coverage
173test-coverage: $(GOCOVMERGE)
174 @set -e; \
Joey Armstronga6af1522023-01-17 16:06:16 -0500175 printf "" > coverage.txt; \
176 for dir in $(ALL_COVERAGE_MOD_DIRS); do \
Abhay Kumara61c5222025-11-10 07:32:50 +0000177 echo "$(GO) test -coverpkg=go.opentelemetry.io/otel/... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" $${dir}/..."; \
Joey Armstronga6af1522023-01-17 16:06:16 -0500178 (cd "$${dir}" && \
Abhay Kumara61c5222025-11-10 07:32:50 +0000179 $(GO) list ./... \
180 | grep -v third_party \
181 | grep -v 'semconv/v.*' \
182 | xargs $(GO) test -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" && \
183 $(GO) tool cover -html=coverage.out -o coverage.html); \
Joey Armstronga6af1522023-01-17 16:06:16 -0500184 done; \
Abhay Kumara61c5222025-11-10 07:32:50 +0000185 $(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt
Joey Armstronga6af1522023-01-17 16:06:16 -0500186
Abhay Kumara61c5222025-11-10 07:32:50 +0000187.PHONY: benchmark
188benchmark: $(OTEL_GO_MOD_DIRS:%=benchmark/%)
189benchmark/%:
190 @echo "$(GO) test -run=xxxxxMatchNothingxxxxx -bench=. $*..." \
191 && cd $* \
192 && $(GO) list ./... \
193 | grep -v third_party \
194 | xargs $(GO) test -run=xxxxxMatchNothingxxxxx -bench=.
Joey Armstronga6af1522023-01-17 16:06:16 -0500195
Abhay Kumara61c5222025-11-10 07:32:50 +0000196.PHONY: golangci-lint golangci-lint-fix
197golangci-lint-fix: ARGS=--fix
198golangci-lint-fix: golangci-lint
199golangci-lint: $(OTEL_GO_MOD_DIRS:%=golangci-lint/%)
200golangci-lint/%: DIR=$*
201golangci-lint/%: $(GOLANGCI_LINT)
202 @echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \
203 && cd $(DIR) \
204 && $(GOLANGCI_LINT) run --allow-serial-runners $(ARGS)
205
206.PHONY: crosslink
207crosslink: $(CROSSLINK)
208 @echo "Updating intra-repository dependencies in all go modules" \
209 && $(CROSSLINK) --root=$(shell pwd) --prune
210
211.PHONY: go-mod-tidy
212go-mod-tidy: $(ALL_GO_MOD_DIRS:%=go-mod-tidy/%)
213go-mod-tidy/%: DIR=$*
214go-mod-tidy/%: crosslink
215 @echo "$(GO) mod tidy in $(DIR)" \
216 && cd $(DIR) \
217 && $(GO) mod tidy -compat=1.21
218
219.PHONY: lint
220lint: misspell go-mod-tidy golangci-lint govulncheck
221
222.PHONY: vanity-import-check
223vanity-import-check: $(PORTO)
224 @$(PORTO) --include-internal -l . || ( echo "(run: make vanity-import-fix)"; exit 1 )
225
226.PHONY: misspell
227misspell: $(MISSPELL)
228 @$(MISSPELL) -w $(ALL_DOCS)
229
230.PHONY: govulncheck
231govulncheck: $(OTEL_GO_MOD_DIRS:%=govulncheck/%)
232govulncheck/%: DIR=$*
233govulncheck/%: $(GOVULNCHECK)
234 @echo "govulncheck ./... in $(DIR)" \
235 && cd $(DIR) \
236 && $(GOVULNCHECK) ./...
237
238.PHONY: codespell
239codespell: $(CODESPELL)
240 @$(DOCKERPY) $(CODESPELL)
241
242.PHONY: toolchain-check
243toolchain-check:
244 @toolchainRes=$$(for f in $(ALL_GO_MOD_DIRS); do \
245 awk '/^toolchain/ { found=1; next } END { if (found) print FILENAME }' $$f/go.mod; \
246 done); \
247 if [ -n "$${toolchainRes}" ]; then \
248 echo "toolchain checking failed:"; echo "$${toolchainRes}"; \
249 exit 1; \
250 fi
251
252.PHONY: license-check
253license-check:
254 @licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' \) ! -path '**/third_party/*' ! -path './.git/*' ) ; do \
255 awk '/Copyright The OpenTelemetry Authors|generated|GENERATED/ && NR<=4 { found=1; next } END { if (!found) print FILENAME }' $$f; \
256 done); \
257 if [ -n "$${licRes}" ]; then \
258 echo "license header checking failed:"; echo "$${licRes}"; \
259 exit 1; \
260 fi
Joey Armstronga6af1522023-01-17 16:06:16 -0500261
262.PHONY: check-clean-work-tree
263check-clean-work-tree:
264 @if ! git diff --quiet; then \
265 echo; \
266 echo 'Working tree is not clean, did you forget to run "make precommit"?'; \
267 echo; \
268 git status; \
269 exit 1; \
270 fi
271
Abhay Kumara61c5222025-11-10 07:32:50 +0000272# The weaver docker image to use for semconv-generate.
273WEAVER_IMAGE := $(shell awk '$$4=="weaver" {print $$2}' $(DEPENDENCIES_DOCKERFILE))
Joey Armstronga6af1522023-01-17 16:06:16 -0500274
Abhay Kumara61c5222025-11-10 07:32:50 +0000275SEMCONVPKG ?= "semconv/"
276.PHONY: semconv-generate
277semconv-generate: $(SEMCONVKIT)
278 [ "$(TAG)" ] || ( echo "TAG unset: missing opentelemetry semantic-conventions tag"; exit 1 )
279 # Ensure the target directory for source code is available.
280 mkdir -p $(PWD)/$(SEMCONVPKG)/${TAG}
281 # Note: We mount a home directory for downloading/storing the semconv repository.
282 # Weaver will automatically clean the cache when finished, but the directories will remain.
283 mkdir -p ~/.weaver
284 docker run --rm \
285 -u $(DOCKER_USER) \
286 --env HOME=/tmp/weaver \
287 --mount 'type=bind,source=$(PWD)/semconv,target=/home/weaver/templates/registry/go,readonly' \
288 --mount 'type=bind,source=$(PWD)/semconv/${TAG},target=/home/weaver/target' \
289 --mount 'type=bind,source=$(HOME)/.weaver,target=/tmp/weaver/.weaver' \
290 $(WEAVER_IMAGE) registry generate \
291 --registry=https://github.com/open-telemetry/semantic-conventions/archive/refs/tags/$(TAG).zip[model] \
292 --templates=/home/weaver/templates \
293 --param tag=$(TAG) \
294 go \
295 /home/weaver/target
296 $(SEMCONVKIT) -semconv "$(SEMCONVPKG)" -tag "$(TAG)"
Joey Armstronga6af1522023-01-17 16:06:16 -0500297
Abhay Kumara61c5222025-11-10 07:32:50 +0000298.PHONY: gorelease
299gorelease: $(OTEL_GO_MOD_DIRS:%=gorelease/%)
300gorelease/%: DIR=$*
301gorelease/%:| $(GORELEASE)
302 @echo "gorelease in $(DIR):" \
303 && cd $(DIR) \
304 && $(GORELEASE) \
305 || echo ""
Joey Armstronga6af1522023-01-17 16:06:16 -0500306
Abhay Kumara61c5222025-11-10 07:32:50 +0000307.PHONY: verify-mods
308verify-mods: $(MULTIMOD)
309 $(MULTIMOD) verify
Joey Armstronga6af1522023-01-17 16:06:16 -0500310
Abhay Kumara61c5222025-11-10 07:32:50 +0000311.PHONY: prerelease
312prerelease: verify-mods
313 @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
314 $(MULTIMOD) prerelease -m ${MODSET}
Joey Armstronga6af1522023-01-17 16:06:16 -0500315
Abhay Kumara61c5222025-11-10 07:32:50 +0000316COMMIT ?= "HEAD"
317.PHONY: add-tags
318add-tags: verify-mods
319 @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
320 $(MULTIMOD) tag -m ${MODSET} -c ${COMMIT}
Joey Armstronga6af1522023-01-17 16:06:16 -0500321
Abhay Kumara61c5222025-11-10 07:32:50 +0000322MARKDOWNIMAGE := $(shell awk '$$4=="markdown" {print $$2}' $(DEPENDENCIES_DOCKERFILE))
323.PHONY: lint-markdown
324lint-markdown:
325 docker run --rm -u $(DOCKER_USER) -v "$(CURDIR):$(WORKDIR)" $(MARKDOWNIMAGE) -c $(WORKDIR)/.markdownlint.yaml $(WORKDIR)/**/*.md
Joey Armstronga6af1522023-01-17 16:06:16 -0500326
Abhay Kumara61c5222025-11-10 07:32:50 +0000327.PHONY: verify-readmes
328verify-readmes: $(VERIFYREADMES)
329 $(VERIFYREADMES)