Skip to content

Commit d10f4e7

Browse files
committed
Makefile: add/use foreach target
To not repeat shell's for loop for multiple targets, add "foreach" target and use it. It can also be used from the command line: $ make foreach Usage: make foreach CMD="commands to run for every package" make: *** [Makefile:17: foreach] Error 1 $ make foreach CMD="gofmt -w ." set -eu; \ for p in mountinfo mount sequential signal symlink user userns ; do \ (cd $p; gofmt -w .;) \ done Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 243daa2 commit d10f4e7

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

Makefile

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@ all: clean lint test cross
1212
clean:
1313
$(RM) mount/go-local.*
1414

15-
.PHONY: test
16-
test: test-local
15+
.PHONY: foreach
16+
foreach: ## Run $(CMD) for every package.
17+
@if test -z "$(CMD)"; then \
18+
echo 'Usage: make foreach CMD="commands to run for every package"'; \
19+
exit 1; \
20+
fi
1721
set -eu; \
1822
for p in $(PACKAGES); do \
19-
(cd $$p; go test $(RUN_VIA_SUDO) -v .); \
23+
(cd $$p; $(CMD);) \
2024
done
2125

26+
.PHONY: test
27+
test: test-local
28+
test: CMD=go test $(RUN_VIA_SUDO) -v .
29+
test: foreach
30+
2231
.PHONY: tidy
23-
tidy:
24-
set -eu; \
25-
for p in $(PACKAGES); do \
26-
(cd $$p; go mod tidy); \
27-
done
32+
tidy: CMD=go mod tidy
33+
tidy: foreach
2834

2935
# Test the mount module against the local mountinfo source code instead of the
3036
# release specified in its go.mod. This allows catching regressions / breaking
@@ -39,13 +45,10 @@ test-local:
3945

4046
.PHONY: lint
4147
lint: $(BINDIR)/golangci-lint
48+
lint: CMD=go mod download; ../$(BINDIR)/golangci-lint run
49+
lint: foreach
50+
lint:
4251
$(BINDIR)/golangci-lint version
43-
set -eu; \
44-
for p in $(PACKAGES); do \
45-
(cd $$p; \
46-
go mod download; \
47-
../$(BINDIR)/golangci-lint run); \
48-
done
4952

5053
$(BINDIR)/golangci-lint: $(BINDIR)
5154
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(BINDIR) v1.59.1

0 commit comments

Comments
 (0)