Makefile (3217B)
1 MODULE := code.dwrz.net/src 2 CMDS := $(notdir $(wildcard ./cmd/*)) 3 BINARIES := $(addprefix bin/,$(CMDS)) 4 5 GO := go 6 GOFMT := go fmt 7 GOFIX := $(GO) fix 8 GOVET := go vet 9 GOIMPORTS := go tool goimports 10 DEADCODE := go tool deadcode 11 STATICCHECK := go tool staticcheck 12 GOVULNCHECK := go tool govulncheck 13 14 BUILDFLAGS := -buildvcs=true 15 PLATFORMS := linux/amd64 16 17 .PHONY: all build build-all buildinfo check clean deadcode deps fix fmt help \ 18 imports install lint run staticcheck test tidy tools uninstall \ 19 verify vet vulncheck $(CMDS) 20 21 ## Build all binaries (default target) 22 all: build 23 24 ## Display this help screen 25 help: 26 @awk ' \ 27 /^##/ { doc = substr($$0, 4); next } \ 28 /^[a-zA-Z0-9_-]+:/ && doc { \ 29 split($$1, target, ":"); \ 30 printf "\033[36m%-20s\033[0m %s\n", target[1], doc; \ 31 doc = "" \ 32 } \ 33 ' $(MAKEFILE_LIST) 34 35 ## Build all binaries to bin/ 36 build: $(BINARIES) 37 38 ## Build binaries for all defined platforms 39 build-all: 40 @for cmd in $(CMDS); do \ 41 for platform in $(PLATFORMS); do \ 42 echo "Building $$cmd for $$platform..."; \ 43 out="bin/$$cmd-$${platform%/*}-$${platform#*/}"; \ 44 GOOS=$${platform%/*} GOARCH=$${platform#*/} \ 45 $(GO) build $(BUILDFLAGS) -o $$out ./cmd/$$cmd; \ 46 done; \ 47 done 48 49 ## Build specific binaries (pattern rule) 50 bin/%: ./cmd/%/main.go 51 $(GO) build $(BUILDFLAGS) -o $@ ./cmd/$* 52 53 ## Shortcut targets to build specific commands (e.g., 'make control') 54 $(CMDS): 55 $(GO) build $(BUILDFLAGS) -o bin/$@ ./cmd/$@ 56 57 ## Show version info for built binaries 58 buildinfo: build 59 @for bin in $(BINARIES); do \ 60 if [ -x $$bin ]; then \ 61 echo "Info for $$bin:"; \ 62 $(GO) version -m $$bin || true; \ 63 fi; \ 64 done 65 66 ## Run tests with race detection and coverage 67 test: 68 $(GO) test -cover -race ./... 69 70 ## Run all linters 71 lint: fix fmt imports vet deadcode vulncheck staticcheck 72 73 ## Automatically fix code for newer Go versions 74 fix: 75 $(GOFIX) ./... 76 77 ## Format code using go fmt 78 fmt: 79 $(GOFMT) ./... 80 81 ## Fix imports and format using goimports 82 imports: 83 $(GOIMPORTS) -w -local "$(MODULE)" . 84 85 ## Run go vet 86 vet: 87 $(GOVET) ./... 88 89 ## Find dead code 90 deadcode: 91 $(DEADCODE) ./... 92 93 ## Check for vulnerabilities 94 vulncheck: 95 $(GOVULNCHECK) ./... 96 97 ## Run staticcheck 98 staticcheck: 99 $(STATICCHECK) ./... 100 101 ## Run integration checks (alias for lint) 102 check: lint 103 104 ## Tidy go.mod dependencies 105 tidy: 106 $(GO) mod tidy 107 108 ## Download dependencies 109 deps: 110 $(GO) mod download 111 112 ## Verify dependencies 113 verify: 114 $(GO) mod verify 115 116 ## Install development tools 117 tools: 118 go get -tool golang.org/x/tools/cmd/deadcode@latest 119 go get -tool golang.org/x/tools/cmd/goimports@latest 120 go get -tool honnef.co/go/tools/cmd/staticcheck@latest 121 go get -tool golang.org/x/vuln/cmd/govulncheck@latest 122 123 ## Run the default binary (use ARGS="..." to pass arguments) 124 run: clean bin/$(CMD) 125 ./bin/$(CMD) $(ARGS) 126 127 ## Clean build artifacts 128 clean: 129 $(GO) clean 130 rm -rf bin/* 131 132 ## Install binaries to $GOBIN 133 install: 134 $(GO) install $(BUILDFLAGS) ./cmd/... 135 136 ## Uninstall binaries from $GOBIN 137 uninstall: 138 @for cmd in $(CMDS); do \ 139 path=$$( $(GO) env GOBIN )/$$cmd; \ 140 if [ -z "$$( $(GO) env GOBIN )" ]; \ 141 then path=$$( $(GO) env GOPATH )/bin/$$cmd; fi; \ 142 echo "Removing $$path"; \ 143 rm -f $$path; \ 144 done