1include .bingo/Variables.mk
2
3SHELL=/bin/bash
4
5PROVIDER_MODULES ?= $(shell ls -d $(PWD)/providers/*)
6MODULES          ?= $(PROVIDER_MODULES) $(PWD)/
7
8GOBIN             ?= $(firstword $(subst :, ,${GOPATH}))/bin
9
10PROTOC_VERSION    ?= 3.12.3
11PROTOC            ?= $(GOBIN)/protoc-$(PROTOC_VERSION)
12TMP_GOPATH        ?= /tmp/gopath
13
14
15GO111MODULE       ?= on
16export GO111MODULE
17GOPROXY           ?= https://proxy.golang.org
18export GOPROXY
19
20define require_clean_work_tree
21	@git update-index -q --ignore-submodules --refresh
22
23    @if ! git diff-files --quiet --ignore-submodules --; then \
24        echo >&2 "cannot $1: you have unstaged changes."; \
25        git diff-files --name-status -r --ignore-submodules -- >&2; \
26        echo >&2 "Please commit or stash them."; \
27        exit 1; \
28    fi
29
30    @if ! git diff-index --cached --quiet HEAD --ignore-submodules --; then \
31        echo >&2 "cannot $1: your index contains uncommitted changes."; \
32        git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2; \
33        echo >&2 "Please commit or stash them."; \
34        exit 1; \
35    fi
36
37endef
38
39all: fmt proto lint test
40
41.PHONY: fmt
42fmt: $(GOIMPORTS)
43	@echo "Running fmt for all modules: $(MODULES)"
44	@$(GOIMPORTS) -local github.com/grpc-ecosystem/go-grpc-middleware/v2 -w $(MODULES)
45
46.PHONY: proto
47proto: ## Generates Go files from Thanos proto files.
48proto: $(GOIMPORTS) $(PROTOC) $(PROTOC_GEN_GOGOFAST) ./grpctesting/testpb/test.proto
49	@GOIMPORTS_BIN="$(GOIMPORTS)" PROTOC_BIN="$(PROTOC)" PROTOC_GEN_GOGOFAST_BIN="$(PROTOC_GEN_GOGOFAST)" scripts/genproto.sh
50
51.PHONY: test
52test:
53	@echo "Running tests for all modules: $(MODULES)"
54	for dir in $(MODULES) ; do \
55		$(MAKE) test_module DIR=$${dir} ; \
56	done
57	./scripts/test_all.sh
58
59.PHONY: test_module
60test_module:
61	@echo "Running tests for dir: $(DIR)"
62	cd $(DIR) && go test -v -race ./...
63
64.PHONY: lint
65# PROTIP:
66# Add
67#      --cpu-profile-path string   Path to CPU profile output file
68#      --mem-profile-path string   Path to memory profile output file
69# to debug big allocations during linting.
70lint: ## Runs various static analysis tools against our code.
71lint: fmt $(FAILLINT) $(GOLANGCI_LINT) $(MISSPELL)
72	@echo "Running lint for all modules: $(MODULES)"
73	./scripts/git-tree.sh
74	@echo ">> verifying modules being imported"
75	@$(FAILLINT) -paths "errors=github.com/pkg/errors,fmt.{Print,Printf,Println}" ./...
76	@echo ">> examining all of the Go files"
77	@go vet -stdmethods=false ./...
78	@echo ">> linting all of the Go files GOGC=${GOGC}"
79	@$(GOLANGCI_LINT) run
80	@echo ">> detecting misspells"
81	@find . -type f | grep -v vendor/ | grep -vE '\./\..*' | xargs $(MISSPELL) -error
82	@echo ">> ensuring generated proto files are up to date"
83	@$(MAKE) proto
84	./scripts/git-tree.sh
85$(PROTOC):
86	@mkdir -p $(TMP_GOPATH)
87	@echo ">> fetching protoc@${PROTOC_VERSION}"
88	@PROTOC_VERSION="$(PROTOC_VERSION)" TMP_GOPATH="$(TMP_GOPATH)" scripts/installprotoc.sh
89	@echo ">> installing protoc@${PROTOC_VERSION}"
90	@mv -- "$(TMP_GOPATH)/bin/protoc" "$(GOBIN)/protoc-$(PROTOC_VERSION)"
91	@echo ">> produced $(GOBIN)/protoc-$(PROTOC_VERSION)"
92