-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
199 lines (180 loc) · 7.24 KB
/
Copy pathMakefile
File metadata and controls
199 lines (180 loc) · 7.24 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
# RequestBite Proxy Makefile
# Cross-platform build automation for macOS, Linux, and Windows
# Extract version from git tag (strip 'v' prefix), fallback to "dev"
# If on exact tag like v1.2.3, VERSION = 1.2.3
# If ahead of tag, VERSION = 1.2.3-abc1234 (tag-commit)
# If no tags, VERSION = dev
# Use environment variable VERSION if already set (e.g., from CI/CD)
VERSION ?= $(shell if git describe --tags --exact-match 2>/dev/null >/dev/null; then \
git describe --tags --exact-match | sed 's/^v//'; \
else \
git describe --tags 2>/dev/null | sed 's/^v//' | sed 's/-[0-9]\+-g/-/' || echo "dev"; \
fi)
# Binary name
BINARY_NAME := rbite-proxy
# Build metadata
BUILD_TIME := $(shell date -u '+%Y-%m-%d %H:%M:%S UTC')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Build flags
LDFLAGS := -s -w \
-X 'main.Version=$(VERSION)' \
-X 'main.BuildTime=$(BUILD_TIME)' \
-X 'main.GitCommit=$(GIT_COMMIT)'
BUILD_FLAGS := -ldflags="$(LDFLAGS)" -trimpath
# Target platforms
PLATFORMS := \
darwin/amd64 \
darwin/arm64 \
linux/amd64 \
windows/amd64
# Output directories
BUILD_DIR := build
DIST_DIR := dist
# Colors for output
COLOR_RESET := \033[0m
COLOR_BOLD := \033[1m
COLOR_GREEN := \033[32m
COLOR_BLUE := \033[34m
COLOR_YELLOW := \033[33m
.PHONY: all build build-all release clean install dev help
# Default target
all: build
# Build for current platform (development)
build:
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Building $(BINARY_NAME) v$(VERSION) for current platform...$(COLOR_RESET)"
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=0 go build $(BUILD_FLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/requestbite-proxy
@echo "$(COLOR_GREEN)✓ Build complete: $(BUILD_DIR)/$(BINARY_NAME)$(COLOR_RESET)"
# Build for all platforms
build-all: clean
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Building $(BINARY_NAME) v$(VERSION) for all platforms...$(COLOR_RESET)"
@mkdir -p $(BUILD_DIR)
@$(foreach platform,$(PLATFORMS),\
$(call build_platform,$(platform)))
@echo "$(COLOR_GREEN)✓ All builds complete$(COLOR_RESET)"
@echo ""
@echo "Built binaries:"
@ls -lh $(BUILD_DIR)/$(BINARY_NAME)-*
# Build for a specific platform (internal function)
define build_platform
$(eval OS := $(word 1,$(subst /, ,$(1))))
$(eval ARCH := $(word 2,$(subst /, ,$(1))))
$(eval OUTPUT := $(BUILD_DIR)/$(BINARY_NAME)-$(VERSION)-$(OS)-$(ARCH)$(if $(filter windows,$(OS)),.exe,))
@echo " Building for $(OS)/$(ARCH)..."
@CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build $(BUILD_FLAGS) -o $(OUTPUT) ./cmd/requestbite-proxy
endef
# Create release archives and checksums
release: build-all
@echo ""
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Creating release archives...$(COLOR_RESET)"
@mkdir -p $(DIST_DIR)
@cd $(BUILD_DIR) && \
for binary in $(BINARY_NAME)-$(VERSION)-*; do \
if [ -f "$$binary" ]; then \
base=$$(basename "$$binary"); \
os_arch=$$(echo "$$base" | sed 's/$(BINARY_NAME)-$(VERSION)-//'); \
\
if echo "$$os_arch" | grep -q "windows"; then \
archive="$(BINARY_NAME)-$(VERSION)-$${os_arch%.exe}.zip"; \
echo " Creating $$archive..."; \
cp ../LICENSE . 2>/dev/null || true; \
cp ../README.md . 2>/dev/null || true; \
zip -q "../$(DIST_DIR)/$$archive" "$$binary" LICENSE README.md 2>/dev/null || zip -q "../$(DIST_DIR)/$$archive" "$$binary"; \
rm -f LICENSE README.md; \
else \
archive="$(BINARY_NAME)-$(VERSION)-$$os_arch.tar.gz"; \
echo " Creating $$archive..."; \
temp_dir="$(BINARY_NAME)"; \
mkdir -p "$$temp_dir"; \
cp "$$binary" "$$temp_dir/$(BINARY_NAME)"; \
cp ../LICENSE "$$temp_dir/" 2>/dev/null || true; \
cp ../README.md "$$temp_dir/" 2>/dev/null || true; \
tar -czf "../$(DIST_DIR)/$$archive" "$$temp_dir"; \
rm -rf "$$temp_dir"; \
fi; \
fi; \
done
@echo ""
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Generating checksums...$(COLOR_RESET)"
@cd $(DIST_DIR) && \
if command -v shasum >/dev/null 2>&1; then \
shasum -a 256 *.tar.gz *.zip 2>/dev/null > SHA256SUMS; \
else \
sha256sum *.tar.gz *.zip 2>/dev/null > SHA256SUMS; \
fi
@echo "$(COLOR_GREEN)✓ Release archives created$(COLOR_RESET)"
@echo ""
@echo "Release artifacts:"
@ls -lh $(DIST_DIR)/*.tar.gz $(DIST_DIR)/*.zip 2>/dev/null || true
@echo ""
@echo "Checksums (SHA256SUMS):"
@cat $(DIST_DIR)/SHA256SUMS
# Clean build artifacts
clean:
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Cleaning build artifacts...$(COLOR_RESET)"
@rm -rf $(BUILD_DIR)
@rm -rf $(DIST_DIR)
@rm -rf tmp/
@rm -f build-errors.log
@echo "$(COLOR_GREEN)✓ Clean complete$(COLOR_RESET)"
# Install locally for testing (to ~/.local/bin)
install: build
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Installing $(BINARY_NAME) to ~/.local/bin...$(COLOR_RESET)"
@mkdir -p ~/.local/bin
@cp $(BUILD_DIR)/$(BINARY_NAME) ~/.local/bin/
@chmod +x ~/.local/bin/$(BINARY_NAME)
@echo "$(COLOR_GREEN)✓ Installed to ~/.local/bin/$(BINARY_NAME)$(COLOR_RESET)"
@echo ""
@if echo "$$PATH" | grep -q "$$HOME/.local/bin"; then \
echo "Ready to use: $(BINARY_NAME) --version"; \
else \
echo "$(COLOR_YELLOW)Warning:$(COLOR_RESET) ~/.local/bin is not in your PATH"; \
echo "Add to PATH: export PATH=\"\$$HOME/.local/bin:\$$PATH\""; \
fi
# Run with hot reload (requires air)
# Usage: make dev ARGS="--enable-local-files --port 9090"
dev:
@# For --help or --version, run directly without Air
@if echo "$(ARGS)" | grep -qE "(-h|--help|-v|--version)"; then \
echo "$(COLOR_BLUE)Running without hot reload (--help or --version detected)$(COLOR_RESET)"; \
$(MAKE) build > /dev/null 2>&1; \
./$(BUILD_DIR)/$(BINARY_NAME) $(ARGS); \
elif command -v air > /dev/null; then \
air -- $(ARGS); \
else \
echo "Air is not installed. Install it with: go install github.com/air-verse/air@latest"; \
echo "Or run without hot reload using: make build && ./build/$(BINARY_NAME) $(ARGS)"; \
exit 1; \
fi
# Show version
version:
@echo "$(BINARY_NAME) v$(VERSION)"
@echo "Build time: $(BUILD_TIME)"
@echo "Git commit: $(GIT_COMMIT)"
# Show help
help:
@echo "$(COLOR_BOLD)RequestBite Proxy - Build System$(COLOR_RESET)"
@echo ""
@echo "$(COLOR_BOLD)Usage:$(COLOR_RESET)"
@echo " make [target]"
@echo ""
@echo "$(COLOR_BOLD)Targets:$(COLOR_RESET)"
@echo " build - Build for current platform (default)"
@echo " dev - Run with hot reload using Air (for development)"
@echo " build-all - Build for all platforms (darwin/amd64, darwin/arm64, linux/amd64, windows/amd64)"
@echo " release - Build all platforms and create release archives with checksums"
@echo " clean - Remove all build artifacts"
@echo " install - Install locally to ~/.local/bin (for testing)"
@echo " version - Show version information"
@echo " help - Show this help message"
@echo ""
@echo "$(COLOR_BOLD)Examples:$(COLOR_RESET)"
@echo " make build # Quick build for development"
@echo " make dev # Run with hot reload"
@echo " make dev ARGS=\"--enable-local-files\" # Run with CLI arguments"
@echo " make dev ARGS=\"--port 9090\" # Run on custom port"
@echo " make build-all # Build for all platforms"
@echo " make release # Create release archives"
@echo " make install # Install locally"
@echo ""
@echo "$(COLOR_BOLD)Current version:$(COLOR_RESET) $(VERSION)"