Makefile 5.7 KB

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