|
1 | 1 | # ----------------------------------------------------------------------------
|
2 | 2 | # global
|
3 | 3 |
|
4 |
| -.DEFAULT_GOAL = test |
| 4 | +SHELL=/usr/bin/env bash |
| 5 | +.DEFAULT_GOAL=test |
| 6 | +comma := , |
| 7 | +empty := |
| 8 | +space := $(empty) $(empty) |
| 9 | + |
| 10 | +# ---------------------------------------------------------------------------- |
| 11 | +# go |
| 12 | + |
| 13 | +ifneq ($(shell command -v go),) |
| 14 | +GO_PATH ?= $(shell go env GOPATH) |
| 15 | +GO_OS ?= $(shell go env GOOS) |
| 16 | +GO_ARCH ?= $(shell go env GOARCH) |
| 17 | +TOOLS_BIN=${CURDIR}/bin |
| 18 | + |
| 19 | +PKG := $(subst $(GO_PATH)/src/,,$(CURDIR)) |
| 20 | +GO_PKGS := $(shell go list ./... | grep -v -e '.pb.go') |
| 21 | +GO_TEST_PKGS := $(shell go list -f='{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...) |
| 22 | + |
| 23 | +export GOTESTSUM_FORMAT=pkgname-and-test-fails |
| 24 | +GO_TEST ?= go test |
| 25 | +GO_TEST_FUNC ?= . |
| 26 | +GO_TEST_FLAGS ?= |
| 27 | +GO_BENCH_FUNC ?= . |
| 28 | +GO_BENCH_FLAGS ?= -benchmem |
| 29 | +GO_LINT_FLAGS ?= |
| 30 | + |
| 31 | +CGO_ENABLED ?= 0 |
| 32 | +GO_BUILDTAGS=osusergo netgo static static_build |
| 33 | +GO_LDFLAGS=-s -w "-extldflags=-static" |
| 34 | +GO_FLAGS ?= -tags='$(subst $(space),$(comma),${GO_BUILDTAGS})' -ldflags='${GO_LDFLAGS}' -installsuffix=netgo |
| 35 | +endif |
| 36 | + |
| 37 | +# ---------------------------------------------------------------------------- |
| 38 | +# defines |
| 39 | + |
| 40 | +GOPHER = "" |
| 41 | +define target |
| 42 | +@printf "$(GOPHER) \\x1b[1;32m$(patsubst ,$@,$(1))\\x1b[0m\\n" |
| 43 | +endef |
5 | 44 |
|
6 | 45 | # ----------------------------------------------------------------------------
|
7 | 46 | # target
|
8 | 47 |
|
9 | 48 | .PHONY: all
|
10 | 49 | all: mod pkg/install
|
11 | 50 |
|
12 |
| -# ---------------------------------------------------------------------------- |
13 |
| -# include |
| 51 | +.PHONY: pkg/install |
| 52 | +pkg/install: |
| 53 | + $(call target) |
| 54 | + CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GO_OS) GOARCH=$(GO_ARCH) go install -v $(shell go list -f '{{if and (or .GoFiles .CgoFiles) (ne .Name "main")}}{{.ImportPath}}{{end}}' ${PKG}/...) |
14 | 55 |
|
15 |
| -include hack/make/go.mk |
| 56 | +##@ tools |
16 | 57 |
|
17 |
| -# ---------------------------------------------------------------------------- |
18 |
| -# overlays |
| 58 | +.PHONY: tools |
| 59 | +tools: ## Install tools |
| 60 | + @cd tools; \ |
| 61 | + for t in $$(go list -f '{{ join .Imports " " }}' -tags=tools); do \ |
| 62 | + GOBIN=${CURDIR}/bin go install -v -x -mod=vendor "$${t}" > /dev/null 2>&1; \ |
| 63 | + done |
| 64 | + |
| 65 | +##@ test, bench and coverage |
| 66 | + |
| 67 | +.PHONY: test |
| 68 | +test: CGO_ENABLED=1 |
| 69 | +test: ## Runs package test including race condition. |
| 70 | + $(call target) |
| 71 | + CGO_ENABLED=$(CGO_ENABLED) $(GO_TEST) -v -race -count 1 -run=$(GO_TEST_FUNC) $(strip ${GO_FLAGS}) $(GO_TEST_PKGS) |
19 | 72 |
|
20 | 73 | .PHONY: test/gojay
|
21 | 74 | test/gojay: GO_BUILDTAGS+=gojay
|
22 | 75 | test/gojay: test
|
23 | 76 |
|
24 |
| -.PHONY: coverage/ci/gojay |
25 |
| -coverage/ci/gojay: GO_BUILDTAGS+=gojay |
26 |
| -coverage/ci/gojay: coverage/ci |
| 77 | +.PHONY: bench |
| 78 | +bench: ## Take a package benchmark. |
27 | 79 | $(call target)
|
| 80 | + CGO_ENABLED=$(CGO_ENABLED) $(GO_TEST) -run='^$$' -bench=$(GO_BENCH_FUNC) -benchmem $(strip $(GO_FLAGS)) $(GO_TEST_PKGS) |
28 | 81 |
|
29 |
| -.PHONY: tools |
30 |
| -tools: |
31 |
| - cd tools; \ |
32 |
| - for t in $$(go list -f '{{ join .Imports " " }}' -tags=tools); do \ |
33 |
| - GOBIN=${CURDIR}/bin go install -v -x -mod=vendor "$${t}"; \ |
34 |
| - done |
| 82 | +.PHONY: coverage |
| 83 | +coverage: GO_TEST=${TOOLS_BIN}/gotestsum -- |
| 84 | +coverage: CGO_ENABLED=1 |
| 85 | +coverage: ## Takes packages test coverage. |
| 86 | + $(call target) |
| 87 | + CGO_ENABLED=$(CGO_ENABLED) $(GO_TEST) -race -covermode=atomic -coverpkg=$(PKG)/... -coverprofile=coverage.out $(strip $(GO_FLAGS)) $(GO_PKGS) |
| 88 | + |
| 89 | +coverage/gojay: GO_BUILDTAGS+=gojay |
| 90 | +coverage/gojay: coverage |
| 91 | + |
| 92 | +##@ lint |
| 93 | + |
| 94 | +.PHONY: lint |
| 95 | +lint: lint/golangci-lint ## Run all linters. |
| 96 | + |
| 97 | +.PHONY: lint/golangci-lint |
| 98 | +lint/golangci-lint: tools .golangci.yml ## Run golangci-lint. |
| 99 | + $(call target) |
| 100 | + @golangci-lint run $(strip ${GO_LINT_FLAGS}) ./... |
| 101 | + |
| 102 | + |
| 103 | +##@ clean |
| 104 | + |
| 105 | +.PHONY: clean |
| 106 | +clean: ## Cleanups binaries and extra files in the package. |
| 107 | + $(call target) |
| 108 | + @$(RM) $(APP) *.out *.test *.prof trace.log |
| 109 | + |
| 110 | + |
| 111 | +##@ miscellaneous |
| 112 | + |
| 113 | +.PHONY: AUTHORS |
| 114 | +AUTHORS: ## Creates AUTHORS file. |
| 115 | + @$(file >$@,# This file lists all individuals having contributed content to the repository.) |
| 116 | + @$(file >>$@,# For how it is generated, see `make AUTHORS`.) |
| 117 | + @printf "$(shell git log --format="\n%aN <%aE>" | LC_ALL=C.UTF-8 sort -uf)" >> $@ |
| 118 | + |
| 119 | +.PHONY: todo |
| 120 | +TODO: ## Print the all of (TODO|BUG|XXX|FIXME|NOTE) in packages. |
| 121 | + @rg -e '(TODO|BUG|XXX|FIXME|NOTE)(\(.+\):|:)' --follow --hidden --glob='!.git' --glob='!vendor' --glob='!internal' --glob='!Makefile' --glob='!snippets' --glob='!indent' |
| 122 | + |
| 123 | + |
| 124 | +##@ help |
| 125 | + |
| 126 | +.PHONY: help |
| 127 | +help: ## Show make target help. |
| 128 | + @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) |
0 commit comments