forked from hashicorp/http-echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
236 lines (208 loc) · 6.56 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Metadata about this makefile and position
MKFILE_PATH := $(lastword $(MAKEFILE_LIST))
CURRENT_DIR := $(patsubst %/,%,$(dir $(realpath $(MKFILE_PATH))))
# Ensure GOPATH
GOPATH ?= $(HOME)/go
# List all our actual files, excluding vendor
GOFILES ?= $(shell go list $(TEST) | grep -v /vendor/)
# Tags specific for building
GOTAGS ?=
# Number of procs to use
GOMAXPROCS ?= 4
# Get the project metadata
GOVERSION := 1.9.2
PROJECT := $(CURRENT_DIR:$(GOPATH)/src/%=%)
OWNER := $(notdir $(patsubst %/,%,$(dir $(PROJECT))))
NAME := $(notdir $(PROJECT))
GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
VERSION := $(shell awk -F\" '/Version/ { print $$2; exit }' "${CURRENT_DIR}/version/version.go")
EXTERNAL_TOOLS = \
github.com/golang/dep/cmd/dep
# Current system information
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
# Default os-arch combination to build
XC_OS ?= darwin linux windows
XC_ARCH ?= amd64
XC_EXCLUDE ?=
# GPG Signing key (blank by default, means no GPG signing)
GPG_KEY ?=
# List of ldflags
LD_FLAGS ?= \
-s \
-w \
-X ${PROJECT}/version.Name=${NAME} \
-X ${PROJECT}/version.GitCommit=${GIT_COMMIT}
# List of Docker targets to build
DOCKER_TARGETS ?= scratch
# List of tests to run
TEST ?= ./...
# Create a cross-compile target for every os-arch pairing. This will generate
# a make target for each os/arch like "make linux/amd64" as well as generate a
# meta target (build) for compiling everything.
define make-xc-target
$1/$2:
ifneq (,$(findstring ${1}/${2},$(XC_EXCLUDE)))
@printf "%s%20s %s\n" "-->" "${1}/${2}:" "${PROJECT} (excluded)"
else
@printf "%s%20s %s\n" "-->" "${1}/${2}:" "${PROJECT}"
@docker run \
--interactive \
--rm \
--dns="8.8.8.8" \
--volume="${CURRENT_DIR}:/go/src/${PROJECT}" \
--workdir="/go/src/${PROJECT}" \
"golang:${GOVERSION}" \
env \
CGO_ENABLED="0" \
GOOS="${1}" \
GOARCH="${2}" \
go build \
-a \
-o="pkg/${1}_${2}/${NAME}${3}" \
-ldflags "${LD_FLAGS}" \
-tags "${GOTAGS}"
endif
.PHONY: $1/$2
$1:: $1/$2
.PHONY: $1
build:: $1/$2
.PHONY: build
endef
$(foreach goarch,$(XC_ARCH),$(foreach goos,$(XC_OS),$(eval $(call make-xc-target,$(goos),$(goarch),$(if $(findstring windows,$(goos)),.exe,)))))
# bootstrap installs the necessary go tools for development or build.
bootstrap:
@echo "==> Bootstrapping ${PROJECT}"
@for t in ${EXTERNAL_TOOLS}; do \
echo "--> Installing $$t" ; \
go get -u "$$t"; \
done
.PHONY: bootstrap
# deps updates all dependencies for this project.
deps:
@echo "==> Updating deps for ${PROJECT}"
@dep ensure -update
@dep prune
.PHONY: deps
# dev builds and installs the project locally.
dev:
@echo "==> Installing ${NAME} for ${GOOS}/${GOARCH}"
@rm -f "${GOPATH}/pkg/${GOOS}_${GOARCH}/${PROJECT}/version.a" # ldflags change and go doesn't detect
@env \
CGO_ENABLED="0" \
go install \
-ldflags "${LD_FLAGS}" \
-tags "${GOTAGS}"
.PHONY: dev
# dist builds the binaries and then signs and packages them for distribution
dist:
ifndef GPG_KEY
@echo "==> ERROR: No GPG key specified! Without a GPG key, this release cannot"
@echo " be signed. Set the environment variable GPG_KEY to the ID of"
@echo " the GPG key to continue."
@exit 127
else
@$(MAKE) -f "${MKFILE_PATH}" _cleanup
@$(MAKE) -f "${MKFILE_PATH}" -j4 build
@$(MAKE) -f "${MKFILE_PATH}" _compress _checksum _sign
endif
.PHONY: dist
# Create a docker compile and push target for each container. This will create
# docker-build/scratch, docker-push/scratch, etc. It will also create two meta
# targets: docker-build and docker-push, which will build and push all
# configured Docker containers. Each container must have a folder in docker/
# named after itself with a Dockerfile (docker/alpine/Dockerfile).
define make-docker-target
docker-build/$1:
@echo "==> Building ${1} Docker container for ${PROJECT}"
@docker build \
--rm \
--force-rm \
--no-cache \
--squash \
--compress \
--file="docker/${1}/Dockerfile" \
--build-arg="LD_FLAGS=${LD_FLAGS}" \
--build-arg="GOTAGS=${GOTAGS}" \
$(if $(filter $1,scratch),--tag="${OWNER}/${NAME}",) \
--tag="${OWNER}/${NAME}:${1}" \
--tag="${OWNER}/${NAME}:${VERSION}-${1}" \
"${CURRENT_DIR}"
.PHONY: docker-build/$1
docker-build:: docker-build/$1
.PHONY: docker-build
docker-push/$1:
@echo "==> Pushing ${1} to Docker registry"
$(if $(filter $1,scratch),@docker push "${OWNER}/${NAME}",)
@docker push "${OWNER}/${NAME}:${1}"
@docker push "${OWNER}/${NAME}:${VERSION}-${1}"
.PHONY: docker-push/$1
docker-push:: docker-push/$1
.PHONY: docker-push
endef
$(foreach target,$(DOCKER_TARGETS),$(eval $(call make-docker-target,$(target))))
# test runs the test suite.
test:
@echo "==> Testing ${NAME}"
@go test -timeout=30s -parallel=20 -tags="${GOTAGS}" ${GOFILES} ${TESTARGS}
.PHONY: test
# test-race runs the test suite.
test-race:
@echo "==> Testing ${NAME} (race)"
@go test -timeout=60s -race -tags="${GOTAGS}" ${GOFILES} ${TESTARGS}
.PHONY: test-race
# _cleanup removes any previous binaries
_cleanup:
@rm -rf "${CURRENT_DIR}/pkg/"
@rm -rf "${CURRENT_DIR}/bin/"
# _compress compresses all the binaries in pkg/* as tarball and zip.
_compress:
@mkdir -p "${CURRENT_DIR}/pkg/dist"
@for platform in $$(find ./pkg -mindepth 1 -maxdepth 1 -type d); do \
osarch=$$(basename "$$platform"); \
if [ "$$osarch" = "dist" ]; then \
continue; \
fi; \
\
ext=""; \
if test -z "$${osarch##*windows*}"; then \
ext=".exe"; \
fi; \
cd "$$platform"; \
tar -czf "${CURRENT_DIR}/pkg/dist/${NAME}_${VERSION}_$${osarch}.tgz" "${NAME}$${ext}"; \
zip -q "${CURRENT_DIR}/pkg/dist/${NAME}_${VERSION}_$${osarch}.zip" "${NAME}$${ext}"; \
cd - &>/dev/null; \
done
.PHONY: _compress
# _checksum produces the checksums for the binaries in pkg/dist
_checksum:
@cd "${CURRENT_DIR}/pkg/dist" && \
shasum --algorithm 256 * > ${CURRENT_DIR}/pkg/dist/${NAME}_${VERSION}_SHA256SUMS && \
cd - &>/dev/null
.PHONY: _checksum
# _sign signs the binaries using the given GPG_KEY. This should not be called
# as a separate function.
_sign:
@echo "==> Signing ${PROJECT} at v${VERSION}"
@gpg \
--default-key "${GPG_KEY}" \
--detach-sig "${CURRENT_DIR}/pkg/dist/${NAME}_${VERSION}_SHA256SUMS"
@git commit \
--allow-empty \
--gpg-sign="${GPG_KEY}" \
--message "Release v${VERSION}" \
--quiet \
--signoff
@git tag \
--annotate \
--create-reflog \
--local-user "${GPG_KEY}" \
--message "Version ${VERSION}" \
--sign \
"v${VERSION}" master
@echo "--> Do not forget to run:"
@echo ""
@echo " git push && git push --tags"
@echo ""
@echo "And then upload the binaries in dist/!"
.PHONY: _sign