...

Text file src/github.com/prometheus/alertmanager/Makefile.common

Documentation: github.com/prometheus/alertmanager

     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))
    43	GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM)
    44	GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM)
    45else
    46	GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)
    47endif
    48
    49GOTEST := $(GO) test
    50GOTEST_DIR :=
    51ifneq ($(CIRCLE_JOB),)
    52ifneq ($(shell which gotestsum),)
    53	GOTEST_DIR := test-results
    54	GOTEST := gotestsum --junitfile $(GOTEST_DIR)/unit-tests.xml --
    55endif
    56endif
    57
    58PROMU_VERSION ?= 0.14.0
    59PROMU_URL     := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz
    60
    61SKIP_GOLANGCI_LINT :=
    62GOLANGCI_LINT :=
    63GOLANGCI_LINT_OPTS ?=
    64GOLANGCI_LINT_VERSION ?= v1.49.0
    65# golangci-lint only supports linux, darwin and windows platforms on i386/amd64.
    66# windows isn't included here because of the path separator being different.
    67ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
    68	ifeq ($(GOHOSTARCH),$(filter $(GOHOSTARCH),amd64 i386))
    69		# If we're in CI and there is an Actions file, that means the linter
    70		# is being run in Actions, so we don't need to run it here.
    71		ifneq (,$(SKIP_GOLANGCI_LINT))
    72			GOLANGCI_LINT :=
    73		else ifeq (,$(CIRCLE_JOB))
    74			GOLANGCI_LINT := $(FIRST_GOPATH)/bin/golangci-lint
    75		else ifeq (,$(wildcard .github/workflows/golangci-lint.yml))
    76			GOLANGCI_LINT := $(FIRST_GOPATH)/bin/golangci-lint
    77		endif
    78	endif
    79endif
    80
    81PREFIX                  ?= $(shell pwd)
    82BIN_DIR                 ?= $(shell pwd)
    83DOCKER_IMAGE_TAG        ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
    84DOCKERFILE_PATH         ?= ./Dockerfile
    85DOCKERBUILD_CONTEXT     ?= ./
    86DOCKER_REPO             ?= prom
    87
    88DOCKER_ARCHS            ?= amd64
    89
    90BUILD_DOCKER_ARCHS = $(addprefix common-docker-,$(DOCKER_ARCHS))
    91PUBLISH_DOCKER_ARCHS = $(addprefix common-docker-publish-,$(DOCKER_ARCHS))
    92TAG_DOCKER_ARCHS = $(addprefix common-docker-tag-latest-,$(DOCKER_ARCHS))
    93
    94ifeq ($(GOHOSTARCH),amd64)
    95        ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows))
    96                # Only supported on amd64
    97                test-flags := -race
    98        endif
    99endif
   100
   101# This rule is used to forward a target like "build" to "common-build".  This
   102# allows a new "build" target to be defined in a Makefile which includes this
   103# one and override "common-build" without override warnings.
   104%: common-% ;
   105
   106.PHONY: common-all
   107common-all: precheck style check_license lint yamllint unused build test
   108
   109.PHONY: common-style
   110common-style:
   111	@echo ">> checking code style"
   112	@fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
   113	if [ -n "$${fmtRes}" ]; then \
   114		echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \
   115		echo "Please ensure you are using $$($(GO) version) for formatting code."; \
   116		exit 1; \
   117	fi
   118
   119.PHONY: common-check_license
   120common-check_license:
   121	@echo ">> checking license header"
   122	@licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \
   123               awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \
   124       done); \
   125       if [ -n "$${licRes}" ]; then \
   126               echo "license header checking failed:"; echo "$${licRes}"; \
   127               exit 1; \
   128       fi
   129
   130.PHONY: common-deps
   131common-deps:
   132	@echo ">> getting dependencies"
   133	$(GO) mod download
   134
   135.PHONY: update-go-deps
   136update-go-deps:
   137	@echo ">> updating Go dependencies"
   138	@for m in $$($(GO) list -mod=readonly -m -f '{{ if and (not .Indirect) (not .Main)}}{{.Path}}{{end}}' all); do \
   139		$(GO) get -d $$m; \
   140	done
   141	$(GO) mod tidy
   142
   143.PHONY: common-test-short
   144common-test-short: $(GOTEST_DIR)
   145	@echo ">> running short tests"
   146	$(GOTEST) -short $(GOOPTS) $(pkgs)
   147
   148.PHONY: common-test
   149common-test: $(GOTEST_DIR)
   150	@echo ">> running all tests"
   151	$(GOTEST) $(test-flags) $(GOOPTS) $(pkgs)
   152
   153$(GOTEST_DIR):
   154	@mkdir -p $@
   155
   156.PHONY: common-format
   157common-format:
   158	@echo ">> formatting code"
   159	$(GO) fmt $(pkgs)
   160
   161.PHONY: common-vet
   162common-vet:
   163	@echo ">> vetting code"
   164	$(GO) vet $(GOOPTS) $(pkgs)
   165
   166.PHONY: common-lint
   167common-lint: $(GOLANGCI_LINT)
   168ifdef GOLANGCI_LINT
   169	@echo ">> running golangci-lint"
   170# 'go list' needs to be executed before staticcheck to prepopulate the modules cache.
   171# Otherwise staticcheck might fail randomly for some reason not yet explained.
   172	$(GO) list -e -compiled -test=true -export=false -deps=true -find=false -tags= -- ./... > /dev/null
   173	$(GOLANGCI_LINT) run $(GOLANGCI_LINT_OPTS) $(pkgs)
   174endif
   175
   176.PHONY: common-yamllint
   177common-yamllint:
   178	@echo ">> running yamllint on all YAML files in the repository"
   179ifeq (, $(shell which yamllint))
   180	@echo "yamllint not installed so skipping"
   181else
   182	yamllint .
   183endif
   184
   185# For backward-compatibility.
   186.PHONY: common-staticcheck
   187common-staticcheck: lint
   188
   189.PHONY: common-unused
   190common-unused:
   191	@echo ">> running check for unused/missing packages in go.mod"
   192	$(GO) mod tidy
   193	@git diff --exit-code -- go.sum go.mod
   194
   195.PHONY: common-build
   196common-build: promu
   197	@echo ">> building binaries"
   198	$(PROMU) build --prefix $(PREFIX) $(PROMU_BINARIES)
   199
   200.PHONY: common-tarball
   201common-tarball: promu
   202	@echo ">> building release tarball"
   203	$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)
   204
   205.PHONY: common-docker $(BUILD_DOCKER_ARCHS)
   206common-docker: $(BUILD_DOCKER_ARCHS)
   207$(BUILD_DOCKER_ARCHS): common-docker-%:
   208	docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" \
   209		-f $(DOCKERFILE_PATH) \
   210		--build-arg ARCH="$*" \
   211		--build-arg OS="linux" \
   212		$(DOCKERBUILD_CONTEXT)
   213
   214.PHONY: common-docker-publish $(PUBLISH_DOCKER_ARCHS)
   215common-docker-publish: $(PUBLISH_DOCKER_ARCHS)
   216$(PUBLISH_DOCKER_ARCHS): common-docker-publish-%:
   217	docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)"
   218
   219DOCKER_MAJOR_VERSION_TAG = $(firstword $(subst ., ,$(shell cat VERSION)))
   220.PHONY: common-docker-tag-latest $(TAG_DOCKER_ARCHS)
   221common-docker-tag-latest: $(TAG_DOCKER_ARCHS)
   222$(TAG_DOCKER_ARCHS): common-docker-tag-latest-%:
   223	docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:latest"
   224	docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:v$(DOCKER_MAJOR_VERSION_TAG)"
   225
   226.PHONY: common-docker-manifest
   227common-docker-manifest:
   228	DOCKER_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))
   229	DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)"
   230
   231.PHONY: promu
   232promu: $(PROMU)
   233
   234$(PROMU):
   235	$(eval PROMU_TMP := $(shell mktemp -d))
   236	curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP)
   237	mkdir -p $(FIRST_GOPATH)/bin
   238	cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu
   239	rm -r $(PROMU_TMP)
   240
   241.PHONY: proto
   242proto:
   243	@echo ">> generating code from proto files"
   244	@./scripts/genproto.sh
   245
   246ifdef GOLANGCI_LINT
   247$(GOLANGCI_LINT):
   248	mkdir -p $(FIRST_GOPATH)/bin
   249	curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/$(GOLANGCI_LINT_VERSION)/install.sh \
   250		| sed -e '/install -d/d' \
   251		| sh -s -- -b $(FIRST_GOPATH)/bin $(GOLANGCI_LINT_VERSION)
   252endif
   253
   254.PHONY: precheck
   255precheck::
   256
   257define PRECHECK_COMMAND_template =
   258precheck:: $(1)_precheck
   259
   260PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
   261.PHONY: $(1)_precheck
   262$(1)_precheck:
   263	@if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
   264		echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \
   265		exit 1; \
   266	fi
   267endef

View as plain text