1818# Licensed under the Apache License, Version 2.0
1919# -----------------------------------------------------------------------------
2020
21+ # #########################
22+ # Configuration
23+ # #########################
24+ PACKAGE := "github.com/containerd/nerdctl/v2"
25+ ORG_PREFIXES := "github.com/containerd"
26+
2127DOCKER ?= docker
2228GO ?= go
2329GOOS ?= $(shell $(GO ) env GOOS)
2430ifeq ($(GOOS ) ,windows)
2531 BIN_EXT := .exe
2632endif
2733
28- PACKAGE := github.com/containerd/nerdctl/v2
29-
30- # distro builders might wanna override these
34+ # distro builders might want to override these
3135PREFIX ?= /usr/local
3236BINDIR ?= $(PREFIX ) /bin
3337DATADIR ?= $(PREFIX ) /share
3438DOCDIR ?= $(DATADIR ) /doc
3539
40+ BINARY ?= "nerdctl"
3641MAKEFILE_DIR := $(patsubst % /,% ,$(dir $(abspath $(lastword $(MAKEFILE_LIST ) ) ) ) )
3742VERSION ?= $(shell git -C $(MAKEFILE_DIR ) describe --match 'v[0-9]* ' --dirty='.m' --always --tags)
3843VERSION_TRIMMED := $(VERSION:v%=% )
3944REVISION ?= $(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)
45+ LINT_COMMIT_RANGE ?= main..HEAD
46+ GO_BUILD_LDFLAGS ?= -s -w
47+ GO_BUILD_FLAGS ?=
4048
49+ # #########################
50+ # Helpers
51+ # #########################
4152ifdef VERBOSE
4253 VERBOSE_FLAG := -v
4354 VERBOSE_FLAG_LONG := --verbose
4455endif
4556
46- GO_BUILD_LDFLAGS ?= -s -w
47- GO_BUILD_FLAGS ?=
4857export 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 ) "
4958
59+ ifndef NO_COLORS
60+ NC := \033[0m
61+ GREEN := \033[1;32m
62+ ORANGE := \033[1;33m
63+ endif
64+
5065recursive_wildcard =$(wildcard $1$2) $(foreach e,$(wildcard $1* ) ,$(call recursive_wildcard,$e/,$2) )
5166
67+ define title
68+ @printf "$(GREEN ) ____________________________________________________________________________________________________\n"
69+ @printf "$(GREEN ) %*s\n" $$(( ( $(shell echo "🤓$(1 ) 🤓" | wc -c ) + 100 ) / 2 )) "🤓$(1 ) 🤓"
70+ @printf "$(GREEN ) ____________________________________________________________________________________________________\n$(ORANGE ) "
71+ endef
72+
73+ define footer
74+ @printf "$(GREEN ) > %s: done!\n" "$(1 ) "
75+ @printf "$(GREEN ) ____________________________________________________________________________________________________\n$(NC ) "
76+ endef
77+
78+ # #########################
79+ # High-level tasks definitions
80+ # #########################
5281all : binaries
5382
83+ lint : lint-go-all lint-imports lint-yaml lint-shell lint-commits lint-mod lint-licenses-all
84+
85+ fix : fix-mod fix-imports fix-go-all
86+
87+ # TODO: fix race task and add it
88+ test : test-unit # test-unit-race test-unit-bench
89+
5490help :
5591 @echo " Usage: make <target>"
5692 @echo
57- @echo " * 'install' - Install binaries to system locations."
93+ @echo " * 'lint' - Run linters against codebase."
94+ @echo " * 'fix' - Automatically fixes imports, modules, and simple formatting."
95+ @echo " * 'test' - Run basic unit testing."
5896 @echo " * 'binaries' - Build nerdctl."
97+ @echo " * 'install' - Install binaries to system locations."
5998 @echo " * 'clean' - Clean artifacts."
60- @echo " * 'lint' - Run various linters."
6199
62- nerdctl :
63- $(GO_BUILD ) $(GO_BUILD_FLAGS ) $(VERBOSE_FLAG ) -o $(CURDIR ) /_output/nerdctl$(BIN_EXT ) ./cmd/nerdctl
100+ # #########################
101+ # Building and installation tasks
102+ # #########################
103+ binaries : $(CURDIR ) /_output/$(BINARY )$(BIN_EXT )
104+
105+ $(CURDIR ) /_output/$(BINARY )$(BIN_EXT ) :
106+ $(call title, $@ )
107+ $(GO_BUILD ) $(GO_BUILD_FLAGS ) $(VERBOSE_FLAG ) -o $(CURDIR ) /_output/$(BINARY )$(BIN_EXT ) ./cmd/nerdctl
108+ $(call footer, $@ )
109+
110+ install :
111+ $(call title, $@ )
112+ install -D -m 755 $(CURDIR ) /_output/$(BINARY ) $(DESTDIR )$(BINDIR ) /$(BINARY )
113+ install -D -m 755 $(MAKEFILE_DIR ) /extras/rootless/containerd-rootless.sh $(DESTDIR )$(BINDIR ) /containerd-rootless.sh
114+ install -D -m 755 $(MAKEFILE_DIR ) /extras/rootless/containerd-rootless-setuptool.sh $(DESTDIR )$(BINDIR ) /containerd-rootless-setuptool.sh
115+ install -D -m 644 -t $(DESTDIR )$(DOCDIR ) /nerdctl $(MAKEFILE_DIR ) /docs/* .md
116+ $(call footer, $@ )
64117
65118clean :
119+ $(call title, $@ )
66120 find . -name \* ~ -delete
67121 find . -name \#\* -delete
68122 rm -rf $(CURDIR ) /_output/* $(MAKEFILE_DIR ) /vendor
123+ $(call footer, $@ )
69124
70- lint : lint-go lint-imports lint-yaml lint-shell
71-
125+ # #########################
126+ # Linting tasks
127+ # #########################
72128lint-go :
73- cd $(MAKEFILE_DIR ) && GOOS=linux golangci-lint run $(VERBOSE_FLAG_LONG ) ./... && \
74- GOOS=windows golangci-lint run $(VERBOSE_FLAG_LONG ) ./... && \
75- GOOS=freebsd golangci-lint run $(VERBOSE_FLAG_LONG ) ./...
129+ $(call title, $@ : $(GOOS ) )
130+ @cd $(MAKEFILE_DIR ) \
131+ && golangci-lint run $(VERBOSE_FLAG_LONG ) ./...
132+ $(call footer, $@ )
133+
134+ lint-go-all :
135+ $(call title, $@ )
136+ @cd $(MAKEFILE_DIR ) \
137+ && GOOS=linux make lint-go \
138+ && GOOS=windows make lint-go \
139+ && GOOS=freebsd make lint-go
140+ $(call footer, $@ )
76141
77142lint-imports :
78- cd $( MAKEFILE_DIR ) && goimports-reviser -recursive -list-diff -set-exit-status -output stdout -company-prefixes " github.com/containerd " ./...
79-
80- lint-fix-imports :
81- cd $( MAKEFILE_DIR ) && goimports-reviser -company-prefixes " github.com/containerd " ./...
143+ $( call title, $@ )
144+ @cd $( MAKEFILE_DIR ) \
145+ && goimports-reviser -recursive -list-diff -set-exit-status -output stdout -company-prefixes " $( ORG_PREFIXES ) " ./...
146+ $( call footer, $@ )
82147
83148lint-yaml :
84- cd $(MAKEFILE_DIR ) && yamllint .
149+ $(call title, $@ )
150+ cd $(MAKEFILE_DIR ) \
151+ && yamllint .
152+ $(call footer, $@ )
85153
86154lint-shell : $(call recursive_wildcard,$(MAKEFILE_DIR ) /,* .sh)
155+ $(call title, $@ )
87156 shellcheck -a -x $^
88-
157+ $(call footer, $@ )
158+
159+ lint-commits :
160+ $(call title, $@ )
161+ @cd $(MAKEFILE_DIR ) \
162+ && git-validation $(VERBOSE_FLAG ) -run DCO,short-subject,dangling-whitespace -range " $( LINT_COMMIT_RANGE) "
163+ $(call footer, $@ )
164+
165+ lint-mod :
166+ $(call title, $@ )
167+ @cd $(MAKEFILE_DIR ) \
168+ && go mod tidy --diff
169+ $(call footer, $@ )
170+
171+ lint-licenses :
172+ $(call title, $@ : $(GOOS ) )
173+ @cd $(MAKEFILE_DIR ) \
174+ && ./hack/make-lint-licenses.sh
175+ $(call footer, $@ )
176+
177+ lint-licenses-all :
178+ $(call title, $@ )
179+ @cd $(MAKEFILE_DIR ) \
180+ && GOOS=linux make lint-licenses \
181+ && GOOS=freebsd make lint-licenses \
182+ && GOOS=windows make lint-licenses
183+ $(call footer, $@ )
184+
185+ # #########################
186+ # Automated fixing tasks
187+ # #########################
188+ fix-go :
189+ $(call title, $@ : $(GOOS ) )
190+ @cd $(MAKEFILE_DIR ) \
191+ && golangci-lint run --fix
192+ $(call footer, $@ )
193+
194+ fix-go-all :
195+ $(call title, $@ )
196+ @cd $(MAKEFILE_DIR ) \
197+ && GOOS=linux make fix-go \
198+ && GOOS=freebsd make fix-go \
199+ && GOOS=windows make fix-go
200+ $(call footer, $@ )
201+
202+ fix-imports :
203+ $(call title, $@ )
204+ @cd $(MAKEFILE_DIR ) \
205+ && goimports-reviser -company-prefixes $(ORG_PREFIXES ) ./...
206+ $(call footer, $@ )
207+
208+ fix-mod :
209+ $(call title, $@ )
210+ @cd $(MAKEFILE_DIR ) \
211+ && go mod tidy
212+ $(call footer, $@ )
213+
214+ # #########################
215+ # Development tools installation
216+ # #########################
217+ install-dev-tools :
218+ $(call title, $@ )
219+ # golangci: v1.64.5
220+ # git-validation: main from 2023/11
221+ # ltag: v0.2.5
222+ # go-licenses: v2.0.0-alpha.1
223+ # goimports-reviser: v3.8.2
224+ @cd $(MAKEFILE_DIR ) \
225+ && go install github.com/golangci/golangci-lint/cmd/golangci-lint@0a603e49e5e9870f5f9f2035bcbe42cd9620a9d5 \
226+ && go install github.com/vbatts/git-validation@679e5cad8c50f1605ab3d8a0a947aaf72fb24c07 \
227+ && go install github.com/kunalkushwaha/ltag@b0cfa33e4cc9383095dc584d3990b62c95096de0 \
228+ && go install github.com/google/go-licenses/v2@d01822334fba5896920a060f762ea7ecdbd086e8 \
229+ && go install github.com/incu6us/goimports-reviser/v3@f034195cc8a7ffc7cc70d60aa3a25500874eaf04 \
230+ && go install gotest.tools/gotestsum@ac6dad9c7d87b969004f7749d1942938526c9716
231+ @echo " Remember to add GOROOT/bin to your path"
232+ $(call footer, $@ )
233+
234+ # #########################
235+ # Testing tasks
236+ # #########################
89237test-unit :
90- go test -v $(MAKEFILE_DIR ) /pkg/...
91-
92- binaries : nerdctl
93-
94- install :
95- install -D -m 755 $(CURDIR ) /_output/nerdctl $(DESTDIR )$(BINDIR ) /nerdctl
96- install -D -m 755 $(MAKEFILE_DIR ) /extras/rootless/containerd-rootless.sh $(DESTDIR )$(BINDIR ) /containerd-rootless.sh
97- install -D -m 755 $(MAKEFILE_DIR ) /extras/rootless/containerd-rootless-setuptool.sh $(DESTDIR )$(BINDIR ) /containerd-rootless-setuptool.sh
98- install -D -m 644 -t $(DESTDIR )$(DOCDIR ) /nerdctl $(MAKEFILE_DIR ) /docs/* .md
99-
238+ $(call title, $@ )
239+ @go test $(VERBOSE_FLAG ) $(MAKEFILE_DIR ) /pkg/...
240+ $(call footer, $@ )
241+
242+ test-unit-bench :
243+ $(call title, $@ )
244+ @go test $(VERBOSE_FLAG ) $(MAKEFILE_DIR ) /pkg/... -bench=.
245+ $(call footer, $@ )
246+
247+ test-unit-race :
248+ $(call title, $@ )
249+ @go test $(VERBOSE_FLAG ) $(MAKEFILE_DIR ) /pkg/... -race
250+ $(call footer, $@ )
251+
252+ # #########################
253+ # Release tasks
254+ # #########################
100255# Note that these options will not work on macOS - unless you use gnu-tar instead of tar
101256TAR_OWNER0_FLAGS=--owner =0 --group=0
102257TAR_FLATTEN_FLAGS =--transform 's/.*\///g'
@@ -107,6 +262,7 @@ define make_artifact_full_linux
107262endef
108263
109264artifacts : clean
265+ $(call title, $@ )
110266 GOOS=linux GOARCH=amd64 make -C $(CURDIR ) -f $(MAKEFILE_DIR ) /Makefile binaries
111267 tar $(TAR_OWNER0_FLAGS ) $(TAR_FLATTEN_FLAGS ) -czvf $(CURDIR ) /_output/nerdctl-$(VERSION_TRIMMED ) -linux-amd64.tar.gz $(CURDIR ) /_output/nerdctl $(MAKEFILE_DIR ) /extras/rootless/*
112268
@@ -138,15 +294,19 @@ artifacts: clean
138294
139295 $(GO) -C $(MAKEFILE_DIR) mod vendor
140296 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
297+ $(call footer, $@)
141298
142299.PHONY : \
300+ all \
301+ lint \
302+ fix \
303+ test \
143304 help \
144- nerdctl \
145- clean \
146305 binaries \
147306 install \
307+ clean \
308+ lint-go lint-go-all lint-imports lint-yaml lint-shell lint-commits lint-mod lint-licenses lint-licenses-all \
309+ fix-go fix-go-all fix-imports fix-mod \
310+ install-dev-tools \
311+ test-unit test-unit-race test-unit-bench \
148312 artifacts
149- lint \
150- lint-yaml \
151- lint-go \
152- lint-shell
0 commit comments