Skip to content

Commit 273f5ad

Browse files
committed
Makefile: update Makefile
1 parent fff0d85 commit 273f5ad

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

Makefile

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ PKG := $(subst $(GO_PATH)/src/,,$(CURDIR))
1818
GO_PKGS := $(shell go list ./... | grep -v -e '.pb.go')
1919
GO_TEST_PKGS := $(shell go list -f='{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...)
2020

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 --
2323
GO_TEST_FUNC ?= .
24-
GO_TEST_FLAGS ?=
24+
GO_TEST_FLAGS ?= -race -count=1
2525
GO_BENCH_FUNC ?= .
2626
GO_BENCH_FLAGS ?= -benchmem
2727
GO_LINT_FLAGS ?=
@@ -30,39 +30,35 @@ CGO_ENABLED ?= 0
3030
GO_BUILDTAGS=osusergo netgo static
3131
GO_LDFLAGS=-s -w "-extldflags=-static"
3232
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
3442

3543
# ----------------------------------------------------------------------------
3644
# defines
3745

38-
GOPHER = ""
3946
define target
40-
@printf "$(GOPHER) \\x1b[1;32m$(patsubst ,$@,$(1))\\x1b[0m\\n"
47+
@printf "+ $(patsubst ,$@,$(1))\\n" >&2
4148
endef
4249

4350
# ----------------------------------------------------------------------------
4451
# target
4552

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-
5753
##@ test, bench, coverage
5854

5955
.PHONY: test
60-
test: ${TOOLS_BIN}
56+
test: ${TOOLS_BIN}/gotestsum
6157
test: CGO_ENABLED=1
62-
test: GO_FLAGS=-tags='$(subst $(space),$(comma),${GO_BUILDTAGS})'
58+
test: GO_FLAGS=-tags='$(subst ${space},${comma},${GO_BUILDTAGS})'
6359
test: ## Runs package test including race condition.
6460
$(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}
6662

6763
.PHONY: test/gojay
6864
test/gojay: GO_BUILDTAGS+=gojay
@@ -71,36 +67,58 @@ test/gojay: test
7167
.PHONY: bench
7268
bench: ## Take a package benchmark.
7369
$(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}
7571

7672
.PHONY: coverage
77-
coverage: ${TOOLS_BIN}
73+
coverage: ${TOOLS_BIN}/gotestsum
7874
coverage: CGO_ENABLED=1
7975
coverage: ## Takes packages test coverage.
8076
$(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}
8278

8379
coverage/gojay: GO_BUILDTAGS+=gojay
8480
coverage/gojay: coverage
8581

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
8790

8891
.PHONY: lint
8992
lint: lint/golangci-lint ## Run all linters.
9093

91-
9294
.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.
9496
$(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
96114

97115

98116
##@ clean
99117

100118
.PHONY: clean
101119
clean: ## Cleanups binaries and extra files in the package.
102120
$(call target)
103-
@rm -rf *.out *.test *.prof trace.log ${TOOLS_BIN}
121+
@rm -rf *.out *.test *.prof trace.txt ${TOOLS_BIN}
104122

105123

106124
##@ miscellaneous
@@ -109,9 +127,13 @@ clean: ## Cleanups binaries and extra files in the package.
109127
TODO: ## Print the all of (TODO|BUG|XXX|FIXME|NOTE) in packages.
110128
@grep -E '(TODO|BUG|XXX|FIXME|NOTE)(\(.+\):|:)' $(find . -type f -name '*.go' -and -not -iwholename '*vendor*')
111129

130+
.PHONY: env/%
131+
env/%: ## Print the value of MAKEFILE_VARIABLE. Use `make env/GO_FLAGS` or etc.
132+
@echo $($*)
133+
112134

113135
##@ help
114136

115137
.PHONY: help
116138
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

Comments
 (0)