-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathMakefile
312 lines (258 loc) · 10.8 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
311
312
# Copyright The containerd Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
# Portions from /~https://github.com/kubernetes-sigs/cri-tools/blob/v1.19.0/Makefile
# Copyright The Kubernetes Authors.
# Licensed under the Apache License, Version 2.0
# -----------------------------------------------------------------------------
##########################
# Configuration
##########################
PACKAGE := "github.com/containerd/nerdctl/v2"
ORG_PREFIXES := "github.com/containerd"
DOCKER ?= docker
GO ?= go
GOOS ?= $(shell $(GO) env GOOS)
ifeq ($(GOOS),windows)
BIN_EXT := .exe
endif
# distro builders might want to override these
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DATADIR ?= $(PREFIX)/share
DOCDIR ?= $(DATADIR)/doc
BINARY ?= "nerdctl"
MAKEFILE_DIR := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
VERSION ?= $(shell git -C $(MAKEFILE_DIR) describe --match 'v[0-9]*' --dirty='.m' --always --tags)
VERSION_TRIMMED := $(VERSION:v%=%)
REVISION ?= $(shell git -C $(MAKEFILE_DIR) rev-parse HEAD)$(shell if ! git -C $(MAKEFILE_DIR) diff --no-ext-diff --quiet --exit-code; then echo .m; fi)
LINT_COMMIT_RANGE ?= main..HEAD
GO_BUILD_LDFLAGS ?= -s -w
GO_BUILD_FLAGS ?=
##########################
# Helpers
##########################
ifdef VERBOSE
VERBOSE_FLAG := -v
VERBOSE_FLAG_LONG := --verbose
endif
export GO_BUILD=CGO_ENABLED=0 GOOS=$(GOOS) $(GO) -C $(MAKEFILE_DIR) build -ldflags "$(GO_BUILD_LDFLAGS) $(VERBOSE_FLAG) -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.Revision=$(REVISION)"
ifndef NO_COLOR
NC := \033[0m
GREEN := \033[1;32m
ORANGE := \033[1;33m
endif
recursive_wildcard=$(wildcard $1$2) $(foreach e,$(wildcard $1*),$(call recursive_wildcard,$e/,$2))
define title
@printf "$(GREEN)____________________________________________________________________________________________________\n"
@printf "$(GREEN)%*s\n" $$(( ( $(shell echo "🤓$(1) 🤓" | wc -c ) + 100 ) / 2 )) "🤓$(1) 🤓"
@printf "$(GREEN)____________________________________________________________________________________________________\n$(ORANGE)"
endef
define footer
@printf "$(GREEN)> %s: done!\n" "$(1)"
@printf "$(GREEN)____________________________________________________________________________________________________\n$(NC)"
endef
##########################
# High-level tasks definitions
##########################
all: binaries
lint: lint-go-all lint-imports lint-yaml lint-shell lint-commits lint-mod lint-licenses-all
fix: fix-mod fix-imports fix-go-all
# TODO: fix race task and add it
test: test-unit # test-unit-race test-unit-bench
help:
@echo "Usage: make <target>"
@echo
@echo " * 'lint' - Run linters against codebase."
@echo " * 'fix' - Automatically fixes imports, modules, and simple formatting."
@echo " * 'test' - Run basic unit testing."
@echo " * 'binaries' - Build nerdctl."
@echo " * 'install' - Install binaries to system locations."
@echo " * 'clean' - Clean artifacts."
##########################
# Building and installation tasks
##########################
binaries: $(CURDIR)/_output/$(BINARY)$(BIN_EXT)
$(CURDIR)/_output/$(BINARY)$(BIN_EXT):
$(call title, $@)
$(GO_BUILD) $(GO_BUILD_FLAGS) $(VERBOSE_FLAG) -o $(CURDIR)/_output/$(BINARY)$(BIN_EXT) ./cmd/nerdctl
$(call footer, $@)
install:
$(call title, $@)
install -D -m 755 $(CURDIR)/_output/$(BINARY) $(DESTDIR)$(BINDIR)/$(BINARY)
install -D -m 755 $(MAKEFILE_DIR)/extras/rootless/containerd-rootless.sh $(DESTDIR)$(BINDIR)/containerd-rootless.sh
install -D -m 755 $(MAKEFILE_DIR)/extras/rootless/containerd-rootless-setuptool.sh $(DESTDIR)$(BINDIR)/containerd-rootless-setuptool.sh
install -D -m 644 -t $(DESTDIR)$(DOCDIR)/nerdctl $(MAKEFILE_DIR)/docs/*.md
$(call footer, $@)
clean:
$(call title, $@)
find . -name \*~ -delete
find . -name \#\* -delete
rm -rf $(CURDIR)/_output/* $(MAKEFILE_DIR)/vendor
$(call footer, $@)
##########################
# Linting tasks
##########################
lint-go:
$(call title, $@: $(GOOS))
@cd $(MAKEFILE_DIR) \
&& golangci-lint run $(VERBOSE_FLAG_LONG) ./...
$(call footer, $@)
lint-go-all:
$(call title, $@)
@cd $(MAKEFILE_DIR) \
&& GOOS=linux make lint-go \
&& GOOS=windows make lint-go \
&& GOOS=freebsd make lint-go
$(call footer, $@)
lint-imports:
$(call title, $@)
@cd $(MAKEFILE_DIR) \
&& goimports-reviser -recursive -list-diff -set-exit-status -output stdout -company-prefixes "$(ORG_PREFIXES)" ./...
$(call footer, $@)
lint-yaml:
$(call title, $@)
cd $(MAKEFILE_DIR) \
&& yamllint .
$(call footer, $@)
lint-shell: $(call recursive_wildcard,$(MAKEFILE_DIR)/,*.sh)
$(call title, $@)
shellcheck -a -x $^
$(call footer, $@)
lint-commits:
$(call title, $@)
@cd $(MAKEFILE_DIR) \
&& git-validation $(VERBOSE_FLAG) -run DCO,short-subject,dangling-whitespace -range "$(LINT_COMMIT_RANGE)"
$(call footer, $@)
lint-mod:
$(call title, $@)
@cd $(MAKEFILE_DIR) \
&& go mod tidy --diff
$(call footer, $@)
lint-licenses:
$(call title, $@: $(GOOS))
@cd $(MAKEFILE_DIR) \
&& ./hack/make-lint-licenses.sh
$(call footer, $@)
lint-licenses-all:
$(call title, $@)
@cd $(MAKEFILE_DIR) \
&& GOOS=linux make lint-licenses \
&& GOOS=freebsd make lint-licenses \
&& GOOS=windows make lint-licenses
$(call footer, $@)
##########################
# Automated fixing tasks
##########################
fix-go:
$(call title, $@: $(GOOS))
@cd $(MAKEFILE_DIR) \
&& golangci-lint run --fix
$(call footer, $@)
fix-go-all:
$(call title, $@)
@cd $(MAKEFILE_DIR) \
&& GOOS=linux make fix-go \
&& GOOS=freebsd make fix-go \
&& GOOS=windows make fix-go
$(call footer, $@)
fix-imports:
$(call title, $@)
@cd $(MAKEFILE_DIR) \
&& goimports-reviser -company-prefixes $(ORG_PREFIXES) ./...
$(call footer, $@)
fix-mod:
$(call title, $@)
@cd $(MAKEFILE_DIR) \
&& go mod tidy
$(call footer, $@)
##########################
# Development tools installation
##########################
install-dev-tools:
$(call title, $@)
# golangci: v1.64.5
# git-validation: main from 2023/11
# ltag: v0.2.5
# go-licenses: v2.0.0-alpha.1
# goimports-reviser: v3.8.2
@cd $(MAKEFILE_DIR) \
&& go install github.com/golangci/golangci-lint/cmd/golangci-lint@0a603e49e5e9870f5f9f2035bcbe42cd9620a9d5 \
&& go install github.com/vbatts/git-validation@679e5cad8c50f1605ab3d8a0a947aaf72fb24c07 \
&& go install github.com/kunalkushwaha/ltag@b0cfa33e4cc9383095dc584d3990b62c95096de0 \
&& go install github.com/google/go-licenses/v2@d01822334fba5896920a060f762ea7ecdbd086e8 \
&& go install github.com/incu6us/goimports-reviser/v3@f034195cc8a7ffc7cc70d60aa3a25500874eaf04 \
&& go install gotest.tools/gotestsum@ac6dad9c7d87b969004f7749d1942938526c9716
@echo "Remember to add GOROOT/bin to your path"
$(call footer, $@)
##########################
# Testing tasks
##########################
test-unit:
$(call title, $@)
@go test $(VERBOSE_FLAG) $(MAKEFILE_DIR)/pkg/...
$(call footer, $@)
test-unit-bench:
$(call title, $@)
@go test $(VERBOSE_FLAG) $(MAKEFILE_DIR)/pkg/... -bench=.
$(call footer, $@)
test-unit-race:
$(call title, $@)
@go test $(VERBOSE_FLAG) $(MAKEFILE_DIR)/pkg/... -race
$(call footer, $@)
##########################
# Release tasks
##########################
# Note that these options will not work on macOS - unless you use gnu-tar instead of tar
TAR_OWNER0_FLAGS=--owner=0 --group=0
TAR_FLATTEN_FLAGS=--transform 's/.*\///g'
define make_artifact_full_linux
$(DOCKER) build --output type=tar,dest=$(CURDIR)/_output/nerdctl-full-$(VERSION_TRIMMED)-linux-$(1).tar --target out-full --platform $(1) --build-arg GO_VERSION -f $(MAKEFILE_DIR)/Dockerfile $(MAKEFILE_DIR)
gzip -9 $(CURDIR)/_output/nerdctl-full-$(VERSION_TRIMMED)-linux-$(1).tar
endef
artifacts: clean
$(call title, $@)
GOOS=linux GOARCH=amd64 make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-amd64.tar.gz $(CURDIR)/_output/nerdctl $(MAKEFILE_DIR)/extras/rootless/*
GOOS=linux GOARCH=arm64 make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-arm64.tar.gz $(CURDIR)/_output/nerdctl $(MAKEFILE_DIR)/extras/rootless/*
GOOS=linux GOARCH=arm GOARM=7 make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-arm-v7.tar.gz $(CURDIR)/_output/nerdctl $(MAKEFILE_DIR)/extras/rootless/*
GOOS=linux GOARCH=ppc64le make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-ppc64le.tar.gz $(CURDIR)/_output/nerdctl $(MAKEFILE_DIR)/extras/rootless/*
GOOS=linux GOARCH=riscv64 make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-riscv64.tar.gz $(CURDIR)/_output/nerdctl $(MAKEFILE_DIR)/extras/rootless/*
GOOS=linux GOARCH=s390x make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-s390x.tar.gz $(CURDIR)/_output/nerdctl $(MAKEFILE_DIR)/extras/rootless/*
GOOS=windows GOARCH=amd64 make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-windows-amd64.tar.gz $(CURDIR)/_output/nerdctl.exe
GOOS=freebsd GOARCH=amd64 make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-freebsd-amd64.tar.gz $(CURDIR)/_output/nerdctl
rm -f $(CURDIR)/_output/nerdctl $(CURDIR)/_output/nerdctl.exe
$(call make_artifact_full_linux,amd64)
$(call make_artifact_full_linux,arm64)
$(GO) -C $(MAKEFILE_DIR) mod vendor
tar $(TAR_OWNER0_FLAGS) -czf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-go-mod-vendor.tar.gz $(MAKEFILE_DIR)/go.mod $(MAKEFILE_DIR)/go.sum $(MAKEFILE_DIR)/vendor
$(call footer, $@)
.PHONY: \
all \
lint \
fix \
test \
help \
binaries \
install \
clean \
lint-go lint-go-all lint-imports lint-yaml lint-shell lint-commits lint-mod lint-licenses lint-licenses-all \
fix-go fix-go-all fix-imports fix-mod \
install-dev-tools \
test-unit test-unit-race test-unit-bench \
artifacts