1# Root directory of the project (absolute path).
2ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
3
4# Used to populate version variable in main package.
5VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always)
6REVISION=$(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)
7
8
9PKG=github.com/docker/distribution
10
11# Project packages.
12PACKAGES=$(shell go list -tags "${BUILDTAGS}" ./... | grep -v /vendor/)
13INTEGRATION_PACKAGE=${PKG}
14COVERAGE_PACKAGES=$(filter-out ${PKG}/registry/storage/driver/%,${PACKAGES})
15
16
17# Project binaries.
18COMMANDS=registry digest registry-api-descriptor-template
19
20# Allow turning off function inlining and variable registerization
21ifeq (${DISABLE_OPTIMIZATION},true)
22	GO_GCFLAGS=-gcflags "-N -l"
23	VERSION:="$(VERSION)-noopt"
24endif
25
26WHALE = "+"
27
28# Go files
29#
30TESTFLAGS_RACE=
31GOFILES=$(shell find . -type f -name '*.go')
32GO_TAGS=$(if $(BUILDTAGS),-tags "$(BUILDTAGS)",)
33GO_LDFLAGS=-ldflags '-s -w -X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PKG) $(EXTRA_LDFLAGS)'
34
35BINARIES=$(addprefix bin/,$(COMMANDS))
36
37# Flags passed to `go test`
38TESTFLAGS ?= -v $(TESTFLAGS_RACE)
39TESTFLAGS_PARALLEL ?= 8
40
41.PHONY: all build binaries check clean test test-race test-full integration coverage
42.DEFAULT: all
43
44all: binaries
45
46# This only needs to be generated by hand when cutting full releases.
47version/version.go:
48	@echo "$(WHALE) $@"
49	./version/version.sh > $@
50
51check: ## run all linters (TODO: enable "unused", "varcheck", "ineffassign", "unconvert", "staticheck", "goimports", "structcheck")
52	@echo "$(WHALE) $@"
53	gometalinter --config .gometalinter.json ./...
54
55test: ## run tests, except integration test with test.short
56	@echo "$(WHALE) $@"
57	@go test ${GO_TAGS} -test.short ${TESTFLAGS} $(filter-out ${INTEGRATION_PACKAGE},${PACKAGES})
58
59test-race: ## run tests, except integration test with test.short and race
60	@echo "$(WHALE) $@"
61	@go test ${GO_TAGS} -race -test.short ${TESTFLAGS} $(filter-out ${INTEGRATION_PACKAGE},${PACKAGES})
62
63test-full: ## run tests, except integration tests
64	@echo "$(WHALE) $@"
65	@go test ${GO_TAGS} ${TESTFLAGS} $(filter-out ${INTEGRATION_PACKAGE},${PACKAGES})
66
67integration: ## run integration tests
68	@echo "$(WHALE) $@"
69	@go test ${TESTFLAGS} -parallel ${TESTFLAGS_PARALLEL} ${INTEGRATION_PACKAGE}
70
71coverage: ## generate coverprofiles from the unit tests
72	@echo "$(WHALE) $@"
73	@rm -f coverage.txt
74	@go test ${GO_TAGS} -i ${TESTFLAGS} $(filter-out ${INTEGRATION_PACKAGE},${COVERAGE_PACKAGES}) 2> /dev/null
75	@( for pkg in $(filter-out ${INTEGRATION_PACKAGE},${COVERAGE_PACKAGES}); do \
76		go test ${GO_TAGS} ${TESTFLAGS} \
77			-cover \
78			-coverprofile=profile.out \
79			-covermode=atomic $$pkg || exit; \
80		if [ -f profile.out ]; then \
81			cat profile.out >> coverage.txt; \
82			rm profile.out; \
83		fi; \
84	done )
85
86FORCE:
87
88# Build a binary from a cmd.
89bin/%: cmd/% FORCE
90	@echo "$(WHALE) $@${BINARY_SUFFIX}"
91	@go build ${GO_GCFLAGS} ${GO_BUILD_FLAGS} -o $@${BINARY_SUFFIX} ${GO_LDFLAGS} ${GO_TAGS}  ./$<
92
93binaries: $(BINARIES) ## build binaries
94	@echo "$(WHALE) $@"
95
96build:
97	@echo "$(WHALE) $@"
98	@go build ${GO_GCFLAGS} ${GO_BUILD_FLAGS} ${GO_LDFLAGS} ${GO_TAGS} $(PACKAGES)
99
100clean: ## clean up binaries
101	@echo "$(WHALE) $@"
102	@rm -f $(BINARIES)
103