/
Makefile
165 lines (128 loc) · 5.43 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
# -----------------------------------------------------------------------------
# global
.DEFAULT_GOAL := help
comma := ,
empty :=
space := $(empty) $(empty)
# -----------------------------------------------------------------------------
# go
GO_PATH ?= $(shell go env GOPATH)
GO_OS ?= $(shell go env GOOS)
GO_ARCH ?= $(shell go env GOARCH)
PKG := $(subst $(GO_PATH)/src/,,$(CURDIR))
ORG_PKG := $(subst /$(notdir $(PKG)),,$(PKG))
CGO_ENABLED ?= 0
GO_BUILDTAGS=osusergo netgo static
GO_LDFLAGS=-s -w
VERSION=$(shell git rev-list --tags --max-count=1)
ifneq (${VERSION},)
GO_LDFLAGS+-X=${PKG}.version=${VERSION}
endif
ifeq (${GO_OS},linux)
GO_LDFLAGS+=-d
endif
GO_LDFLAGS+=-buildid= "-extldflags=-static"
GO_FLAGS ?= -tags='$(subst $(space),$(comma),${GO_BUILDTAGS})' -ldflags='${GO_LDFLAGS}'
GO_PKGS := $(shell go list ./...)
GO_TEST_PKGS ?= $(shell go list -f='{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...)
GO_TEST ?= ${TOOLS_BIN}/gotestsum --
GO_TEST_FLAGS ?= -race -count=1 -shuffle=on
GO_TEST_FUNC ?= .
GO_COVERAGE_OUT ?= coverage.out
GO_BENCH_FLAGS ?= -benchmem
GO_BENCH_FUNC ?= .
GO_LINT_FLAGS ?=
TOOLS_DIR := ${CURDIR}/tools
TOOLS_BIN := ${TOOLS_DIR}/bin
TOOLS = $(shell cd ${TOOLS_DIR}; go list -f '{{ join .Imports " " }}' -tags=tools)
JOBS = $(shell getconf _NPROCESSORS_CONF)
# -----------------------------------------------------------------------------
# defines
define target
@printf "+ $(patsubst ,$@,$(1))\\n" >&2
endef
# -----------------------------------------------------------------------------
# target
##@ build
.PHONY: ${GOBIN}/$(notdir ${PKG})
${GOBIN}/$(notdir ${PKG}):
$(call target,build)
@mkdir -p ./bin
CGO_ENABLED=0 go build -v ${GO_FLAGS} -o ./bin/$(notdir ${PKG}) ${PKG}
build: ${GOBIN}/$(notdir ${PKG}) ## Build binary.
##@ examples
PROTOC_OPTION = -I . -I ${HOME}/src/github.com/googleapis/googleapis
.PHONY: examples/spanner
examples/spanner: build ${TOOLS_BIN}/protoc-gen-go ${TOOLS_BIN}/protoc-gen-go-grpc
@mkdir -p ${@D}
protoc ${PROTOC_OPTION} \
--plugin=protoc-gen-go=$(TOOLS_BIN)/protoc-gen-go \
--plugin=protoc-gen-go-grpc=$(TOOLS_BIN)/protoc-gen-go-grpc \
--plugin=protoc-gen-grpcproxy=${CURDIR}/bin/protoc-gen-grpcproxy \
--go_out=paths=source_relative:examples --go-grpc_out=paths=source_relative:examples --proxy_out=paths=source_relative:examples ${HOME}/src/github.com/googleapis/googleapis/google/spanner/v1/*.proto
.PHONY: examples/spannerproxy
examples/spannerproxy: build
@rm -rf $@
@mkdir -p $@
protoc ${PROTOC_OPTION} \
--plugin=protoc-gen-grpcproxy=${CURDIR}/bin/protoc-gen-grpcproxy \
--proxy_out=paths=source_relative,standalone=true:examples/spannerproxy ${HOME}/src/github.com/googleapis/googleapis/google/spanner/v1/*.proto
@cd $@/google/spanner/v1; \
go mod init > /dev/null 2>&1; \
go mod edit -go=1.19 > /dev/null 2>&1; \
go mod tidy > /dev/null 2>&1
##@ test, bench, coverage
.PHONY: test
test: CGO_ENABLED=1
test: GO_FLAGS=-tags='$(subst ${space},${comma},${GO_BUILDTAGS})'
test: ${TOOLS_BIN}/gotestsum ## Runs package test including race condition.
$(call target)
@CGO_ENABLED=${CGO_ENABLED} GOTESTSUM_FORMAT=standard-verbose ${GO_TEST} ${GO_TEST_FLAGS} -run=${GO_TEST_FUNC} $(strip ${GO_FLAGS}) ${GO_TEST_PKGS}
.PHONY: coverage
coverage: CGO_ENABLED=1
coverage: GO_FLAGS=-tags='$(subst ${space},${comma},${GO_BUILDTAGS})'
coverage: ${TOOLS_BIN}/gotestsum ## Takes packages test coverage.
$(call target)
@CGO_ENABLED=${CGO_ENABLED} ${GO_TEST} ${GO_TEST_FLAGS} -covermode=atomic -coverpkg=${PKG}/... -coverprofile=${GO_COVERAGE_OUT} $(strip ${GO_FLAGS}) ${GO_PKGS}
##@ fmt, lint
.PHONY: fmt
fmt: ${TOOLS_BIN}/goimportz ${TOOLS_BIN}/gofumpt ## Run goimportz and gofumpt.
$(call target)
${TOOLS_BIN}/goimportz -local=${PKG},${ORG_PKG} -w $(shell find . -iname "*.go" -not -iname "*pb.go" -not -path "./vendor/**")
${TOOLS_BIN}/gofumpt -extra -w $(shell find . -iname "*.go" -not -iname "*pb.go" -not -path "./vendor/**")
.PHONY: lint
lint: lint/golangci-lint ## Run all linters.
.PHONY: lint/golangci-lint
lint/golangci-lint: ${TOOLS_BIN}/golangci-lint .golangci.yaml ## Run golangci-lint.
$(call target)
${TOOLS_BIN}/golangci-lint -j ${JOBS} run $(strip ${GO_LINT_FLAGS}) ./...
##@ tools
.PHONY: tools
tools: ${TOOLS_BIN}/'' ## Install tools
${TOOLS_BIN}/%: ${TOOLS_DIR}/go.sum ${TOOLS_DIR}/go.mod
@cd ${TOOLS_DIR}; \
for t in ${TOOLS}; do \
if [ ! $$(basename $$t) = 'cmd' ] && [ -z '$*' ] || [ $$(basename $$t) = '$*' ]; then \
echo "Install $$t"; \
GOBIN=${TOOLS_BIN} CGO_ENABLED=0 go install -v -mod=readonly ${GO_FLAGS} "$${t}"; \
fi \
done
##@ clean
.PHONY: clean
clean: ## Cleanups binaries and extra files in the package.
$(call target)
@rm -rf *.out *.test *.prof trace.txt ./bin ${TOOLS_BIN}
##@ help
.PHONY: help
help: ## Show this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[33m<target>\033[0m\n"} /^[a-zA-Z_0-9\/%_-]+:.*?##/ { printf " \033[1;32m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ miscellaneous
.PHONY: todo
todo: ## Print the all of (TODO|BUG|XXX|FIXME|NOTE) in packages.
@grep -E '(TODO|BUG|XXX|FIXME)(\(.+\):|:)' $(shell find . -type f -name '*.go' -and -not -iwholename '*vendor*')
.PHONY: nolint
nolint: ## Print the all of //nolint:... pragma in packages.
@rg -t go -C 3 -e '//nolint.+' --follow --hidden --glob='!vendor' --glob='!internal'
.PHONY: env/%
env/%: ## Print the value of MAKEFILE_VARIABLE. Use `make env/GO_FLAGS` or etc.
@echo $($*)