11
22COVERDIR = .coverage
33TOOLDIR = tools
4+ BINDIR = bin
5+ RELEASEDIR = release
46
5- GO_SRC := $(shell find . -name '* .go' ! -path '* /vendor/* ' ! -path 'tools/* ' )
6- GO_DIRS := $(shell find . -type d -name '* .go' ! -path '* /vendor/* ' ! -path 'tools/* ' )
7+ DIRS = $(BINDIR ) $(RELEASEDIR )
8+
9+ GO_SRC := $(shell find . -name '* .go' ! -path '* /vendor/* ' ! -path 'tools/* ' ! -path 'bin/* ' ! -path 'release/* ' )
10+ GO_DIRS := $(shell find . -type d -name '* .go' ! -path '* /vendor/* ' ! -path 'tools/* ' ! -path 'bin/* ' ! -path 'release/* ' )
711GO_PKGS := $(shell go list ./... | grep -v '/vendor/')
812
913CONTAINER_NAME ?= wrouesnel/postgres_exporter:latest
10- VERSION ?= $(shell git describe --dirty)
14+ BINARY := $(shell basename $(shell pwd) )
15+ VERSION ?= $(shell git describe --dirty 2>/dev/null)
16+ VERSION_SHORT ?= $(shell git describe --abbrev=0 2>/dev/null)
17+
18+ ifeq ($(VERSION ) ,)
19+ VERSION := v0.0.0
20+ endif
21+
22+ ifeq ($(VERSION_SHORT ) ,)
23+ VERSION_SHORT := v0.0.0
24+ endif
25+
26+ # By default this list is filtered down to some common platforms.
27+ platforms := $(subst /,-,$(shell go tool dist list | grep -e linux -e windows -e darwin | grep -e 386 -e amd64) )
28+ PLATFORM_BINS := $(patsubst % ,$(BINDIR ) /$(BINARY ) _$(VERSION_SHORT ) _% /$(BINARY ) ,$(platforms ) )
29+ PLATFORM_DIRS := $(patsubst % ,$(BINDIR ) /$(BINARY ) _$(VERSION_SHORT ) _% ,$(platforms ) )
30+ PLATFORM_TARS := $(patsubst % ,$(RELEASEDIR ) /$(BINARY ) _$(VERSION_SHORT ) _% .tar.gz,$(platforms ) )
31+
32+ # These are evaluated on use, and so will have the correct values in the build
33+ # rule (https://vic.demuzere.be/articles/golang-makefile-crosscompile/)
34+ PLATFORMS_TEMP = $(subst -, ,$(patsubst $(BINDIR ) /$(BINARY ) _$(VERSION_SHORT ) _% /$(BINARY ) ,% ,$@ ) )
35+ GOOS = $(word 1, $(PLATFORMS_TEMP ) )
36+ GOARCH = $(word 2, $(PLATFORMS_TEMP ) )
37+
38+ CURRENT_PLATFORM := $(BINDIR ) /$(BINARY ) _$(VERSION_SHORT ) _$(shell go env GOOS) -$(shell go env GOARCH) /$(BINARY )
39+
40+ CONCURRENT_LINTERS ?=
41+ ifeq ($(CONCURRENT_LINTERS ) ,)
42+ CONCURRENT_LINTERS = $(shell gometalinter --help | grep -o 'concurrency=\w* ' | cut -d= -f2 | cut -d' ' -f1)
43+ endif
1144
12- CONCURRENT_LINTERS ?= $(shell cat /proc/cpuinfo | grep processor | wc -l)
1345LINTER_DEADLINE ?= 30s
1446
47+ $(shell mkdir -p $(DIRS))
48+
1549export PATH := $(TOOLDIR ) /bin:$(PATH )
1650SHELL := env PATH=$(PATH ) /bin/bash
1751
18- all : style lint test postgres_exporter
52+ all : style lint test binary
1953
20- # Cross compilation (e.g. if you are on a Mac)
21- cross : docker-build docker
54+ binary : $(BINARY )
2255
23- # Simple go build
24- postgres_exporter : $(GO_SRC )
25- CGO_ENABLED=0 go build -a -ldflags " -extldflags '-static' -X main.Version=$( VERSION) " -o postgres_exporter .
56+ $(BINARY ) : $(CURRENT_PLATFORM )
57+ ln -sf $< $@
2658
27- postgres_exporter_integration_test : $(GO_SRC )
28- CGO_ENABLED=0 go test -c -tags integration \
29- -a -ldflags " -extldflags '-static' -X main.Version=$( VERSION) " -o postgres_exporter_integration_test -cover -covermode count .
59+ $(PLATFORM_BINS ) : $(GO_SRC )
60+ CGO_ENABLED=0 GOOS=$(GOOS ) GOARCH=$(GOARCH ) go build -a \
61+ -ldflags " -extldflags '-static' -X main.Version=$( VERSION) " \
62+ -o $@ .
63+
64+ $(PLATFORM_DIRS ) : $(PLATFORM_BINS )
65+
66+ $(PLATFORM_TARS ) : $(RELEASEDIR ) /% .tar.gz : $(BINDIR ) /%
67+ tar -czf $@ -C $(BINDIR ) $$(basename $< )
68+
69+ release-bin : $(PLATFORM_BINS )
70+
71+ release : $(PLATFORM_TARS )
3072
3173# Take a go build and turn it into a minimal container
32- docker : postgres_exporter
33- docker build -t $(CONTAINER_NAME ) .
74+ docker : $( CURRENT_PLATFORM )
75+ docker build --build-arg=binary= $( CURRENT_PLATFORM ) - t $(CONTAINER_NAME ) .
3476
3577style : tools
3678gometalinter --disable-all --enable=gofmt --vendor
@@ -42,14 +84,17 @@ lint: tools
4284fmt : tools
4385gofmt -s -w $(GO_SRC )
4486
45- run-tests : tools
46- mkdir -p $(COVERDIR )
47- rm -f $(COVERDIR ) /*
87+ postgres_exporter_integration_test : $(GO_SRC )
88+ CGO_ENABLED=0 go test -c -tags integration \
89+ -a -ldflags " -extldflags '-static' -X main.Version=$( VERSION) " \
90+ -o postgres_exporter_integration_test -cover -covermode count .
91+
92+ test : tools
93+ @mkdir -p $(COVERDIR )
94+ @rm -f $(COVERDIR ) /*
4895for pkg in $( GO_PKGS) ; do \
4996go test -v -covermode count -coverprofile=$(COVERDIR ) /$$(echo $$pkg | tr '/' '-' ) .out $$ pkg || exit 1 ; \
5097done
51-
52- test : run-tests
5398gocovmerge $(shell find $(COVERDIR ) -name '* .out') > cover.test.out
5499
55100test-integration : postgres_exporter postgres_exporter_integration_test
@@ -58,24 +103,13 @@ test-integration: postgres_exporter postgres_exporter_integration_test
58103cover.out : tools
59104gocovmerge cover.* .out > cover.out
60105
61- # Do a self-contained docker build - we pull the official upstream container
62- # and do a self-contained build.
63- docker-build :
64- docker run -v $(shell pwd) :/go/src/github.com/wrouesnel/postgres_exporter \
65- -v $(shell pwd) :/real_src \
66- -e SHELL_UID=$(shell id -u) -e SHELL_GID=$(shell id -g) \
67- -w /go/src/github.com/wrouesnel/postgres_exporter \
68- golang:1.9-wheezy \
69- /bin/bash -c " make >&2 && chown $$ SHELL_UID:$$ SHELL_GID ./postgres_exporter"
70- docker build -t $(CONTAINER_NAME ) .
71-
72- push :
73- docker push $(CONTAINER_NAME )
74-
106+ clean :
107+ [ ! -z $( BINDIR) ] && [ -e $( BINDIR) ] && find $(BINDIR ) -print -delete || /bin/true
108+ [ ! -z $( COVERDIR) ] && [ -e $( COVERDIR) ] && find $(COVERDIR ) -print -delete || /bin/true
109+ [ ! -z $( RELEASEDIR) ] && [ -e $( RELEASEDIR) ] && find $(RELEASEDIR ) -print -delete || /bin/true
110+ rm -f postgres_exporter postgres_exporter_integration_test
111+
75112tools :
76113$(MAKE ) -C $(TOOLDIR )
77-
78- clean :
79- rm -rf postgres_exporter postgres_exporter_integration_test $(COVERDIR )
80-
81- .PHONY : tools docker-build docker lint fmt test vet push cross clean
114+
115+ .PHONY : tools style fmt test all release binary clean
0 commit comments