cubefs
264 строки · 8.7 Кб
1# Copyright 2018 The Prometheus Authors
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14
15# A common Makefile that includes rules to be reused in different prometheus projects.
16# !!! Open PRs only against the prometheus/prometheus/Makefile.common repository!
17
18# Example usage :
19# Create the main Makefile in the root project directory.
20# include Makefile.common
21# customTarget:
22# @echo ">> Running customTarget"
23#
24
25# Ensure GOBIN is not set during build so that promu is installed to the correct path
26unexport GOBIN
27
28GO ?= go
29GOFMT ?= $(GO)fmt
30FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
31GOOPTS ?=
32GOHOSTOS ?= $(shell $(GO) env GOHOSTOS)
33GOHOSTARCH ?= $(shell $(GO) env GOHOSTARCH)
34
35GO_VERSION ?= $(shell $(GO) version)
36GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
37PRE_GO_111 ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.')
38
39PROMU := $(FIRST_GOPATH)/bin/promu
40pkgs = ./...
41
42ifeq (arm, $(GOHOSTARCH))
43GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM)
44GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM)
45else
46GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)
47endif
48
49GOTEST := $(GO) test
50GOTEST_DIR :=
51ifneq ($(CIRCLE_JOB),)
52ifneq ($(shell which gotestsum),)
53GOTEST_DIR := test-results
54GOTEST := gotestsum --junitfile $(GOTEST_DIR)/unit-tests.xml --
55endif
56endif
57
58PROMU_VERSION ?= 0.13.0
59PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz
60
61GOLANGCI_LINT :=
62GOLANGCI_LINT_OPTS ?=
63GOLANGCI_LINT_VERSION ?= v1.45.2
64# golangci-lint only supports linux, darwin and windows platforms on i386/amd64.
65# windows isn't included here because of the path separator being different.
66ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
67ifeq ($(GOHOSTARCH),$(filter $(GOHOSTARCH),amd64 i386))
68# If we're in CI and there is an Actions file, that means the linter
69# is being run in Actions, so we don't need to run it here.
70ifeq (,$(CIRCLE_JOB))
71GOLANGCI_LINT := $(FIRST_GOPATH)/bin/golangci-lint
72else ifeq (,$(wildcard .github/workflows/golangci-lint.yml))
73GOLANGCI_LINT := $(FIRST_GOPATH)/bin/golangci-lint
74endif
75endif
76endif
77
78PREFIX ?= $(shell pwd)
79BIN_DIR ?= $(shell pwd)
80DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
81DOCKERFILE_PATH ?= ./Dockerfile
82DOCKERBUILD_CONTEXT ?= ./
83DOCKER_REPO ?= prom
84
85DOCKER_ARCHS ?= amd64
86
87BUILD_DOCKER_ARCHS = $(addprefix common-docker-,$(DOCKER_ARCHS))
88PUBLISH_DOCKER_ARCHS = $(addprefix common-docker-publish-,$(DOCKER_ARCHS))
89TAG_DOCKER_ARCHS = $(addprefix common-docker-tag-latest-,$(DOCKER_ARCHS))
90
91ifeq ($(GOHOSTARCH),amd64)
92ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows))
93# Only supported on amd64
94test-flags := -race
95endif
96endif
97
98# This rule is used to forward a target like "build" to "common-build". This
99# allows a new "build" target to be defined in a Makefile which includes this
100# one and override "common-build" without override warnings.
101%: common-% ;
102
103.PHONY: common-all
104common-all: precheck style check_license lint yamllint unused build test
105
106.PHONY: common-style
107common-style:
108@echo ">> checking code style"
109@fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
110if [ -n "$${fmtRes}" ]; then \
111echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \
112echo "Please ensure you are using $$($(GO) version) for formatting code."; \
113exit 1; \
114fi
115
116.PHONY: common-check_license
117common-check_license:
118@echo ">> checking license header"
119@licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \
120awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \
121done); \
122if [ -n "$${licRes}" ]; then \
123echo "license header checking failed:"; echo "$${licRes}"; \
124exit 1; \
125fi
126
127.PHONY: common-deps
128common-deps:
129@echo ">> getting dependencies"
130$(GO) mod download
131
132.PHONY: update-go-deps
133update-go-deps:
134@echo ">> updating Go dependencies"
135@for m in $$($(GO) list -mod=readonly -m -f '{{ if and (not .Indirect) (not .Main)}}{{.Path}}{{end}}' all); do \
136$(GO) get -d $$m; \
137done
138$(GO) mod tidy
139
140.PHONY: common-test-short
141common-test-short: $(GOTEST_DIR)
142@echo ">> running short tests"
143$(GOTEST) -short $(GOOPTS) $(pkgs)
144
145.PHONY: common-test
146common-test: $(GOTEST_DIR)
147@echo ">> running all tests"
148$(GOTEST) $(test-flags) $(GOOPTS) $(pkgs)
149
150$(GOTEST_DIR):
151@mkdir -p $@
152
153.PHONY: common-format
154common-format:
155@echo ">> formatting code"
156$(GO) fmt $(pkgs)
157
158.PHONY: common-vet
159common-vet:
160@echo ">> vetting code"
161$(GO) vet $(GOOPTS) $(pkgs)
162
163.PHONY: common-lint
164common-lint: $(GOLANGCI_LINT)
165ifdef GOLANGCI_LINT
166@echo ">> running golangci-lint"
167# 'go list' needs to be executed before staticcheck to prepopulate the modules cache.
168# Otherwise staticcheck might fail randomly for some reason not yet explained.
169$(GO) list -e -compiled -test=true -export=false -deps=true -find=false -tags= -- ./... > /dev/null
170$(GOLANGCI_LINT) run $(GOLANGCI_LINT_OPTS) $(pkgs)
171endif
172
173.PHONY: common-yamllint
174common-yamllint:
175@echo ">> running yamllint on all YAML files in the repository"
176ifeq (, $(shell which yamllint))
177@echo "yamllint not installed so skipping"
178else
179yamllint .
180endif
181
182# For backward-compatibility.
183.PHONY: common-staticcheck
184common-staticcheck: lint
185
186.PHONY: common-unused
187common-unused:
188@echo ">> running check for unused/missing packages in go.mod"
189$(GO) mod tidy
190@git diff --exit-code -- go.sum go.mod
191
192.PHONY: common-build
193common-build: promu
194@echo ">> building binaries"
195$(PROMU) build --prefix $(PREFIX) $(PROMU_BINARIES)
196
197.PHONY: common-tarball
198common-tarball: promu
199@echo ">> building release tarball"
200$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)
201
202.PHONY: common-docker $(BUILD_DOCKER_ARCHS)
203common-docker: $(BUILD_DOCKER_ARCHS)
204$(BUILD_DOCKER_ARCHS): common-docker-%:
205docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" \
206-f $(DOCKERFILE_PATH) \
207--build-arg ARCH="$*" \
208--build-arg OS="linux" \
209$(DOCKERBUILD_CONTEXT)
210
211.PHONY: common-docker-publish $(PUBLISH_DOCKER_ARCHS)
212common-docker-publish: $(PUBLISH_DOCKER_ARCHS)
213$(PUBLISH_DOCKER_ARCHS): common-docker-publish-%:
214docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)"
215
216DOCKER_MAJOR_VERSION_TAG = $(firstword $(subst ., ,$(shell cat VERSION)))
217.PHONY: common-docker-tag-latest $(TAG_DOCKER_ARCHS)
218common-docker-tag-latest: $(TAG_DOCKER_ARCHS)
219$(TAG_DOCKER_ARCHS): common-docker-tag-latest-%:
220docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:latest"
221docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:v$(DOCKER_MAJOR_VERSION_TAG)"
222
223.PHONY: common-docker-manifest
224common-docker-manifest:
225DOCKER_CLI_EXPERIMENTAL=enabled docker manifest create -a "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" $(foreach ARCH,$(DOCKER_ARCHS),$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$(ARCH):$(DOCKER_IMAGE_TAG))
226DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)"
227
228.PHONY: promu
229promu: $(PROMU)
230
231$(PROMU):
232$(eval PROMU_TMP := $(shell mktemp -d))
233curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP)
234mkdir -p $(FIRST_GOPATH)/bin
235cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu
236rm -r $(PROMU_TMP)
237
238.PHONY: proto
239proto:
240@echo ">> generating code from proto files"
241@./scripts/genproto.sh
242
243ifdef GOLANGCI_LINT
244$(GOLANGCI_LINT):
245mkdir -p $(FIRST_GOPATH)/bin
246curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/$(GOLANGCI_LINT_VERSION)/install.sh \
247| sed -e '/install -d/d' \
248| sh -s -- -b $(FIRST_GOPATH)/bin $(GOLANGCI_LINT_VERSION)
249endif
250
251.PHONY: precheck
252precheck::
253
254define PRECHECK_COMMAND_template =
255precheck:: $(1)_precheck
256
257PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
258.PHONY: $(1)_precheck
259$(1)_precheck:
260@if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
261echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \
262exit 1; \
263fi
264endef
265