-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile.COMMON
94 lines (77 loc) · 3.08 KB
/
Makefile.COMMON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# * NAME - vanity
#
# Many of the variables defined below are defined conditionally (using '?'),
# which allows the project's main Makefile to override any of these settings, if
# needed. See also:
#
# https://www.gnu.org/software/make/manual/html_node/Flavors.html#Flavors.
#
# The including Makefile may define any number of extra targets that are
# specific to that project.
#
# Many inspiration has been taken from:
# * https://github.com/prometheus/alertmanager/blob/master/Makefile
# * https://github.com/prometheus/node_exporter/blob/master/Makefile.COMMON
#
NAME ?= $(error NAME not set in including Makefile)
VERSION ?= git
PACKAGE ?= main
CGO_ENABLED ?= 0
EXTLDFLAGS ?= -extldflags '-static'
# get the current git tag
TAG=$(shell git rev-parse --short=8 HEAD)
BINARY=${NAME}
BUILD_TIME=`date +%FT%T%z`
LDFLAGS=-ldflags "${EXTLDFLAGS} -s -w -X ${PACKAGE}.Version=${VERSION} -X ${PACKAGE}.BuildTime=${BUILD_TIME} -X ${PACKAGE}.Commit=${TAG}"
SOURCEDIR=.
SOURCES := $(shell find $(SOURCEDIR) -name '*.go')
GO := CGO_ENABLED=${CGO_ENABLED} GO15VENDOREXPERIMENT=1 go
pkgs = $(shell $(GO) list ./... | grep -v "/vendor/" | grep -v ".pb." | grep -v "/autogen/" | grep -v "/Godeps/" | grep -v "/assets")
DIR=`pwd`
.DEFAULT_GOAL: help
.PHONY: help
help:
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
all: docker-test docker-image
##############################
# Binary
##############################
$(BINARY): $(SOURCES)
${GO} build -a -tags netgo -o ${BINARY} ${LDFLAGS}
.PHONY: install
install: ## Compile and install to GOBIN
${GO} install -a -tags netgo ${LDFLAGS}
.PHONY: clean
clean: ## Remove generated files
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
###############################
# Test
###############################
.PHONY: test
test: test-base test-meta ## Run all tests
.PHONY: test-base
test-base: ## Run the tests with race detector enabled
@go test ${LDFLAGS} -race -v $(pkgs)
test-base-norace: ## Run tests without the race detector
@${GO} test ${LDFLAGS} -v $(pkgs)
test-meta: ## Run lint/style checks
@echo $(pkgs) | xargs -n1 golint --set_exit_status
@go vet $(pkgs)
@vendorcheck ./...
################################
# Docker
################################
# Images
docker-image: docker-image-tag docker-image-latest ## Create tag and latest docker image
docker-image-test: ## Create the test image
docker build -t ${NAME}-test:${TAG} -f Dockerfile.test .
docker-image-tag: docker-image-test ## Create the tag image
if [ -f ${NAME} ]; then rm -f ${NAME} ; fi
docker run -i -v ${DIR}:/go/src/github.com/dominikschulz/${NAME} -w /go/src/github.com/dominikschulz/${NAME} ${NAME}-test:${TAG} make ${NAME}
docker build -t ${NAME}:${TAG} -f Dockerfile .
# Tests
docker-test: docker-test-base docker-test-meta ## Run all tests in docker
docker-test-base: docker-image-test ## Run base test in docker
docker run -i -e "ENVIRONMENT=test" ${NAME}-test:${TAG} make test-base-norace
docker-test-meta: docker-image-test ## Run meta (lint/style) test in docker
docker run -i ${NAME}-test:${TAG} make test-meta