Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
176 lines (146 loc) · 7.71 KB
/
Copy pathMakefile
File metadata and controls
176 lines (146 loc) · 7.71 KB
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
.PHONY: help all build build-all release-all check test coverage coverage-html coverage-clean coverage-check cover fmt fmt-check vet lint lint-ci vuln install clean e2e fuzz coverage-pkg-check docs-check
help: ## This help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help
# Ignore a parent go.work (e.g. otfabric/go.work) so this module builds standalone.
export GOWORK := off
# CI formats and lints with the Go version from go.mod, not with the toolchain installed here.
# gofmt's output changes between Go releases (comment alignment, for one), so formatting is
# done with that version, and `fmt-check` also requires the local gofmt to agree: a file that
# two versions format differently fails CI on one side or the other.
GO_MOD_VERSION := $(shell sed -nE 's/^go ([0-9]+\.[0-9]+).*/\1/p' go.mod | head -n1)
GOFMT_CI = $(shell GOTOOLCHAIN=go$(GO_MOD_VERSION).0 go env GOROOT)/bin/gofmt
APP_NAME = sunspecctl
APP_SRC = .
ARCHS = linux/amd64 linux/arm64 linux/arm/v7 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64
RELEASE_DIR = release
# Override on the command line or in CI (matches the ldflags of the release workflow).
VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null)
TAG ?= $(shell git describe --tags --exact-match 2>/dev/null)
# -dirty when the working tree has changes, as Go itself marks vcs.modified
# (git status --porcelain, untracked files included); empty outside git.
DIRTY := $(shell [ -z "$$(git status --porcelain 2>/dev/null)" ] || echo -dirty)
COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo "")$(DIRTY)
BUILD_DATE ?= $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
LDFLAGS = -ldflags "-s -w -X main.version=$(VERSION) -X main.tag=$(TAG) -X main.commit=$(COMMIT) -X main.buildDate=$(BUILD_DATE)"
# Packages measured by the coverage gate: everything, as in CI.
TEST_PKGS := ./...
COVERAGE_MIN := 90
# Per-package floor (TEST-012): what the weakest package measures now, rounded down.
COVER_PKG_MIN := 90
# docsync release of docs-check (DOC-008).
DOCSYNC_VERSION := v0.4.0
all: check build ## Run all checks and build the CLI
coverage-pkg-check: coverage-check ## Fail when a package is below COVER_PKG_MIN
@awk -v m="$(COVER_PKG_MIN)" 'NR > 1 { n[$$1] = $$2; if ($$3 > 0) h[$$1] = 1 } \
END { for (k in n) { p = k; sub(/\/[^\/]*$$/, "", p); s[p] += n[k]; if (k in h) c[p] += n[k] } \
for (p in s) { v = 100 * c[p] / s[p]; if (v < m) { printf "%s: %.1f%% < %s%%\n", p, v, m; bad = 1 } } exit bad }' coverage.out
check: fmt fmt-check vet lint lint-ci vuln test coverage-pkg-check docs-check ## Run all checks (format, vet, lint, test, coverage, docs)
test: ## Run the tests with the race detector
@echo "Running tests (race detector)"
@go test -count=1 -race ./...
coverage: ## Run tests with coverage profile and text summary
@echo "Running coverage on $(TEST_PKGS)"
@go test -count=1 -race -coverprofile=coverage.out -covermode=atomic $(TEST_PKGS)
@go tool cover -func=coverage.out | tee coverage.txt
coverage-html: coverage ## Generate HTML coverage report (coverage.html)
@echo "Generating HTML coverage report"
@go tool cover -html=coverage.out -o coverage.html
coverage-check: ## Fail if total coverage is below $(COVERAGE_MIN)%
@echo "Running coverage check (minimum $(COVERAGE_MIN)%) on $(TEST_PKGS)"
@go test -count=1 -race -coverprofile=coverage.out -covermode=atomic $(TEST_PKGS)
@go tool cover -func=coverage.out | tee coverage.txt
@go tool cover -func=coverage.out | grep 'total:' | awk -v min=$(COVERAGE_MIN) '{gsub(/%/,""); p=$$NF+0; if (p < min) { printf "Coverage %.1f%% is below %d%%\n", p, min; exit 1 } else { printf "Coverage %.1f%% (>= %d%%)\n", p, min } }'
coverage-clean: ## Remove coverage artifacts
@echo "Removing coverage artifacts"
@rm -f coverage.out coverage.txt coverage.html
cover: coverage-html ## Open coverage report in browser
@echo "Opening coverage report in browser"
@go tool cover -html=coverage.out
fmt: ## Format Go code with the gofmt of the Go version in go.mod (what CI uses)
@echo "Running gofmt (go$(GO_MOD_VERSION))"
@$(GOFMT_CI) -w .
fmt-check: ## Fail if the go.mod-version gofmt or the local gofmt would change any file
@echo "Checking formatting (gofmt go$(GO_MOD_VERSION) and local $$(go env GOVERSION))"
@ci="$$($(GOFMT_CI) -l .)"; loc="$$(gofmt -l .)"; \
if [ -n "$$ci$$loc" ]; then \
[ -z "$$ci" ] || { echo "Not formatted for gofmt go$(GO_MOD_VERSION) (CI):"; echo "$$ci"; }; \
[ -z "$$loc" ] || { echo "Not formatted for the local gofmt:"; echo "$$loc"; }; \
echo "Files must be stable under both. Run 'make fmt'; if the two versions still disagree,"; \
echo "restructure the code they format differently (e.g. move trailing comments onto their own lines)."; \
exit 1; \
fi
vet: ## Run go vet on all packages
@echo "Running go vet"
@go vet ./...
lint: ## Run staticcheck
@echo "Running staticcheck"
@staticcheck ./...
lint-ci: ## Run golangci-lint (uses .golangci.yml)
@echo "Running golangci-lint"
@golangci-lint run ./...
vuln: ## Run govulncheck
@echo "Running govulncheck"
@govulncheck ./...
build: ## Build sunspecctl into ./bin
@mkdir -p bin
@echo "Building $(APP_NAME) $(VERSION)"
@go build $(LDFLAGS) -o bin/$(APP_NAME) $(APP_SRC)
build-all: ## Build sunspecctl for all architectures
@mkdir -p $(RELEASE_DIR)
@for arch in $(ARCHS); do \
os=$${arch%%/*}; \
rest=$${arch#*/}; \
cpu=$${rest%%/*}; \
variant=$${rest#*/}; \
if [ "$$os" = "windows" ]; then \
echo "Building $(APP_NAME)-$$os-$$cpu.exe..."; \
GOOS=$$os GOARCH=$$cpu go build $(LDFLAGS) -o $(RELEASE_DIR)/$(APP_NAME)-$$os-$$cpu.exe $(APP_SRC); \
elif [ "$$cpu" = "arm" ] && [ "$$variant" = "v7" ]; then \
echo "Building $(APP_NAME)-$$os-armv7..."; \
GOOS=$$os GOARCH=$$cpu GOARM=7 go build $(LDFLAGS) -o $(RELEASE_DIR)/$(APP_NAME)-$$os-armv7 $(APP_SRC); \
else \
echo "Building $(APP_NAME)-$$os-$$cpu..."; \
GOOS=$$os GOARCH=$$cpu go build $(LDFLAGS) -o $(RELEASE_DIR)/$(APP_NAME)-$$os-$$cpu $(APP_SRC); \
fi \
done
release-all: build-all ## Package CLI binaries into tar.gz (Unix) and zip (Windows)
@for arch in $(ARCHS); do \
os=$${arch%%/*}; \
rest=$${arch#*/}; \
cpu=$${rest%%/*}; \
variant=$${rest#*/}; \
if [ "$$os" = "windows" ]; then \
bin=$(APP_NAME)-$$os-$$cpu.exe; \
echo "Packaging $(APP_NAME)-$$os-$$cpu.zip..."; \
zip -j $(RELEASE_DIR)/$(APP_NAME)-$$os-$$cpu.zip $(RELEASE_DIR)/$$bin; \
elif [ "$$cpu" = "arm" ] && [ "$$variant" = "v7" ]; then \
bin=$(APP_NAME)-$$os-armv7; \
echo "Packaging $$bin.tar.gz..."; \
tar czf $(RELEASE_DIR)/$$bin.tar.gz -C $(RELEASE_DIR) $$bin; \
else \
bin=$(APP_NAME)-$$os-$$cpu; \
echo "Packaging $$bin.tar.gz..."; \
tar czf $(RELEASE_DIR)/$$bin.tar.gz -C $(RELEASE_DIR) $$bin; \
fi; \
done
# The documents against the code (DOC-008): docsync, as docs-check.yml runs it.
docs-check: ## Check that the documents agree with the code (docsync)
@go run github.com/otfabric/docsync@$(DOCSYNC_VERSION) check .
FUZZTIME ?= 30s
fuzz: ## Run every fuzz target for FUZZTIME each (default 30s)
@for target in $$(go test -list '^Fuzz' . | grep '^Fuzz'); do \
echo "Fuzzing $$target"; \
go test -run '^$$' -fuzz "^$$target$$" -fuzztime $(FUZZTIME) . || exit 1; \
done
e2e: ## Build the binary and run the e2e suite against it
@mkdir -p bin
@go build $(LDFLAGS) -o bin/sunspecctl $(APP_SRC)
@SUNSPECCTL_E2E_BIN=$(CURDIR)/bin/sunspecctl SUNSPECCTL_E2E_VERSION=$(VERSION) go test -count=1 -tags=e2e ./e2e/...
install: build ## Install sunspecctl to /usr/local/bin
@echo "Installing $(APP_NAME) to /usr/local/bin"
@sudo install -m 0755 bin/$(APP_NAME) /usr/local/bin/$(APP_NAME)
clean: coverage-clean ## Clean build artifacts
@echo "Cleaning build artifacts"
@rm -rf bin
@rm -rf $(RELEASE_DIR)