-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
410 lines (351 loc) · 15.6 KB
/
Copy pathMakefile
File metadata and controls
410 lines (351 loc) · 15.6 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
# Project Configuration
NAME := bloco-wallet
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "v0.1.0")
PACKAGE := blocowallet
GO ?= go
OUTPUT_BIN ?= build/${NAME}
GO_FLAGS ?=
GO_TAGS ?= netgo
CGO_ENABLED ?= 1
GIT_REV ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
# Install directory: GOBIN when set (e.g. mise/asdf-managed Go), else GOPATH/bin
GOBIN_DIR ?= $(shell $(GO) env GOBIN 2>/dev/null)
ifeq ($(strip $(GOBIN_DIR)),)
GOBIN_DIR := $(shell $(GO) env GOPATH)/bin
endif
# Container Configuration
IMG_NAME := ghcr.io/italoag/${NAME}
IMAGE := ${IMG_NAME}:${VERSION}
BUILD_PLATFORMS ?= linux/amd64,linux/arm64
# macOS CGO Linker Fix
ifeq ($(shell uname), Darwin)
CGO_LDFLAGS ?= -Wl,-w
else
CGO_LDFLAGS ?=
endif
# Date handling for different OS
SOURCE_DATE_EPOCH ?= $(shell date +%s)
ifeq ($(shell uname), Darwin)
DATE ?= $(shell TZ=UTC date -j -f "%s" ${SOURCE_DATE_EPOCH} +"%Y-%m-%dT%H:%M:%SZ")
else
DATE ?= $(shell date -u -d @${SOURCE_DATE_EPOCH} +"%Y-%m-%dT%H:%M:%SZ")
endif
RUN_GO_ISOLATED = TEST_GOPATH="$$($(GO) env GOPATH)"; TEST_HOME="$$(mktemp -d)"; trap '[ ! -d "$$TEST_HOME" ] || chmod -R u+w "$$TEST_HOME" 2>/dev/null; rm -rf "$$TEST_HOME"' EXIT; export HOME="$$TEST_HOME" XDG_CONFIG_HOME="$$TEST_HOME/.config" XDG_CACHE_HOME="$$TEST_HOME/.cache" APPDATA="$$TEST_HOME/AppData/Roaming" LOCALAPPDATA="$$TEST_HOME/AppData/Local" USERPROFILE="$$TEST_HOME" GOPATH="$$TEST_GOPATH";
# Build matrix for cross-compilation
PLATFORMS := linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64
BUILD_DIR := build
DIST_DIR := dist
CMD_DIR := cmd/blocowallet
# LDFLAGS for version injection
LDFLAGS := -w -s \
-X main.version=${VERSION} \
-X main.commit=${GIT_REV} \
-X main.date=${DATE}
# Colors for output
RED := \033[31m
GREEN := \033[32m
YELLOW := \033[33m
BLUE := \033[34m
PURPLE := \033[35m
CYAN := \033[36m
WHITE := \033[37m
RESET := \033[0m
.PHONY: help
default: help
##@ Build Targets
.PHONY: build
build: ## Build the application for current platform
@echo "$(CYAN)Building ${NAME} for current platform...$(RESET)"
@mkdir -p ${BUILD_DIR}
@CGO_ENABLED=${CGO_ENABLED} go build ${GO_FLAGS} \
-ldflags "${LDFLAGS}" \
-a -tags=${GO_TAGS} \
-o ${OUTPUT_BIN} \
./${CMD_DIR}
@echo "$(GREEN)✓ Build complete: ${OUTPUT_BIN}$(RESET)"
.PHONY: build-static
build-static: ## Build static binary using pure Go SQLite driver
@echo "$(CYAN)Building ${NAME} with pure Go SQLite driver...$(RESET)"
@echo "$(YELLOW)Note: Using modernc.org/sqlite driver for CGO_ENABLED=0 compatibility$(RESET)"
@mkdir -p ${BUILD_DIR}
@CGO_ENABLED=0 go build ${GO_FLAGS} \
-ldflags "${LDFLAGS}" \
-a -tags="${GO_TAGS}" \
-o ${OUTPUT_BIN}-static \
./${CMD_DIR}
@echo "$(GREEN)✓ Static build complete: ${OUTPUT_BIN}-static$(RESET)"
.PHONY: build-all
build-all: clean ## Build functional pure-Go SQLite binaries for all supported platforms
@echo "$(CYAN)Building ${NAME} for all platforms...$(RESET)"
@mkdir -p ${DIST_DIR}
@$(foreach platform,$(PLATFORMS),$(call build_platform,$(platform)))
@echo "$(GREEN)✓ All platform builds complete$(RESET)"
.PHONY: build-linux
build-linux: ## Build for Linux platforms (amd64, arm64)
@echo "$(CYAN)Building ${NAME} for Linux platforms...$(RESET)"
@mkdir -p ${DIST_DIR}
@$(call build_platform,linux/amd64)
@$(call build_platform,linux/arm64)
@echo "$(GREEN)✓ Linux builds complete$(RESET)"
.PHONY: build-darwin
build-darwin: ## Build for macOS platforms (amd64, arm64)
@echo "$(CYAN)Building ${NAME} for macOS platforms...$(RESET)"
@mkdir -p ${DIST_DIR}
@$(call build_platform,darwin/amd64)
@$(call build_platform,darwin/arm64)
@echo "$(GREEN)✓ macOS builds complete$(RESET)"
.PHONY: build-windows
build-windows: ## Build for Windows platform (amd64)
@echo "$(CYAN)Building ${NAME} for Windows...$(RESET)"
@mkdir -p ${DIST_DIR}
@$(call build_platform,windows/amd64)
@echo "$(GREEN)✓ Windows build complete$(RESET)"
##@ Testing Targets
.PHONY: test
test: ## Run all tests with optimized parameters
@echo "$(CYAN)Running tests with fast parameters...$(RESET)"
@$(GO) clean --testcache
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./... -v -race -count=1 -shuffle=on
@echo "$(GREEN)✓ Tests complete$(RESET)"
.PHONY: test-fast
test-fast: ## Run tests with fastest parameters (development)
@echo "$(CYAN)Running fast tests for development...$(RESET)"
@$(GO) clean --testcache
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./... -short -v -count=1 -shuffle=on
@echo "$(GREEN)✓ Fast tests complete$(RESET)"
# Alias for common development workflow
.PHONY: t
t: test-fast ## Alias for test-fast (quick development testing)
.PHONY: test-production
test-production: ## Run tests with production-grade security parameters
@echo "$(CYAN)Running tests with production parameters...$(RESET)"
@echo "$(YELLOW)⚠️ This will take longer due to secure scrypt parameters$(RESET)"
@$(GO) clean --testcache
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./... -v -race -count=1 -shuffle=on -tags=production -timeout=25m
@echo "$(GREEN)✓ Production tests complete$(RESET)"
.PHONY: test-quiet
test-quiet: ## Run tests suppressing CGO linker warnings
@echo "$(CYAN)Running tests (quiet mode)...$(RESET)"
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./... -v -race -count=1 -shuffle=on 2>/dev/null || \
CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./... -v -race -count=1 -shuffle=on
@echo "$(GREEN)✓ Tests complete$(RESET)"
.PHONY: cover
cover: ## Run test coverage suite
@echo "$(CYAN)Generating test coverage...$(RESET)"
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./... -count=1 -shuffle=on --coverprofile=coverage.out
@$(GO) tool cover --html=coverage.out -o coverage.html
@echo "$(GREEN)✓ Coverage report generated: coverage.html$(RESET)"
.PHONY: cover-production
cover-production: ## Run test coverage with production parameters
@echo "$(CYAN)Generating test coverage with production parameters...$(RESET)"
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./... -count=1 -shuffle=on --coverprofile=coverage.out -tags=production
@$(GO) tool cover --html=coverage.out -o coverage.html
@echo "$(GREEN)✓ Production coverage report generated: coverage.html$(RESET)"
.PHONY: bench
bench: ## Run benchmarks
@echo "$(CYAN)Running benchmarks...$(RESET)"
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./... -bench=. -benchmem
@echo "$(GREEN)✓ Benchmarks complete$(RESET)"
.PHONY: test-wallet
test-wallet: ## Run only wallet package tests (fastest)
@echo "$(CYAN)Running wallet package tests...$(RESET)"
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./internal/wallet/... -v -count=1 -shuffle=on
@echo "$(GREEN)✓ Wallet tests complete$(RESET)"
.PHONY: test-ui
test-ui: ## Run only UI package tests
@echo "$(CYAN)Running UI package tests...$(RESET)"
@$(RUN_GO_ISOLATED) CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test ./internal/ui/... -v -count=1 -shuffle=on
@echo "$(GREEN)✓ UI tests complete$(RESET)"
##@ Code Quality Targets
.PHONY: lint
lint: ## Run linting checks
@echo "$(CYAN)Running linter...$(RESET)"
@if command -v golangci-lint >/dev/null 2>&1; then \
golangci-lint run --timeout=5m; \
else \
echo "$(YELLOW)⚠ golangci-lint not installed, running go vet instead$(RESET)"; \
go vet ./...; \
fi
@echo "$(GREEN)✓ Linting complete$(RESET)"
.PHONY: fmt
fmt: ## Format code
@echo "$(CYAN)Formatting code...$(RESET)"
@go fmt ./...
@echo "$(GREEN)✓ Code formatted$(RESET)"
.PHONY: vet
vet: ## Run go vet
@echo "$(CYAN)Running go vet...$(RESET)"
@go vet ./...
@echo "$(GREEN)✓ Vet complete$(RESET)"
.PHONY: mod
mod: ## Tidy and download modules
@echo "$(CYAN)Tidying modules...$(RESET)"
@go mod tidy
@go mod download
@echo "$(GREEN)✓ Modules updated$(RESET)"
##@ Container Targets
.PHONY: docker-build
docker-build: ## Build multi-platform Docker images
@echo "$(CYAN)Building Docker images for platforms: ${BUILD_PLATFORMS}$(RESET)"
@docker buildx build \
--platform ${BUILD_PLATFORMS} \
--build-arg VERSION=${VERSION} \
--build-arg GIT_REV=${GIT_REV} \
--build-arg BUILD_DATE=${DATE} \
--rm -t ${IMAGE} \
--load .
@echo "$(GREEN)✓ Docker images built$(RESET)"
.PHONY: docker-push
docker-push: ## Push Docker images to registry
@echo "$(CYAN)Pushing Docker images to registry...$(RESET)"
@docker buildx build \
--platform ${BUILD_PLATFORMS} \
--build-arg VERSION=${VERSION} \
--build-arg GIT_REV=${GIT_REV} \
--build-arg BUILD_DATE=${DATE} \
--rm -t ${IMAGE} \
--push .
@echo "$(GREEN)✓ Docker images pushed$(RESET)"
.PHONY: docker-run
docker-run: ## Run the container locally
@echo "$(CYAN)Running container locally...$(RESET)"
@docker run --rm -it ${IMAGE}
##@ Release Targets
.PHONY: release-prep
release-prep: clean build-all checksums ## Prepare release artifacts
@echo "$(GREEN)✓ Release artifacts prepared in ${DIST_DIR}$(RESET)"
.PHONY: release-local
release-local: release-prep ## Create local release package
@echo "$(CYAN)Creating local release package...$(RESET)"
@mkdir -p ${DIST_DIR}/release
@cd ${DIST_DIR} && tar -czf release/${NAME}-${VERSION}-release.tar.gz *.tar.gz *.zip checksums.txt
@echo "$(GREEN)✓ Local release package created: ${DIST_DIR}/release/${NAME}-${VERSION}-release.tar.gz$(RESET)"
.PHONY: checksums
checksums: ## Generate checksums for release artifacts
@echo "$(CYAN)Generating checksums...$(RESET)"
@cd ${DIST_DIR} && \
find . -name "*.tar.gz" -o -name "*.zip" | \
xargs shasum -a 256 > checksums.txt
@echo "$(GREEN)✓ Checksums generated: ${DIST_DIR}/checksums.txt$(RESET)"
##@ Utility Targets
.PHONY: clean
clean: ## Clean build artifacts
@echo "$(CYAN)Cleaning build artifacts...$(RESET)"
@rm -rf ${BUILD_DIR} ${DIST_DIR}
@rm -f coverage.out coverage.html
@echo "$(GREEN)✓ Clean complete$(RESET)"
.PHONY: deps
deps: ## Install build dependencies
@echo "$(CYAN)Installing dependencies...$(RESET)"
@go mod download
@if ! command -v golangci-lint >/dev/null 2>&1; then \
echo "$(YELLOW)Installing golangci-lint...$(RESET)"; \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.61.0; \
fi
@echo "$(GREEN)✓ Dependencies installed$(RESET)"
.PHONY: install
install: build ## Install the binary to GOBIN (or GOPATH/bin)
@echo "$(CYAN)Installing ${NAME} to ${GOBIN_DIR}...$(RESET)"
@mkdir -p ${GOBIN_DIR}
@cp ${OUTPUT_BIN} ${GOBIN_DIR}/${NAME}
@echo "$(GREEN)✓ ${NAME} installed to ${GOBIN_DIR}${RESET}"
.PHONY: uninstall
uninstall: ## Remove the binary from GOBIN (or GOPATH/bin)
@echo "$(CYAN)Uninstalling ${NAME}...$(RESET)"
@rm -f ${GOBIN_DIR}/${NAME}
@echo "$(GREEN)✓ ${NAME} uninstalled${RESET}"
.PHONY: version
version: ## Show version information
@echo "Name: ${NAME}"
@echo "Version: ${VERSION}"
@echo "Git Commit: ${GIT_REV}"
@echo "Git Branch: ${GIT_BRANCH}"
@echo "Build Date: ${DATE}"
@echo "Go Version: $$(go version)"
##@ Release Gates
.PHONY: release-smoke
release-smoke: build ## Smoke-test the real artifact and validate the version banner
@echo "$(CYAN)Smoke-testing ${OUTPUT_BIN}...$(RESET)"
@${OUTPUT_BIN} --version | grep -q "bloco-wallet-manager version" || (echo "$(RED)✗ version banner missing$(RESET)"; exit 1)
@${OUTPUT_BIN} --version | grep -q "${VERSION}" || (echo "$(RED)✗ version banner does not match ${VERSION}$(RESET)"; exit 1)
@echo "$(GREEN)✓ Artifact runs and reports version $(VERSION)$(RESET)"
@if ls ${DIST_DIR}/*.tar.gz ${DIST_DIR}/*.zip >/dev/null 2>&1; then \
echo "$(CYAN)Smoke-testing promoted artifacts...$(RESET)"; \
for archive in ${DIST_DIR}/*.tar.gz ${DIST_DIR}/*.zip; do \
shasum -a 256 "$$archive" | grep -q "^" || exit 1; \
done; \
@echo "$(GREEN)✓ Promoted artifacts present and checksummed$(RESET)"; \
fi
.PHONY: sbom
sbom: ## Generate a CycloneDX-style SBOM from the module graph
@echo "$(CYAN)Generating SBOM...$(RESET)"
@mkdir -p ${DIST_DIR}
@go list -m all | python3 -c 'import sys,time,json; modules=[(p[0],p[1]) for l in sys.stdin if l.strip() for p in [l.split()] if len(p) >= 2 and p[1] != "=>"]; doc={"bomFormat":"CycloneDX","specVersion":"1.5","version":1,"metadata":{"timestamp":time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime()),"component":{"type":"application","name":"${NAME}","version":"${VERSION}"}},"components":[{"type":"library","name":m[0],"version":m[1]} for m in sorted(modules)]}; open("${DIST_DIR}/sbom.json","w").write(json.dumps(doc,indent=2))'
@echo "$(GREEN)✓ SBOM generated: ${DIST_DIR}/sbom.json$(RESET)"
.PHONY: release-check
release-check: test test-production lint release-smoke sbom ## All release gates before promotion
@echo "$(GREEN)✓ Release gates passed$(RESET)"
.PHONY: info
info: ## Show build environment information
@echo "$(CYAN)Build Environment Information:$(RESET)"
@echo "Name: ${NAME}"
@echo "Version: ${VERSION}"
@echo "Package: ${PACKAGE}"
@echo "Git Commit: ${GIT_REV}"
@echo "Git Branch: ${GIT_BRANCH}"
@echo "Build Date: ${DATE}"
@echo "Go Version: $$(go version)"
@echo "Go Env GOOS: $$(go env GOOS)"
@echo "Go Env GOARCH: $$(go env GOARCH)"
@echo "CGO Enabled: ${CGO_ENABLED}"
@echo "Build Tags: ${GO_TAGS}"
@echo "Platforms: $(PLATFORMS)"
##@ Help
.PHONY: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\n$(CYAN)Usage:$(RESET)\n make $(YELLOW)<target>$(RESET)\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " $(YELLOW)%-15s$(RESET) %s\n", $$1, $$2 } /^##@/ { printf "\n$(CYAN)%s$(RESET)\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
# Build function for pure-Go cross-compilation
define build_platform
$(eval GOOS := $(word 1,$(subst /, ,$(1))))
$(eval GOARCH := $(word 2,$(subst /, ,$(1))))
$(eval EXT := $(if $(filter windows,$(GOOS)),.exe,))
$(eval OUTPUT := ${DIST_DIR}/${NAME}-${VERSION}-$(GOOS)-$(GOARCH)$(EXT))
$(eval ARCHIVE := $(if $(filter windows,$(GOOS)),${DIST_DIR}/${NAME}-${VERSION}-$(GOOS)-$(GOARCH).zip,${DIST_DIR}/${NAME}-${VERSION}-$(GOOS)-$(GOARCH).tar.gz))
@echo "$(BLUE) Building for $(GOOS)/$(GOARCH) (pure Go SQLite)...$(RESET)"
@CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) \
go build ${GO_FLAGS} \
-ldflags "${LDFLAGS}" \
-a -tags="${GO_TAGS},nocgo" \
-o $(OUTPUT) \
./${CMD_DIR}
@if [ "$(GOOS)" = "windows" ]; then \
cd ${DIST_DIR} && zip -q $(notdir $(ARCHIVE)) $(notdir $(OUTPUT)); \
else \
cd ${DIST_DIR} && tar -czf $(notdir $(ARCHIVE)) $(notdir $(OUTPUT)); \
fi
@rm -f $(OUTPUT)
@echo "$(GREEN) ✓ $(GOOS)/$(GOARCH) → $(notdir $(ARCHIVE))$(RESET)"
endef
# Build function for static cross-compilation (CGO_ENABLED=0)
define build_platform_static
$(eval GOOS := $(word 1,$(subst /, ,$(1))))
$(eval GOARCH := $(word 2,$(subst /, ,$(1))))
$(eval EXT := $(if $(filter windows,$(GOOS)),.exe,))
$(eval OUTPUT := ${DIST_DIR}/${NAME}-${VERSION}-$(GOOS)-$(GOARCH)-static$(EXT))
$(eval ARCHIVE := $(if $(filter windows,$(GOOS)),${DIST_DIR}/${NAME}-${VERSION}-$(GOOS)-$(GOARCH)-static.zip,${DIST_DIR}/${NAME}-${VERSION}-$(GOOS)-$(GOARCH)-static.tar.gz))
@echo "$(BLUE) Building for $(GOOS)/$(GOARCH) (static)...$(RESET)"
@CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) \
go build ${GO_FLAGS} \
-ldflags "${LDFLAGS}" \
-a -tags="${GO_TAGS},nocgo" \
-o $(OUTPUT) \
./${CMD_DIR}
@if [ "$(GOOS)" = "windows" ]; then \
cd ${DIST_DIR} && zip -q $(notdir $(ARCHIVE)) $(notdir $(OUTPUT)); \
else \
cd ${DIST_DIR} && tar -czf $(notdir $(ARCHIVE)) $(notdir $(OUTPUT)); \
fi
@rm -f $(OUTPUT)
@echo "$(GREEN) ✓ $(GOOS)/$(GOARCH) (static) → $(notdir $(ARCHIVE))$(RESET)"
endef