-
Notifications
You must be signed in to change notification settings - Fork 54
/
Makefile
198 lines (162 loc) · 6.01 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
all: clean test build
# Docsrv: configure the languages whose api-doc can be auto generated
LANGUAGES = "go"
# Docs: do not edit this
DOCS_REPOSITORY := https://github.com/src-d/docs
SHARED_PATH ?= $(shell pwd)/.docsrv-resources
DOCS_PATH ?= $(SHARED_PATH)/.docs
$(DOCS_PATH)/Makefile.inc:
git clone --quiet --depth 1 $(DOCS_REPOSITORY) $(DOCS_PATH);
-include $(DOCS_PATH)/Makefile.inc
# Package configuration
PROJECT = bblfshd
GO_PKG = github.com/bblfsh/$(PROJECT)
COMMANDS = bblfshd bblfshctl
DEPENDENCIES = \
golang.org/x/tools/cmd/cover
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
# Build information
BUILD ?= $(shell date +%FT%H:%M:%S%z)
GIT_COMMIT=$(shell git rev-parse HEAD | cut -c1-7)
GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "-dirty" || true)
DEV_PREFIX := dev
VERSION ?= $(DEV_PREFIX)-$(GIT_COMMIT)$(GIT_DIRTY)
TAG_DATE=$(shell date +%F)
# Go parameters
GO_CMD = go
GO_BUILD = GO111MODULE=on $(GO_CMD) build
GO_GET = GO111MODULE=on $(GO_CMD) get -v
GO_TEST = GO111MODULE=on $(GO_CMD) test -v
# Packages content
PKG_OS_bblfshctl = darwin linux windows
PKG_OS_bblfshd = linux
PKG_ARCH = amd64
# Coverage
COVERAGE_REPORT = coverage.txt
COVERAGE_PROFILE = profile.out
COVERAGE_MODE = atomic
ifneq ($(origin TRAVIS_TAG), undefined)
VERSION := $(TRAVIS_TAG)
endif
ifneq ($(origin CRON_TAG), undefined)
VERSION := $(CRON_TAG)
endif
# Build
LDFLAGS = -X main.version=$(VERSION) -X main.build=$(BUILD)
# Docker
DOCKER_CMD = docker
DOCKER_BUILD = $(DOCKER_CMD) build --build-arg BBLFSHD_VERSION=$(VERSION) --build-arg BBLFSHD_BUILD="$(BUILD)"
DOCKER_RUN = $(DOCKER_CMD) run --rm
DOCKER_BUILD_IMAGE = bblfshd-build
DOCKER_TAG ?= $(DOCKER_CMD) tag
DOCKER_PUSH ?= $(DOCKER_CMD) push
DOCKER_PULL ?= $(DOCKER_CMD) pull
# 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
# if we are not in master, and it's not a tag the push is disabled
ifneq ($(TRAVIS_BRANCH), master)
ifeq ($(TRAVIS_TAG), )
pushdisabled = "push disabled for non-master branches"
endif
endif
# if this is a pull request, the push is disabled
ifneq ($(TRAVIS_PULL_REQUEST), false)
pushdisabled = "push disabled for pull-requests"
endif
DOCKER_IMAGE ?= bblfsh/bblfshd
DOCKER_IMAGE_VERSIONED ?= $(call escape_docker_tag,$(DOCKER_IMAGE):$(VERSION))
DOCKER_IMAGE_FIXTURE ?= $(DOCKER_IMAGE):fixture
# Rules
dependencies: build-fixture
docker-build:
docker build --target=builder -t $(DOCKER_BUILD_IMAGE) .
test: dependencies docker-build
$(DOCKER_RUN) --privileged -v /var/run/docker.sock:/var/run/docker.sock -v $(GOPATH)/src/$(GO_PKG):/go/src/$(GO_PKG) -e TEST_NETWORKING=1 $(DOCKER_BUILD_IMAGE) $(GO_TEST) ./...
test-coverage: dependencies docker-build
mkdir -p reports
$(DOCKER_RUN) --privileged -v /var/run/docker.sock:/var/run/docker.sock -v $(GOPATH)/src/$(GO_PKG)/reports:/go/src/$(GO_PKG)/reports $(DOCKER_BUILD_IMAGE) make test-coverage-internal
mv ./reports/* ./ && rm -rf reports
test-coverage-internal:
export TEST_NETWORKING=1; \
echo "" > reports/$(COVERAGE_REPORT); \
for dir in $(NOVENDOR_PACKAGES); do \
$(GO_TEST) $$dir -coverprofile=reports/$(COVERAGE_PROFILE) -covermode=$(COVERAGE_MODE); \
if [ $$? != 0 ]; then \
exit 2; \
fi; \
if [ -f reports/$(COVERAGE_PROFILE) ]; then \
cat reports/$(COVERAGE_PROFILE) >> reports/$(COVERAGE_REPORT); \
rm reports/$(COVERAGE_PROFILE); \
fi; \
done;
build: dependencies docker-build
$(DOCKER_BUILD) -t $(call unescape_docker_tag,$(DOCKER_IMAGE_VERSIONED)) .
build-fixture:
cd $(BASE_PATH)/runtime/fixture/; \
docker build -t $(DOCKER_IMAGE_FIXTURE) .
build-drivers:
ifeq ($(TRAVIS_EVENT_TYPE), cron)
$(DOCKER_PULL) $(DOCKER_IMAGE):$(VERSION)
else
make build
endif
echo $(DOCKER_IMAGE):$(VERSION)
docker build -f Dockerfile.drivers --build-arg TAG="$(VERSION)" -t "$(DOCKER_IMAGE):$(VERSION)-drivers-$(TAG_DATE)" .
clean:
rm -rf $(BUILD_PATH); \
$(GO_CLEAN) .
push: build
$(if $(pushdisabled),$(error $(pushdisabled)))
@if [ "$$DOCKER_USERNAME" != "" ]; then \
$(DOCKER_CMD) login -u="$$DOCKER_USERNAME" -p="$$DOCKER_PASSWORD"; \
fi;
$(DOCKER_PUSH) $(call unescape_docker_tag,$(DOCKER_IMAGE_VERSIONED))
@if [ "$$TRAVIS_TAG" != "" ]; then \
$(DOCKER_TAG) $(call unescape_docker_tag,$(DOCKER_IMAGE_VERSIONED)) \
$(call unescape_docker_tag,$(DOCKER_IMAGE)):latest; \
$(DOCKER_PUSH) $(call unescape_docker_tag,$(DOCKER_IMAGE):latest); \
fi;
push-drivers: build-drivers
$(if $(pushdisabled),$(error $(pushdisabled)))
@if [ "$$DOCKER_USERNAME" != "" ]; then \
$(DOCKER_CMD) login -u="$$DOCKER_USERNAME" -p="$$DOCKER_PASSWORD"; \
fi;
$(DOCKER_PUSH) "$(call unescape_docker_tag,$(DOCKER_IMAGE_VERSIONED))-drivers-$(TAG_DATE)"
@if [ "$$TRAVIS_TAG" != "" ]; then \
$(DOCKER_TAG) "$(call unescape_docker_tag,$(DOCKER_IMAGE_VERSIONED))-drivers-$(TAG_DATE)" \
$(call unescape_docker_tag,$(DOCKER_IMAGE)):latest-drivers; \
$(DOCKER_PUSH) $(call unescape_docker_tag,$(DOCKER_IMAGE):latest-drivers); \
fi;
packages: dependencies docker-build
$(DOCKER_RUN) -v $(BUILD_PATH):/go/src/$(GO_PKG)/build \
-e TRAVIS_BRANCH=$(TRAVIS_BRANCH) \
-e TRAVIS_TAG=$(TRAVIS_TAG) \
-e TRAVIS_PULL_REQUEST=$(TRAVIS_PULL_REQUEST) \
$(DOCKER_BUILD_IMAGE) make packages-internal
packages-internal: $(COMMANDS)
$(COMMANDS):
for arch in $(PKG_ARCH); do \
for os in $(PKG_OS_$@); do \
mkdir -p $(BUILD_PATH)/$@_$${os}_$${arch}; \
echo ; \
echo "$${os} - $@"; \
GOOS=$${os} GOARCH=$${arch} $(GO_BUILD) $$([ $${os} = "linux" ] && echo -tags ostree) \
--ldflags "$(LDFLAGS)" \
-o "$(BUILD_PATH)/$@_$${os}_$${arch}/$@" \
$(CMD_PATH)/$@/main.go; \
cd $(BUILD_PATH); \
tar -cvzf $@_$(VERSION)_$${os}_$${arch}.tar.gz $@_$${os}_$${arch}/; \
cd $(BASE_PATH); \
done; \
done; \