Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
195 lines (161 loc) · 8.75 KB
/
Copy pathMakefile
File metadata and controls
195 lines (161 loc) · 8.75 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
.PHONY: help all run test coverage cover compile cross lint lint-ci fmt fmt-check vet vuln build build-nocheck build-all release-all install clean e2e coverage-pkg-check fuzz 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
# Parent otfabric/go.work does not list this module; isolate toolchain commands.
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 = ethernetipctl
APP_SRC = main.go
BIN_DIR = bin
BINARY = $(BIN_DIR)/$(APP_NAME)
ARCHS = linux/amd64 linux/arm64 linux/arm/v7 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64
# Override on the command line or in CI (matches reusable release workflow ldflags).
# VERSION and TAG only when HEAD is exactly on a tag; otherwise the binary reports dev.
VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null)
TAG ?= $(shell git describe --tags --exact-match 2>/dev/null || echo "")
# -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)$(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)'"
RELEASE_DIR := release
# docsync release of docs-check (DOC-008).
DOCSYNC_VERSION := v0.4.0
# Minimum total statement coverage (percent) enforced by `make coverage`.
COVERAGE_MIN ?= 90
# Per-package floor (TEST-012), from the coverage.out of `make coverage`.
COVER_PKG_MIN := 85
FUZZTIME ?= 10s
all: test build build-all ## Test and build the application
run: ## Run the application
@echo "Running $(APP_NAME)"
@go run $(APP_SRC)
check: fmt fmt-check lint lint-ci vet cross vuln test coverage coverage-pkg-check docs-check ## Run all checks (format, lint, vet, 32-bit build, vulnerability scan, test, coverage, docs)
test: ## Run unit tests with race detector
@echo "Running unit tests (race detector)"
@go test -count=1 -race ./...
coverage: ## Run tests with whole-module coverage (writes coverage.out); fails below COVERAGE_MIN percent
@echo "Running tests with coverage"
@go test -count=1 -race -coverprofile=coverage.out -covermode=atomic -coverpkg=./... ./...
@total=$$(go tool cover -func=coverage.out | awk '/^total:/ { sub("%", "", $$3); print $$3 }'); \
echo "Total coverage: $$total% (minimum $(COVERAGE_MIN)%)"; \
awk -v t="$$total" -v m="$(COVERAGE_MIN)" 'BEGIN { exit (t + 0 < m + 0) ? 1 : 0 }' || \
{ echo "Coverage $$total% is below the required $(COVERAGE_MIN)%"; exit 1; }
coverage-pkg-check: coverage ## 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
fuzz: ## Run every fuzz target for FUZZTIME (targets discovered, never listed)
@for pkg in $$(go list ./...); do \
for t in $$(go test -list '^Fuzz' $$pkg | grep '^Fuzz'); do \
echo "fuzz $$pkg $$t"; \
go test -run '^$$' -fuzz "^$$t$$" -fuzztime $(FUZZTIME) $$pkg || exit 1; \
done; \
done
cover: coverage ## Open coverage report in browser (run coverage first)
@echo "Opening coverage report in browser"
@go tool cover -html=coverage.out
compile: ## Typecheck entire module (catches redeclarations / same errors as gopls DuplicateDecl)
@echo "Typechecking all packages (go build ./...)"
@go build ./...
cross: ## Typecheck the module, tests included, for 32-bit targets (linux/arm/v7 is a release target): int is 32 bits there
@echo "Typechecking for linux/arm/v7 and linux/386"
@GOOS=linux GOARCH=arm GOARM=7 go vet ./...
@GOOS=linux GOARCH=386 go vet ./...
lint: compile ## Run staticcheck after full-module compile
@echo "Running staticcheck"
@staticcheck ./...
lint-ci: compile ## go vet + golangci-lint (typecheck + configured linters; see issues.new in .golangci.yml)
@echo "Running go vet"
@go vet ./...
@echo "Running golangci-lint"
@golangci-lint run ./...
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 project packages
@echo "Running go vet"
@go vet ./...
# 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 .
vuln: ## Scan the module and its dependencies for known vulnerabilities (govulncheck)
@echo "Running govulncheck"
@govulncheck ./...
build: check ## Build the application
@echo "Building $(BINARY)"
@mkdir -p $(BIN_DIR)
@go build $(LDFLAGS) -o $(BINARY) $(APP_SRC)
build-nocheck: fmt lint lint-ci vet ## Build without running tests
@echo "Building $(BINARY)"
@mkdir -p $(BIN_DIR)
@go build $(LDFLAGS) -o $(BINARY) $(APP_SRC)
build-all: ## Build the application 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)-$(or $(VERSION),dev)-$$os-$$cpu.exe..."; \
GOOS=$$os GOARCH=$$cpu go build $(LDFLAGS) -o $(RELEASE_DIR)/$(APP_NAME)-$(or $(VERSION),dev)-$$os-$$cpu.exe $(APP_SRC); \
elif [ "$$cpu" = "arm" ] && [ "$$variant" = "v7" ]; then \
echo "Building $(APP_NAME)-$(or $(VERSION),dev)-$$os-armv7..."; \
GOOS=$$os GOARCH=$$cpu GOARM=7 go build $(LDFLAGS) -o $(RELEASE_DIR)/$(APP_NAME)-$(or $(VERSION),dev)-$$os-armv7 $(APP_SRC); \
else \
echo "Building $(APP_NAME)-$(or $(VERSION),dev)-$$os-$$cpu..."; \
GOOS=$$os GOARCH=$$cpu go build $(LDFLAGS) -o $(RELEASE_DIR)/$(APP_NAME)-$(or $(VERSION),dev)-$$os-$$cpu $(APP_SRC); \
fi \
done
release-all: build-all ## Package the build binaries into tar.gz (Unix) or zip (Windows) for all architectures
@mkdir -p $(RELEASE_DIR)
@for arch in $(ARCHS); do \
os=$${arch%%/*}; \
rest=$${arch#*/}; \
cpu=$${rest%%/*}; \
variant=$${rest#*/}; \
if [ "$$os" = "windows" ]; then \
bin=$(APP_NAME)-$(or $(VERSION),dev)-$$os-$$cpu.exe; \
echo "Packaging $$bin into $(APP_NAME)-$(or $(VERSION),dev)-$$os-$$cpu.zip..."; \
cd $(RELEASE_DIR) && zip -q $(APP_NAME)-$(or $(VERSION),dev)-$$os-$$cpu.zip $$bin && cd - > /dev/null; \
elif [ "$$cpu" = "arm" ] && [ "$$variant" = "v7" ]; then \
bin=$(APP_NAME)-$(or $(VERSION),dev)-$$os-armv7; \
echo "Packaging $$bin.tar.gz..."; \
tar czf $(RELEASE_DIR)/$$bin.tar.gz -C $(RELEASE_DIR) $$bin; \
else \
bin=$(APP_NAME)-$(or $(VERSION),dev)-$$os-$$cpu; \
echo "Packaging $$bin.tar.gz..."; \
tar czf $(RELEASE_DIR)/$$bin.tar.gz -C $(RELEASE_DIR) $$bin; \
fi; \
done
e2e: ## Build the binary and run the e2e suite against it
@mkdir -p bin
@go build $(LDFLAGS) -o bin/ethernetipctl $(APP_SRC)
@ETHERNETIPCTL_E2E_BIN=$(CURDIR)/bin/ethernetipctl ETHERNETIPCTL_E2E_VERSION=$(VERSION) go test -count=1 -tags=e2e ./e2e/...
install: build ## Install the built binary to /usr/local/bin
@echo "Installing $(APP_NAME) to /usr/local/bin"
@sudo install -m 0755 $(BINARY) /usr/local/bin/$(APP_NAME)
examples: ## Regenerate docs/examples.md: real runs of every documented command against the simulated adapter
@scripts/examples.sh
clean: ## Clean the build artifacts
@echo "Cleaning build artifacts"
@rm -rf $(BIN_DIR) coverage.out
@rm -f $(RELEASE_DIR)/*