-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
254 lines (216 loc) · 7.64 KB
/
Makefile
File metadata and controls
254 lines (216 loc) · 7.64 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
GOFMT=gofmt
GOLINT=golangci-lint
# Build variables
BINARY_NAME=arc
BINARY_UNIX=$(BINARY_NAME)_unix
BUILD_DIR=./build
COVERAGE_DIR=./coverage
# Protobuf variables
PROTO_DIR=./api/v1
THIRD_PARTY_DIR=./third_party
PROTO_FILES=$(wildcard $(PROTO_DIR)/*.proto)
# Shared library names
SHAREDLIB_DARWIN_ARM64=signer-arm64.dylib
SHAREDLIB_LINUX_AMD64=signer-amd64.so
# Source files
MAIN_SOURCE=./cmd/$(BINARY_NAME)/main.go
SHAREDLIB_SOURCE=./sharedlib/sharedlib.go
# Git info
GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GIT_TAG=$(shell git describe --tags --abbrev=0 2>/dev/null || echo "dev")
BUILD_DATE=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
# Build flags
LDFLAGS=-ldflags "-X main.Version=$(GIT_TAG) -X main.GitCommit=$(GIT_COMMIT) -X main.BuildDate=$(BUILD_DATE)"
.PHONY: all build clean test coverage lint fmt vet vendor help sharedlib-darwin sharedlib-linux sharedlib-all setup proto proto-clean proto-install server server-build server-run
# Default target
all: test build
## help: Show this help message
help:
@echo 'Usage:'
@echo ' make <target>'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " %-20s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
## setup: Install development dependencies
setup:
@echo "Installing development dependencies..."
@$(GOCMD) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@$(GOCMD) install golang.org/x/tools/cmd/goimports@latest
@$(GOCMD) install github.com/securego/gosec/v2/cmd/gosec@latest
@echo "Setup complete!"
## proto-install: Install protobuf tools
proto-install:
@echo "Installing protobuf tools..."
@$(GOCMD) install google.golang.org/protobuf/cmd/protoc-gen-go@latest
@$(GOCMD) install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
@$(GOCMD) install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
@$(GOCMD) install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
@echo "Protobuf tools installed!"
## proto: Generate Go code from protobuf definitions
proto:
@echo "Generating protobuf code..."
@mkdir -p $(THIRD_PARTY_DIR)/googleapis/google/api
@if [ ! -f $(THIRD_PARTY_DIR)/googleapis/google/api/annotations.proto ]; then \
curl -L https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto \
-o $(THIRD_PARTY_DIR)/googleapis/google/api/annotations.proto; \
fi
@if [ ! -f $(THIRD_PARTY_DIR)/googleapis/google/api/http.proto ]; then \
curl -L https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto \
-o $(THIRD_PARTY_DIR)/googleapis/google/api/http.proto; \
fi
@protoc \
-I $(PROTO_DIR) \
-I $(THIRD_PARTY_DIR)/googleapis \
--go_out=$(PROTO_DIR) \
--go_opt=paths=source_relative \
--go-grpc_out=$(PROTO_DIR) \
--go-grpc_opt=paths=source_relative \
--grpc-gateway_out=$(PROTO_DIR) \
--grpc-gateway_opt=paths=source_relative \
$(PROTO_FILES)
@echo "Protobuf code generated!"
## proto-clean: Clean generated protobuf files
proto-clean:
@echo "Cleaning generated protobuf files..."
@rm -f $(PROTO_DIR)/*.pb.go
@rm -f $(PROTO_DIR)/*.pb.gw.go
@rm -f $(BUILD_DIR)/*.swagger.json
@rm -rf $(THIRD_PARTY_DIR)
## server-build: Build the gRPC/HTTP server
server-build: proto vendor
@echo "Building ARC server..."
@mkdir -p $(BUILD_DIR)
@$(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/arc-server -v ./cmd/server/
## server-run: Run the gRPC/HTTP server
server-run: server-build
@echo "Starting ARC server..."
@$(BUILD_DIR)/arc-server
## server: Generate protobuf, build and run server
server: proto server-build server-run
## build: Build the binary
build: vendor
@mkdir -p $(BUILD_DIR)
@if [ -f $(MAIN_SOURCE) ]; then \
$(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) -v $(MAIN_SOURCE); \
else \
echo "Warning: $(MAIN_SOURCE) not found. Skipping binary build."; \
fi
## sharedlib-darwin: Build shared library for Darwin (macOS)
sharedlib-darwin: vendor
@mkdir -p $(BUILD_DIR)
@if [ -f $(SHAREDLIB_SOURCE) ]; then \
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 $(GOBUILD) -buildmode=c-shared -o $(BUILD_DIR)/$(SHAREDLIB_DARWIN_ARM64) $(SHAREDLIB_SOURCE); \
echo "Built $(BUILD_DIR)/$(SHAREDLIB_DARWIN_ARM64)"; \
else \
echo "Error: $(SHAREDLIB_SOURCE) not found"; \
exit 1; \
fi
## sharedlib-linux: Build shared library for Linux
sharedlib-linux: vendor
@mkdir -p $(BUILD_DIR)
@if [ -f $(SHAREDLIB_SOURCE) ]; then \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 $(GOBUILD) -buildmode=c-shared -o $(BUILD_DIR)/$(SHAREDLIB_LINUX_AMD64) $(SHAREDLIB_SOURCE); \
echo "Built $(BUILD_DIR)/$(SHAREDLIB_LINUX_AMD64)"; \
else \
echo "Error: $(SHAREDLIB_SOURCE) not found"; \
exit 1; \
fi
## sharedlib-all: Build all shared libraries
sharedlib-all: sharedlib-darwin sharedlib-linux
## clean: Remove build artifacts
clean:
@$(GOCLEAN)
@rm -rf $(BUILD_DIR)
@rm -rf $(COVERAGE_DIR)
@rm -rf vendor/
## test: Run tests
test:
@$(GOTEST) -v -race ./...
## test-short: Run short tests
test-short:
@$(GOTEST) -v -short ./...
## coverage: Run tests with coverage
coverage:
@mkdir -p $(COVERAGE_DIR)
@$(GOTEST) -v -race -coverprofile=$(COVERAGE_DIR)/coverage.out -covermode=atomic ./...
@$(GOCMD) tool cover -html=$(COVERAGE_DIR)/coverage.out -o $(COVERAGE_DIR)/coverage.html
@echo "Coverage report generated at $(COVERAGE_DIR)/coverage.html"
## benchmark: Run benchmarks
benchmark:
@$(GOTEST) -bench=. -benchmem ./...
## lint: Run golangci-lint
lint:
@if command -v golangci-lint >/dev/null 2>&1; then \
$(GOLINT) run ./...; \
else \
echo "golangci-lint not installed. Run 'make setup' first."; \
exit 1; \
fi
## fmt: Format code
fmt:
@$(GOFMT) -s -w .
@if command -v goimports >/dev/null 2>&1; then \
goimports -w .; \
fi
## vet: Run go vet
vet:
@$(GOCMD) vet ./...
## security: Run security scan
security:
@if command -v gosec >/dev/null 2>&1; then \
gosec -fmt=json -out=$(BUILD_DIR)/security-report.json ./... || true; \
echo "Security report generated at $(BUILD_DIR)/security-report.json"; \
else \
echo "gosec not installed. Run 'make setup' first."; \
fi
## vendor: Vendor dependencies
vendor:
@$(GOMOD) vendor
@$(GOMOD) tidy
## deps: Download dependencies
deps:
@$(GOMOD) download
## deps-update: Update dependencies
deps-update:
@$(GOMOD) tidy
@$(GOCMD) get -u ./...
@$(GOMOD) tidy
## build-cross: Cross compile for multiple platforms
build-cross: vendor
@mkdir -p $(BUILD_DIR)
@if [ -f $(MAIN_SOURCE) ]; then \
echo "Building for Linux..."; \
GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 -v $(MAIN_SOURCE); \
echo "Building for Darwin..."; \
GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 -v $(MAIN_SOURCE); \
GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 -v $(MAIN_SOURCE); \
echo "Building for Windows..."; \
GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe -v $(MAIN_SOURCE); \
else \
echo "Warning: $(MAIN_SOURCE) not found. Skipping cross compilation."; \
fi
## run: Run the application
run: build
@if [ -f $(BUILD_DIR)/$(BINARY_NAME) ]; then \
$(BUILD_DIR)/$(BINARY_NAME); \
else \
echo "Binary not found. Build it first with 'make build'"; \
fi
## docker-build: Build Docker image
docker-build:
@docker build -t $(BINARY_NAME):$(GIT_TAG) .
## ci: Run CI pipeline locally
ci: vendor lint vet test
## release: Create a new release
release: clean vendor lint vet test build-cross
@echo "Release artifacts built in $(BUILD_DIR)/"
@ls -la $(BUILD_DIR)/
# Keep the build directory
.PRECIOUS: $(BUILD_DIR)