Makefile 10 KB

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