-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
168 lines (132 loc) · 4.64 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
GO ?= go
TIMEOUT ?= 60
BUILD_TAGS ?=
TOOLS_MOD_DIR := ./internal/tools
ALL_DOCS := $(shell find . -name '*.md' -type f | sort)
ALL_GO_MOD_DIRS := $(shell find . -name go.mod | xargs dirname | sort)
ALL_COVERAGE_MOD_DIRS := $(shell find . -type f -name go.mod | xargs dirname | egrep -v '^./example|^$(TOOLS_MOD_DIR)' | sort)
GO_MOD_DIRS := $(filter-out $(TOOLS_MOD_DIR), $(ALL_GO_MOD_DIRS))
# Tools
.PHONY: tools
TOOLS = $(CURDIR)/.tools
$(TOOLS):
@mkdir -p $@
$(TOOLS)/%: | $(TOOLS)
cd $(TOOLS_MOD_DIR) && \
$(GO) build -o $@ $(PACKAGE)
GOLANGCI_LINT = $(TOOLS)/golangci-lint
$(GOLANGCI_LINT): PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint
MISSPELL = $(TOOLS)/misspell
$(MISSPELL): PACKAGE=github.com/client9/misspell/cmd/misspell
MULTIMOD = $(TOOLS)/multimod
$(MULTIMOD): PACKAGE=go.opentelemetry.io/build-tools/multimod
STRINGER = $(TOOLS)/stringer
$(STRINGER): PACKAGE=golang.org/x/tools/cmd/stringer
GOCOVMERGE = $(TOOLS)/gocovmerge
$(GOCOVMERGE): PACKAGE=github.com/wadey/gocovmerge
JUNIT = $(TOOLS)/junit
$(JUNIT): PACKAGE=github.com/jstemmer/go-junit-report/v2
tools: $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(MULTIMOD) $(JUNIT)
# Linting
.PHONY: golangci-lint golangci-lint-fix
golangci-lint-fix: ARGS=--fix
golangci-lint-fix: golangci-lint
golangci-lint: $(GO_MOD_DIRS:%=golangci-lint/%)
golangci-lint/%: DIR=$*
golangci-lint/%: | $(GOLANGCI_LINT)
@echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \
&& cd $(DIR) \
&& $(GOLANGCI_LINT) run --allow-serial-runners $(ARGS)
.PHONY: go-mod-tidy
go-mod-tidy: $(ALL_GO_MOD_DIRS:%=go-mod-tidy/%)
go-mod-tidy/%: DIR=$*
go-mod-tidy/%:
@echo "$(GO) mod tidy in $(DIR)" \
&& cd $(DIR) \
&& $(GO) mod tidy
.PHONY: misspell
misspell: | $(MISSPELL)
@$(MISSPELL) -w $(ALL_DOCS)
.PHONY: lint
lint: go-mod-tidy golangci-lint misspell
# Build
.PHONY: generate build
generate: $(GO_MOD_DIRS:%=generate/%)
generate/%: DIR=$*
generate/%: | $(STRINGER)
@echo "$(GO) generate $(DIR)/..." \
&& cd $(DIR) \
&& PATH="$(TOOLS):$${PATH}" $(GO) generate ./...
build: generate $(GO_MOD_DIRS:%=build/%) $(GO_MOD_DIRS:%=build-tests/%)
build/%: DIR=$*
build/%:
@echo "$(GO) build ${BUILD_TAGS} $(DIR)/..." \
&& cd $(DIR) \
&& $(GO) build ${BUILD_TAGS} ./...
build-tests/%: DIR=$*
build-tests/%:
@echo "$(GO) build tests ${BUILD_TAGS} $(DIR)/..." \
&& cd $(DIR) \
&& $(GO) list ./... \
| grep -v third_party \
| xargs $(GO) test ${BUILD_TAGS} -vet=off -run xxxxxMatchNothingxxxxx >/dev/null
# Tests
TEST_TARGETS := test-default test-bench test-short test-verbose test-race
.PHONY: $(TEST_TARGETS) test
test-default test-race: ARGS=-race
test-bench: ARGS=-run=xxxxxMatchNothingxxxxx -test.benchtime=1ms -bench=.
test-short: ARGS=-short
test-verbose: ARGS=-v
$(TEST_TARGETS): test
test: $(GO_MOD_DIRS:%=test/%)
test/%: DIR=$*
test/%:
@echo "$(GO) test ${BUILD_TAGS} -v -timeout $(TIMEOUT)s $(ARGS) $(DIR)/..." \
&& cd $(DIR) \
&& $(GO) test ${BUILD_TAGS} -v -timeout $(TIMEOUT)s $(ARGS) ./...
COVERAGE_MODE = atomic
COVERAGE_PROFILE = coverage.out
.PHONY: test-coverage
test-coverage: $(ALL_COVERAGE_MOD_DIRS:%=test-coverage/%) | $(GOCOVMERGE)
@printf "" > coverage.txt \
&& $(GOCOVMERGE) $$(find . -name $(COVERAGE_PROFILE)) > coverage.txt
test-coverage/%: DIR=$*
test-coverage/%:
@set -e; \
CMD="$(GO) test ${BUILD_TAGS} -race -covermode=$(COVERAGE_MODE) -coverprofile=$(COVERAGE_PROFILE)"; \
echo "$(DIR)" | grep -q 'test$$' \
&& CMD="$$CMD -coverpkg=go.opentelemetry.io/contrib/$$( dirname "$(DIR)" | sed -e "s/^\.\///g" )/..."; \
echo "$$CMD $(DIR)/..."; \
cd "$(DIR)" \
&& $$CMD ./... \
&& $(GO) tool cover -html=coverage.out -o coverage.html;
.PHONY: test-junit
test-junit: $(ALL_COVERAGE_MOD_DIRS:%=test-junit/%) | $(JUNIT)
test-junit/%: DIR=$*
test-junit/%:
@echo "$(GO) test ${BUILD_TAGS} -v 2>&1 $(DIR)/..." \
&& cd $(DIR) \
&& mkdir -p test-results \
&& $(GO) test ${BUILD_TAGS} -v 2>&1 ./... | $(JUNIT) -set-exit-code > ./test-results/report.xml
# Workspace
go-work-init:
@rm -f go.work*
@$(GO) work init
@$(GO) work use -r .
# Releasing
.PHONY: prerelease
prerelease: | $(MULTIMOD)
@[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
$(MULTIMOD) verify && $(MULTIMOD) prerelease -m ${MODSET}
prerelease-all: | $(MULTIMOD)
$(MULTIMOD) verify && $(MULTIMOD) prerelease -a
COMMIT ?= "HEAD"
.PHONY: add-tags
add-tags: | $(MULTIMOD)
@[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
$(MULTIMOD) verify && $(MULTIMOD) tag -m ${MODSET} -c ${COMMIT}
VERSION ?=
TAGS ?= $(shell git tag -l | grep "$(VERSION)")
push-tags:
@[ "${VERSION}" ] || ( echo ">> env var VERSION is not set"; exit 1 )
for t in $(TAGS); do git push origin "$$t"; done