|
| 1 | +# Copyright 2015 Dhasthagheer < [email protected]> |
| 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 | +# THE AUTHORITATIVE VERSION OF THIS MAKEFILE LIVES IN: |
| 15 | +# |
| 16 | +# https://github.com/prometheus/utils |
| 17 | +# |
| 18 | +# PLEASE MAKE ANY CHANGES THERE AND PROPAGATE THEM TO ALL PROMETHEUS |
| 19 | +# REPOSITORIES THAT ARE USING THIS MAKEFILE. |
| 20 | +# |
| 21 | +# This file provides common Makefile infrastructure for several Prometheus |
| 22 | +# components. This includes make tasks for downloading Go, setting up a |
| 23 | +# self-contained build environment, fetching Go dependencies, building |
| 24 | +# binaries, running tests, and doing release management. This file is intended |
| 25 | +# to be included from a project's Makefile, which needs to define the following |
| 26 | +# variables, at a minimum: |
| 27 | +# |
| 28 | +# * VERSION - The current version of the project in question. |
| 29 | +# * TARGET - The desired name of the built binary. |
| 30 | +# |
| 31 | +# Many of the variables defined below are defined conditionally (using '?'), |
| 32 | +# which allows the project's main Makefile to override any of these settings, if |
| 33 | +# needed. See also: |
| 34 | +# |
| 35 | +# https://www.gnu.org/software/make/manual/html_node/Flavors.html#Flavors. |
| 36 | +# |
| 37 | +# The including Makefile may define any number of extra targets that are |
| 38 | +# specific to that project. |
| 39 | + |
| 40 | +VERSION ?= $(error VERSION not set in including Makefile) |
| 41 | +TARGET ?= $(error TARGET not set in including Makefile) |
| 42 | + |
| 43 | +SRC ?= $(shell find . -type f -name "*.go" ! -path "./.build/*") |
| 44 | +GOOS ?= $(shell uname | tr A-Z a-z) |
| 45 | +GOARCH ?= $(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) |
| 46 | + |
| 47 | +ifeq ($(GOOS),darwin) |
| 48 | + RELEASE_SUFFIX ?= -osx$(shell sw_vers -productVersion) |
| 49 | +endif |
| 50 | + |
| 51 | +GO_VERSION ?= 1.4.2 |
| 52 | +GOURL ?= https://golang.org/dl |
| 53 | +GOPKG ?= go$(GO_VERSION).$(GOOS)-$(GOARCH)$(RELEASE_SUFFIX).tar.gz |
| 54 | +GOPATH := $(CURDIR)/.build/gopath |
| 55 | + |
| 56 | +# Check for the correct version of go in the path. If we find it, use it. |
| 57 | +# Otherwise, prepare to build go locally. |
| 58 | +ifeq ($(shell command -v "go" >/dev/null && go version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/'), $(GO_VERSION)) |
| 59 | + GOCC ?= $(shell command -v "go") |
| 60 | + GOFMT ?= $(shell command -v "gofmt") |
| 61 | + GO ?= GOPATH=$(GOPATH) $(GOCC) |
| 62 | +else |
| 63 | + GOROOT ?= $(CURDIR)/.build/go$(GO_VERSION) |
| 64 | + GOCC ?= $(GOROOT)/bin/go |
| 65 | + GOFMT ?= $(GOROOT)/bin/gofmt |
| 66 | + GO ?= GOROOT=$(GOROOT) GOPATH=$(GOPATH) $(GOCC) |
| 67 | +endif |
| 68 | + |
| 69 | +# Never honor GOBIN, should it be set at all. |
| 70 | +unexport GOBIN |
| 71 | + |
| 72 | +SUFFIX ?= $(GOOS)-$(GOARCH) |
| 73 | +BINARY ?= $(TARGET) |
| 74 | +ARCHIVE ?= $(TARGET)-$(VERSION).$(SUFFIX).tar.gz |
| 75 | +ROOTPKG ?= github.com/prometheus/$(TARGET) |
| 76 | +SELFLINK ?= $(GOPATH)/src/$(ROOTPKG) |
| 77 | + |
| 78 | +default: $(BINARY) |
| 79 | + |
| 80 | +$(GOCC): |
| 81 | + @echo Go version $(GO_VERSION) required but not found in PATH. |
| 82 | + @echo About to download and install go$(GO_VERSION) to $(GOROOT) |
| 83 | + @echo Abort now if you want to manually install it system-wide instead. |
| 84 | + @echo |
| 85 | + @sleep 5 |
| 86 | + mkdir -p $(GOROOT) |
| 87 | + curl -L $(GOURL)/$(GOPKG) | tar -C $(GOROOT) --strip 1 -xz |
| 88 | + |
| 89 | +$(SELFLINK): |
| 90 | + mkdir -p $(dir $@) |
| 91 | + ln -s $(CURDIR) $@ |
| 92 | + |
| 93 | +dependencies-stamp: $(GOCC) $(SRC) | $(SELFLINK) |
| 94 | + $(GO) get -d |
| 95 | + touch $@ |
| 96 | + |
| 97 | +$(BINARY): $(GOCC) $(SRC) dependencies-stamp Makefile Makefile.COMMON |
| 98 | + $(GO) build $(GOFLAGS) -o $@ |
| 99 | + |
| 100 | +.PHONY: archive |
| 101 | +archive: $(ARCHIVE) |
| 102 | + |
| 103 | +$(ARCHIVE): $(BINARY) |
| 104 | + tar -czf $@ $< |
| 105 | + |
| 106 | +.PHONY: tag |
| 107 | +tag: |
| 108 | + git tag $(VERSION) |
| 109 | + git push --tags |
| 110 | + |
| 111 | +.PHONY: test |
| 112 | +test: $(GOCC) dependencies-stamp |
| 113 | + $(GO) test ./... |
| 114 | + |
| 115 | +.PHONY: format |
| 116 | +format: $(GOCC) |
| 117 | + find . -iname '*.go' | egrep -v "^\./\.build|./generated|\./Godeps|\.(l|y)\.go" | xargs -n1 $(GOFMT) -w -s=true |
| 118 | + |
| 119 | +.PHONY: clean |
| 120 | +clean: |
| 121 | + rm -rf $(BINARY) $(ARCHIVE) .build *-stamp |
0 commit comments