-
Notifications
You must be signed in to change notification settings - Fork 22
/
makefile
170 lines (148 loc) · 5.46 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
169
170
# start project configuration
name := curator
ifeq (${GOOS}, windows)
binary := $(name).exe
else
binary := $(name)
endif
buildDir := build
testPackages := operations repobuilder greenbay greenbay-check
allPackages := $(testPackages) $(name) barquesubmit
compilePackages := $(subst $(name),,$(subst -,/,$(foreach target,$(allPackages),./$(target))))
srcFiles := makefile $(shell find . -name "*.go" -not -path "./$(buildDir)/*" -not -name "*_test.go" )
testSrcFiles := makefile $(shell find . -name "*.go" -not -path "./$(buildDir)/*")
orgPath := github.com/mongodb
projectPath := $(orgPath)/$(name)
# end project configuration
# start environment setup
gobin := go
ifneq (,$(GOROOT))
gobin := $(GOROOT)/bin/go
endif
goCache := $(GOCACHE)
ifeq (,$(goCache))
goCache := $(abspath $(buildDir)/.cache)
endif
goModCache := $(GOMODCACHE)
ifeq (,$(goModCache))
goModCache := $(abspath $(buildDir)/.mod-cache)
endif
lintCache := $(GOLANGCI_LINT_CACHE)
ifeq (,$(lintCache))
lintCache := $(abspath $(buildDir)/.lint-cache)
endif
ifeq ($(OS),Windows_NT)
gobin := $(shell cygpath $(gobin))
goCache := $(shell cygpath -m $(goCache))
goModCache := $(shell cygpath -m $(goModCache))
lintCache := $(shell cygpath -m $(lintCache))
export GOROOT := $(shell cygpath -m $(GOROOT))
endif
ifneq ($(goCache),$(GOCACHE))
export GOCACHE := $(goCache)
endif
ifneq ($(goModCache),$(GOMODCACHE))
export GOMODCACHE := $(goModCache)
endif
ifneq ($(lintCache),$(GOLANGCI_LINT_CACHE))
export GOLANGCI_LINT_CACHE := $(lintCache)
endif
ifneq (,$(RACE_DETECTOR))
# cgo is required for using the race detector.
export CGO_ENABLED := 1
else
export CGO_ENABLED := 0
endif
# end environment setup
# Ensure the build directory exists, since most targets require it.
$(shell mkdir -p $(buildDir))
.DEFAULT_GOAL := $(binary)
# start lint setup targets
$(buildDir)/golangci-lint:
@curl --retry 10 --retry-max-time 60 -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(buildDir) v1.51.2 >/dev/null 2>&1
$(buildDir)/run-linter: cmd/run-linter/run-linter.go $(buildDir)/golangci-lint
$(gobin) build -o $@ $<
# end lint setup targets
# start output files
lintOutput := $(foreach target,$(allPackages),$(buildDir)/output.$(target).lint)
testOutput := $(foreach target,$(testPackages),$(buildDir)/output.$(target).test)
coverageOutput := $(foreach target,$(testPackages),$(buildDir)/output.$(target).coverage)
htmlCoverageOutput := $(foreach target,$(testPackages),$(buildDir)/output.$(target).coverage.html)
.PRECIOUS: $(testOutput) $(lintOutput) $(coverageOutput) $(htmlCoverageOutput)
# end output files
# start basic development operations
compile:
$(gobin) build $(compileFlags) $(compilePackages)
test: $(testOutput)
lint: $(lintOutput)
coverage: $(coverageOutput)
html-coverage: $(htmlCoverageOutput)
phony := compile lint test coverage html-coverage
# start convenience targets for running tests and coverage tasks on a
# specific package.
test-%: $(buildDir)/output.%.test
coverage-%: $(buildDir)/output.%.coverage
html-coverage-%: $(buildDir)/output.%.coverage $(buildDir)/output.%.coverage.html
lint-%: $(buildDir)/output.%.lint
# end convenience targets
# end basic development operations
# start test and coverage artifacts
testArgs := -v -timeout=15m
ifneq (,$(RUN_TEST))
testArgs += -run='$(RUN_TEST)'
endif
ifneq (,$(RUN_COUNT))
testArgs += -count=$(RUN_COUNT)
endif
ifneq (,$(RACE_DETECTOR))
testArgs += -race
endif
$(buildDir)/output.%.test: .FORCE
$(gobin) test $(testArgs) ./$(if $(subst $(name),,$*),$(subst -,/,$*),) 2>&1 | tee $@
@grep -s -q -e "^PASS" $@
$(buildDir)/output.%.coverage: .FORCE
$(gobin) test $(testArgs) ./$(if $(subst $(name),,$*),$(subst -,/,$*),) -covermode=count -coverprofile $@ | tee $(subst coverage,test,$@)
@-[ -f $@ ] && $(gobin) tool cover -func=$@ | sed 's%$(projectPath)/%%' | column -t
@grep -s -q -e "^PASS" $(subst coverage,test,$@)
$(buildDir)/output.%.coverage.html: $(buildDir)/output.%.coverage
$(gobin) tool cover -html=$< -o $@
ifneq (go,$(gobin))
# We have to handle the PATH specially for linting in CI, because if the PATH has a different version of the Go
# binary in it, the linter won't work properly.
lintEnvVars := PATH="$(shell dirname $(gobin)):$(PATH)"
endif
$(buildDir)/output.%.lint: $(buildDir)/run-linter .FORCE
@$(lintEnvVars) ./$< --output=$@ --lintBin=$(buildDir)/golangci-lint --packages='$*'
# end test and coverage artifacts
# start cli and distribution targets
ldFlags += $(if $(DEBUG_ENABLED),,-w -s)
ldFlags += -X=github.com/mongodb/curator.BuildRevision=$(shell git rev-parse HEAD)
compileFlags += -ldflags="$(ldFlags)" -trimpath
# convenience link in the working directory to the binary
$(binary): $(buildDir)/$(binary)
@[ -e $@ ] || ln -s $<
$(buildDir)/$(binary): $(srcFiles)
$(gobin) build $(compileFlags) -o $@ cmd/$(name)/$(name).go
phony += $(buildDir)/$(binary)
dist: $(buildDir)/dist.tar.gz
$(buildDir)/dist.tar.gz: $(buildDir)/$(binary)
tar -C $(buildDir) -czvf $@ $(binary)
# end cli and distribution targets
# start module management targets
mod-tidy:
$(gobin) mod tidy
# Check if go.mod and go.sum are clean. If they're clean, then mod tidy should not produce a different result.
verify-mod-tidy:
$(gobin) run cmd/verify-mod-tidy/verify-mod-tidy.go -goBin="$(gobin)"
phony += mod-tidy verify-mod-tidy
# end module management targets
# start cleanup targets
clean:
rm -rf $(buildDir) $(binary)
clean-results:
rm -rf $(buildDir)/output.*
phony += clean clean-results
# end cleanup targets
# configure phony targets
.FORCE:
.PHONY: $(phony) .FORCE