2
2
# global
3
3
4
4
SHELL = /usr/bin/env bash
5
+
5
6
ifneq ($(shell command -v go) ,)
6
7
GO_PATH ?= $(shell go env GOPATH)
7
8
GO_OS ?= $(shell go env GOOS)
@@ -11,10 +12,7 @@ PKG := $(subst $(GO_PATH)/src/,,$(CURDIR))
11
12
GO_PKGS := $(shell go list ./... | grep -v -e '.pb.go')
12
13
GO_APP_PKGS := $(shell go list -f '{{if and (or .GoFiles .CgoFiles) (ne .Name "main")}}{{.ImportPath}}{{end}}' ${PKG}/...)
13
14
GO_TEST_PKGS := $(shell go list -f='{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...)
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/...)
17
- endif
15
+ GO_VENDOR_PKGS := $(shell go list -f '{{if and (or .GoFiles .CgoFiles) (ne .Name "main")}}./vendor/{{.ImportPath}}{{end}}' ./vendor/...)
18
16
19
17
GO_TEST ?= go test
20
18
GO_TEST_FUNC ?= .
@@ -23,11 +21,8 @@ GO_BENCH_FUNC ?= .
23
21
GO_BENCH_FLAGS ?= -benchmem
24
22
25
23
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
24
+ GO_LDFLAGS =-s -w
25
+ GO_LDFLAGS_STATIC =-s -w '-extldflags=-static'
31
26
32
27
ifneq ($(wildcard go.mod) ,) # exist go.mod
33
28
ifeq ($(GO111MODULE ) ,off)
52
47
# ----------------------------------------------------------------------------
53
48
# targets
54
49
50
+ # # build and install
51
+
52
+ .PHONY : pkg/install
53
+ pkg/install :
54
+ $(call target)
55
+ GO111MODULE=on CGO_ENABLED=$(CGO_ENABLED ) GOOS=$(GO_OS ) GOARCH=$(GO_ARCH ) go install -v ${GO_APP_PKGS}
56
+
55
57
# # test, bench and coverage
56
58
57
59
.PHONY : test
@@ -60,30 +62,30 @@ test: GO_BUILDTAGS+=${GO_BUILDTAGS_STATIC}
60
62
test : GO_FLAGS+=-installsuffix ${GO_INSTALLSUFFIX_STATIC}
61
63
test : # # Runs package test including race condition.
62
64
$(call target)
63
- $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -run=$(GO_TEST_FUNC ) $(GO_TEST_PKGS )
65
+ @GO111MODULE=on $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -run=$(GO_TEST_FUNC ) $(GO_TEST_PKGS )
64
66
65
67
.PHONY : bench
66
68
bench : GO_LDFLAGS=${GO_LDFLAGS_STATIC}
67
69
bench : GO_BUILDTAGS+=${GO_BUILDTAGS_STATIC}
68
70
bench : GO_FLAGS+=-installsuffix ${GO_INSTALLSUFFIX_STATIC}
69
71
bench : # # Take a package benchmark.
70
72
$(call target)
71
- $(GO_TEST ) -v $(strip $(GO_FLAGS ) ) -run=' ^$$' -bench=$(GO_BENCH_FUNC ) -benchmem $(GO_TEST_PKGS )
73
+ @GO111MODULE=on $(GO_TEST ) -v $(strip $(GO_FLAGS ) ) -run=' ^$$' -bench=$(GO_BENCH_FUNC ) -benchmem $(GO_TEST_PKGS )
72
74
73
75
.PHONY : bench/race
74
76
bench/race : # # Takes packages benchmarks with the race condition.
75
77
$(call target)
76
- $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -run=' ^$$' -bench=$(GO_BENCH_FUNC ) -benchmem $(GO_TEST_PKGS )
78
+ @GO111MODULE=on $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -run=' ^$$' -bench=$(GO_BENCH_FUNC ) -benchmem $(GO_TEST_PKGS )
77
79
78
80
.PHONY : bench/trace
79
81
bench/trace : # # Take a package benchmark with take a trace profiling.
80
82
$(GO_TEST ) -v -c -o bench-trace.test $(PKG ) /stackdriver
81
- GODEBUG=allocfreetrace=1 ./bench-trace.test -test.run=none -test.bench=$(GO_BENCH_FUNC ) -test.benchmem -test.benchtime=10ms 2> trace.log
83
+ GO111MODULE=on GODEBUG=allocfreetrace=1 ./bench-trace.test -test.run=none -test.bench=$(GO_BENCH_FUNC ) -test.benchmem -test.benchtime=10ms 2> trace.log
82
84
83
85
.PHONY : coverage
84
86
coverage : # # Takes packages test coverage.
85
87
$(call target)
86
- $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -covermode=atomic -coverpkg=$(PKG ) /... -coverprofile=coverage.out $(GO_PKGS )
88
+ GO111MODULE=on $(GO_TEST ) -v -race $(strip $(GO_FLAGS ) ) -covermode=atomic -coverpkg=$(PKG ) /... -coverprofile=coverage.out $(GO_PKGS )
87
89
88
90
$(GO_PATH ) /bin/go-junit-report :
89
91
@GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
@@ -99,23 +101,23 @@ coverage/ci: cmd/go-junit-report
99
101
coverage/ci : # # Takes packages test coverage, and output coverage results to CI artifacts.
100
102
$(call target)
101
103
@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
104
+ GO111MODULE=on $(GO_TEST ) -a - v -race $(strip $(GO_FLAGS ) ) -covermode=atomic -coverpkg=$(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
105
@if [[ -f ' /tmp/ci/artifacts/coverage.out' ]]; then go tool cover -html=/tmp/ci/artifacts/coverage.out -o /tmp/ci/artifacts/coverage.html; fi
104
106
105
107
106
108
# # lint
107
109
108
110
.PHONY : lint
109
- lint : lint/golangci-lint # # Runs the all linters.
111
+ lint : lint/golangci-lint # # Run all linters.
110
112
111
113
$(GO_PATH ) /bin/golangci-lint :
112
- GO111MODULE=off go get -u -v github.com/golangci/golangci-lint/cmd/golangci-lint
114
+ @ GO111MODULE=off go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
113
115
114
116
.PHONY : cmd/golangci-lint
115
- cmd/golangci-lint : $(GO_PATH ) /bin/golangci-lint # go get 'golangci-lint' binary.
117
+ cmd/golangci-lint : $(GO_PATH ) /bin/golangci-lint # go get 'golangci-lint' binary
116
118
117
119
.PHONY : golangci-lint
118
- lint/golangci-lint : cmd/golangci-lint .golangci.yml # # Runs the ` golangci-lint` linter .
120
+ lint/golangci-lint : cmd/golangci-lint .golangci.yml # # Run golangci-lint.
119
121
$(call target)
120
122
@GO111MODULE=on golangci-lint run ./...
121
123
@@ -191,7 +193,7 @@ boilerplate/go/%: BOILERPLATE_PKG_DIR=$(shell printf $@ | cut -d'/' -f3- | rev |
191
193
boilerplate/go/% : BOILERPLATE_PKG_NAME=$(if $(findstring main,$@ ) ,main,$(shell printf $@ | rev | cut -d/ -f2 | rev) )
192
194
boilerplate/go/% : hack/boilerplate/boilerplate.go.txt
193
195
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
196
+ @if [[ ! ${BOILERPLATE_PKG_DIR} == * ' .go' * ] ] && [ ! -d ${BOILERPLATE_PKG_DIR} ]; then mkdir -p ${BOILERPLATE_PKG_DIR} ; fi
195
197
@cat hack/boilerplate/boilerplate.go.txt <( printf " package ${BOILERPLATE_PKG_NAME} \\ n" ) > $*
196
198
@sed -i " s|YEAR|$( shell date ' +%Y' ) |g" $*
197
199
0 commit comments