-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMakefile
142 lines (111 loc) · 3.69 KB
/
Makefile
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Package configuration
PROJECT = bblfsh-tools
COMMANDS = bblfsh-tools
DEPENDENCIES = \
golang.org/x/tools/cmd/cover \
github.com/Masterminds/glide
NOVENDOR_PACKAGES := $(shell go list ./... | grep -v '/vendor/')
# Environment
BASE_PATH := $(shell pwd)
VENDOR_PATH := $(BASE_PATH)/vendor
BUILD_PATH := $(BASE_PATH)/build
CMD_PATH := $(BASE_PATH)/cmd
SHA1 := $(shell git log --format='%H' -n 1 | cut -c1-10)
BUILD := $(shell date +"%m-%d-%Y_%H_%M_%S")
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
# Go parameters
GO_CMD = go
GO_BUILD = $(GO_CMD) build -a
GO_CLEAN = $(GO_CMD) clean
GO_GET = $(GO_CMD) get -v
GO_TEST = $(GO_CMD) test -v
GLIDE = glide
# Coverage
COVERAGE_REPORT = coverage.txt
COVERAGE_PROFILE = profile.out
COVERAGE_MODE = atomic
ifneq ($(origin TRAVIS_TAG), undefined)
BRANCH := $(TRAVIS_TAG)
endif
# Build
LDFLAGS = -X main.version=$(BRANCH) -X main.build=$(BUILD)
# Docker
DOCKER_CMD = docker
DOCKER_BUILD = $(DOCKER_CMD) build
DOCKER_RUN = $(DOCKER_CMD) run --rm
DOCKER_BUILD_IMAGE = bblfsh-tools-build
DOCKER_TAG ?= $(DOCKER_CMD) tag
DOCKER_PUSH ?= $(DOCKER_CMD) push
# escape_docker_tag escape colon char to allow use a docker tag as rule
define escape_docker_tag
$(subst :,--,$(1))
endef
# unescape_docker_tag an escaped docker tag to be use in a docker command
define unescape_docker_tag
$(subst --,:,$(1))
endef
DOCKER_DEV_PREFIX := dev
DOCKER_VERSION ?= $(DOCKER_DEV_PREFIX)-$(shell git rev-parse HEAD | cut -c1-7)
# if TRAVIS_TAG defined DOCKER_VERSION is overrided
ifneq ($(TRAVIS_TAG), )
DOCKER_VERSION := $(TRAVIS_TAG)
endif
# if we are not in tag, the push is disabled
ifeq ($(firstword $(subst -, ,$(DOCKER_VERSION))), $(DOCKER_DEV_PREFIX))
pushdisabled = "push disabled for development versions"
endif
DOCKER_IMAGE ?= bblfsh/tools
DOCKER_IMAGE_VERSIONED ?= $(call escape_docker_tag,$(DOCKER_IMAGE):$(DOCKER_VERSION))
# Rules
all: clean build
dependencies: $(DEPENDENCIES) $(VENDOR_PATH) $(NOVENDOR_PACKAGES)
$(DEPENDENCIES):
$(GO_GET) $@/...
$(NOVENDOR_PACKAGES):
$(GO_GET) $@
$(VENDOR_PATH):
$(GLIDE) install
docker-build:
$(DOCKER_BUILD) -f Dockerfile.build -t $(DOCKER_BUILD_IMAGE) .
test: dependencies docker-build
$(DOCKER_RUN) --privileged -v $(GOPATH):/go $(DOCKER_BUILD_IMAGE) make test-internal
test-internal:
export TEST_NETWORKING=1; \
$(GO_TEST) $(NOVENDOR_PACKAGES)
test-coverage-internal: dependencies docker-build
$(DOCKER_RUN) --privileged -v $(GOPATH):/go $(DOCKER_BUILD_IMAGE) make test-coverage
test-coverage:
export TEST_NETWORKING=1; \
echo "" > $(COVERAGE_REPORT); \
for dir in $(NOVENDOR_PACKAGES); do \
$(GO_TEST) $$dir -coverprofile=$(COVERAGE_PROFILE) -covermode=$(COVERAGE_MODE); \
if [ $$? != 0 ]; then \
exit 2; \
fi; \
if [ -f $(COVERAGE_PROFILE) ]; then \
cat $(COVERAGE_PROFILE) >> $(COVERAGE_REPORT); \
rm $(COVERAGE_PROFILE); \
fi; \
done;
build: dependencies docker-build
$(DOCKER_RUN) -v $(GOPATH):/go $(DOCKER_BUILD_IMAGE) make build-internal
build-internal:
mkdir -p $(BUILD_PATH); \
for cmd in $(COMMANDS); do \
cd $(CMD_PATH)/$${cmd}; \
$(GO_BUILD) --ldflags '$(LDFLAGS)' -o $(BUILD_PATH)/$${cmd} .; \
done;
docker-image-build: build
$(DOCKER_BUILD) -t $(call unescape_docker_tag,$(DOCKER_IMAGE_VERSIONED)) .
clean:
rm -rf $(BUILD_PATH); \
$(GO_CLEAN) .
push: docker-image-build
$(if $(pushdisabled),$(error $(pushdisabled)))
@if [ "$$DOCKER_USERNAME" != "" ]; then \
$(DOCKER_CMD) login -u="$$DOCKER_USERNAME" -p="$$DOCKER_PASSWORD"; \
fi;
$(DOCKER_TAG) $(call unescape_docker_tag,$(DOCKER_IMAGE_VERSIONED)) \
$(call unescape_docker_tag,$(DOCKER_IMAGE)):latest
$(DOCKER_PUSH) $(call unescape_docker_tag,$(DOCKER_IMAGE_VERSIONED))
$(DOCKER_PUSH) $(call unescape_docker_tag,$(DOCKER_IMAGE):latest)