Debian Codegraph @

Makefile

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