Skip to content

Commit 45faa7e

Browse files
committed
hack/make/go: update go.mk to latest
1 parent 101b92d commit 45faa7e

File tree

1 file changed

+95
-68
lines changed

1 file changed

+95
-68
lines changed

hack/make/go.mk

Lines changed: 95 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,18 @@
22
# global
33

44
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)
69

710
PKG := $(subst $(GO_PATH)/src/,,$(CURDIR))
811
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}/...)
1013
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/...)
3117
endif
3218

3319
GO_TEST ?= go test
@@ -36,14 +22,31 @@ GO_TEST_FLAGS ?=
3622
GO_BENCH_FUNC ?= .
3723
GO_BENCH_FLAGS ?= -benchmem
3824

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
4043

4144
# ----------------------------------------------------------------------------
4245
# defines
4346

4447
GOPHER = ""
4548
define target
46-
@printf "$(GOPHER) \\033[32m$(patsubst ,$@,$(1))\\033[0m\\n"
49+
@printf "$(GOPHER) \\x1b[1;32m$(patsubst ,$@,$(1))\\x1b[0m\\n"
4750
endef
4851

4952
# ----------------------------------------------------------------------------
@@ -52,134 +55,158 @@ endef
5255
## test, bench and coverage
5356

5457
.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.
5662
$(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)
5864

5965
.PHONY: bench
66+
bench: GO_LDFLAGS=${GO_LDFLAGS_STATIC}
67+
bench: GO_BUILDTAGS+=${GO_BUILDTAGS_STATIC}
68+
bench: GO_FLAGS+=-installsuffix ${GO_INSTALLSUFFIX_STATIC}
6069
bench: ## Take a package benchmark.
6170
$(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)
6372

6473
.PHONY: bench/race
65-
bench/race: ## Take a package benchmark with checks race condition.
74+
bench/race: ## Takes packages benchmarks with the race condition.
6675
$(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)
6877

6978
.PHONY: bench/trace
7079
bench/trace: ## Take a package benchmark with take a trace profiling.
7180
$(GO_TEST) -v -c -o bench-trace.test $(PKG)/stackdriver
7281
GODEBUG=allocfreetrace=1 ./bench-trace.test -test.run=none -test.bench=$(GO_BENCH_FUNC) -test.benchmem -test.benchtime=10ms 2> trace.log
7382

7483
.PHONY: coverage
75-
coverage: ## Take test coverage.
84+
coverage: ## Takes packages test coverage.
7685
$(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)
7887

7988
$(GO_PATH)/bin/go-junit-report:
8089
@GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
8190

8291
.PHONY: cmd/go-junit-report
8392
cmd/go-junit-report: $(GO_PATH)/bin/go-junit-report # go get 'go-junit-report' binary
8493

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.
87100
$(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
90104

91105

92106
## lint
93107

94108
.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.
101110

102111
$(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
104113

105114
.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.
107116

108117
.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.
110119
$(call target)
111-
@golangci-lint run ./...
120+
@GO111MODULE=on golangci-lint run ./...
112121

113122

114123
## mod
115124

116125
.PHONY: mod/init
117-
mod/init:
126+
mod/init: ## Initializes and writes a new `go.mod` to the current directory.
118127
$(call target)
119-
@GO111MODULE=on go mod init || true
128+
@GO111MODULE=on go mod init > /dev/null 2>&1 || true
120129

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.
123132
$(call target)
124133
@GO111MODULE=on go get -u -m -v -x ./...
125134

126135
.PHONY: mod/tidy
127-
mod/tidy:
136+
mod/tidy: ## Makes sure go.mod matches the source code in the module.
128137
$(call target)
129138
@GO111MODULE=on go mod tidy -v
130139

131140
.PHONY: mod/vendor
132-
mod/vendor:
141+
mod/vendor: mod/tidy ## Resets the module's vendor directory and fetch all modules packages.
133142
$(call target)
134143
@GO111MODULE=on go mod vendor -v
135144

136145
.PHONY: mod/graph
137-
mod/graph:
146+
mod/graph: ## Prints the module requirement graph with replacements applied.
138147
$(call target)
139148
@GO111MODULE=on go mod graph
140149

141150
.PHONY: mod/clean
142-
mod/clean:
151+
mod/clean: ## Cleanups go.sum and vendor/modules.txt files.
143152
$(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)
146154

147155
.PHONY: mod/install
148156
mod/install: mod/tidy mod/vendor
157+
mod/install: ## Install the module vendor package as an object file.
149158
$(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
151172

152173
.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.
154176
@sed -i ':a;N;$$!ba;s|go 1\.12\n\n||g' go.mod
155177

156-
.PHONY: mod/update
157-
mod/update: mod/goget mod/tidy mod/vendor mod/install ## Updates all vendor packages.
158178

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
159185

160-
## miscellaneous
186+
187+
## boilerplate
161188

162189
.PHONY: boilerplate/go/%
163190
boilerplate/go/%: BOILERPLATE_PKG_DIR=$(shell printf $@ | cut -d'/' -f3- | rev | cut -d'/' -f2- | rev)
164191
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
167195
@cat hack/boilerplate/boilerplate.go.txt <(printf "package ${BOILERPLATE_PKG_NAME}\\n") > $*
168196
@sed -i "s|YEAR|$(shell date '+%Y')|g" $*
169197

170198

199+
## miscellaneous
200+
171201
.PHONY: AUTHORS
172202
AUTHORS: ## Creates AUTHORS file.
173203
@$(file >$@,# This file lists all individuals having contributed content to the repository.)
174204
@$(file >>$@,# For how it is generated, see `make AUTHORS`.)
175205
@printf "$(shell git log --format="\n%aN <%aE>" | LC_ALL=C.UTF-8 sort -uf)" >> $@
176206

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'
183210

184211
.PHONY: help
185212
help: ## Show make target help.

0 commit comments

Comments
 (0)