2
2
# global
3
3
4
4
SHELL = /usr/bin/env bash
5
- GO_PATH = $(shell go env GOPATH)
5
+ ifneq ($(shell command -v go) ,)
6
+ GO_PATH ?= $(shell go env GOPATH)
7
+ GO_OS ?= $(shell go env GOOS)
8
+ GO_ARCH ?= $(shell go env GOARCH)
6
9
7
10
PKG := $(subst $(GO_PATH ) /src/,,$(CURDIR ) )
8
11
GO_PKGS := $(shell go list ./... | grep -v -e '.pb.go')
9
- GO_PKGS_ABS := $(shell go list -f '$( GO_PATH ) /src/ {{.ImportPath}}' . /... | grep -v -e '.pb.go' )
12
+ GO_APP_PKGS := $(shell go list -f '{{if and (or .GoFiles .CgoFiles) (ne .Name "main")}} {{.ImportPath}}{{end}}' ${PKG} /...)
10
13
GO_TEST_PKGS := $(shell go list -f='{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...)
11
- GO_VENDOR_PKGS := $(shell go list -f '{{if and (or .GoFiles .CgoFiles) (ne .Name "main")}}./vendor/{{.ImportPath}}{{end}}' ./vendor/...)
12
-
13
- GIT_COMMIT := $(shell git rev-parse --short HEAD)
14
- GIT_UNTRACKED_CHANGES: = $(shell git status --porcelain --untracked-files=no)
15
- ifneq ($(GIT_UNTRACKED_CHANGES ) ,)
16
- GIT_COMMIT := $(GIT_COMMIT)-dirty
17
- endif
18
-
19
- CGO_ENABLED ?= 0
20
- GO_LDFLAGS: =-ldflags "-w"
21
- GO_LDFLAGS_STATIC: =-ldflags "-w -extldflags -static"
22
- GO_BUILD_TAGS := osusergo
23
- GO_FLAGS ?= -tags '$(GO_BUILD_TAGS ) '
24
-
25
- ifneq ($(wildcard go.mod) ,) # exist go.mod file
26
- ifeq ($(CI ) ,) # $CI is empty
27
- GO_FLAGS+=-mod=vendor
28
- else
29
- GO_FLAGS+=-mod=readonly
30
- endif
14
+ GO_VENDOR_PKGS =
15
+ ifneq ($(wildcard ./vendor) ,)
16
+ GO_VENDOR_PKGS = $(shell go list -f '{{if and (or .GoFiles .CgoFiles) (ne .Name "main")}}./vendor/{{.ImportPath}}{{end}}' ./vendor/...)
31
17
endif
32
18
33
19
GO_TEST ?= go test
@@ -36,14 +22,31 @@ GO_TEST_FLAGS ?=
36
22
GO_BENCH_FUNC ?= .
37
23
GO_BENCH_FLAGS ?= -benchmem
38
24
39
- IMAGE_REGISTRY := gcr.io/container-image
25
+ CGO_ENABLED ?= 0
26
+ GO_LDFLAGS =-s -w $(CTIMEVAR )
27
+ GO_LDFLAGS_STATIC =-s -w '-extldflags=-static' $(CTIMEVAR )
28
+ ifneq ($(GO_OS ) ,darwin)
29
+ GO_LDFLAGS_STATIC+=-d
30
+ endif
31
+
32
+ ifneq ($(wildcard go.mod) ,) # exist go.mod
33
+ ifeq ($(GO111MODULE ) ,off)
34
+ GO_FLAGS=-mod=vendor
35
+ endif
36
+ endif
37
+
38
+ GO_BUILDTAGS =osusergo netgo
39
+ GO_BUILDTAGS_STATIC =static static_build
40
+ GO_FLAGS ?= -tags='$(GO_BUILDTAGS ) ' -ldflags="${GO_LDFLAGS}"
41
+ GO_INSTALLSUFFIX_STATIC =netgo
42
+ endif
40
43
41
44
# ----------------------------------------------------------------------------
42
45
# defines
43
46
44
47
GOPHER = ""
45
48
define target
46
- @printf "$(GOPHER ) \\033[ 32m$(patsubst ,$@ ,$(1 ) ) \\033 [0m\\n"
49
+ @printf "$(GOPHER ) \\x1b[1; 32m$(patsubst ,$@ ,$(1 ) ) \\x1b [0m\\n"
47
50
endef
48
51
49
52
# ----------------------------------------------------------------------------
@@ -52,134 +55,158 @@ endef
52
55
# # test, bench and coverage
53
56
54
57
.PHONY : test
55
- test : # # Run the package test with checks race condition.
58
+ test : GO_LDFLAGS=${GO_LDFLAGS_STATIC}
59
+ test : GO_BUILDTAGS+=${GO_BUILDTAGS_STATIC}
60
+ test : GO_FLAGS+=-installsuffix ${GO_INSTALLSUFFIX_STATIC}
61
+ test : # # Runs package test including race condition.
56
62
$(call target)
57
- $(GO_TEST ) -v -race $(strip $(GOFLAGS ) ) -run=$(GO_TEST_FUNC ) $(GO_TEST_PKGS )
63
+ $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -run=$(GO_TEST_FUNC ) $(GO_TEST_PKGS )
58
64
59
65
.PHONY : bench
66
+ bench : GO_LDFLAGS=${GO_LDFLAGS_STATIC}
67
+ bench : GO_BUILDTAGS+=${GO_BUILDTAGS_STATIC}
68
+ bench : GO_FLAGS+=-installsuffix ${GO_INSTALLSUFFIX_STATIC}
60
69
bench : # # Take a package benchmark.
61
70
$(call target)
62
- $(GO_TEST ) -v $(strip $(GOFLAGS ) ) -run=' ^$$' -bench=$(GO_BENCH_FUNC ) -benchmem $(GO_TEST_PKGS )
71
+ $(GO_TEST ) -v $(strip $(GO_FLAGS ) ) -run=' ^$$' -bench=$(GO_BENCH_FUNC ) -benchmem $(GO_TEST_PKGS )
63
72
64
73
.PHONY : bench/race
65
- bench/race : # # Take a package benchmark with checks race condition.
74
+ bench/race : # # Takes packages benchmarks with the race condition.
66
75
$(call target)
67
- $(GO_TEST ) -v -race $(strip $(GOFLAGS ) ) -run=' ^$$' -bench=$(GO_BENCH_FUNC ) -benchmem $(GO_TEST_PKGS )
76
+ $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -run=' ^$$' -bench=$(GO_BENCH_FUNC ) -benchmem $(GO_TEST_PKGS )
68
77
69
78
.PHONY : bench/trace
70
79
bench/trace : # # Take a package benchmark with take a trace profiling.
71
80
$(GO_TEST ) -v -c -o bench-trace.test $(PKG ) /stackdriver
72
81
GODEBUG=allocfreetrace=1 ./bench-trace.test -test.run=none -test.bench=$(GO_BENCH_FUNC ) -test.benchmem -test.benchtime=10ms 2> trace.log
73
82
74
83
.PHONY : coverage
75
- coverage : # # Take test coverage.
84
+ coverage : # # Takes packages test coverage.
76
85
$(call target)
77
- $(GO_TEST ) -v -race $(strip $(GOFLAGS ) ) -covermode=atomic -coverpkg=$(PKG ) /... -coverprofile=coverage.out $(GO_PKGS )
86
+ $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -covermode=atomic -coverpkg=$(PKG ) /... -coverprofile=coverage.out $(GO_PKGS )
78
87
79
88
$(GO_PATH ) /bin/go-junit-report :
80
89
@GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
81
90
82
91
.PHONY : cmd/go-junit-report
83
92
cmd/go-junit-report : $(GO_PATH ) /bin/go-junit-report # go get 'go-junit-report' binary
84
93
85
- .PHONY : coverage/junit
86
- coverage/junit : cmd/go-junit-report # # Take test coverage and output test results with junit syntax.
94
+ .PHONY : coverage/ci
95
+ coverage/ci : GO_LDFLAGS=${GO_LDFLAGS_STATIC}
96
+ coverage/ci : GO_BUILDTAGS+=${GO_BUILDTAGS_STATIC}
97
+ coverage/ci : GO_FLAGS+=-installsuffix ${GO_INSTALLSUFFIX_STATIC}
98
+ coverage/ci : cmd/go-junit-report
99
+ coverage/ci : # # Takes packages test coverage, and output coverage results to CI artifacts.
87
100
$(call target)
88
- @mkdir -p test-results
89
- $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -covermode=atomic -coverpkg=$(PKG ) /... -coverprofile=coverage.out $(GO_PKGS ) 2>&1 | tee /dev/stderr | go-junit-report -set-exit-code > test-results/report.xml
101
+ @mkdir -p /tmp/ci/artifacts /tmp/ci/test-results
102
+ $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -covermode=atomic -coverpkg=$(PKG ) /pkg/... -coverprofile=/tmp/ci/artifacts/coverage.out $(GO_PKGS ) 2>&1 | tee /dev/stderr | go-junit-report -set-exit-code > /tmp/ci/test-results/junit.xml
103
+ @if [[ -f ' /tmp/ci/artifacts/coverage.out' ]]; then go tool cover -html=/tmp/ci/artifacts/coverage.out -o /tmp/ci/artifacts/coverage.html; fi
90
104
91
105
92
106
# # lint
93
107
94
108
.PHONY : lint
95
- lint : lint/fmt lint/golangci-lint # # Run all linters.
96
-
97
- .PHONY : lint/fmt
98
- lint/fmt : # # Verifies all files have been `gofmt`ed.
99
- $(call target)
100
- @gofmt -s -l . 2>&1 | grep -v -E -e ' testdata' -e ' vendor' -e ' \.pb.go' -e ' _.*' | tee /dev/stderr
109
+ lint : lint/golangci-lint # # Runs the all linters.
101
110
102
111
$(GO_PATH ) /bin/golangci-lint :
103
- @ GO111MODULE=off go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
112
+ GO111MODULE=off go get -u -v github.com/golangci/golangci-lint/cmd/golangci-lint
104
113
105
114
.PHONY : cmd/golangci-lint
106
- cmd/golangci-lint : $(GO_PATH ) /bin/golangci-lint # go get 'golangci-lint' binary
115
+ cmd/golangci-lint : $(GO_PATH ) /bin/golangci-lint # go get 'golangci-lint' binary.
107
116
108
117
.PHONY : golangci-lint
109
- lint/golangci-lint : cmd/golangci-lint .golangci.yml # # Run golangci-lint.
118
+ lint/golangci-lint : cmd/golangci-lint .golangci.yml # # Runs the ` golangci-lint` linter .
110
119
$(call target)
111
- @golangci-lint run ./...
120
+ @GO111MODULE=on golangci-lint run ./...
112
121
113
122
114
123
# # mod
115
124
116
125
.PHONY : mod/init
117
- mod/init :
126
+ mod/init : # # Initializes and writes a new `go.mod` to the current directory.
118
127
$(call target)
119
- @GO111MODULE=on go mod init || true
128
+ @GO111MODULE=on go mod init > /dev/null 2>&1 || true
120
129
121
- .PHONY : mod/goget
122
- mod/goget : # # Update module and go.mod.
130
+ .PHONY : mod/get
131
+ mod/get : # # Updates all module packages and go.mod.
123
132
$(call target)
124
133
@GO111MODULE=on go get -u -m -v -x ./...
125
134
126
135
.PHONY : mod/tidy
127
- mod/tidy :
136
+ mod/tidy : # # Makes sure go.mod matches the source code in the module.
128
137
$(call target)
129
138
@GO111MODULE=on go mod tidy -v
130
139
131
140
.PHONY : mod/vendor
132
- mod/vendor :
141
+ mod/vendor : mod/tidy # # Resets the module's vendor directory and fetch all modules packages.
133
142
$(call target)
134
143
@GO111MODULE=on go mod vendor -v
135
144
136
145
.PHONY : mod/graph
137
- mod/graph :
146
+ mod/graph : # # Prints the module requirement graph with replacements applied.
138
147
$(call target)
139
148
@GO111MODULE=on go mod graph
140
149
141
150
.PHONY : mod/clean
142
- mod/clean :
151
+ mod/clean : # # Cleanups go.sum and vendor/modules.txt files.
143
152
$(call target)
144
- @$(RM ) go.mod go.sum
145
- @$(RM ) -r vendor
153
+ @$(RM ) -r go.sum $(shell find vendor -maxdepth 1 -path "vendor/* " -type d)
146
154
147
155
.PHONY : mod/install
148
156
mod/install : mod/tidy mod/vendor
157
+ mod/install : # # Install the module vendor package as an object file.
149
158
$(call target)
150
- @GO111MODULE=off go install -v $(GO_VENDOR_PKGS ) || GO111MODULE=on go install -mod=vendor -v $(GO_VENDOR_PKGS )
159
+ @GO111MODULE=off go install -v $(strip $(GO_FLAGS ) ) $(GO_VENDOR_PKGS ) || GO111MODULE=on go install -mod=vendor -v $(strip $(GO_FLAGS ) ) $(GO_VENDOR_PKGS )
160
+
161
+ .PHONY : mod/install/static
162
+ mod/install/static : GO_LDFLAGS=${GO_LDFLAGS_STATIC}
163
+ mod/install/static : GO_BUILDTAGS+=${GO_BUILDTAGS_STATIC}
164
+ mod/install/static : GO_FLAGS+=-installsuffix ${GO_INSTALLSUFFIX_STATIC}
165
+ mod/install/static : mod/install
166
+ mod/install/static : # # Install the module vendor package as an object file with static build.
167
+
168
+
169
+ .PHONY : mod/update
170
+ mod/update : mod/goget mod/tidy mod/vendor mod/install # # Updates all of vendor packages.
171
+ @sed -i ' :a;N;$$!ba;s|go 1\.12\n\n||g' go.mod
151
172
152
173
.PHONY : mod
153
- mod : mod/init mod/tidy mod/vendor mod/install # # Updates the vendoring directory via go mod.
174
+ mod : mod/init mod/tidy mod/vendor mod/install
175
+ mod : # # Updates the vendoring directory using go mod.
154
176
@sed -i ' :a;N;$$!ba;s|go 1\.12\n\n||g' go.mod
155
177
156
- .PHONY : mod/update
157
- mod/update : mod/goget mod/tidy mod/vendor mod/install # # Updates all vendor packages.
158
178
179
+ # # clean
180
+
181
+ .PHONY : clean
182
+ clean : # # Cleanups binaries and extra files in the package.
183
+ $(call target)
184
+ @$(RM ) $(APP ) * .out * .test * .prof trace.log
159
185
160
- # # miscellaneous
186
+
187
+ # # boilerplate
161
188
162
189
.PHONY : boilerplate/go/%
163
190
boilerplate/go/% : BOILERPLATE_PKG_DIR=$(shell printf $@ | cut -d'/' -f3- | rev | cut -d'/' -f2- | rev)
164
191
boilerplate/go/% : BOILERPLATE_PKG_NAME=$(if $(findstring main,$@ ) ,main,$(shell printf $@ | rev | cut -d/ -f2 | rev) )
165
- boilerplate/go/% : hack/boilerplate/boilerplate.go.txt # # Create go file from boilerplate.go.txt
166
- @if [ ${BOILERPLATE_PKG_DIR} != " *.go" ] && [ ! -d ${BOILERPLATE_PKG_DIR} ]; then mkdir -p ${BOILERPLATE_PKG_DIR} ; fi
192
+ boilerplate/go/% : hack/boilerplate/boilerplate.go.txt
193
+ boilerplate/go/% : # # Creates a go file based on boilerplate.go.txt in % location.
194
+ @if [ ! ${BOILERPLATE_PKG_DIR:- 3} == " .go" ] && [ ! -d ${BOILERPLATE_PKG_DIR} ]; then mkdir -p ${BOILERPLATE_PKG_DIR} ; fi
167
195
@cat hack/boilerplate/boilerplate.go.txt <( printf " package ${BOILERPLATE_PKG_NAME} \\ n" ) > $*
168
196
@sed -i " s|YEAR|$( shell date ' +%Y' ) |g" $*
169
197
170
198
199
+ # # miscellaneous
200
+
171
201
.PHONY : AUTHORS
172
202
AUTHORS : # # Creates AUTHORS file.
173
203
@$(file >$@ ,# This file lists all individuals having contributed content to the repository.)
174
204
@$(file >>$@ ,# For how it is generated, see `make AUTHORS`.)
175
205
@printf " $( shell git log --format=" \n%aN <%aE>" | LC_ALL=C.UTF-8 sort -uf) " >> $@
176
206
177
-
178
- .PHONY : clean
179
- clean : # # Cleanup any build binaries or packages.
180
- $(call target)
181
- @$(RM ) $(APP ) * .out * .test * .prof trace.log
182
-
207
+ .PHONY : todo
208
+ TODO : # # Print the all of (TODO|BUG|XXX|FIXME|NOTE) in packages.
209
+ @rg -e ' (TODO|BUG|XXX|FIXME|NOTE)(\(.+\):|:)' --follow --hidden --glob=' !.git' --glob=' !vendor' --glob=' !internal' --glob=' !Makefile' --glob=' !snippets' --glob=' !indent'
183
210
184
211
.PHONY : help
185
212
help : # # Show make target help.
0 commit comments