-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
289 lines (257 loc) · 9.58 KB
/
Makefile
File metadata and controls
289 lines (257 loc) · 9.58 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Makefile for gurl - HTTP benchmarking tool
# Variables
BINARY_NAME=gurl
BUILD_DIR=build
CMD_DIR=cmd/gurl
MAIN_PACKAGE=./$(CMD_DIR)
VERSION?=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT?=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME?=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
GOFMT=$(GOCMD) fmt
# Build flags
LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.Commit=$(COMMIT) -X main.BuildTime=$(BUILD_TIME)"
# Default target
.PHONY: all
all: clean build
# Build the binary
.PHONY: build
build:
@echo "Building $(BINARY_NAME)..."
@mkdir -p $(BUILD_DIR)
$(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PACKAGE)
@echo "Build complete: $(BUILD_DIR)/$(BINARY_NAME)"
# Build for multiple platforms
.PHONY: build-all
build-all:
@echo "Building for multiple platforms..."
@mkdir -p $(BUILD_DIR)
# Clean only gurl binaries
rm -f $(BUILD_DIR)/$(BINARY_NAME)-*
# Linux amd64
GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 $(MAIN_PACKAGE)
# Linux arm64
GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 $(MAIN_PACKAGE)
# macOS amd64
GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 $(MAIN_PACKAGE)
# macOS arm64
GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(MAIN_PACKAGE)
# Windows amd64
GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe $(MAIN_PACKAGE)
@echo "Cross-compilation complete"
# Build for Linux (amd64 and arm64)
.PHONY: build-linux
build-linux:
@echo "Building for Linux platforms..."
@mkdir -p $(BUILD_DIR)
GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 $(MAIN_PACKAGE)
GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 $(MAIN_PACKAGE)
@echo "Linux binaries built: $(BUILD_DIR)/$(BINARY_NAME)-linux-*"
# Build for macOS (amd64 and arm64)
.PHONY: build-darwin
build-darwin:
@echo "Building for macOS platforms..."
@mkdir -p $(BUILD_DIR)
GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 $(MAIN_PACKAGE)
GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(MAIN_PACKAGE)
@echo "macOS binaries built: $(BUILD_DIR)/$(BINARY_NAME)-darwin-*"
# Build for Windows (amd64)
.PHONY: build-windows
build-windows:
@echo "Building for Windows..."
@mkdir -p $(BUILD_DIR)
GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe $(MAIN_PACKAGE)
@echo "Windows binary built: $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe"
# Generic cross-compile target: make cross-compile OS=linux ARCH=amd64
.PHONY: cross-compile
cross-compile:
@if [ -z "$(OS)" ] || [ -z "$(ARCH)" ]; then \
echo "Usage: make cross-compile OS=<os> ARCH=<arch>"; \
echo "Examples:"; \
echo " make cross-compile OS=linux ARCH=amd64"; \
echo " make cross-compile OS=darwin ARCH=arm64"; \
echo " make cross-compile OS=windows ARCH=amd64"; \
exit 1; \
fi
@mkdir -p $(BUILD_DIR)
@echo "Cross-compiling for $(OS)/$(ARCH)..."
@if [ "$(OS)" = "windows" ]; then \
GOOS=$(OS) GOARCH=$(ARCH) $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-$(OS)-$(ARCH).exe $(MAIN_PACKAGE); \
echo "Binary built: $(BUILD_DIR)/$(BINARY_NAME)-$(OS)-$(ARCH).exe"; \
else \
GOOS=$(OS) GOARCH=$(ARCH) $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-$(OS)-$(ARCH) $(MAIN_PACKAGE); \
echo "Binary built: $(BUILD_DIR)/$(BINARY_NAME)-$(OS)-$(ARCH)"; \
fi
# Install the binary to GOPATH/bin
.PHONY: install
install:
@echo "Installing $(BINARY_NAME)..."
$(GOBUILD) $(LDFLAGS) -o $(GOPATH)/bin/$(BINARY_NAME) $(MAIN_PACKAGE)
@echo "Installed to $(GOPATH)/bin/$(BINARY_NAME)"
# Run tests
.PHONY: test
test:
@echo "Running tests..."
@mkdir -p .go-tmp
GOTMPDIR=$(PWD)/.go-tmp $(GOTEST) -v ./...
# Run tests with coverage
.PHONY: test-coverage
test-coverage:
@echo "Running tests with coverage..."
@mkdir -p .go-tmp
GOTMPDIR=$(PWD)/.go-tmp $(GOTEST) -v -coverprofile=coverage.out ./...
$(GOCMD) tool cover -html=coverage.out -o coverage.html
@echo "Coverage report generated: coverage.html"
# Run benchmarks
.PHONY: bench
bench:
@echo "Running benchmarks..."
@mkdir -p .go-tmp
GOTMPDIR=$(PWD)/.go-tmp $(GOTEST) -bench=. -benchmem ./...
# Build and run test server (current platform)
.PHONY: test-server
test-server:
@echo "Building and starting test server..."
@mkdir -p $(BUILD_DIR)
$(GOBUILD) -o $(BUILD_DIR)/testserver ./cmd/testserver
./$(BUILD_DIR)/testserver
# Build test server for multiple platforms
.PHONY: test-server-all
test-server-all:
@echo "Building test server for multiple platforms..."
@mkdir -p $(BUILD_DIR)
# Clean only testserver binaries
rm -f $(BUILD_DIR)/testserver-*
# Linux amd64
GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/testserver-linux-amd64 ./cmd/testserver
# Linux arm64
GOOS=linux GOARCH=arm64 $(GOBUILD) -o $(BUILD_DIR)/testserver-linux-arm64 ./cmd/testserver
# macOS amd64
GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/testserver-darwin-amd64 ./cmd/testserver
# macOS arm64
GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(BUILD_DIR)/testserver-darwin-arm64 ./cmd/testserver
@echo "Cross-platform test server binaries built successfully!"
@ls -la $(BUILD_DIR)/testserver-*
# Build and run test server for Linux (useful for testing in containers)
.PHONY: test-server-linux
test-server-linux:
@echo "Building test server for Linux..."
@mkdir -p $(BUILD_DIR)
GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/testserver-linux-amd64 ./cmd/testserver
@echo "Linux test server built: $(BUILD_DIR)/testserver-linux-amd64"
# Build and run test server for macOS
.PHONY: test-server-darwin
test-server-darwin:
@echo "Building test server for macOS..."
@mkdir -p $(BUILD_DIR)
GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/testserver-darwin-amd64 ./cmd/testserver
GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(BUILD_DIR)/testserver-darwin-arm64 ./cmd/testserver
@echo "macOS test server built: $(BUILD_DIR)/testserver-darwin-*"
# Run unit tests only
.PHONY: test-unit
test-unit:
@echo "Running unit tests..."
@mkdir -p .go-tmp
GOTMPDIR=$(PWD)/.go-tmp $(GOTEST) -v ./internal/...
# Run integration tests
.PHONY: test-integration
test-integration:
@echo "Running integration tests..."
$(GOTEST) -v ./test/...
# Run all tests (unit + integration)
.PHONY: test-all
test-all: test-unit test-integration
@echo "All tests completed"
# Format code
.PHONY: fmt
fmt:
@echo "Formatting code..."
$(GOFMT) ./...
# Lint code (requires golangci-lint)
.PHONY: lint
lint:
@echo "Linting code..."
@which golangci-lint > /dev/null || (echo "golangci-lint not found, install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest" && exit 1)
golangci-lint run
# Tidy dependencies
.PHONY: tidy
tidy:
@echo "Tidying dependencies..."
$(GOMOD) tidy
# Download dependencies
.PHONY: deps
deps:
@echo "Downloading dependencies..."
$(GOMOD) download
# Clean build artifacts
.PHONY: clean
clean:
@echo "Cleaning..."
$(GOCLEAN)
rm -rf $(BUILD_DIR)
rm -f coverage.out coverage.html
# Run the application (for development)
.PHONY: run
run: build
@echo "Running $(BINARY_NAME)..."
./$(BUILD_DIR)/$(BINARY_NAME) --help
# Development setup
.PHONY: dev-setup
dev-setup:
@echo "Setting up development environment..."
$(GOMOD) download
@echo "Installing development tools..."
$(GOCMD) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@echo "Development setup complete"
# Quick development build and test
.PHONY: dev
dev: fmt build test
@echo "Development build and test complete"
# Release build (optimized)
.PHONY: release
release: clean
@echo "Building release version..."
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -a -installsuffix cgo -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PACKAGE)
@echo "Release build complete: $(BUILD_DIR)/$(BINARY_NAME)"
# Docker build (if needed in the future)
.PHONY: docker
docker:
@echo "Building Docker image..."
docker build -t $(BINARY_NAME):$(VERSION) .
# Show help
.PHONY: help
help:
@echo "Available targets:"
@echo " all - Clean and build (default)"
@echo " build - Build the binary"
@echo " build-all - Build for multiple platforms"
@echo " build-linux - Build for Linux (amd64, arm64)"
@echo " build-darwin - Build for macOS (amd64, arm64)"
@echo " build-windows- Build for Windows (amd64)"
@echo " cross-compile- Generic cross-compile: make cross-compile OS=<os> ARCH=<arch>"
@echo " install - Install to GOPATH/bin"
@echo " test - Run tests"
@echo " test-coverage- Run tests with coverage report"
@echo " bench - Run benchmarks"
@echo " test-server - Build and run test server (current platform)"
@echo " test-server-all - Build test server for multiple platforms"
@echo " test-server-linux - Build test server for Linux"
@echo " test-server-darwin - Build test server for macOS"
@echo " fmt - Format code"
@echo " lint - Lint code (requires golangci-lint)"
@echo " tidy - Tidy dependencies"
@echo " deps - Download dependencies"
@echo " clean - Clean build artifacts"
@echo " run - Build and run with --help"
@echo " dev-setup - Setup development environment"
@echo " dev - Quick development build and test"
@echo " release - Build optimized release version"
@echo " docker - Build Docker image"
@echo " help - Show this help"