-
Notifications
You must be signed in to change notification settings - Fork 933
/
Makefile
310 lines (261 loc) · 9.85 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
DOMAIN=lxd
POFILES=$(wildcard po/*.po)
MOFILES=$(patsubst %.po,%.mo,$(POFILES))
LINGUAS=$(basename $(POFILES))
POTFILE=po/$(DOMAIN).pot
VERSION=$(or ${CUSTOM_VERSION},$(shell grep "var Version" shared/version/flex.go | cut -d'"' -f2))
ARCHIVE=lxd-$(VERSION).tar
HASH := \#
TAG_SQLITE3=$(shell printf "$(HASH)include <dqlite.h>\nvoid main(){dqlite_node_id n = 1;}" | $(CC) ${CGO_CFLAGS} -o /dev/null -xc - >/dev/null 2>&1 && echo "libsqlite3")
GOPATH ?= $(shell go env GOPATH)
CGO_LDFLAGS_ALLOW ?= (-Wl,-wrap,pthread_create)|(-Wl,-z,now)
SPHINXENV=doc/.sphinx/venv/bin/activate
SPHINXPIPPATH=doc/.sphinx/venv/bin/pip
GOMIN=1.23.3
GOCOVERDIR ?= $(shell go env GOCOVERDIR)
DQLITE_BRANCH=master
ifneq "$(wildcard vendor)" ""
DQLITE_PATH=$(CURDIR)/vendor/dqlite
else
DQLITE_PATH=$(GOPATH)/deps/dqlite
endif
.PHONY: default
default: all
.PHONY: all
all: client lxd lxd-agent lxd-migrate
.PHONY: build
build: lxd
.PHONY: lxd
lxd: lxd-metadata
ifeq "$(TAG_SQLITE3)" ""
@echo "Missing dqlite, run \"make deps\" to setup."
exit 1
endif
ifeq "$(GOCOVERDIR)" ""
CC="$(CC)" CGO_LDFLAGS_ALLOW="$(CGO_LDFLAGS_ALLOW)" go install -v -tags "$(TAG_SQLITE3)" -trimpath $(DEBUG) ./lxd ./lxc-to-lxd
CGO_ENABLED=0 go install -v -tags netgo -trimpath $(DEBUG) ./lxd-user ./lxd-benchmark
else
CC="$(CC)" CGO_LDFLAGS_ALLOW="$(CGO_LDFLAGS_ALLOW)" go install -v -tags "$(TAG_SQLITE3)" -trimpath -cover $(DEBUG) ./lxd ./lxc-to-lxd
CGO_ENABLED=0 go install -v -tags netgo -trimpath -cover $(DEBUG) ./lxd-user ./lxd-benchmark
endif
@echo "LXD built successfully"
.PHONY: client
client:
ifeq "$(GOCOVERDIR)" ""
go install -v -trimpath $(DEBUG) ./lxc
else
go install -v -trimpath -cover $(DEBUG) ./lxc
endif
@echo "LXD client built successfully"
.PHONY: lxd-agent
lxd-agent:
ifeq "$(GOCOVERDIR)" ""
CGO_ENABLED=0 go install -v -trimpath -tags agent,netgo ./lxd-agent
else
CGO_ENABLED=0 go install -v -trimpath -cover -tags agent,netgo ./lxd-agent
endif
@echo "LXD agent built successfully"
.PHONY: lxd-metadata
lxd-metadata:
ifeq "$(GOCOVERDIR)" ""
CGO_ENABLED=0 go install -v -trimpath -tags lxd-metadata ./lxd/lxd-metadata
else
CGO_ENABLED=0 go install -v -trimpath -cover -tags lxd-metadata ./lxd/lxd-metadata
endif
@echo "LXD metadata built successfully"
.PHONY: lxd-migrate
lxd-migrate:
ifeq "$(GOCOVERDIR)" ""
CGO_ENABLED=0 go install -v -trimpath -tags netgo ./lxd-migrate
else
CGO_ENABLED=0 go install -v -trimpath -cover -tags netgo ./lxd-migrate
endif
@echo "LXD-MIGRATE built successfully"
.PHONY: deps
deps:
# dqlite (+raft)
@if [ ! -e "$(DQLITE_PATH)" ]; then \
echo "Retrieving dqlite from ${DQLITE_BRANCH} branch"; \
git clone --depth=1 --branch "${DQLITE_BRANCH}" "https://github.com/canonical/dqlite" "$(DQLITE_PATH)"; \
elif [ -e "$(DQLITE_PATH)/.git" ]; then \
echo "Updating existing dqlite branch"; \
cd "$(DQLITE_PATH)"; git pull; \
fi
cd "$(DQLITE_PATH)" && \
autoreconf -i && \
./configure --enable-build-raft && \
make
# environment
@echo ""
@echo "Please set the following in your environment (possibly ~/.bashrc)"
@echo "export CGO_CFLAGS=\"-I$(DQLITE_PATH)/include/\""
@echo "export CGO_LDFLAGS=\"-L$(DQLITE_PATH)/.libs/\""
@echo "export LD_LIBRARY_PATH=\"$(DQLITE_PATH)/.libs/\""
@echo "export CGO_LDFLAGS_ALLOW=\"(-Wl,-wrap,pthread_create)|(-Wl,-z,now)\""
.PHONY: update-gomod
update-gomod:
ifneq "$(LXD_OFFLINE)" ""
@echo "The update-gomod target cannot be run in offline mode."
exit 1
endif
# Update gomod dependencies
go get -t -v -u ./...
# Static pins
go get github.com/gorilla/[email protected] # Due to riscv64 crashes in LP
# Enforce minimum go version
go mod tidy -go=$(GOMIN)
# Use the bundled toolchain that meets the minimum go version
go get toolchain@none
@echo "Dependencies updated"
if [ -t 0 ] && ! git diff --quiet -- ./go.mod ./go.sum; then \
read -rp "Would you like to commit gomod changes (Y/n)? " answer; \
if [ "$${answer:-y}" = "y" ] || [ "$${answer:-y}" = "Y" ]; then \
git commit -S -sm "gomod: Update dependencies" -- ./go.mod ./go.sum;\
fi;\
fi
.PHONY: update-protobuf
update-protobuf:
protoc --go_out=. ./lxd/migration/migrate.proto
.PHONY: update-schema
update-schema:
cd lxd/db/generate && go build -v -trimpath -o $(GOPATH)/bin/lxd-generate -tags "$(TAG_SQLITE3)" $(DEBUG) && cd -
go generate ./...
gofmt -s -w ./lxd/db/
goimports -w ./lxd/db/
@echo "Code generation completed"
.PHONY: update-api
update-api:
ifeq "$(LXD_OFFLINE)" ""
(cd / ; go install github.com/go-swagger/go-swagger/cmd/swagger@latest)
endif
@# Generate spec and exclude package from dependency which causes a 'classifier: unknown swagger annotation "extendee"' error.
@# For more details see: https://github.com/go-swagger/go-swagger/issues/2917.
swagger generate spec -o doc/rest-api.yaml -w ./lxd -m -x github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options
if [ -t 0 ] && ! git diff --quiet -- ./doc/rest-api.yaml; then \
read -rp "Would you like to commit swagger YAML changes (Y/n)? " answer; \
if [ "$${answer:-y}" = "y" ] || [ "$${answer:-y}" = "Y" ]; then \
git commit -S -sm "doc/rest-api: Refresh swagger YAML" -- ./doc/rest-api.yaml;\
fi;\
fi
.PHONY: update-metadata
update-metadata: build
@echo "Generating golang documentation metadata"
$(GOPATH)/bin/lxd-metadata . --json ./lxd/metadata/configuration.json --txt ./doc/metadata.txt --substitution-db ./doc/substitutions.yaml
if [ -t 0 ] && ! git diff --quiet -- ./lxd/metadata/configuration.json ./doc/metadata.txt; then \
read -rp "Would you like to commit metadata changes (Y/n)? " answer; \
if [ "$${answer:-y}" = "y" ] || [ "$${answer:-y}" = "Y" ]; then \
git commit -S -sm "doc: Update metadata" -- ./lxd/metadata/configuration.json ./doc/metadata.txt;\
fi;\
fi
.PHONY: doc
doc: doc-clean doc-install doc-html doc-objects
.PHONY: doc-incremental
doc-incremental: doc-html
doc-%:
cd doc && $(MAKE) -f Makefile $*
.PHONY: debug
debug:
ifeq "$(TAG_SQLITE3)" ""
@echo "Missing custom libsqlite3, run \"make deps\" to setup."
exit 1
endif
CC="$(CC)" CGO_LDFLAGS_ALLOW="$(CGO_LDFLAGS_ALLOW)" go install -v -tags "$(TAG_SQLITE3) logdebug" $(DEBUG) ./...
CGO_ENABLED=0 go install -v -trimpath -tags "netgo,logdebug" ./lxd-migrate
CGO_ENABLED=0 go install -v -trimpath -tags "agent,netgo,logdebug" ./lxd-agent
@echo "LXD built successfully"
.PHONY: check
check: default check-unit
cd test && ./main.sh
.PHONY: unit
check-unit:
ifeq "$(GOCOVERDIR)" ""
CGO_LDFLAGS_ALLOW="$(CGO_LDFLAGS_ALLOW)" go test -v -failfast -tags "$(TAG_SQLITE3)" $(DEBUG) ./...
else
CGO_LDFLAGS_ALLOW="$(CGO_LDFLAGS_ALLOW)" go test -v -failfast -tags "$(TAG_SQLITE3)" $(DEBUG) ./... -cover -test.gocoverdir="${GOCOVERDIR}"
endif
.PHONY: dist
dist: doc
# Cleanup
rm -Rf $(ARCHIVE).gz
# Create build dir
$(eval TMP := $(shell mktemp -d))
git archive --prefix=lxd-$(VERSION)/ HEAD | tar -x -C $(TMP)
git show-ref HEAD | cut -d' ' -f1 > $(TMP)/lxd-$(VERSION)/.gitref
# Download dependencies
(cd $(TMP)/lxd-$(VERSION) ; go mod vendor)
# Download the dqlite library
git clone --depth=1 --branch "${DQLITE_BRANCH}" https://github.com/canonical/dqlite $(TMP)/lxd-$(VERSION)/vendor/dqlite
(cd $(TMP)/lxd-$(VERSION)/vendor/dqlite ; git show-ref HEAD | cut -d' ' -f1 > .gitref)
# Copy doc output
cp -r doc/_build $(TMP)/lxd-$(VERSION)/doc/html/
# Assemble tarball
tar --exclude-vcs -C $(TMP) -zcf $(ARCHIVE).gz lxd-$(VERSION)/
# Cleanup
rm -Rf $(TMP)
.PHONY: i18n
i18n: update-pot update-po
po/%.mo: po/%.po
msgfmt --statistics -o $@ $<
po/%.po: po/$(DOMAIN).pot
msgmerge -U po/$*.po po/$(DOMAIN).pot
.PHONY: update-po
update-po:
set -eu; \
for lang in $(LINGUAS); do\
msgmerge --backup=none -U $$lang.po po/$(DOMAIN).pot; \
done; \
if [ -t 0 ] && ! git diff --quiet -- po/*.po; then \
read -rp "Would you like to commit i18n changes (Y/n)? " answer; \
if [ "$${answer:-y}" = "y" ] || [ "$${answer:-y}" = "Y" ]; then \
git commit -S -sm "i18n: Update translations." -- po/*.po; \
fi; \
fi
.PHONY: update-pot
update-pot:
ifeq "$(LXD_OFFLINE)" ""
(cd / ; go install github.com/snapcore/snapd/i18n/[email protected])
endif
xgettext-go -o po/$(DOMAIN).pot --add-comments-tag=TRANSLATORS: --sort-output --package-name=$(DOMAIN) [email protected] --keyword=i18n.G --keyword-plural=i18n.NG lxc/*.go lxc/*/*.go
if git diff --quiet --ignore-matching-lines='^\s*"POT-Creation-Date: .*\n"' -- po/*.pot; then git checkout -- po/*.pot; fi
if [ -t 0 ] && ! git diff --quiet --ignore-matching-lines='^\s*"POT-Creation-Date: .*\n"' -- po/*.pot; then \
read -rp "Would you like to commit i18n template changes (Y/n)? " answer; \
if [ "$${answer:-y}" = "y" ] || [ "$${answer:-y}" = "Y" ]; then \
git commit -S -sm "i18n: Update translation templates." -- po/*.pot; \
fi; \
fi
.PHONY: build-mo
build-mo: $(MOFILES)
.PHONY: static-analysis
static-analysis:
ifeq ($(shell command -v go-licenses),)
(cd / ; go install github.com/google/go-licenses@latest)
endif
ifeq ($(shell command -v golangci-lint),)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin
endif
ifneq ($(shell command -v yamllint),)
yamllint .github/workflows/*.yml
endif
ifeq ($(shell command -v shellcheck),)
echo "Please install shellcheck"
exit 1
else
ifneq "$(shell shellcheck --version | grep version: | cut -d ' ' -f2)" "0.8.0"
@echo "WARN: shellcheck version is not 0.8.0"
endif
endif
shellcheck test/*.sh test/includes/*.sh test/suites/*.sh test/backends/*.sh test/lint/*.sh test/extras/*.sh
NOT_EXEC="$(shell find test/lint -type f -not -executable)"; \
if [ -n "$$NOT_EXEC" ]; then \
echo "lint scripts not executable: $$NOT_EXEC"; \
exit 1; \
fi
BAD_NAME="$(shell find test/lint -type f -not -name '*.sh')"; \
if [ -n "$$BAD_NAME" ]; then \
echo "lint scripts missing .sh extension: $$BAD_NAME"; \
exit 1; \
fi
run-parts --verbose --exit-on-error --regex '.sh' test/lint
.PHONY: update-auth
update-auth:
go generate ./lxd/auth