Joey Armstrong | e8c091f | 2023-01-17 16:56:26 -0500 | [diff] [blame] | 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 | |
| 15 | EXAMPLES := $(shell ./get_main_pkgs.sh ./example) |
| 16 | TOOLS_MOD_DIR := ./internal/tools |
| 17 | |
| 18 | # All source code and documents. Used in spell check. |
| 19 | ALL_DOCS := $(shell find . -name '*.md' -type f | sort) |
| 20 | # All directories with go.mod files related to opentelemetry library. Used for building, testing and linting. |
| 21 | ALL_GO_MOD_DIRS := $(filter-out $(TOOLS_MOD_DIR), $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort)) |
| 22 | ALL_COVERAGE_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | egrep -v '^./example|^$(TOOLS_MOD_DIR)' | sort) |
| 23 | |
| 24 | # Mac OS Catalina 10.5.x doesn't support 386. Hence skip 386 test |
| 25 | SKIP_386_TEST = false |
| 26 | UNAME_S := $(shell uname -s) |
| 27 | ifeq ($(UNAME_S),Darwin) |
| 28 | SW_VERS := $(shell sw_vers -productVersion) |
| 29 | ifeq ($(shell echo $(SW_VERS) | egrep '^(10.1[5-9]|1[1-9]|[2-9])'), $(SW_VERS)) |
| 30 | SKIP_386_TEST = true |
| 31 | endif |
| 32 | endif |
| 33 | |
| 34 | GOTEST_MIN = go test -timeout 30s |
| 35 | GOTEST = $(GOTEST_MIN) -race |
| 36 | GOTEST_WITH_COVERAGE = $(GOTEST) -coverprofile=coverage.out -covermode=atomic -coverpkg=./... |
| 37 | |
| 38 | .DEFAULT_GOAL := precommit |
| 39 | |
| 40 | .PHONY: precommit |
| 41 | |
| 42 | TOOLS_DIR := $(abspath ./.tools) |
| 43 | |
| 44 | $(TOOLS_DIR)/golangci-lint: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go |
| 45 | cd $(TOOLS_MOD_DIR) && \ |
| 46 | go build -o $(TOOLS_DIR)/golangci-lint github.com/golangci/golangci-lint/cmd/golangci-lint |
| 47 | |
| 48 | $(TOOLS_DIR)/misspell: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go |
| 49 | cd $(TOOLS_MOD_DIR) && \ |
| 50 | go build -o $(TOOLS_DIR)/misspell github.com/client9/misspell/cmd/misspell |
| 51 | |
| 52 | $(TOOLS_DIR)/stringer: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go |
| 53 | cd $(TOOLS_MOD_DIR) && \ |
| 54 | go build -o $(TOOLS_DIR)/stringer golang.org/x/tools/cmd/stringer |
| 55 | |
| 56 | $(TOOLS_DIR)/gojq: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go |
| 57 | cd $(TOOLS_MOD_DIR) && \ |
| 58 | go build -o $(TOOLS_DIR)/gojq github.com/itchyny/gojq/cmd/gojq |
| 59 | |
| 60 | precommit: dependabot-check license-check generate build lint examples test-benchmarks test |
| 61 | |
| 62 | .PHONY: test-with-coverage |
| 63 | test-with-coverage: |
| 64 | set -e; \ |
| 65 | printf "" > coverage.txt; \ |
| 66 | for dir in $(ALL_COVERAGE_MOD_DIRS); do \ |
| 67 | echo "go test ./... + coverage in $${dir}"; \ |
| 68 | (cd "$${dir}" && \ |
| 69 | $(GOTEST_WITH_COVERAGE) ./... && \ |
| 70 | go tool cover -html=coverage.out -o coverage.html); \ |
| 71 | [ -f "$${dir}/coverage.out" ] && cat "$${dir}/coverage.out" >> coverage.txt; \ |
| 72 | done; \ |
| 73 | sed -i.bak -e '2,$$ { /^mode: /d; }' coverage.txt |
| 74 | |
| 75 | |
| 76 | .PHONY: ci |
| 77 | ci: precommit check-clean-work-tree test-with-coverage test-386 |
| 78 | |
| 79 | .PHONY: check-clean-work-tree |
| 80 | check-clean-work-tree: |
| 81 | @if ! git diff --quiet; then \ |
| 82 | echo; \ |
| 83 | echo 'Working tree is not clean, did you forget to run "make precommit"?'; \ |
| 84 | echo; \ |
| 85 | git status; \ |
| 86 | exit 1; \ |
| 87 | fi |
| 88 | |
| 89 | .PHONY: build |
| 90 | build: |
| 91 | # TODO: Fix this on windows. |
| 92 | set -e; for dir in $(ALL_GO_MOD_DIRS); do \ |
| 93 | echo "compiling all packages in $${dir}"; \ |
| 94 | (cd "$${dir}" && \ |
| 95 | go build ./... && \ |
| 96 | go test -run xxxxxMatchNothingxxxxx ./... >/dev/null); \ |
| 97 | done |
| 98 | |
| 99 | .PHONY: test |
| 100 | test: |
| 101 | set -e; for dir in $(ALL_GO_MOD_DIRS); do \ |
| 102 | echo "go test ./... + race in $${dir}"; \ |
| 103 | (cd "$${dir}" && \ |
| 104 | $(GOTEST) ./...); \ |
| 105 | done |
| 106 | |
| 107 | .PHONY: test-386 |
| 108 | test-386: |
| 109 | if [ $(SKIP_386_TEST) = true ] ; then \ |
| 110 | echo "skipping the test for GOARCH 386 as it is not supported on the current OS"; \ |
| 111 | else \ |
| 112 | set -e; for dir in $(ALL_GO_MOD_DIRS); do \ |
| 113 | echo "go test ./... GOARCH 386 in $${dir}"; \ |
| 114 | (cd "$${dir}" && \ |
| 115 | GOARCH=386 $(GOTEST_MIN) ./...); \ |
| 116 | done; \ |
| 117 | fi |
| 118 | |
| 119 | .PHONY: examples |
| 120 | examples: |
| 121 | @set -e; for ex in $(EXAMPLES); do \ |
| 122 | echo "Building $${ex}"; \ |
| 123 | (cd "$${ex}" && \ |
| 124 | go build .); \ |
| 125 | done |
| 126 | |
| 127 | .PHONY: test-benchmarks |
| 128 | test-benchmarks: |
| 129 | @set -e; for dir in $(ALL_GO_MOD_DIRS); do \ |
| 130 | echo "test benchmarks in $${dir}"; \ |
| 131 | (cd "$${dir}" && go test -test.benchtime=1ms -run=NONE -bench=. ./...) > /dev/null; \ |
| 132 | done |
| 133 | |
| 134 | .PHONY: lint |
| 135 | lint: $(TOOLS_DIR)/golangci-lint $(TOOLS_DIR)/misspell |
| 136 | set -e; for dir in $(ALL_GO_MOD_DIRS); do \ |
| 137 | echo "golangci-lint in $${dir}"; \ |
| 138 | (cd "$${dir}" && \ |
| 139 | $(TOOLS_DIR)/golangci-lint run --fix && \ |
| 140 | $(TOOLS_DIR)/golangci-lint run); \ |
| 141 | done |
| 142 | $(TOOLS_DIR)/misspell -w $(ALL_DOCS) |
| 143 | set -e; for dir in $(ALL_GO_MOD_DIRS) $(TOOLS_MOD_DIR); do \ |
| 144 | echo "go mod tidy in $${dir}"; \ |
| 145 | (cd "$${dir}" && \ |
| 146 | go mod tidy); \ |
| 147 | done |
| 148 | |
| 149 | generate: $(TOOLS_DIR)/stringer |
| 150 | set -e; for dir in $(ALL_GO_MOD_DIRS); do \ |
| 151 | echo "running generators in $${dir}"; \ |
| 152 | (cd "$${dir}" && \ |
| 153 | PATH="$(TOOLS_DIR):$${PATH}" go generate ./...); \ |
| 154 | done |
| 155 | |
| 156 | .PHONY: license-check |
| 157 | license-check: |
| 158 | @licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' \) ! -path './vendor/*' ! -path './exporters/otlp/internal/opentelemetry-proto/*') ; do \ |
| 159 | awk '/Copyright The OpenTelemetry Authors|generated|GENERATED/ && NR<=3 { found=1; next } END { if (!found) print FILENAME }' $$f; \ |
| 160 | done); \ |
| 161 | if [ -n "$${licRes}" ]; then \ |
| 162 | echo "license header checking failed:"; echo "$${licRes}"; \ |
| 163 | exit 1; \ |
| 164 | fi |
| 165 | |
| 166 | .PHONY: dependabot-check |
| 167 | dependabot-check: |
| 168 | @result=$$( \ |
| 169 | for f in $$( find . -type f -name go.mod -exec dirname {} \; | sed 's/^.\/\?/\//' ); \ |
| 170 | do grep -q "$$f" .github/dependabot.yml \ |
| 171 | || echo "$$f"; \ |
| 172 | done; \ |
| 173 | ); \ |
| 174 | if [ -n "$$result" ]; then \ |
| 175 | echo "missing go.mod dependabot check:"; echo "$$result"; \ |
| 176 | exit 1; \ |
| 177 | fi |