Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
292 lines (247 loc) Β· 10.1 KB
/
Copy pathMakefile
File metadata and controls
292 lines (247 loc) Β· 10.1 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
290
291
292
# Makefile for greq project
#
# Usage:
# make build - Build all binaries for current platform
# make build-all - Build binaries for all platforms
# make build-linux - Build binaries for Linux
# make build-darwin - Build binaries for macOS
# make build-windows - Build binaries for Windows
# make test - Run all tests
# make clean - Clean build artifacts
# make install - Install binaries to GOPATH/bin
# Go parameters
GOCMD := go
GOBUILD := $(GOCMD) build
GOCLEAN := $(GOCMD) clean
GOTEST := $(GOCMD) test
GOGET := $(GOCMD) get
GOMOD := $(GOCMD) mod
# Binary names
BINS := greq gbench
# Build directory
BUILD_DIR := dist
# Version info (can be overridden by env vars or git tags)
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%S")
# Ldflags for version injection
LDFLAGS := -ldflags "-s -w -X main.Version=$(VERSION) -X main.GitCommit=$(GIT_COMMIT) -X main.BuildTime=$(BUILD_TIME)"
# Go files (note: find command may have issues on Windows)
# GO_FILES := $(shell find . -name '*.go' -type f)
# Platform-specific variables
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
BINARY_EXT = $(shell go env GOEXE)
# Race detector support (requires CGO on Windows)
ifeq ($(GOOS),windows)
TEST_FLAGS := -v -cover
else
TEST_FLAGS := -v -race -cover
endif
# Default target
.PHONY: all
all: build
# ============================================================================
# Build targets
# ============================================================================
.PHONY: build
build: ## Build all binaries for current platform
@echo "Building binaries for $(GOOS)/$(GOARCH)..."
@for bin in $(BINS); do \
echo " Building $$bin..."; \
mkdir -p $(BUILD_DIR)/$$bin; \
(cd cmd/$$bin && $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/$$bin/$$bin$(BINARY_EXT) .) || exit 1; \
done
@echo "Build complete. Binaries in $(BUILD_DIR)/"
.PHONY: build-greq
build-greq: ## Build greq binary only
@echo "Building greq..."
@mkdir -p $(BUILD_DIR)/greq
cd cmd/greq && $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/greq/greq$(BINARY_EXT) .
upx -6 --no-progress $(BUILD_DIR)/greq/greq$(BINARY_EXT)
.PHONY: build-gbench
build-gbench: ## Build gbench binary only
@echo "Building gbench..."
@mkdir -p $(BUILD_DIR)/gbench
cd cmd/gbench && $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/gbench/gbench$(BINARY_EXT) .
upx -6 --no-progress $(BUILD_DIR)/gbench/gbench$(BINARY_EXT)
# ============================================================================
# Cross-platform build targets
# ============================================================================
# Linux builds
.PHONY: build-linux
build-linux: build-linux-amd64 build-linux-arm64 ## Build binaries for Linux (amd64, arm64)
@echo "Linux build complete."
.PHONY: build-linux-amd64
build-linux-amd64: ## Build binaries for Linux amd64
@for bin in $(BINS); do \
echo "Building $$bin for linux/amd64..."; \
mkdir -p $(BUILD_DIR)/$$bin; \
(cd cmd/$$bin && GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/$$bin/$$bin-linux-amd64 .) || exit 1; \
chmod a+x $(BUILD_DIR)/$$bin/$$bin-linux-amd64; \
done
.PHONY: build-linux-arm64
build-linux-arm64: ## Build binaries for Linux arm64
@for bin in $(BINS); do \
echo "Building $$bin for linux/arm64..."; \
mkdir -p $(BUILD_DIR)/$$bin; \
(cd cmd/$$bin && GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/$$bin/$$bin-linux-arm64 .) || exit 1; \
chmod a+x $(BUILD_DIR)/$$bin/$$bin-linux-arm64; \
done
# macOS builds
.PHONY: build-darwin
build-darwin: build-darwin-amd64 build-darwin-arm64 ## Build binaries for macOS (amd64, arm64)
@echo "macOS build complete."
.PHONY: build-darwin-amd64
build-darwin-amd64: ## Build binaries for macOS amd64 (Intel)
@for bin in $(BINS); do \
echo "Building $$bin for darwin/amd64..."; \
mkdir -p $(BUILD_DIR)/$$bin; \
(cd cmd/$$bin && GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/$$bin/$$bin-darwin-amd64 .) || exit 1; \
chmod a+x $(BUILD_DIR)/$$bin/$$bin-darwin-amd64; \
done
.PHONY: build-darwin-arm64
build-darwin-arm64: ## Build binaries for macOS arm64 (Apple Silicon)
@for bin in $(BINS); do \
echo "Building $$bin for darwin/arm64..."; \
mkdir -p $(BUILD_DIR)/$$bin; \
(cd cmd/$$bin && GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/$$bin/$$bin-darwin-arm64 .) || exit 1; \
chmod a+x $(BUILD_DIR)/$$bin/$$bin-darwin-arm64; \
done
# Windows builds
.PHONY: build-windows
build-windows: build-windows-amd64 build-windows-arm64 ## Build binaries for Windows (amd64, arm64)
@echo "Windows build complete."
.PHONY: build-windows-amd64
build-windows-amd64: ## Build binaries for Windows amd64
@for bin in $(BINS); do \
echo "Building $$bin for windows/amd64..."; \
mkdir -p $(BUILD_DIR)/$$bin; \
(cd cmd/$$bin && GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/$$bin/$$bin-windows-amd64.exe .) || exit 1; \
done
.PHONY: build-windows-arm64
build-windows-arm64: ## Build binaries for Windows arm64
@for bin in $(BINS); do \
echo "Building $$bin for windows/arm64..."; \
mkdir -p $(BUILD_DIR)/$$bin; \
(cd cmd/$$bin && GOOS=windows GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o ../../$(BUILD_DIR)/$$bin/$$bin-windows-arm64.exe .) || exit 1; \
done
# Build all platforms
.PHONY: build-all
build-all: dump-info latest-yaml build-linux build-darwin build-windows ## Build binaries for all platforms
@echo "π All platform builds complete."
@ls -lah $(BUILD_DIR)/greq/ $(BUILD_DIR)/gbench/
# ============================================================================
# Development targets
# ============================================================================
## dump-info: dump build info
dump-info:
@echo "Build Info:"
@echo " VERSION: $(VERSION)"
@echo " GIT_HASH: $(GIT_HASH)"
@echo " BUILD_TIME: $(BUILD_TIME)"
## latest-yaml: generate latest.yaml release metadata
latest-yaml:
@for bin in $(BINS); do \
desc="Lightweight HTTP request tool by Go, like curl."; \
if [ "$$bin" = "gbench" ]; then desc="Lightweight Benchmark HTTP requests, like ab."; fi; \
mkdir -p $(BUILD_DIR)/$$bin; \
{ \
echo "name: $$bin"; \
echo "version: $(VERSION)"; \
echo "released_at: $(BUILD_TIME)"; \
echo "description: $$desc"; \
} > $(BUILD_DIR)/$$bin/latest.yaml; \
echo " β $(BUILD_DIR)/$$bin/latest.yaml"; \
done
.PHONY: test
test: ## Run all tests (library + cmd submodules)
$(GOTEST) $(TEST_FLAGS) ./...
@for bin in $(BINS); do \
echo " Testing cmd/$$bin..."; \
(cd cmd/$$bin && $(GOTEST) $(TEST_FLAGS) ./...) || exit 1; \
done
.PHONY: test-coverage
test-coverage: ## Run tests with coverage report
$(GOTEST) $(TEST_FLAGS) -coverprofile=coverage.out ./...
$(GOCMD) tool cover -html=coverage.out -o coverage.html
@echo "Coverage report generated: coverage.html"
.PHONY: lint
lint: ## Run linters
@which golangci-lint > /dev/null || (echo "Installing golangci-lint..." && go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest)
golangci-lint run ./...
.PHONY: fmt
fmt: ## Format code
$(GOCMD) fmt ./...
.PHONY: vet
vet: ## Run go vet
$(GOCMD) vet ./...
# ============================================================================
# Install targets
# ============================================================================
.PHONY: install
install: ## Install binaries to GOPATH/bin
@echo "Installing binaries..."
@for bin in $(BINS); do \
echo " Installing $$bin..."; \
(cd cmd/$$bin && $(GOCMD) install $(LDFLAGS) .) || exit 1; \
done
@echo "Installation complete."
.PHONY: install-greq
install-greq: ## Install greq to GOPATH/bin
cd cmd/greq && $(GOCMD) install $(LDFLAGS) .
upx -6 --no-progress $(GOPATH)/bin/greq$(BINARY_EXT)
.PHONY: install-gbench
install-gbench: ## Install gbench to GOPATH/bin
cd cmd/gbench && $(GOCMD) install $(LDFLAGS) .
upx -6 --no-progress $(GOPATH)/bin/gbench$(BINARY_EXT)
# ============================================================================
# Clean targets
# ============================================================================
.PHONY: clean
clean: ## Clean build artifacts
@echo "Cleaning..."
@rm -rf $(BUILD_DIR)
@rm -f coverage.out coverage.html
$(GOCLEAN)
@echo "Clean complete."
# ============================================================================
# Utility targets
# ============================================================================
.PHONY: deps
deps: ## Download dependencies
$(GOMOD) download
$(GOMOD) verify
.PHONY: deps-update
deps-update: ## Update dependencies
$(GOMOD) tidy
$(GOGET) -u ./...
.PHONY: version
version: ## Show version info
@echo "Version: $(VERSION)"
@echo "Git Commit: $(GIT_COMMIT)"
@echo "Build Time: $(BUILD_TIME)"
@echo "Go Version: $(shell go version)"
.PHONY: help
help: ## Show this help message
@echo "Usage: make [target]"
@echo ""
@echo "Targets:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-18s\033[0m %s\n", $$1, $$2}'
# ============================================================================
# Compressed release archives
# ============================================================================
.PHONY: release
release: build-all ## Create release archives for all platforms (tar.gz for linux/macOS, zip for Windows)
@echo "Creating release archives..."
@mkdir -p release
@for bin in $(BINS); do \
tar -czf release/$$bin-$(VERSION)-linux-amd64.tar.gz -C $(BUILD_DIR)/$$bin $$bin-linux-amd64; \
tar -czf release/$$bin-$(VERSION)-linux-arm64.tar.gz -C $(BUILD_DIR)/$$bin $$bin-linux-arm64; \
tar -czf release/$$bin-$(VERSION)-darwin-amd64.tar.gz -C $(BUILD_DIR)/$$bin $$bin-darwin-amd64; \
tar -czf release/$$bin-$(VERSION)-darwin-arm64.tar.gz -C $(BUILD_DIR)/$$bin $$bin-darwin-arm64; \
(cd $(BUILD_DIR)/$$bin && zip -q ../../release/$$bin-$(VERSION)-windows-amd64.zip $$bin-windows-amd64.exe); \
(cd $(BUILD_DIR)/$$bin && zip -q ../../release/$$bin-$(VERSION)-windows-arm64.zip $$bin-windows-arm64.exe); \
done
@echo "Release archives created in release/"
@ls -la release/