|
1 | | -PROJECT_ROOT:=${PWD}/../../ |
2 | | -LD_FLAGS="-w -s" |
3 | | -VERSION=$(shell git describe --tags --match=v* --always --dirty) |
4 | | - |
5 | | -SERVER_OUT := "bin/ntpservice" |
6 | | - |
7 | | -PKG := "${PROJECT_ROOT}" |
8 | | -SERVER_PKG_BUILD := "${PKG}/cmd/ntpservice" |
9 | | -PKG_LIST := $(shell go list ${PKG}/...) |
10 | | -GO_FILES := $(shell find . -name '*.go' | grep -v _test.go | grep -v .pb) |
11 | | - |
12 | | -.PHONY: all |
13 | | -all: build test vet lint fmt ## Run build, test, vet, lint and fmt |
14 | | - |
15 | | -.PHONY: build |
16 | | -# build: clean server client ## Build server |
17 | | -build: clean server ## Build both api, server (maybe clients) |
18 | | - |
19 | | -.PHONY: test |
20 | | -test: ## Run unit tests |
21 | | - $(info Running unit tests...) |
22 | | - @go test -cover -short ${PKG_LIST} |
23 | | - |
24 | | -.PHONY: vet |
25 | | -vet: |
26 | | - $(info Running go vet...) |
27 | | - @go vet -all ${PKG_LIST} | tee -a govet-report.out |
28 | | - |
29 | | -.PHONY: lint |
30 | | -lint: # Lint the files |
31 | | - $(info Running golint...) |
| 1 | +# Global Variables (Immutable) |
| 2 | +PROJECT_ROOT := $(CURDIR)/../.. |
| 3 | +VERSION := $(shell git describe --tags --match='v*' --always --dirty) |
| 4 | +GO_MODULE := $(shell go list -m) |
| 5 | + |
| 6 | +# Output Directories |
| 7 | +BIN_DIR := bin |
| 8 | +SERVER_OUT := $(BIN_DIR)/ntpservice |
| 9 | + |
| 10 | +# Build Flags |
| 11 | +GO_LDFLAGS := -w -s |
| 12 | +GO_TAGS := |
| 13 | +GO_TEST_FLAGS := -gcflags=all=-l -v -cover -short |
| 14 | +GO_RACE_FLAGS := -race |
| 15 | +GO_MSAN_FLAGS := -msan |
| 16 | + |
| 17 | +# Packages |
| 18 | +PKG := $(PROJECT_ROOT) |
| 19 | +SERVER_PKG_BUILD := $(PKG)/cmd/ntpservice |
| 20 | +PKG_LIST := $(shell go list $(PKG)/...) |
| 21 | + |
| 22 | +# Tools |
| 23 | +GOLANGCI_LINT_VERSION := latest |
| 24 | +GOSEC_VERSION := latest |
| 25 | +GORELEASER_CONFIG := $(PROJECT_ROOT)/build/package/.goreleaser.yml |
| 26 | + |
| 27 | +# Phony Targets |
| 28 | +.PHONY: all build test govet golint golangcilint gosec race msan coverage coverhtml dep clean deb help |
| 29 | + |
| 30 | +##@ General |
| 31 | +all: build test govet golint golangcilint gosec ## Build, test, govet, golint, golangcilint and gosec (default workflow) |
| 32 | + |
| 33 | +help: ## Display this help |
| 34 | + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) }' $(MAKEFILE_LIST) |
| 35 | + |
| 36 | +##@ Build |
| 37 | +build: dep ## Build server binary |
| 38 | + @mkdir -p $(BIN_DIR) |
| 39 | + @echo "⇒ Building server..." |
| 40 | + @go build -trimpath -tags='$(GO_TAGS)' -ldflags='$(GO_LDFLAGS)' -o $(SERVER_OUT) $(SERVER_PKG_BUILD) |
| 41 | + @echo "✓ Server built: $(SERVER_OUT)" |
| 42 | + |
| 43 | +##@ Testing & Validation |
| 44 | +test: dep ## Run unit tests |
| 45 | + @echo "⇒ Running unit tests..." |
| 46 | + @go test $(GO_TEST_FLAGS) $(PKG_LIST) |
| 47 | + |
| 48 | +govet: dep ## Run static analysis with go vet |
| 49 | + @echo "⇒ Running govet..." |
| 50 | + @go vet -json $(PKG_LIST) 2>&1 | tee -a govet-report.out |
| 51 | + |
| 52 | +golint: dep ## Run golint linter |
| 53 | + @echo "⇒ Running golint..." |
| 54 | + @go install golang.org/x/lint/golint@latest |
32 | 55 | @golint ${PKG_LIST} | tee -a golint-report.out |
33 | 56 |
|
34 | | -.PHONY: fmt |
35 | | -fmt: |
36 | | - $(info Running go fmt...) |
37 | | - @test -z $$(go fmt ${PKG_LIST}) |
| 57 | +golangcilint: dep ## Run golangci-lint linter |
| 58 | + @echo "⇒ Running golangci-lint..." |
| 59 | + @go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) |
| 60 | + @${GOPATH}/bin/golangci-lint run --out-format=checkstyle $(PROJECT_ROOT)/... | tee -a golangcilint-report.xml |
| 61 | + |
| 62 | +gosec: dep ## Run gosec security scanner |
| 63 | + @echo "⇒ Running gosec..." |
| 64 | + @go install github.com/securego/gosec/v2/cmd/gosec@$(GOSEC_VERSION) |
| 65 | + @${GOPATH}/bin/gosec -exclude-dir=.cache -fmt=sonarqube $(PROJECT_ROOT)/... | tee -a gosec-report.json |
38 | 66 |
|
39 | | -race: dep ## Run data race detector |
40 | | - $(info Running data race detector...) |
41 | | - @go test -race -short ${PKG_LIST} |
| 67 | +race: dep ## Run tests with race detector |
| 68 | + @echo "⇒ Running race detector..." |
| 69 | + @go test $(GO_TEST_FLAGS) $(GO_RACE_FLAGS) $(PKG_LIST) |
42 | 70 |
|
43 | 71 | msan: dep ## Run memory sanitizer |
44 | | - $(info Running memory sanitizer...) |
45 | | - @go test -msan -short ${PKG_LIST} |
| 72 | + @echo "⇒ Running memory sanitizer..." |
| 73 | + @go test $(GO_TEST_FLAGS) $(GO_MSAN_FLAGS) $(PKG_LIST) |
46 | 74 |
|
47 | | -coverage: ## Generate global code coverage report |
48 | | - $(info Generating global code coverage report...) |
49 | | - ./coverage.sh; |
| 75 | +##@ Coverage |
| 76 | +coverage: dep ## Generate global code coverage report |
| 77 | + @echo "⇒ Generating global code coverage report..." |
| 78 | + @./coverage.sh |
50 | 79 |
|
51 | | -coverhtml: ## Generate global code coverage report in HTML |
52 | | - $(info Generating global code coverage report in HTML...) |
53 | | - ./coverage.sh html; |
| 80 | +coverhtml: coverage ## Generate global code coverage report in HTML |
| 81 | + @echo "⇒ Generating global code coverage report in HTML..." |
| 82 | + @./coverage.sh html; |
54 | 83 |
|
55 | | -dep: ## Get the dependencies |
56 | | - $(info Get the dependencies...) |
57 | | - @go get -v -d $(PKG)/... |
58 | | - @go get -v golang.org/x/lint/golint |
| 84 | +##@ Dependencies |
| 85 | +dep: ## Install dependencies |
| 86 | + @echo "⇒ Installing tools..." |
59 | 87 | @go mod tidy |
60 | 88 |
|
61 | | -server: dep ## Build the binary file for server |
62 | | - $(info Building server...) |
63 | | - @go build -v -ldflags=$(LD_FLAGS) -o $(SERVER_OUT) $(SERVER_PKG_BUILD) |
64 | | - $(info Built server at $(SERVER_OUT) ) |
65 | | - |
66 | | -clean: ## Remove previous builds |
67 | | - $(info Running clean up, removing previous builds...) |
68 | | - @rm -rf $(SERVER_OUT) |
69 | | - |
70 | | -.PHONY: deb |
71 | | -deb: ## Build Debian package (deb) |
72 | | - @goreleaser -f "${PROJECT_ROOT}build/package/.goreleaser.yml" --snapshot --skip-publish --clean |
73 | | - |
74 | | - |
75 | | -# Absolutely awesome: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html |
76 | | -help: ## Display this help screen |
77 | | - @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' |
| 89 | +##@ Cleanup |
| 90 | +clean: ## Remove build artifacts |
| 91 | + @echo "⇒ Cleaning build artifacts..." |
| 92 | + @rm -rf $(BIN_DIR) dist/ *-report.xml *-report.json *-report.out |
| 93 | + @find . -name "coverage*" ! -name "coverage.sh" -exec rm -rf {} + |
78 | 94 |
|
| 95 | +##@ Packaging |
| 96 | +deb: dep ## Build Debian package |
| 97 | + @echo "⇒ Building Debian package..." |
| 98 | + @goreleaser --config $(GORELEASER_CONFIG) --snapshot --skip=publish --clean |
79 | 99 |
|
80 | 100 | .DEFAULT_GOAL := help |
0 commit comments