-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
422 lines (370 loc) · 13.2 KB
/
Makefile
File metadata and controls
422 lines (370 loc) · 13.2 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Sendry Makefile
# Ensure Go bin is in PATH for tools like nfpm
export PATH := $(PATH):$(HOME)/go/bin
# Variables
BINARY_NAME=sendry
BINARY_WEB=sendry-web
VERSION?=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOTEST=$(GOCMD) test
GOVET=$(GOCMD) vet
GOMOD=$(GOCMD) mod
GOFMT=gofmt
# Build flags
LDFLAGS=-ldflags "-s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.buildTime=$(BUILD_TIME)"
# Directories
BUILD_DIR=build
CMD_DIR=./cmd/sendry
CMD_WEB_DIR=./cmd/sendry-web
# Default target
.PHONY: all
all: build build-web
# Build the main binary
.PHONY: build
build:
@echo "Building $(BINARY_NAME)..."
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(CMD_DIR)
@echo "Binary built: $(BUILD_DIR)/$(BINARY_NAME)"
# Build the web binary (requires CGO for SQLite)
.PHONY: build-web
build-web:
@echo "Building $(BINARY_WEB)..."
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=1 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_WEB) $(CMD_WEB_DIR)
@echo "Binary built: $(BUILD_DIR)/$(BINARY_WEB)"
# Build for all platforms
.PHONY: build-all
build-all: build-linux build-linux-arm64 build-darwin build-darwin-arm64
.PHONY: build-linux
build-linux:
@echo "Building for Linux amd64..."
@mkdir -p $(BUILD_DIR)
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 $(CMD_DIR)
.PHONY: build-linux-arm64
build-linux-arm64:
@echo "Building for Linux arm64..."
@mkdir -p $(BUILD_DIR)
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 $(CMD_DIR)
# Build web for Linux (requires CGO for SQLite, uses Docker for cross-compilation)
.PHONY: build-web-linux
build-web-linux:
@echo "Building $(BINARY_WEB) for Linux amd64..."
@mkdir -p $(BUILD_DIR)
docker run --rm -v $(PWD):/app -w /app golang:1.23-bookworm \
bash -c "apt-get update && apt-get install -y gcc && \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_WEB)-linux-amd64 $(CMD_WEB_DIR)"
.PHONY: build-web-linux-arm64
build-web-linux-arm64:
@echo "Building $(BINARY_WEB) for Linux arm64..."
@mkdir -p $(BUILD_DIR)
docker run --rm -v $(PWD):/app -w /app --platform linux/arm64 golang:1.23-bookworm \
bash -c "apt-get update && apt-get install -y gcc && \
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_WEB)-linux-arm64 $(CMD_WEB_DIR)"
.PHONY: build-darwin
build-darwin:
@echo "Building for macOS amd64..."
@mkdir -p $(BUILD_DIR)
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 $(CMD_DIR)
.PHONY: build-darwin-arm64
build-darwin-arm64:
@echo "Building for macOS arm64..."
@mkdir -p $(BUILD_DIR)
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(CMD_DIR)
# Run tests
.PHONY: test
test:
@echo "Running tests..."
$(GOTEST) -v ./...
# Run tests with coverage
.PHONY: test-cover
test-cover:
@echo "Running tests with coverage..."
@mkdir -p $(BUILD_DIR)
$(GOTEST) -v -coverprofile=$(BUILD_DIR)/coverage.out ./...
$(GOCMD) tool cover -html=$(BUILD_DIR)/coverage.out -o $(BUILD_DIR)/coverage.html
@echo "Coverage report: $(BUILD_DIR)/coverage.html"
# Run tests with race detector
.PHONY: test-race
test-race:
@echo "Running tests with race detector..."
$(GOTEST) -v -race ./...
# Run short tests only
.PHONY: test-short
test-short:
@echo "Running short tests..."
$(GOTEST) -v -short ./...
# Run tests for sendry-web only
.PHONY: test-web
test-web:
@echo "Running sendry-web tests..."
$(GOTEST) -v ./internal/web/...
# Benchmark tests
.PHONY: bench
bench:
@echo "Running benchmarks..."
$(GOTEST) -bench=. -benchmem ./...
# Vet the code
.PHONY: vet
vet:
@echo "Running go vet..."
$(GOVET) ./...
# Format the code
.PHONY: fmt
fmt:
@echo "Formatting code..."
$(GOFMT) -s -w .
# Check formatting
.PHONY: fmt-check
fmt-check:
@echo "Checking code formatting..."
@diff=$$($(GOFMT) -s -d .); \
if [ -n "$$diff" ]; then \
echo "Code is not formatted:"; \
echo "$$diff"; \
exit 1; \
fi
@echo "Code formatting OK"
# Lint (requires golangci-lint)
.PHONY: lint
lint:
@echo "Running linter..."
@which golangci-lint > /dev/null || (echo "golangci-lint not installed" && 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
# Verify dependencies
.PHONY: verify
verify:
@echo "Verifying dependencies..."
$(GOMOD) verify
# Clean build artifacts
.PHONY: clean
clean:
@echo "Cleaning..."
@rm -rf $(BUILD_DIR)
@rm -f $(BINARY_NAME)
@echo "Clean complete"
# Install binary to GOPATH/bin
.PHONY: install
install: build
@echo "Installing $(BINARY_NAME)..."
@cp $(BUILD_DIR)/$(BINARY_NAME) $(GOPATH)/bin/$(BINARY_NAME)
@echo "Installed to $(GOPATH)/bin/$(BINARY_NAME)"
# Run the application (for development)
.PHONY: run
run: build
@echo "Running $(BINARY_NAME)..."
$(BUILD_DIR)/$(BINARY_NAME) serve -c configs/sendry.example.yaml
# Generate DKIM key for testing
.PHONY: dkim-gen
dkim-gen: build
@echo "Generating DKIM key..."
@mkdir -p $(BUILD_DIR)/keys
$(BUILD_DIR)/$(BINARY_NAME) dkim generate --domain example.com --selector sendry --out $(BUILD_DIR)/keys
# Run all checks (for CI)
.PHONY: ci
ci: deps fmt-check vet test
# Full check (format, vet, lint, test)
.PHONY: check
check: fmt vet test
# Show version info
.PHONY: version
version:
@echo "Version: $(VERSION)"
@echo "Commit: $(COMMIT)"
@echo "Build Time: $(BUILD_TIME)"
# Docker image name
DOCKER_IMAGE=sendry
DOCKER_TAG?=$(VERSION)
# Docker build
.PHONY: docker-build
docker-build:
@echo "Building Docker image..."
docker build \
--build-arg VERSION=$(VERSION) \
--build-arg COMMIT=$(COMMIT) \
--build-arg BUILD_TIME=$(BUILD_TIME) \
-t $(DOCKER_IMAGE):$(DOCKER_TAG) \
-t $(DOCKER_IMAGE):latest \
.
@echo "Docker image built: $(DOCKER_IMAGE):$(DOCKER_TAG)"
# Docker build with no cache
.PHONY: docker-build-nc
docker-build-nc:
@echo "Building Docker image (no cache)..."
docker build --no-cache \
--build-arg VERSION=$(VERSION) \
--build-arg COMMIT=$(COMMIT) \
--build-arg BUILD_TIME=$(BUILD_TIME) \
-t $(DOCKER_IMAGE):$(DOCKER_TAG) \
-t $(DOCKER_IMAGE):latest \
.
# Docker build from APK package (smaller image)
.PHONY: docker-build-apk
docker-build-apk: package-apk
@echo "Building Docker image from APK package..."
docker build \
-f Dockerfile.apk \
-t $(DOCKER_IMAGE):$(DOCKER_TAG) \
-t $(DOCKER_IMAGE):latest \
.
@echo "Docker image built: $(DOCKER_IMAGE):$(DOCKER_TAG)"
# Docker run
.PHONY: docker-run
docker-run:
@echo "Running Docker container..."
docker run --rm -it \
-p 25:25 -p 587:587 -p 8080:8080 \
-v $(PWD)/configs/sendry.example.yaml:/etc/sendry/config.yaml:ro \
$(DOCKER_IMAGE):latest
# Docker compose up
.PHONY: docker-up
docker-up:
VERSION=$(VERSION) COMMIT=$(COMMIT) BUILD_TIME=$(BUILD_TIME) \
docker compose up -d --build
# Docker compose down
.PHONY: docker-down
docker-down:
docker compose down
# Docker compose logs
.PHONY: docker-logs
docker-logs:
docker compose logs -f
# Build DEB package (requires nfpm)
.PHONY: package-deb
package-deb: build-linux build-web-linux
@echo "Building DEB package..."
@which nfpm > /dev/null || (echo "nfpm not installed. Install: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@latest" && exit 1)
@mkdir -p $(BUILD_DIR)/packages
@sed 's/$${ARCH}/amd64/g; s/$${VERSION}/$(VERSION)/g' nfpm.yaml > $(BUILD_DIR)/nfpm-amd64.yaml
nfpm package --config $(BUILD_DIR)/nfpm-amd64.yaml --packager deb --target $(BUILD_DIR)/packages/
@echo "DEB package built in $(BUILD_DIR)/packages/"
# Build DEB package for arm64
.PHONY: package-deb-arm64
package-deb-arm64: build-linux-arm64 build-web-linux-arm64
@echo "Building DEB package for arm64..."
@which nfpm > /dev/null || (echo "nfpm not installed" && exit 1)
@mkdir -p $(BUILD_DIR)/packages
@sed 's/$${ARCH}/arm64/g; s/$${VERSION}/$(VERSION)/g' nfpm.yaml > $(BUILD_DIR)/nfpm-arm64.yaml
nfpm package --config $(BUILD_DIR)/nfpm-arm64.yaml --packager deb --target $(BUILD_DIR)/packages/
@echo "DEB package built in $(BUILD_DIR)/packages/"
# Build RPM package (requires nfpm)
.PHONY: package-rpm
package-rpm: build-linux build-web-linux
@echo "Building RPM package..."
@which nfpm > /dev/null || (echo "nfpm not installed. Install: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@latest" && exit 1)
@mkdir -p $(BUILD_DIR)/packages
@sed 's/$${ARCH}/amd64/g; s/$${VERSION}/$(VERSION)/g' nfpm.yaml > $(BUILD_DIR)/nfpm-amd64.yaml
nfpm package --config $(BUILD_DIR)/nfpm-amd64.yaml --packager rpm --target $(BUILD_DIR)/packages/
@echo "RPM package built in $(BUILD_DIR)/packages/"
# Build RPM package for arm64
.PHONY: package-rpm-arm64
package-rpm-arm64: build-linux-arm64 build-web-linux-arm64
@echo "Building RPM package for arm64..."
@which nfpm > /dev/null || (echo "nfpm not installed" && exit 1)
@mkdir -p $(BUILD_DIR)/packages
@sed 's/$${ARCH}/arm64/g; s/$${VERSION}/$(VERSION)/g' nfpm.yaml > $(BUILD_DIR)/nfpm-arm64.yaml
nfpm package --config $(BUILD_DIR)/nfpm-arm64.yaml --packager rpm --target $(BUILD_DIR)/packages/
@echo "RPM package built in $(BUILD_DIR)/packages/"
# Build APK package for Alpine (amd64)
.PHONY: package-apk
package-apk: build-linux build-web-linux
@echo "Building APK package..."
@which nfpm > /dev/null || (echo "nfpm not installed. Install: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@latest" && exit 1)
@mkdir -p $(BUILD_DIR)/packages
@sed 's/$${ARCH}/amd64/g; s/$${VERSION}/$(VERSION)/g' nfpm.yaml > $(BUILD_DIR)/nfpm-amd64.yaml
nfpm package --config $(BUILD_DIR)/nfpm-amd64.yaml --packager apk --target $(BUILD_DIR)/packages/
@echo "APK package built in $(BUILD_DIR)/packages/"
# Build APK package for Alpine (arm64)
.PHONY: package-apk-arm64
package-apk-arm64: build-linux-arm64 build-web-linux-arm64
@echo "Building APK package for arm64..."
@which nfpm > /dev/null || (echo "nfpm not installed" && exit 1)
@mkdir -p $(BUILD_DIR)/packages
@sed 's/$${ARCH}/arm64/g; s/$${VERSION}/$(VERSION)/g' nfpm.yaml > $(BUILD_DIR)/nfpm-arm64.yaml
nfpm package --config $(BUILD_DIR)/nfpm-arm64.yaml --packager apk --target $(BUILD_DIR)/packages/
@echo "APK package built in $(BUILD_DIR)/packages/"
# Build all packages
.PHONY: package-all
package-all: package-deb package-deb-arm64 package-rpm package-rpm-arm64 package-apk package-apk-arm64
@echo "All packages built in $(BUILD_DIR)/packages/"
# Full release (binaries + packages + docker)
.PHONY: release
release: clean build-all package-all docker-build
@echo ""
@echo "Release $(VERSION) complete!"
@echo "Binaries: $(BUILD_DIR)/"
@echo "Packages: $(BUILD_DIR)/packages/"
@echo "Docker: $(DOCKER_IMAGE):$(DOCKER_TAG)"
# Help
.PHONY: help
help:
@echo "Sendry Makefile"
@echo ""
@echo "Usage: make [target]"
@echo ""
@echo "Build targets:"
@echo " build Build the main binary (sendry)"
@echo " build-web Build the web binary (sendry-web)"
@echo " build-all Build for all platforms (linux, darwin, amd64, arm64)"
@echo " build-web-linux Build sendry-web for Linux amd64 (via Docker)"
@echo " build-web-linux-arm64 Build sendry-web for Linux arm64 (via Docker)"
@echo " clean Clean build artifacts"
@echo " install Install binary to GOPATH/bin"
@echo ""
@echo "Test targets:"
@echo " test Run all tests"
@echo " test-web Run sendry-web tests only"
@echo " test-cover Run tests with coverage report"
@echo " test-race Run tests with race detector"
@echo " bench Run benchmarks"
@echo ""
@echo "Code quality:"
@echo " fmt Format code"
@echo " fmt-check Check code formatting"
@echo " vet Run go vet"
@echo " lint Run linter (requires golangci-lint)"
@echo " check Run all checks (fmt, vet, test)"
@echo " ci Run CI checks"
@echo ""
@echo "Docker targets:"
@echo " docker-build Build Docker image (multi-stage)"
@echo " docker-build-apk Build Docker image from APK package"
@echo " docker-build-nc Build Docker image (no cache)"
@echo " docker-run Run Docker container"
@echo " docker-up Docker compose up"
@echo " docker-down Docker compose down"
@echo " docker-logs Docker compose logs"
@echo ""
@echo "Package targets:"
@echo " package-deb Build DEB package (amd64)"
@echo " package-deb-arm64 Build DEB package (arm64)"
@echo " package-rpm Build RPM package (amd64)"
@echo " package-rpm-arm64 Build RPM package (arm64)"
@echo " package-apk Build APK package for Alpine (amd64)"
@echo " package-apk-arm64 Build APK package for Alpine (arm64)"
@echo " package-all Build all packages"
@echo ""
@echo "Release:"
@echo " release Full release (binaries + packages + docker)"
@echo ""
@echo "Other:"
@echo " run Build and run the application"
@echo " dkim-gen Generate test DKIM key"
@echo " deps Download dependencies"
@echo " tidy Tidy dependencies"
@echo " version Show version info"
@echo " help Show this help"