-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
270 lines (225 loc) · 10.4 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# The binaries to build (just the basenames).
BINARY_NAMES := seabattle
# Where to push the docker image.
REGISTRY ?= docker.pkg.github.com/teratron/seabattle
# This version-strategy uses git tags to set the version string
#VERSION ?= $(shell git describe --tags --always --dirty)
# This version-strategy uses a manual value to set the version string
VERSION ?= 0.0.1
# Directories which hold app source (not vendored)
SRC_DIRS := cmd pkg
ALL_PLATFORMS := linux/amd64 linux/arm linux/arm64 linux/ppc64le linux/s390x windows/amd64
# Used internally. Users should pass GOOS and/or GOARCH.
OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS))
ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH))
TAG := $(VERSION)__$(OS)_$(ARCH)
BASE_IMAGE ?= gcr.io/distroless/static
BUILD_IMAGE ?= golang:1.17-alpine
BIN_EXTENSION :=
ifeq ($(OS), windows)
BIN_EXTENSION := .exe
endif
deps: ## install deps
go get -u gopkg.in/yaml.v2
go get -u github.com/go-sql-driver/mysql
go mod tidy
go mod vendor
all: build ## builds binaries for one platform ($OS/$ARCH)
# For the following OS/ARCH expansions, we transform OS/ARCH into OS_ARCH
# because make pattern rules don't match with embedded '/' characters.
build-%:
@$(MAKE) build \
--no-print-directory \
GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*))
container-%:
@$(MAKE) container \
--no-print-directory \
GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*))
push-%:
@$(MAKE) push \
--no-print-directory \
GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*))
all-build: $(addprefix build-, $(subst /,_, $(ALL_PLATFORMS))) ## builds binaries for all platforms
all-container: $(addprefix container-, $(subst /,_, $(ALL_PLATFORMS))) ## builds containers for all platforms
all-push: $(addprefix push-, $(subst /,_, $(ALL_PLATFORMS))) ## pushes containers for all platforms to the defined registry
# The following structure defeats Go's (intentional) behavior to always touch
# result files, even if they have not changed. This will still run `go` but
# will not trigger further work if nothing has actually changed.
OUTBINS = $(foreach bin,$(BINARY_NAMES),bin/$(OS)_$(ARCH)/$(bin)$(BIN_EXTENSION))
build: $(OUTBINS)
# Directories that we need created to build/test.
BUILD_DIRS := bin/$(OS)_$(ARCH) \
.go/bin/$(OS)_$(ARCH) \
.go/cache
# Each outbin target is just a facade for the respective stampfile target.
# This `eval` establishes the dependencies for each.
$(foreach outbin, $(OUTBINS), $(eval $(outbin): .go/$(outbin).stamp))
# This is the target definition for all outbins.
$(OUTBINS):
@true
# Each stampfile target can reference an $(OUTBIN) variable.
$(foreach outbin,$(OUTBINS),$(eval $(strip \
.go/$(outbin).stamp: OUTBIN = $(outbin) \
)))
# This is the target definition for all stampfiles.
# This will build the binary under ./.go and update the real binary iff needed.
STAMPS = $(foreach outbin,$(OUTBINS),.go/$(outbin).stamp)
.PHONY: $(STAMPS)
$(STAMPS): go-build
@echo "binary: $(OUTBIN)"
@if ! cmp -s .go/$(OUTBIN) $(OUTBIN); then \
mv .go/$(OUTBIN) $(OUTBIN); \
date >$@; \
fi
go-build: $(BUILD_DIRS) ## this runs the actual `go build` which updates all binaries
@echo
@echo "building for $(OS)/$(ARCH)"
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
$(BUILD_IMAGE) \
/bin/sh -c " \
ARCH=$(ARCH) \
OS=$(OS) \
VERSION=$(VERSION) \
./build/build.sh \
"
# Example: make shell CMD="-c 'date > datefile'"
shell: $(BUILD_DIRS) ## launches a shell in the containerized build environment
@echo "launching a shell in the containerized build environment"
@docker run \
-ti \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
$(BUILD_IMAGE) \
/bin/sh $(CMD)
CONTAINER_DOTFILES = $(foreach bin,$(BINARY_NAMES),.container-$(subst /,_,$(REGISTRY)/$(bin))-$(TAG))
container containers: $(CONTAINER_DOTFILES) ## build(s) container(s) for one platform ($OS/$ARCH)
@for bin in $(BINARY_NAMES); \
do echo "container: $(REGISTRY)/$$bin:$(TAG)"; \
done
# Each container-dotfile target can reference a $(BIN) variable.
# This is done in 2 steps to enable target-specific variables.
$(foreach bin,$(BINARY_NAMES),$(eval $(strip \
.container-$(subst /,_,$(REGISTRY)/$(bin))-$(TAG): BIN = $(bin) \
)))
$(foreach bin,$(BINARY_NAMES),$(eval \
.container-$(subst /,_,$(REGISTRY)/$(bin))-$(TAG): bin/$(OS)_$(ARCH)/$(bin)$(BIN_EXTENSION) in.Dockerfile \
))
# This is the target definition for all container-dotfiles.
# These are used to track build state in hidden files.
$(CONTAINER_DOTFILES):
@sed \
-e 's|{ARG_BIN}|$(BIN)$(BIN_EXTENSION)|g' \
-e 's|{ARG_ARCH}|$(ARCH)|g' \
-e 's|{ARG_OS}|$(OS)|g' \
-e 's|{ARG_FROM}|$(BASE_IMAGE)|g' \
in.Dockerfile > .dockerfile-$(BIN)-$(OS)_$(ARCH)
@docker build -t $(REGISTRY)/$(BIN):$(TAG) -t $(REGISTRY)/$(BIN):latest -f .dockerfile-$(BIN)-$(OS)_$(ARCH) .
@docker images -q $(REGISTRY)/$(BIN):$(TAG) > $@
@echo
push: $(CONTAINER_DOTFILES) ## pushes the container for one platform ($OS/$ARCH) to the defined registry
@for bin in $(BINARY_NAMES); do \
docker push $(REGISTRY)/$$bin:$(TAG); \
done
manifest-list: all-push ## builds a manifest list of containers for all platforms
@for bin in $(BINARY_NAMES); do \
platforms=$$(echo $(ALL_PLATFORMS) | sed 's/ /,/g'); \
manifest-tool \
--username=oauth2accesstoken \
--password=$$(gcloud auth print-access-token) \
push from-args \
--platforms "$$platforms" \
--template $(REGISTRY)/$$bin:$(VERSION)__OS_ARCH \
--target $(REGISTRY)/$$bin:$(VERSION)
version: ## outputs the version string
@echo $(VERSION)
test: $(BUILD_DIRS) ## runs tests, as defined in ./build/test.sh
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
$(BUILD_IMAGE) \
/bin/sh -c " \
ARCH=$(ARCH) \
OS=$(OS) \
VERSION=$(VERSION) \
./build/test.sh $(SRC_DIRS) \
"
$(BUILD_DIRS):
@mkdir -p $@
clean: container-clean bin-clean ## removes built binaries and temporary files
container-clean: ## removes temporary files
rm -rf .container-* .dockerfile-*
bin-clean: ## removes built binaries
rm -rf .go bin
fmt: ## go fmt
$(call print-target)
go fmt ./...
.PHONY: help
help:
@echo "VARIABLES:"
@echo " BINS = $(BINARY_NAMES)"
@echo " OS = $(OS)"
@echo " ARCH = $(ARCH)"
@echo " REGISTRY = $(REGISTRY)"
@echo
@echo "TARGETS:"
@awk ' \
BEGIN {FS = ":.*?## "} \
/^[a-zA-Z_-]+:.*?## / \
{printf "\033[36m%-16s\033[0m %s\n", $$1, $$2} \
' \
$(MAKEFILE_LIST)
.DEFAULT_GOAL := help
# help: @HELP prints this message
#help:
# @echo "VARIABLES:"
# @echo " BINS = $(BINARY_NAMES)"
# @echo " OS = $(OS)"
# @echo " ARCH = $(ARCH)"
# @echo " REGISTRY = $(REGISTRY)"
# @echo
# @echo "TARGETS:"
# @grep -E '^.*: *@HELP' $(MAKEFILE_LIST) \
# | awk ' \
# BEGIN {FS = ": *@HELP"}; \
# { printf " %-20s %s\n", $$1, $$2 }; \
# '
#.PHONY: help2
#help2:
# @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
# | sort \
# | awk ' \
# BEGIN {FS = ":.*?## "}; \
# {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}; \
# '
#.DEFAULT_GOAL := help2
define print-target
@printf "Executing target: \033[36m$@\033[0m\n"
endef