Makefile

  1# Copyright The OpenTelemetry Authors
  2# SPDX-License-Identifier: Apache-2.0
  3
  4TOOLS_MOD_DIR := ./internal/tools
  5
  6ALL_DOCS := $(shell find . -name '*.md' -type f | sort)
  7ALL_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)
 10
 11GO = go
 12TIMEOUT = 60
 13
 14# User to run as in docker images.
 15DOCKER_USER=$(shell id -u):$(shell id -g)
 16DEPENDENCIES_DOCKERFILE=./dependencies.Dockerfile
 17
 18.DEFAULT_GOAL := precommit
 19
 20.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
 23
 24# Tools
 25
 26TOOLS = $(CURDIR)/.tools
 27
 28$(TOOLS):
 29	@mkdir -p $@
 30$(TOOLS)/%: $(TOOLS_MOD_DIR)/go.mod | $(TOOLS)
 31	cd $(TOOLS_MOD_DIR) && \
 32	$(GO) build -o $@ $(PACKAGE)
 33
 34MULTIMOD = $(TOOLS)/multimod
 35$(TOOLS)/multimod: PACKAGE=go.opentelemetry.io/build-tools/multimod
 36
 37SEMCONVGEN = $(TOOLS)/semconvgen
 38$(TOOLS)/semconvgen: PACKAGE=go.opentelemetry.io/build-tools/semconvgen
 39
 40CROSSLINK = $(TOOLS)/crosslink
 41$(TOOLS)/crosslink: PACKAGE=go.opentelemetry.io/build-tools/crosslink
 42
 43SEMCONVKIT = $(TOOLS)/semconvkit
 44$(TOOLS)/semconvkit: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/semconvkit
 45
 46GOLANGCI_LINT = $(TOOLS)/golangci-lint
 47$(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint
 48
 49MISSPELL = $(TOOLS)/misspell
 50$(TOOLS)/misspell: PACKAGE=github.com/client9/misspell/cmd/misspell
 51
 52GOCOVMERGE = $(TOOLS)/gocovmerge
 53$(TOOLS)/gocovmerge: PACKAGE=github.com/wadey/gocovmerge
 54
 55STRINGER = $(TOOLS)/stringer
 56$(TOOLS)/stringer: PACKAGE=golang.org/x/tools/cmd/stringer
 57
 58PORTO = $(TOOLS)/porto
 59$(TOOLS)/porto: PACKAGE=github.com/jcchavezs/porto/cmd/porto
 60
 61GOTMPL = $(TOOLS)/gotmpl
 62$(GOTMPL): PACKAGE=go.opentelemetry.io/build-tools/gotmpl
 63
 64GORELEASE = $(TOOLS)/gorelease
 65$(GORELEASE): PACKAGE=golang.org/x/exp/cmd/gorelease
 66
 67GOVULNCHECK = $(TOOLS)/govulncheck
 68$(TOOLS)/govulncheck: PACKAGE=golang.org/x/vuln/cmd/govulncheck
 69
 70.PHONY: tools
 71tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(SEMCONVGEN) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)
 72
 73# Virtualized python tools via docker
 74
 75# The directory where the virtual environment is created.
 76VENVDIR := venv
 77
 78# The directory where the python tools are installed.
 79PYTOOLS := $(VENVDIR)/bin
 80
 81# The pip executable in the virtual environment.
 82PIP := $(PYTOOLS)/pip
 83
 84# The directory in the docker image where the current directory is mounted.
 85WORKDIR := /workdir
 86
 87# The python image to use for the virtual environment.
 88PYTHONIMAGE := $(shell awk '$$4=="python" {print $$2}' $(DEPENDENCIES_DOCKERFILE))
 89
 90# Run the python image with the current directory mounted.
 91DOCKERPY := docker run --rm -u $(DOCKER_USER) -v "$(CURDIR):$(WORKDIR)" -w $(WORKDIR) $(PYTHONIMAGE)
 92
 93# Create a virtual environment for Python tools.
 94$(PYTOOLS):
 95# The `--upgrade` flag is needed to ensure that the virtual environment is
 96# created with the latest pip version.
 97	@$(DOCKERPY) bash -c "python3 -m venv $(VENVDIR) && $(PIP) install --upgrade --cache-dir=$(WORKDIR)/.cache/pip pip"
 98
 99# Install python packages into the virtual environment.
100$(PYTOOLS)/%: $(PYTOOLS)
101	@$(DOCKERPY) $(PIP) install --cache-dir=$(WORKDIR)/.cache/pip -r requirements.txt
102
103CODESPELL = $(PYTOOLS)/codespell
104$(CODESPELL): PACKAGE=codespell
105
106# Generate
107
108.PHONY: generate
109generate: go-generate vanity-import-fix
110
111.PHONY: go-generate
112go-generate: $(OTEL_GO_MOD_DIRS:%=go-generate/%)
113go-generate/%: DIR=$*
114go-generate/%: $(STRINGER) $(GOTMPL)
115	@echo "$(GO) generate $(DIR)/..." \
116		&& cd $(DIR) \
117		&& PATH="$(TOOLS):$${PATH}" $(GO) generate ./...
118
119.PHONY: vanity-import-fix
120vanity-import-fix: $(PORTO)
121	@$(PORTO) --include-internal -w .
122
123# Generate go.work file for local development.
124.PHONY: go-work
125go-work: $(CROSSLINK)
126	$(CROSSLINK) work --root=$(shell pwd) --go=1.22.7
127
128# Build
129
130.PHONY: build
131
132build: $(OTEL_GO_MOD_DIRS:%=build/%) $(OTEL_GO_MOD_DIRS:%=build-tests/%)
133build/%: DIR=$*
134build/%:
135	@echo "$(GO) build $(DIR)/..." \
136		&& cd $(DIR) \
137		&& $(GO) build ./...
138
139build-tests/%: DIR=$*
140build-tests/%:
141	@echo "$(GO) build tests $(DIR)/..." \
142		&& cd $(DIR) \
143		&& $(GO) list ./... \
144		| grep -v third_party \
145		| xargs $(GO) test -vet=off -run xxxxxMatchNothingxxxxx >/dev/null
146
147# Tests
148
149TEST_TARGETS := test-default test-bench test-short test-verbose test-race test-concurrent-safe
150.PHONY: $(TEST_TARGETS) test
151test-default test-race: ARGS=-race
152test-bench:   ARGS=-run=xxxxxMatchNothingxxxxx -test.benchtime=1ms -bench=.
153test-short:   ARGS=-short
154test-verbose: ARGS=-v -race
155test-concurrent-safe: ARGS=-run=ConcurrentSafe -count=100 -race
156test-concurrent-safe: TIMEOUT=120
157$(TEST_TARGETS): test
158test: $(OTEL_GO_MOD_DIRS:%=test/%)
159test/%: DIR=$*
160test/%:
161	@echo "$(GO) test -timeout $(TIMEOUT)s $(ARGS) $(DIR)/..." \
162		&& cd $(DIR) \
163		&& $(GO) list ./... \
164		| grep -v third_party \
165		| xargs $(GO) test -timeout $(TIMEOUT)s $(ARGS)
166
167COVERAGE_MODE    = atomic
168COVERAGE_PROFILE = coverage.out
169.PHONY: test-coverage
170test-coverage: $(GOCOVMERGE)
171	@set -e; \
172	printf "" > coverage.txt; \
173	for dir in $(ALL_COVERAGE_MOD_DIRS); do \
174	  echo "$(GO) test -coverpkg=go.opentelemetry.io/otel/... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" $${dir}/..."; \
175	  (cd "$${dir}" && \
176	    $(GO) list ./... \
177	    | grep -v third_party \
178	    | grep -v 'semconv/v.*' \
179	    | xargs $(GO) test -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" && \
180	  $(GO) tool cover -html=coverage.out -o coverage.html); \
181	done; \
182	$(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt
183
184.PHONY: benchmark
185benchmark: $(OTEL_GO_MOD_DIRS:%=benchmark/%)
186benchmark/%:
187	@echo "$(GO) test -run=xxxxxMatchNothingxxxxx -bench=. $*..." \
188		&& cd $* \
189		&& $(GO) list ./... \
190		| grep -v third_party \
191		| xargs $(GO) test -run=xxxxxMatchNothingxxxxx -bench=.
192
193.PHONY: golangci-lint golangci-lint-fix
194golangci-lint-fix: ARGS=--fix
195golangci-lint-fix: golangci-lint
196golangci-lint: $(OTEL_GO_MOD_DIRS:%=golangci-lint/%)
197golangci-lint/%: DIR=$*
198golangci-lint/%: $(GOLANGCI_LINT)
199	@echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \
200		&& cd $(DIR) \
201		&& $(GOLANGCI_LINT) run --allow-serial-runners $(ARGS)
202
203.PHONY: crosslink
204crosslink: $(CROSSLINK)
205	@echo "Updating intra-repository dependencies in all go modules" \
206		&& $(CROSSLINK) --root=$(shell pwd) --prune
207
208.PHONY: go-mod-tidy
209go-mod-tidy: $(ALL_GO_MOD_DIRS:%=go-mod-tidy/%)
210go-mod-tidy/%: DIR=$*
211go-mod-tidy/%: crosslink
212	@echo "$(GO) mod tidy in $(DIR)" \
213		&& cd $(DIR) \
214		&& $(GO) mod tidy -compat=1.21
215
216.PHONY: lint-modules
217lint-modules: go-mod-tidy
218
219.PHONY: lint
220lint: misspell lint-modules 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
261
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
272# The weaver docker image to use for semconv-generate.
273WEAVER_IMAGE := $(shell awk '$$4=="weaver" {print $$2}' $(DEPENDENCIES_DOCKERFILE))
274
275SEMCONVPKG ?= "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) -output "$(SEMCONVPKG)/$(TAG)" -tag "$(TAG)"
297
298.PHONY: gorelease
299gorelease: $(OTEL_GO_MOD_DIRS:%=gorelease/%)
300gorelease/%: DIR=$*
301gorelease/%:| $(GORELEASE)
302	@echo "gorelease in $(DIR):" \
303		&& cd $(DIR) \
304		&& $(GORELEASE) \
305		|| echo ""
306
307.PHONY: verify-mods
308verify-mods: $(MULTIMOD)
309	$(MULTIMOD) verify
310
311.PHONY: prerelease
312prerelease: verify-mods
313	@[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
314	$(MULTIMOD) prerelease -m ${MODSET}
315
316COMMIT ?= "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}
321
322.PHONY: lint-markdown
323lint-markdown:
324	docker run -v "$(CURDIR):$(WORKDIR)" avtodev/markdown-lint:v1 -c $(WORKDIR)/.markdownlint.yaml $(WORKDIR)/**/*.md
325
326.PHONY: verify-readmes
327verify-readmes:
328	./verify_readmes.sh