github.com/zchee/zap-cloudlogging@v0.0.0-20220819025602-19b026d3900e/Makefile (about) 1 # ----------------------------------------------------------------------------- 2 # global 3 4 .DEFAULT_GOAL := help 5 6 # ----------------------------------------------------------------------------- 7 # go 8 9 GO_PATH ?= $(shell go env GOPATH) 10 GO_OS ?= $(shell go env GOOS) 11 GO_ARCH ?= $(shell go env GOARCH) 12 13 PACKAGES := $(subst $(GO_PATH)/src/,,$(CURDIR)) 14 CGO_ENABLED ?= 0 15 GO_BUILDTAGS=osusergo,netgo,static 16 GO_LDFLAGS=-s -w 17 ifeq (${GO_OS},linux) 18 GO_LDFLAGS+=-d 19 endif 20 GO_LDFLAGS+=-buildid= "-extldflags=-static" 21 GO_FLAGS ?= -tags='${GO_BUILDTAGS}' -ldflags='${GO_LDFLAGS}' 22 23 GO_PACKAGES = $(shell go list ./...) 24 GO_TEST_PACKAGES ?= $(shell go list -f='{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...) 25 26 GO_TEST ?= ${TOOLS_BIN}/gotestsum -f standard-verbose -- 27 GO_TEST_FLAGS ?= -race -count=1 -shuffle=on 28 GO_TEST_FUNC ?= . 29 GO_COVERAGE_OUT ?= coverage.out 30 GO_BENCH_FLAGS ?= -benchmem 31 GO_BENCH_FUNC ?= . 32 GO_LINT_FLAGS ?= 33 34 TOOLS_DIR := ${CURDIR}/tools 35 TOOLS_BIN := ${TOOLS_DIR}/bin 36 TOOLS = $(shell cd ${TOOLS_DIR}; go list -f '{{ join .Imports " " }}' -tags=tools) 37 38 JOBS = $(shell getconf _NPROCESSORS_CONF) 39 40 # ----------------------------------------------------------------------------- 41 # defines 42 43 define target 44 @printf "+ $(patsubst ,$@,$(1))\\n" >&2 45 endef 46 47 # ----------------------------------------------------------------------------- 48 # target 49 50 ##@ test, bench, coverage 51 52 .PHONY: test 53 test: CGO_ENABLED=1 54 test: GO_FLAGS=-tags='$(subst ${space},${comma},${GO_BUILDTAGS})' 55 test: ${TOOLS_BIN}/gotestsum ## Runs package test including race condition. 56 $(call target) 57 @CGO_ENABLED=${CGO_ENABLED} GOTESTSUM_FORMAT=standard-verbose ${GO_TEST} ${GO_TEST_FLAGS} -run=${GO_TEST_FUNC} $(strip ${GO_FLAGS}) ${GO_TEST_PACKAGES} 58 59 .PHONY: coverage 60 coverage: CGO_ENABLED=1 61 coverage: GO_FLAGS=-tags='$(subst ${space},${comma},${GO_BUILDTAGS})' 62 coverage: ${TOOLS_BIN}/gotestsum ## Takes packages test coverage. 63 $(call target) 64 @CGO_ENABLED=${CGO_ENABLED} ${GO_TEST} ${GO_TEST_FLAGS} -covermode=atomic -coverpkg=./... -coverprofile=${GO_COVERAGE_OUT} $(strip ${GO_FLAGS}) ${GO_PACKAGES} 65 66 ##@ fmt, lint 67 68 .PHONY: fmt 69 fmt: ${TOOLS_BIN}/goimportz ${TOOLS_BIN}/gofumpt ## Run goimportz and gofumpt. 70 $(call target) 71 @${TOOLS_BIN}/goimportz -local=${PACKAGES} -w $(shell find $$PWD -iname "*.go" -not -iname "*pb.go" -not -iwholename "*vendor*") 72 @${TOOLS_BIN}/gofumpt -extra -w $(shell find $$PWD -iname "*.go" -not -iname "*pb.go" -not -iwholename "*vendor*") 73 74 .PHONY: lint 75 lint: lint/golangci-lint ## Run all linters. 76 77 .PHONY: lint/golangci-lint 78 lint/golangci-lint: ${TOOLS_BIN}/golangci-lint .golangci.yaml ## Run golangci-lint. 79 $(call target) 80 @${TOOLS_BIN}/golangci-lint -j ${JOBS} run $(strip ${GO_LINT_FLAGS}) ./... 81 82 ##@ tools 83 84 .PHONY: tools 85 tools: ${TOOLS_BIN}/'' ## Install tools 86 87 ${TOOLS_BIN}/%: ${TOOLS_DIR}/go.sum ${TOOLS_DIR}/go.mod 88 @cd ${TOOLS_DIR}; \ 89 for t in ${TOOLS}; do \ 90 if [ ! $$(basename $$t) = 'cmd' ] && [ -z '$*' ] || [ $$(basename $$t) = '$*' ]; then \ 91 echo "Install $$t"; \ 92 GOBIN=${TOOLS_BIN} CGO_ENABLED=0 go install -v -mod=readonly ${GO_FLAGS} "$${t}"; \ 93 fi \ 94 done 95 96 ##@ clean 97 98 .PHONY: clean 99 clean: ## Cleanups binaries and extra files in the package. 100 $(call target) 101 @rm -rf *.out *.test *.prof trace.txt ${TOOLS_BIN} 102 103 ##@ help 104 105 .PHONY: help 106 help: ## Show this help. 107 @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) 108 109 ##@ misc 110 111 .PHONY: todo 112 todo: ## Print the all of (TODO|BUG|XXX|FIXME|NOTE) in packages. 113 @grep -E '(TODO|BUG|XXX|FIXME)(\(.+\):|:)' $(shell find . -type f -name '*.go' -and -not -iwholename '*vendor*') 114 115 .PHONY: nolint 116 nolint: ## Print the all of //nolint:... pragma in packages. 117 @rg -t go -C 3 -e '//nolint.+' --follow --hidden --glob='!vendor' --glob='!internal' 118 119 .PHONY: env/% 120 env/%: ## Print the value of MAKEFILE_VARIABLE. Use `make env/GO_FLAGS` or etc. 121 @echo $($*)