-
Notifications
You must be signed in to change notification settings - Fork 14
/
base.mk
252 lines (208 loc) · 6.96 KB
/
base.mk
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
# Managed by makego. DO NOT EDIT.
# Must be set
$(call _assert_var,MAKEGO)
# Must be set
$(call _assert_var,MAKEGO_REMOTE)
# Must be set
$(call _assert_var,PROJECT)
# Must be set
$(call _assert_var,GO_MODULE)
UNAME_OS := $(shell uname -s)
UNAME_ARCH := $(shell uname -m)
ENV_DIR := .env
ENV_SH := $(ENV_DIR)/env.sh
ENV_BACKUP_DIR := $(HOME)/.config/$(PROJECT)/env
TMP := .tmp
# Settable
# These are exact file paths that are added to .gitignore and .dockerignore
FILE_IGNORES := $(FILE_IGNORES) $(ENV_DIR)/ $(TMP)/
# Settable
# These will be added without any modification to .gitignore
GIT_FILE_IGNORES ?=
# Settable
# These will be added without any modification to .dockerignore
DOCKER_FILE_IGNORES ?=
# Settable
CACHE_BASE ?= $(HOME)/.cache/$(PROJECT)
CACHE := $(CACHE_BASE)/$(UNAME_OS)/$(UNAME_ARCH)
CACHE_BIN := $(CACHE)/bin
CACHE_INCLUDE := $(CACHE)/include
CACHE_VERSIONS := $(CACHE)/versions
CACHE_ENV := $(CACHE)/env
CACHE_GO := $(CACHE)/go
# CACHE_GOBIN is the location where binaries are installed for Golang projects
# This is as opposed to CACHE_BIN, where dependencies binaries are installed
# The separation is needed for i.e. buf, where we need to bootstrap with a
# download from releases, but want to have a different namespace for the
# version of buf installed from source
# CACHE_GOBIN takes precedence over CACHE_BIN in PATH
CACHE_GOBIN := $(CACHE)/gobin
# CACHE_GOCACHE is where the build cache is stored.
CACHE_GOCACHE := $(CACHE)/gocache
ifeq ($(UNAME_OS),Darwin)
OPEN_CMD := open
endif
ifeq ($(UNAME_OS),Linux)
OPEN_CMD := xdg-open
endif
ifeq ($(UNAME_OS),Darwin)
# Explicitly use the "BSD" sed shipped with Darwin. Otherwise if the user has a
# different sed (such as gnu-sed) on their PATH this will fail in an opaque
# manner. /usr/bin/sed can only be modified if SIP is disabled, so this should
# be relatively safe.
SED_I := /usr/bin/sed -i ''
endif
ifeq ($(UNAME_OS),Linux)
SED_I := sed -i
endif
# Runtime ALL
# All variables exported here must also be added to env.sh
# via the direnv target below
export GO111MODULE := on
ifdef GOPRIVATE
export GOPRIVATE := $(GOPRIVATE),$(GO_MODULE)
else
export GOPRIVATE := $(GO_MODULE)
endif
export GOPATH := $(abspath $(CACHE_GO))
export GOBIN := $(abspath $(CACHE_GOBIN))
export GOCACHE := $(abspath $(CACHE_GOCACHE))
export GOMODCACHE := $(GOPATH)/pkg/mod
ifdef EXTRAPATH
EXTRAPATH := $(GOBIN):$(abspath $(CACHE_BIN)):$(EXTRAPATH)
else
EXTRAPATH := $(GOBIN):$(abspath $(CACHE_BIN))
endif
export PATH := $(EXTRAPATH):$(PATH)
export DOCKER_BUILDKIT := 1
# A bug when using BuildKit on Darwin/arm64 causes docker to misbehave and try
# to pull remote images instead of using local tagged images. Current
# workaround is to explicitly set the platform. See
# https://github.com/docker/for-mac/issues/5873 for details.
ifeq ($(UNAME_OS)/$(UNAME_ARCH),Darwin/arm64)
export DOCKER_DEFAULT_PLATFORM=linux/arm64
endif
# https://gist.github.com/prwhite/8168133#gistcomment-3785627
HELP_COLUMN_SIZE := 20
.PHONY: help
help: ## Show help message.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n\n\033[36m\033[0m"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-$(HELP_COLUMN_SIZE)s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
print-%:
@echo $($*)
.PHONY: envbackup
envbackup:
rm -rf "$(ENV_BACKUP_DIR)"
mkdir -p "$(dir $(ENV_BACKUP_DIR))"
cp -R "$(ENV_DIR)" "$(ENV_BACKUP_DIR)"
.PHONY: envrestore
envrestore:
@ if [ ! -d "$(ENV_BACKUP_DIR)" ]; then echo "no backup stored in $(ENV_BACKUP_DIR)"; exit 1; fi
rm -rf "$(ENV_DIR)"
cp -R "$(ENV_BACKUP_DIR)" "$(ENV_DIR)"
# All variables set in env.sh by this target need to also be exported
# above in the Runtime ALL section, except for MAKEFLAGS.
.PHONY: direnv
direnv:
@mkdir -p $(CACHE_ENV)
@rm -f $(CACHE_ENV)/env.sh
@echo 'export CACHE="$(abspath $(CACHE))"' >> $(CACHE_ENV)/env.sh
@echo 'export GO111MODULE="$(GO111MODULE)"' >> $(CACHE_ENV)/env.sh
@echo 'export GOPRIVATE="$(GOPRIVATE)"' >> $(CACHE_ENV)/env.sh
@echo 'export GOPATH="$(GOPATH)"' >> $(CACHE_ENV)/env.sh
@echo 'export GOBIN="$(GOBIN)"' >> $(CACHE_ENV)/env.sh
@echo 'export GOCACHE="$(GOCACHE)"' >> $(CACHE_ENV)/env.sh
@echo 'export GOMODCACHE="$(GOPATH)/pkg/mod"' >> $(CACHE_ENV)/env.sh
@echo 'export PATH="$(EXTRAPATH):$${PATH}"' >> $(CACHE_ENV)/env.sh
@echo 'export DOCKER_BUILDKIT=1' >> $(CACHE_ENV)/env.sh
@echo 'export MAKEFLAGS="-j $$(exec 2>/dev/null; nproc || getconf _NPROCESSORS_ONLN || sysctl -n hw.ncpu || echo 1)"' >> $(CACHE_ENV)/env.sh
ifneq ($(DOCKER_DEFAULT_PLATFORM),)
@echo 'export DOCKER_DEFAULT_PLATFORM="$(DOCKER_DEFAULT_PLATFORM)"' >> $(CACHE_ENV)/env.sh
endif
@echo '[ -f "$(abspath $(ENV_SH))" ] && . "$(abspath $(ENV_SH))"' >> $(CACHE_ENV)/env.sh
@echo $(CACHE_ENV)/env.sh
.PHONY: clean
clean: ## Run git clean with proper ignores.
git clean -xdf -e /$(ENV_DIR)/
.PHONY: cleancache
cleancache:
rm -rf $(CACHE_BASE)
.PHONY: nuke
nuke: clean
sudo rm -rf $(CACHE_GO)/pkg/mod
rm -rf $(CACHE_BASE)
.PHONY: dockerdeps
dockerdeps::
.PHONY: deps
deps:: dockerdeps
.PHONY: preinstallgenerate
preinstallgenerate::
.PHONY: pregenerate
pregenerate::
.PHONY: prepostgenerate
prepostgenerate::
.PHONY: postprepostgenerate
postprepostgenerate::
.PHONY: postgenerate
postgenerate::
.PHONY: licensegenerate
licensegenerate::
.PHONY: format
format::
.PHONY: generate
generate: ## Run all generation steps.
@$(MAKE) preinstallgenerate
@$(MAKE) pregenerate
@$(MAKE) prepostgenerate
@$(MAKE) postprepostgenerate
@$(MAKE) postgenerate
@$(MAKE) licensegenerate
@$(MAKE) format
.PHONY: checknodiffgenerated
checknodiffgenerated:
@ if [[ -d .git || -f .git ]]; then \
$(MAKE) __checknodiffgeneratedinternal; \
else \
echo "skipping make checknodiffgenerated due to no .git repository" >&2; \
fi
.PHONY: preupgrade
preupgrade::
.PHONY: postupgrade
postupgrade::
.PHONY: upgrade
upgrade: ## Upgrade all dependencies.
@$(MAKE) preupgrade
@$(MAKE) generate
@$(MAKE) postupgrade
.PHONY: upgradenopost
upgradenopost:
@$(MAKE) preupgrade
@$(MAKE) generate
.PHONY: copyfrommakego
copyfrommakego:
@rm -rf $(TMP)/makego
@mkdir -p $(TMP)
git clone --depth 1 $(MAKEGO_REMOTE) $(TMP)/makego
rm -rf $(MAKEGO)
cp -R $(TMP)/makego/make/go $(MAKEGO)
@rm -rf $(TMP)/makego
.PHONY: copytomakego
copytomakego:
@rm -rf $(TMP)/makego
@mkdir -p $(TMP)
git clone $(MAKEGO_REMOTE) $(TMP)/makego
rm -rf $(TMP)/makego/make/go
cp -R $(MAKEGO) $(TMP)/makego/make/go
bash $(MAKEGO)/scripts/pushall.bash $(TMP)/makego
.PHONY: initmakego
initmakego::
.PHONY: updategitignores
updategitignores:
@rm -f .gitignore
@echo '# Autogenerated by makego. DO NOT EDIT.' > .gitignore
@$(foreach file_ignore,$(sort $(FILE_IGNORES)),echo /$(file_ignore) >> .gitignore || exit 1; )
@$(foreach git_file_ignore,$(sort $(GIT_FILE_IGNORES)),echo $(git_file_ignore) >> .gitignore || exit 1; )
pregenerate:: updategitignores
.PHONY: __checknodiffgeneratedinternal
__checknodiffgeneratedinternal:
@echo bash $(MAKEGO)/scripts/checknodiffgenerated.bash make generate
@bash $(MAKEGO)/scripts/checknodiffgenerated.bash $(MAKE) generate