forked from mongodb/grip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
216 lines (197 loc) · 9.84 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# project configuration
name := grip
buildDir := build
packages := logging message send slogger $(name)
orgPath := github.com/mongodb
projectPath := $(orgPath)/$(name)
# end project configuration
# start linting configuration
# package, testing, and linter dependencies specified
# separately. This is a temporary solution: eventually we should
# vendorize all of these dependencies.
lintDeps := github.com/alecthomas/gometalinter
# include test files and give linters 40s to run to avoid timeouts
lintArgs := --tests --deadline=1m --vendor
# gotype produces false positives because it reads .a files which
# are rarely up to date.
lintArgs += --disable="gotype" --disable="gas" --disable="gocyclo" --disable="aligncheck"
lintArgs += --skip="$(buildDir)" --skip="buildscripts"
# add and configure additional linters
lintArgs += --enable="goimports" --enable="misspell"
lintArgs += --line-length=100 --dupl-threshold=175
# two similar functions triggered the duplicate warning, but they're not.
lintArgs += --exclude="duplicate of registry.go"
lintArgs += --exclude="don.t use underscores.*_DependencyState.*"
lintArgs += --exclude="file is not goimported" # test files aren't imported
# golint doesn't handle splitting package comments between multiple files.
lintArgs += --exclude="package comment should be of the form .Package .* \(golint\)"
lintArgs += --exclude="error return value not checked \(defer.*"
# there are a lot of logging methods that don't have doc strings, and probably shouldn't
lintArgs += --exclude="exported method Grip\..*should have comment or be unexported.*"
lintArgs += --exclude="exported function (Catch|Log|Default|Emergency|Alert|Critical|Error|Warning|Notice|Info|Debug).* should have comment.*"
lintArgs += --exclude="exported func.*InternalLogger returns unexported type.*"
lintArgs += --exclude="exported method (Log|SystemInfo|InternalSender)\..+ should have comment"
# end lint suppressions
######################################################################
##
## Everything below this point is generic, and does not contain
## project specific configuration. (with one noted case in the "build"
## target for library-only projects)
##
######################################################################
# start dependency installation tools
# implementation details for being able to lazily install dependencies
gopath := $(shell go env GOPATH)
lintDeps := $(addprefix $(gopath)/src/,$(lintDeps))
srcFiles := makefile $(shell find . -name "*.go" -not -path "./$(buildDir)/*" -not -name "*_test.go")
testSrcFiles := makefile $(shell find . -name "*.go" -not -path "./$(buildDir)/*")
testOutput := $(foreach target,$(packages),$(buildDir)/output.$(target).test)
raceOutput := $(foreach target,$(packages),$(buildDir)/output.$(target).race)
testBin := $(foreach target,$(packages),$(buildDir)/test.$(target))
raceBin := $(foreach target,$(packages),$(buildDir)/race.$(target))
coverageOutput := $(foreach target,$(packages),$(buildDir)/output.$(target).coverage)
coverageHtmlOutput := $(foreach target,$(packages),$(buildDir)/output.$(target).coverage.html)
$(gopath)/src/%:
@-[ ! -d $(gopath) ] && mkdir -p $(gopath) || true
go get $(subst $(gopath)/src/,,$@)
$(buildDir)/run-linter:buildscripts/run-linter.go $(buildDir)/.lintSetup
$(vendorGopath) go build -o $@ $<
$(buildDir)/.lintSetup:$(lintDeps)
@-$(gopath)/bin/gometalinter --install >/dev/null && touch $@
# end dependency installation tools
# userfacing targets for basic build and development operations
lint:$(buildDir)/output.lint
build:$(deps) $(srcFiles) $(gopath)/src/$(projectPath)
$(vendorGopath) go build $(subst $(name),,$(subst -,/,$(foreach pkg,$(packages),./$(pkg))))
build-race:$(deps) $(srcFiles) $(gopath)/src/$(projectPath)
$(vendorGopath) go build -race $(subst -,/,$(foreach pkg,$(packages),./$(pkg)))
test:$(testOutput)
race:$(raceOutput)
coverage:$(coverageOutput)
coverage-html:$(coverageHtmlOutput)
phony := lint build build-race race test coverage coverage-html deps
.PRECIOUS:$(testOutput) $(raceOutput) $(coverageOutput) $(coverageHtmlOutput)
.PRECIOUS:$(foreach target,$(packages),$(buildDir)/test.$(target))
.PRECIOUS:$(foreach target,$(packages),$(buildDir)/race.$(target))
.PRECIOUS:$(foreach target,$(packages),$(buildDir)/output.$(target).lint)
.PRECIOUS:$(buildDir)/output.lint
# end front-ends
# implementation details for building the binary and creating a
# convienent link in the working directory
$(gopath)/src/$(orgPath):
@mkdir -p $@
$(gopath)/src/$(projectPath):$(gopath)/src/$(orgPath)
@[ -L $@ ] || ln -s $(shell pwd) $@
$(name):$(buildDir)/$(name)
@[ -L $@ ] || ln -s $< $@
$(buildDir)/$(name):$(gopath)/src/$(projectPath) $(srcFiles) $(deps)
$(vendorGopath) go build -o $@ main/$(name).go
$(buildDir)/$(name).race:$(gopath)/src/$(projectPath) $(srcFiles) $(deps)
$(vendorGopath) go build -race -o $@ main/$(name).go
# end main build
# convenience targets for runing tests and coverage tasks on a
# specific package.
makeArgs := --no-print-directory
race-%:$(buildDir)/output.%.race
@grep -s -q -e "^PASS" $< && ! grep -s -q "^WARNING: DATA RACE" $<
test-%:$(buildDir)/output.%.test
@grep -s -q -e "^PASS" $<
coverage-%:$(buildDir)/output.%.coverage
@grep -s -q -e "^PASS" $<
html-coverage-%:$(buildDir)/output.%.coverage $(buildDir)/output.%.coverage.html
@grep -s -q -e "^PASS" $<
lint-%:$(buildDir)/output.%.lint
@grep -v -s -q "^--- FAIL" $<
# end convienence targets
# start test and coverage artifacts
# tests have compile and runtime deps. This varable has everything
# that the tests actually need to run. (The "build" target is
# intentional and makes these targets rerun as expected.)
testArgs := -test.v --test.timeout=5m
ifneq (,$(RUN_TEST))
testArgs += -test.run='$(RUN_TEST)'
endif
# to avoid vendoring the coverage tool, install it as needed
coverDeps := golang.org/x/tools/cmd/cover
coverDeps := $(addprefix $(gopath)/src/,$(coverDeps))
# implementation for package coverage and test running,mongodb to produce
# and save test output.
$(buildDir)/test.%:$(testSrcFiles) $(coverDeps)
$(vendorGopath) go test $(if $(DISABLE_COVERAGE),,-covermode=count) -c -o $@ ./$(subst -,/,$*)
$(buildDir)/race.%:$(testSrcFiles)
$(vendorGopath) go test -race -c -o $@ ./$(subst -,/,$*)
# targets to run any tests in the top-level package
$(buildDir)/test.$(name):$(testSrcFiles) $(coverDeps)
$(vendorGopath) go test $(if $(DISABLE_COVERAGE),,-covermode=count) -c -o $@ ./
$(buildDir)/race.$(name):$(testSrcFiles)
$(vendorGopath) go test -race -c -o $@ ./
# targets to run the tests and report the output
$(buildDir)/output.%.test:$(buildDir)/test.% .FORCE
$(testRunEnv) ./$< $(testArgs) 2>&1 | tee $@
$(buildDir)/output.%.race:$(buildDir)/race.% .FORCE
$(testRunEnv) ./$< $(testArgs) 2>&1 | tee $@
# targets to generate gotest output from the linter.
$(buildDir)/output.%.lint:$(buildDir)/run-linter $(testSrcFiles) .FORCE
@./$< --output=$@ --lintArgs='$(lintArgs)' --packages='$*'
$(buildDir)/output.lint:$(buildDir)/run-linter .FORCE
@./$< --output="$@" --lintArgs='$(lintArgs)' --packages="$(packages)"
# targets to process and generate coverage reports
$(buildDir)/output.%.coverage:$(buildDir)/test.% .FORCE $(coverDeps)
$(testRunEnv) ./$< $(testArgs) -test.coverprofile=$@ | tee $(subst coverage,test,$@)
@-[ -f $@ ] && go tool cover -func=$@ | sed 's%$(projectPath)/%%' | column -t
$(buildDir)/output.%.coverage.html:$(buildDir)/output.%.coverage $(coverDeps)
$(vendorGopath) go tool cover -html=$< -o $@
# end test and coverage artifacts
# start vendoring configuration
# begin with configuration of dependencies
vendorDeps := github.com/Masterminds/glide
vendorDeps := $(addprefix $(gopath)/src/,$(vendorDeps))
vendor-deps:$(vendorDeps)
# this allows us to store our vendored code in vendor and use
# symlinks to support vendored code both in the legacy style and with
# new-style vendor directories. When this codebase can drop support
# for go1.4, we can delete most of this.
-include $(buildDir)/makefile.vendor
nestedVendored :=
nestedVendored := $(foreach project,$(nestedVendored),$(project)/build/vendor)
$(buildDir)/makefile.vendor:$(buildDir)/render-gopath makefile
@mkdir -p $(buildDir)
@echo "vendorGopath := \$$(shell \$$(buildDir)/render-gopath $(nestedVendored))" >| $@
# targets for the directory components and manipulating vendored files.
vendor-sync:$(vendorDeps)
glide install -s
vendor-clean:
rm -rf vendor/gopkg.in/mgo.v2/harness/
rm -rf vendor/github.com/stretchr/testify/vendor/
rm -rf vendor/github.com/mongodb/amboy/vendor/github.com/tychoish/grip/
rm -rf vendor/github.com/mongodb/amboy/vendor/golang.org/x/net/
sed -ri 's/json:"(.*)"/json:"\1" bson:"\1"/' `grep -R -l 'json:\".*\" [^bson]' vendor/github.com/shirou/gopsutil/*` || true
find vendor/github.com/shirou/gopsutil/ -name "*.go" | xargs -n 1 go fmt || true
find vendor/ -name "*.gif" -o -name "*.gz" -o -name "*.png" -o -name "*.ico" -o -name "*.dat" -o -name "*testdata" | xargs rm -fr
change-go-version:
rm -rf $(buildDir)/make-vendor $(buildDir)/render-gopath
@$(MAKE) $(makeArgs) vendor > /dev/null 2>&1
vendor:$(buildDir)/vendor/src
$(buildDir)/vendor/src:$(buildDir)/make-vendor $(buildDir)/render-gopath
@./$(buildDir)/make-vendor
# targets to build the small programs used to support vendoring.
$(buildDir)/make-vendor:buildscripts/make-vendor.go
@mkdir -p $(buildDir)
go build -o $@ $<
$(buildDir)/render-gopath:buildscripts/render-gopath.go
@mkdir -p $(buildDir)
go build -o $@ $<
# define dependencies for buildscripts
buildscripts/make-vendor.go:buildscripts/vendoring/vendoring.go
buildscripts/render-gopath.go:buildscripts/vendoring/vendoring.go
# add phony targets
phony += vendor vendor-deps vendor-clean vendor-sync change-go-version
# end vendoring tooling configuration
# clean and other utility targets
clean:
rm -rf $(name) $(lintDeps) $(buildDir)/test.* $(buildDir)/coverage.* $(buildDir)/race.*
phony += clean
# end dependency targets
# configure phony targets
.FORCE:
.PHONY:$(phony) .FORCE