@@ -18,10 +18,10 @@ PKG := $(subst $(GO_PATH)/src/,,$(CURDIR))
18
18
GO_PKGS := $(shell go list ./... | grep -v -e '.pb.go')
19
19
GO_TEST_PKGS := $(shell go list -f='{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...)
20
20
21
- export GOTESTSUM_FORMAT =pkgname-and-test-fails
22
- GO_TEST ?= ${TOOLS_BIN}/gotestsum --format=standard-verbose --
21
+ export GOTESTSUM_FORMAT =standard-verbose
22
+ GO_TEST ?= ${TOOLS_BIN}/gotestsum --
23
23
GO_TEST_FUNC ?= .
24
- GO_TEST_FLAGS ?=
24
+ GO_TEST_FLAGS ?= -race -count=1
25
25
GO_BENCH_FUNC ?= .
26
26
GO_BENCH_FLAGS ?= -benchmem
27
27
GO_LINT_FLAGS ?=
@@ -30,39 +30,35 @@ CGO_ENABLED ?= 0
30
30
GO_BUILDTAGS =osusergo netgo static
31
31
GO_LDFLAGS =-s -w "-extldflags=-static"
32
32
GO_FLAGS ?= -tags='$(subst $(space ) ,$(comma ) ,${GO_BUILDTAGS}) ' -ldflags='${GO_LDFLAGS}' -installsuffix=netgo
33
- GO_FLAGS_TOOLS ?= -tags='$(subst $(space ) ,$(comma ) ,${GO_BUILDTAGS}) ' -ldflags='-s -w' -installsuffix=netgo
33
+
34
+ # Set build environment
35
+ JOBS := $(shell getconf _NPROCESSORS_CONF)
36
+ # If $CIRCLECI is true, assume linux environment, parse actual share CPU count.
37
+ # $CIRCLECI env is automatically set by CircleCI. See also https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables
38
+ ifeq ($(CIRCLECI ) ,true)
39
+ # https://circleci.com/changelog#container-cgroup-limits-now-visible-inside-the-docker-executor
40
+ JOBS := $(shell echo $$(($$(cat /sys/fs/cgroup/cpu/cpu.shares) / 1024)))
41
+ endif
34
42
35
43
# ----------------------------------------------------------------------------
36
44
# defines
37
45
38
- GOPHER = ""
39
46
define target
40
- @printf "$( GOPHER ) \\x1b[1;32m $(patsubst ,$@ ,$(1 ) ) \\x1b[0m\\n"
47
+ @printf "+ $(patsubst ,$@ ,$(1 ) ) \\n" >&2
41
48
endef
42
49
43
50
# ----------------------------------------------------------------------------
44
51
# target
45
52
46
- # #@ tools
47
-
48
- .PHONY : tools
49
- tools : ${TOOLS_BIN} # # Install tools
50
-
51
- ${TOOLS_BIN} :
52
- cd tools; \
53
- for t in $$ (go list -f ' {{ join .Imports " " }}' -tags=tools); do \
54
- GOBIN=${TOOLS_BIN} CGO_ENABLED=0 go install -v -mod=mod ${GO_FLAGS_TOOLS} " $$ {t}" ; \
55
- done
56
-
57
53
# #@ test, bench, coverage
58
54
59
55
.PHONY : test
60
- test : ${TOOLS_BIN}
56
+ test : ${TOOLS_BIN}/gotestsum
61
57
test : CGO_ENABLED=1
62
- test : GO_FLAGS=-tags='$(subst $( space ) , $( comma ) ,${GO_BUILDTAGS}) '
58
+ test : GO_FLAGS=-tags='$(subst ${ space},${ comma} ,${GO_BUILDTAGS}) '
63
59
test : # # Runs package test including race condition.
64
60
$(call target)
65
- CGO_ENABLED=$( CGO_ENABLED ) $( GO_TEST ) -race -count 1 - run=$( GO_TEST_FUNC ) $(strip ${GO_FLAGS}) $( GO_TEST_PKGS )
61
+ @ CGO_ENABLED=${ CGO_ENABLED} ${ GO_TEST} ${GO_TEST_FLAGS} - run=${ GO_TEST_FUNC} $(strip ${GO_FLAGS}) ${ GO_TEST_PKGS}
66
62
67
63
.PHONY : test/gojay
68
64
test/gojay : GO_BUILDTAGS+=gojay
@@ -71,36 +67,58 @@ test/gojay: test
71
67
.PHONY : bench
72
68
bench : # # Take a package benchmark.
73
69
$(call target)
74
- CGO_ENABLED=$( CGO_ENABLED ) $( GO_TEST ) -run=' ^$$' -bench=$( GO_BENCH_FUNC ) -benchmem $(strip $( GO_FLAGS ) ) $( GO_TEST_PKGS )
70
+ @ CGO_ENABLED=${ CGO_ENABLED} ${ GO_TEST} -run=' ^$$' -bench=${ GO_BENCH_FUNC} -benchmem $(strip ${ GO_FLAGS}) ${ GO_TEST_PKGS}
75
71
76
72
.PHONY : coverage
77
- coverage : ${TOOLS_BIN}
73
+ coverage : ${TOOLS_BIN}/gotestsum
78
74
coverage : CGO_ENABLED=1
79
75
coverage : # # Takes packages test coverage.
80
76
$(call target)
81
- CGO_ENABLED=$( CGO_ENABLED ) $( GO_TEST ) -race -covermode=atomic -coverpkg=$( PKG ) /... -coverprofile=coverage.out $(strip $( GO_FLAGS ) ) $( GO_PKGS )
77
+ @ CGO_ENABLED=${ CGO_ENABLED} ${ GO_TEST} ${GO_TEST_FLAGS} -covermode=atomic -coverpkg=${ PKG} /... -coverprofile=coverage.out $(strip ${ GO_FLAGS}) ${ GO_PKGS}
82
78
83
79
coverage/gojay : GO_BUILDTAGS+=gojay
84
80
coverage/gojay : coverage
85
81
86
- # #@ lint
82
+
83
+ # #@ fmt, lint
84
+
85
+ .PHONY : fmt
86
+ fmt : ${TOOLS_BIN}/gofumpt ${TOOLS_BIN}/gofumports
87
+ fmt : # # Run gofumpt and gofumports.
88
+ find . -iname " *.go" -not -path " ./vendor/**" | xargs -P ${JOBS} ${TOOLS_BIN} /gofumpt -s -extra -w
89
+ find . -iname " *.go" -not -path " ./vendor/**" | xargs -P ${JOBS} ${TOOLS_BIN} /gofumports -local=${PKG} ,$(subst /jsonrpc2,,$(PKG ) ) -w
87
90
88
91
.PHONY : lint
89
92
lint : lint/golangci-lint # # Run all linters.
90
93
91
-
92
94
.PHONY : lint/golangci-lint
93
- lint/golangci-lint : ${TOOLS_BIN} .golangci.yml # # Run golangci-lint.
95
+ lint/golangci-lint : ${TOOLS_BIN}/golangci-lint .golangci.yml # # Run golangci-lint.
94
96
$(call target)
95
- @${TOOLS_BIN} /golangci-lint run $(strip ${GO_LINT_FLAGS}) ./...
97
+ @${TOOLS_BIN} /golangci-lint -j ${JOBS} run $(strip ${GO_LINT_FLAGS}) ./...
98
+
99
+
100
+ # #@ tools
101
+
102
+ .PHONY : tools
103
+ tools : ${TOOLS_BIN}/'' # # Install tools
104
+ tools/% : # # install an individual dependent tool
105
+
106
+ ${TOOLS_BIN}/% :
107
+ @cd tools; \
108
+ for t in $$ (go list -f ' {{ join .Imports " " }}' -tags=tools); do \
109
+ if [ $$ (basename $$ t) = ' $*' ]; then \
110
+ echo " Install $$ t ..." ; \
111
+ GOBIN=${TOOLS_BIN} CGO_ENABLED=0 go install -v -mod=mod ${GO_FLAGS} " $$ {t}" ; \
112
+ fi \
113
+ done
96
114
97
115
98
116
# #@ clean
99
117
100
118
.PHONY : clean
101
119
clean : # # Cleanups binaries and extra files in the package.
102
120
$(call target)
103
- @rm -rf * .out * .test * .prof trace.log ${TOOLS_BIN}
121
+ @rm -rf * .out * .test * .prof trace.txt ${TOOLS_BIN}
104
122
105
123
106
124
# #@ miscellaneous
@@ -109,9 +127,13 @@ clean: ## Cleanups binaries and extra files in the package.
109
127
TODO : # # Print the all of (TODO|BUG|XXX|FIXME|NOTE) in packages.
110
128
@grep -E ' (TODO|BUG|XXX|FIXME|NOTE)(\(.+\):|:)' $(find . -type f -name '*.go' -and -not -iwholename '*vendor*' )
111
129
130
+ .PHONY : env/%
131
+ env/% : # # Print the value of MAKEFILE_VARIABLE. Use `make env/GO_FLAGS` or etc.
132
+ @echo $($* )
133
+
112
134
113
135
# #@ help
114
136
115
137
.PHONY : help
116
138
help : # # Show this help.
117
- @awk ' BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[33m<target>\033[0m\n"} /^[a-zA-Z_0-9\/_-]+:.*?##/ { printf " \033[36m%-25s \033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST )
139
+ @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 )
0 commit comments