1
1
# This is the default Clever Golang Makefile.
2
2
# It is stored in the dev-handbook repo, github.com/Clever/dev-handbook
3
3
# Please do not alter this file directly.
4
- GOLANG_MK_VERSION := 1.0.0
4
+ GOLANG_MK_VERSION := 1.3.1
5
5
6
6
SHELL := /bin/bash
7
7
SYSTEM := $(shell uname -a | cut -d" " -f1 | tr '[:upper:]' '[:lower:]')
@@ -11,7 +11,7 @@ SYSTEM := $(shell uname -a | cut -d" " -f1 | tr '[:upper:]' '[:lower:]')
11
11
export TZ =UTC
12
12
13
13
# go build flags for use across all commands which accept them
14
- GO_BUILD_FLAGS := " -mod=vendor"
14
+ export GOFLAGS := -mod=vendor $( GOFLAGS )
15
15
16
16
# if the gopath includes several directories, use only the first
17
17
GOPATH =$(shell echo $$GOPATH | cut -d: -f1)
@@ -39,17 +39,19 @@ endef
39
39
# so we're defended against it breaking or changing in the future.
40
40
FGT := $(GOPATH ) /bin/fgt
41
41
$(FGT ) :
42
- go get github.com/GeertJohan/fgt@262f7b11eec07dc7b147c44641236f3212fee89d
42
+ go install -mod=readonly github.com/GeertJohan/fgt@262f7b11eec07dc7b147c44641236f3212fee89d
43
43
44
44
golang-ensure-curl-installed :
45
45
@command -v curl > /dev/null 2>&1 || { echo >&2 " curl not installed. Please install curl." ; exit 1; }
46
46
47
47
# Golint is a tool for linting Golang code for common errors.
48
48
# We pin its version because an update could add a new lint check which would make
49
49
# previously passing tests start failing without changing our code.
50
+ # this package is deprecated and frozen
51
+ # Infra recommendation is to eventually move to https://github.com/golangci/golangci-lint so don't fail on linting error for now
50
52
GOLINT := $(GOPATH ) /bin/golint
51
53
$(GOLINT ) :
52
- go get golang.org/x/lint/golint@738671d3881b9731cc63024d5d88cf28db875626
54
+ go install -mod=readonly golang.org/x/lint/golint@738671d3881b9731cc63024d5d88cf28db875626
53
55
54
56
# golang-fmt-deps requires the FGT tool for checking output
55
57
golang-fmt-deps : $(FGT )
@@ -74,22 +76,14 @@ endef
74
76
# golang-lint-deps-strict requires the golint tool for golang linting.
75
77
golang-lint-deps-strict : $(GOLINT ) $(FGT )
76
78
77
- # golang-lint-strict calls golint on all golang files in the pkg and fails if any lint
78
- # errors are found.
79
- # arg1: pkg path
80
- define golang-lint-strict
81
- @echo "LINTING $(1 ) ..."
82
- @PKG_PATH=$$(go list -f '{{.Dir}}' $(1 ) ) ; find $${PKG_PATH}/*.go -type f | grep -v gen_ | xargs $(FGT ) $(GOLINT )
83
- endef
84
-
85
79
# golang-test-deps is here for consistency
86
80
golang-test-deps :
87
81
88
82
# golang-test uses the Go toolchain to run all tests in the pkg.
89
83
# arg1: pkg path
90
84
define golang-test
91
85
@echo "TESTING $(1 ) ..."
92
- @go test $( GO_BUILD_FLAGS ) -v $(1 )
86
+ @go test -v $(1 )
93
87
endef
94
88
95
89
# golang-test-strict-deps is here for consistency
@@ -99,7 +93,22 @@ golang-test-strict-deps:
99
93
# arg1: pkg path
100
94
define golang-test-strict
101
95
@echo "TESTING $(1 ) ..."
102
- @go test -v $(GO_BUILD_FLAGS ) -race $(1 )
96
+ @go test -v -race $(1 )
97
+ endef
98
+
99
+ # golang-test-strict-cover-deps is here for consistency
100
+ golang-test-strict-cover-deps :
101
+
102
+ # golang-test-strict-cover uses the Go toolchain to run all tests in the pkg with the race and cover flag.
103
+ # appends coverage results to coverage.txt
104
+ # arg1: pkg path
105
+ define golang-test-strict-cover
106
+ @echo "TESTING $(1 ) ..."
107
+ @go test -v -race -cover -coverprofile=profile.tmp -covermode=atomic $(1 )
108
+ @if [ -f profile.tmp ]; then \
109
+ cat profile.tmp | tail -n +2 >> coverage.txt; \
110
+ rm profile.tmp; \
111
+ fi;
103
112
endef
104
113
105
114
# golang-vet-deps is here for consistency
@@ -109,7 +118,7 @@ golang-vet-deps:
109
118
# arg1: pkg path
110
119
define golang-vet
111
120
@echo "VETTING $(1 ) ..."
112
- @go vet $(GO_BUILD_FLAGS ) $( 1 )
121
+ @go vet $(1 )
113
122
endef
114
123
115
124
# golang-test-all-deps installs all dependencies needed for different test cases.
@@ -132,24 +141,52 @@ golang-test-all-strict-deps: golang-fmt-deps golang-lint-deps-strict golang-test
132
141
# arg1: pkg path
133
142
define golang-test-all-strict
134
143
$(call golang-fmt,$(1 ) )
135
- $(call golang-lint-strict ,$(1 ) )
144
+ $(call golang-lint,$(1 ) )
136
145
$(call golang-vet,$(1 ) )
137
146
$(call golang-test-strict,$(1 ) )
138
147
endef
139
148
140
- # golang-build: builds a golang binary. ensures CGO build is done during CI. This is needed to make a binary that works with a Docker alpine image.
149
+ # golang-test-all-strict-cover-deps: installs all dependencies needed for different test cases.
150
+ golang-test-all-strict-cover-deps : golang-fmt-deps golang-lint-deps-strict golang-test-strict-cover-deps golang-vet-deps
151
+
152
+ # golang-test-all-strict-cover calls fmt, lint, vet and test on the specified pkg with strict and cover
153
+ # requirements that no errors are thrown while linting.
154
+ # arg1: pkg path
155
+ define golang-test-all-strict-cover
156
+ $(call golang-fmt,$(1 ) )
157
+ $(call golang-lint,$(1 ) )
158
+ $(call golang-vet,$(1 ) )
159
+ $(call golang-test-strict-cover,$(1 ) )
160
+ endef
161
+
162
+ # golang-build: builds a golang binary
141
163
# arg1: pkg path
142
164
# arg2: executable name
143
165
define golang-build
144
- @echo "BUILDING..."
145
- @if [ -z "$$CI" ]; then \
146
- go build $(GO_BUILD_FLAGS ) -o bin/$(2 ) $(1 ) ; \
147
- else \
148
- echo "-> Building CGO binary"; \
149
- CGO_ENABLED=0 go build $(GO_BUILD_FLAGS ) -installsuffix cgo -o bin/$(2 ) $(1 ) ; \
150
- fi;
166
+ @echo "BUILDING $(2 ) ..."
167
+ @CGO_ENABLED=0 go build -o bin/$(2 ) $(1 ) ;
168
+ endef
169
+
170
+ # golang-debug-build: builds a golang binary with debugging capabilities
171
+ # arg1: pkg path
172
+ # arg2: executable name
173
+ define golang-debug-build
174
+ @echo "BUILDING $(2 ) FOR DEBUG..."
175
+ @CGO_ENABLED=0 go build -gcflags="all=-N -l" -o bin/$(2 ) $(1 ) ;
151
176
endef
152
177
178
+ # golang-cgo-build: builds a golang binary with CGO
179
+ # arg1: pkg path
180
+ # arg2: executable name
181
+ define golang-cgo-build
182
+ @echo "BUILDING $(2 ) WITH CGO ..."
183
+ @CGO_ENABLED=1 go build -installsuffix cgo -o bin/$(2 ) $(1 ) ;
184
+ endef
185
+
186
+ # golang-setup-coverage: set up the coverage file
187
+ golang-setup-coverage :
188
+ @echo " mode: atomic" > coverage.txt
189
+
153
190
# golang-update-makefile downloads latest version of golang.mk
154
191
golang-update-makefile :
155
192
@wget https://raw.githubusercontent.com/Clever/dev-handbook/master/make/golang-v1.mk -O /tmp/golang.mk 2> /dev/null
0 commit comments