Makefile 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # Copyright The OpenTelemetry Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. TOOLS_MOD_DIR := ./internal/tools
  15. ALL_DOCS := $(shell find . -name '*.md' -type f | sort)
  16. ALL_GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort)
  17. OTEL_GO_MOD_DIRS := $(filter-out $(TOOLS_MOD_DIR), $(ALL_GO_MOD_DIRS))
  18. ALL_COVERAGE_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | grep -E -v '^./example|^$(TOOLS_MOD_DIR)' | sort)
  19. GO = go
  20. TIMEOUT = 60
  21. .DEFAULT_GOAL := precommit
  22. .PHONY: precommit ci
  23. precommit: dependabot-generate license-check vanity-import-fix misspell go-mod-tidy golangci-lint-fix test-default
  24. ci: dependabot-check license-check lint vanity-import-check build test-default check-clean-work-tree test-coverage
  25. # Tools
  26. TOOLS = $(CURDIR)/.tools
  27. $(TOOLS):
  28. @mkdir -p $@
  29. $(TOOLS)/%: | $(TOOLS)
  30. cd $(TOOLS_MOD_DIR) && \
  31. $(GO) build -o $@ $(PACKAGE)
  32. MULTIMOD = $(TOOLS)/multimod
  33. $(TOOLS)/multimod: PACKAGE=go.opentelemetry.io/build-tools/multimod
  34. SEMCONVGEN = $(TOOLS)/semconvgen
  35. $(TOOLS)/semconvgen: PACKAGE=go.opentelemetry.io/build-tools/semconvgen
  36. CROSSLINK = $(TOOLS)/crosslink
  37. $(TOOLS)/crosslink: PACKAGE=go.opentelemetry.io/build-tools/crosslink
  38. SEMCONVKIT = $(TOOLS)/semconvkit
  39. $(TOOLS)/semconvkit: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/semconvkit
  40. DBOTCONF = $(TOOLS)/dbotconf
  41. $(TOOLS)/dbotconf: PACKAGE=go.opentelemetry.io/build-tools/dbotconf
  42. GOLANGCI_LINT = $(TOOLS)/golangci-lint
  43. $(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint
  44. MISSPELL = $(TOOLS)/misspell
  45. $(TOOLS)/misspell: PACKAGE=github.com/client9/misspell/cmd/misspell
  46. GOCOVMERGE = $(TOOLS)/gocovmerge
  47. $(TOOLS)/gocovmerge: PACKAGE=github.com/wadey/gocovmerge
  48. STRINGER = $(TOOLS)/stringer
  49. $(TOOLS)/stringer: PACKAGE=golang.org/x/tools/cmd/stringer
  50. PORTO = $(TOOLS)/porto
  51. $(TOOLS)/porto: PACKAGE=github.com/jcchavezs/porto/cmd/porto
  52. GOJQ = $(TOOLS)/gojq
  53. $(TOOLS)/gojq: PACKAGE=github.com/itchyny/gojq/cmd/gojq
  54. .PHONY: tools
  55. tools: $(CROSSLINK) $(DBOTCONF) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(GOJQ) $(SEMCONVGEN) $(MULTIMOD) $(SEMCONVKIT)
  56. # Build
  57. .PHONY: generate build
  58. generate: $(OTEL_GO_MOD_DIRS:%=generate/%)
  59. generate/%: DIR=$*
  60. generate/%: | $(STRINGER) $(PORTO)
  61. @echo "$(GO) generate $(DIR)/..." \
  62. && cd $(DIR) \
  63. && PATH="$(TOOLS):$${PATH}" $(GO) generate ./... && $(PORTO) -w .
  64. build: generate $(OTEL_GO_MOD_DIRS:%=build/%) $(OTEL_GO_MOD_DIRS:%=build-tests/%)
  65. build/%: DIR=$*
  66. build/%:
  67. @echo "$(GO) build $(DIR)/..." \
  68. && cd $(DIR) \
  69. && $(GO) build ./...
  70. build-tests/%: DIR=$*
  71. build-tests/%:
  72. @echo "$(GO) build tests $(DIR)/..." \
  73. && cd $(DIR) \
  74. && $(GO) list ./... \
  75. | grep -v third_party \
  76. | xargs $(GO) test -vet=off -run xxxxxMatchNothingxxxxx >/dev/null
  77. # Tests
  78. TEST_TARGETS := test-default test-bench test-short test-verbose test-race
  79. .PHONY: $(TEST_TARGETS) test
  80. test-default test-race: ARGS=-race
  81. test-bench: ARGS=-run=xxxxxMatchNothingxxxxx -test.benchtime=1ms -bench=.
  82. test-short: ARGS=-short
  83. test-verbose: ARGS=-v -race
  84. $(TEST_TARGETS): test
  85. test: $(OTEL_GO_MOD_DIRS:%=test/%)
  86. test/%: DIR=$*
  87. test/%:
  88. @echo "$(GO) test -timeout $(TIMEOUT)s $(ARGS) $(DIR)/..." \
  89. && cd $(DIR) \
  90. && $(GO) list ./... \
  91. | grep -v third_party \
  92. | xargs $(GO) test -timeout $(TIMEOUT)s $(ARGS)
  93. COVERAGE_MODE = atomic
  94. COVERAGE_PROFILE = coverage.out
  95. .PHONY: test-coverage
  96. test-coverage: | $(GOCOVMERGE)
  97. @set -e; \
  98. printf "" > coverage.txt; \
  99. for dir in $(ALL_COVERAGE_MOD_DIRS); do \
  100. echo "$(GO) test -coverpkg=go.opentelemetry.io/otel/... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" $${dir}/..."; \
  101. (cd "$${dir}" && \
  102. $(GO) list ./... \
  103. | grep -v third_party \
  104. | grep -v 'semconv/v.*' \
  105. | xargs $(GO) test -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" && \
  106. $(GO) tool cover -html=coverage.out -o coverage.html); \
  107. done; \
  108. $(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt
  109. .PHONY: golangci-lint golangci-lint-fix
  110. golangci-lint-fix: ARGS=--fix
  111. golangci-lint-fix: golangci-lint
  112. golangci-lint: $(OTEL_GO_MOD_DIRS:%=golangci-lint/%)
  113. golangci-lint/%: DIR=$*
  114. golangci-lint/%: | $(GOLANGCI_LINT)
  115. @echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \
  116. && cd $(DIR) \
  117. && $(GOLANGCI_LINT) run --allow-serial-runners $(ARGS)
  118. .PHONY: crosslink
  119. crosslink: | $(CROSSLINK)
  120. @echo "Updating intra-repository dependencies in all go modules" \
  121. && $(CROSSLINK) --root=$(shell pwd) --prune
  122. .PHONY: go-mod-tidy
  123. go-mod-tidy: $(ALL_GO_MOD_DIRS:%=go-mod-tidy/%)
  124. go-mod-tidy/%: DIR=$*
  125. go-mod-tidy/%: | crosslink
  126. @echo "$(GO) mod tidy in $(DIR)" \
  127. && cd $(DIR) \
  128. && $(GO) mod tidy -compat=1.18
  129. .PHONY: lint-modules
  130. lint-modules: go-mod-tidy
  131. .PHONY: lint
  132. lint: misspell lint-modules golangci-lint
  133. .PHONY: vanity-import-check
  134. vanity-import-check: | $(PORTO)
  135. @$(PORTO) --include-internal -l . || echo "(run: make vanity-import-fix)"
  136. .PHONY: vanity-import-fix
  137. vanity-import-fix: | $(PORTO)
  138. @$(PORTO) --include-internal -w .
  139. .PHONY: misspell
  140. misspell: | $(MISSPELL)
  141. @$(MISSPELL) -w $(ALL_DOCS)
  142. .PHONY: license-check
  143. license-check:
  144. @licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' \) ! -path '**/third_party/*' ! -path './.git/*' ) ; do \
  145. awk '/Copyright The OpenTelemetry Authors|generated|GENERATED/ && NR<=3 { found=1; next } END { if (!found) print FILENAME }' $$f; \
  146. done); \
  147. if [ -n "$${licRes}" ]; then \
  148. echo "license header checking failed:"; echo "$${licRes}"; \
  149. exit 1; \
  150. fi
  151. DEPENDABOT_CONFIG = .github/dependabot.yml
  152. .PHONY: dependabot-check
  153. dependabot-check: | $(DBOTCONF)
  154. @$(DBOTCONF) verify $(DEPENDABOT_CONFIG) || echo "(run: make dependabot-generate)"
  155. .PHONY: dependabot-generate
  156. dependabot-generate: | $(DBOTCONF)
  157. @$(DBOTCONF) generate > $(DEPENDABOT_CONFIG)
  158. .PHONY: check-clean-work-tree
  159. check-clean-work-tree:
  160. @if ! git diff --quiet; then \
  161. echo; \
  162. echo 'Working tree is not clean, did you forget to run "make precommit"?'; \
  163. echo; \
  164. git status; \
  165. exit 1; \
  166. fi
  167. SEMCONVPKG ?= "semconv/"
  168. .PHONY: semconv-generate
  169. semconv-generate: | $(SEMCONVGEN) $(SEMCONVKIT)
  170. [ "$(TAG)" ] || ( echo "TAG unset: missing opentelemetry specification tag"; exit 1 )
  171. [ "$(OTEL_SPEC_REPO)" ] || ( echo "OTEL_SPEC_REPO unset: missing path to opentelemetry specification repo"; exit 1 )
  172. $(SEMCONVGEN) -i "$(OTEL_SPEC_REPO)/semantic_conventions/." --only=span -p conventionType=trace -f trace.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  173. $(SEMCONVGEN) -i "$(OTEL_SPEC_REPO)/semantic_conventions/." --only=event -p conventionType=event -f event.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  174. $(SEMCONVGEN) -i "$(OTEL_SPEC_REPO)/semantic_conventions/." --only=resource -p conventionType=resource -f resource.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  175. $(SEMCONVKIT) -output "$(SEMCONVPKG)/$(TAG)" -tag "$(TAG)"
  176. .PHONY: prerelease
  177. prerelease: | $(MULTIMOD)
  178. @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
  179. $(MULTIMOD) verify && $(MULTIMOD) prerelease -m ${MODSET}
  180. COMMIT ?= "HEAD"
  181. .PHONY: add-tags
  182. add-tags: | $(MULTIMOD)
  183. @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
  184. $(MULTIMOD) verify && $(MULTIMOD) tag -m ${MODSET} -c ${COMMIT}