Makefile 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. EXAMPLES := $(shell ./get_main_pkgs.sh ./example)
  15. TOOLS_MOD_DIR := ./internal/tools
  16. # All source code and documents. Used in spell check.
  17. ALL_DOCS := $(shell find . -name '*.md' -type f | sort)
  18. # All directories with go.mod files related to opentelemetry library. Used for building, testing and linting.
  19. ALL_GO_MOD_DIRS := $(filter-out $(TOOLS_MOD_DIR), $(shell find . -type f -name 'go.mod' -exec dirname {} \; | egrep -v '^./example' | sort)) $(shell find ./example -type f -name 'go.mod' -exec dirname {} \; | sort)
  20. ALL_COVERAGE_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | egrep -v '^./example|^$(TOOLS_MOD_DIR)' | sort)
  21. GO = go
  22. TIMEOUT = 60
  23. .DEFAULT_GOAL := precommit
  24. .PHONY: precommit ci
  25. precommit: dependabot-check license-check lint build examples test-default
  26. ci: precommit check-clean-work-tree test-coverage
  27. # Tools
  28. TOOLS = $(CURDIR)/.tools
  29. $(TOOLS):
  30. @mkdir -p $@
  31. $(TOOLS)/%: | $(TOOLS)
  32. cd $(TOOLS_MOD_DIR) && \
  33. $(GO) build -o $@ $(PACKAGE)
  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/otel/$(TOOLS_MOD_DIR)/crosslink
  38. GOLANGCI_LINT = $(TOOLS)/golangci-lint
  39. $(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint
  40. MISSPELL = $(TOOLS)/misspell
  41. $(TOOLS)/misspell: PACKAGE= github.com/client9/misspell/cmd/misspell
  42. GOCOVMERGE = $(TOOLS)/gocovmerge
  43. $(TOOLS)/gocovmerge: PACKAGE= github.com/wadey/gocovmerge
  44. STRINGER = $(TOOLS)/stringer
  45. $(TOOLS)/stringer: PACKAGE=golang.org/x/tools/cmd/stringer
  46. $(TOOLS)/gojq: PACKAGE=github.com/itchyny/gojq/cmd/gojq
  47. .PHONY: tools
  48. tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(TOOLS)/gojq $(SEMCONVGEN)
  49. # Build
  50. .PHONY: examples generate build
  51. examples:
  52. @set -e; for dir in $(EXAMPLES); do \
  53. echo "$(GO) build $${dir}/..."; \
  54. (cd "$${dir}" && \
  55. $(GO) build .); \
  56. done
  57. generate: $(STRINGER)
  58. set -e; for dir in $(ALL_GO_MOD_DIRS); do \
  59. echo "$(GO) generate $${dir}/..."; \
  60. (cd "$${dir}" && \
  61. PATH="$(TOOLS):$${PATH}" $(GO) generate ./...); \
  62. done
  63. build: generate
  64. # Build all package code including testing code.
  65. set -e; for dir in $(ALL_GO_MOD_DIRS); do \
  66. echo "$(GO) build $${dir}/..."; \
  67. (cd "$${dir}" && \
  68. $(GO) build ./... && \
  69. $(GO) list ./... \
  70. | grep -v third_party \
  71. | xargs $(GO) test -vet=off -run xxxxxMatchNothingxxxxx >/dev/null); \
  72. done
  73. # Tests
  74. TEST_TARGETS := test-default test-bench test-short test-verbose test-race
  75. .PHONY: $(TEST_TARGETS) test
  76. test-default: ARGS=-v -race
  77. test-bench: ARGS=-run=xxxxxMatchNothingxxxxx -test.benchtime=1ms -bench=.
  78. test-short: ARGS=-short
  79. test-verbose: ARGS=-v
  80. test-race: ARGS=-race
  81. $(TEST_TARGETS): test
  82. test:
  83. @set -e; for dir in $(ALL_GO_MOD_DIRS); do \
  84. echo "$(GO) test -timeout $(TIMEOUT)s $(ARGS) $${dir}/..."; \
  85. (cd "$${dir}" && \
  86. $(GO) list ./... \
  87. | grep -v third_party \
  88. | xargs $(GO) test -timeout $(TIMEOUT)s $(ARGS)); \
  89. done
  90. COVERAGE_MODE = atomic
  91. COVERAGE_PROFILE = coverage.out
  92. .PHONY: test-coverage
  93. test-coverage: | $(GOCOVMERGE)
  94. @set -e; \
  95. printf "" > coverage.txt; \
  96. for dir in $(ALL_COVERAGE_MOD_DIRS); do \
  97. echo "$(GO) test -coverpkg=go.opentelemetry.io/otel/... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" $${dir}/..."; \
  98. (cd "$${dir}" && \
  99. $(GO) list ./... \
  100. | grep -v third_party \
  101. | xargs $(GO) test -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" && \
  102. $(GO) tool cover -html=coverage.out -o coverage.html); \
  103. done; \
  104. $(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt
  105. .PHONY: lint
  106. lint: misspell lint-modules | $(GOLANGCI_LINT)
  107. set -e; for dir in $(ALL_GO_MOD_DIRS); do \
  108. echo "golangci-lint in $${dir}"; \
  109. (cd "$${dir}" && \
  110. $(GOLANGCI_LINT) run --fix && \
  111. $(GOLANGCI_LINT) run); \
  112. done
  113. .PHONY: misspell
  114. misspell: | $(MISSPELL)
  115. $(MISSPELL) -w $(ALL_DOCS)
  116. .PHONY: lint-modules
  117. lint-modules: | $(CROSSLINK)
  118. set -e; for dir in $(ALL_GO_MOD_DIRS) $(TOOLS_MOD_DIR); do \
  119. echo "$(GO) mod tidy in $${dir}"; \
  120. (cd "$${dir}" && \
  121. $(GO) mod tidy); \
  122. done
  123. echo "cross-linking all go modules"
  124. $(CROSSLINK)
  125. .PHONY: license-check
  126. license-check:
  127. @licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' \) ! -path '**/third_party/*') ; do \
  128. awk '/Copyright The OpenTelemetry Authors|generated|GENERATED/ && NR<=3 { found=1; next } END { if (!found) print FILENAME }' $$f; \
  129. done); \
  130. if [ -n "$${licRes}" ]; then \
  131. echo "license header checking failed:"; echo "$${licRes}"; \
  132. exit 1; \
  133. fi
  134. .PHONY: dependabot-check
  135. dependabot-check:
  136. @result=$$( \
  137. for f in $$( find . -type f -name go.mod -exec dirname {} \; | sed 's/^.//' ); \
  138. do grep -q "directory: \+$$f" .github/dependabot.yml \
  139. || echo "$$f"; \
  140. done; \
  141. ); \
  142. if [ -n "$$result" ]; then \
  143. echo "missing go.mod dependabot check:"; echo "$$result"; \
  144. echo "new modules need to be added to the .github/dependabot.yml file"; \
  145. exit 1; \
  146. fi
  147. .PHONY: check-clean-work-tree
  148. check-clean-work-tree:
  149. @if ! git diff --quiet; then \
  150. echo; \
  151. echo 'Working tree is not clean, did you forget to run "make precommit"?'; \
  152. echo; \
  153. git status; \
  154. exit 1; \
  155. fi