Makefile 10 KB

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