-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
146 lines (123 loc) · 5.3 KB
/
Copy pathMakefile
File metadata and controls
146 lines (123 loc) · 5.3 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
## sshc — Makefile
APP := sshc
MAIN_DIR := ./cmd/sshc
GOEXE = $(shell go env GOEXE)
GOPATH = $(shell go env GOPATH)
BINARY := $(APP)$(GOEXE)
GO_TAGS ?= embed_web
# Build metadata
BUILD_TIME := $(shell date +%Y-%m-%dT%H:%M:%S)
GIT_HASH := $(shell git rev-parse --short=8 HEAD 2>/dev/null || echo "unknown")
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null | sed 's/^v//' || echo "dev-$(GIT_HASH)")
LDFLAGS := -s -w \
-X main.Version=$(VERSION) \
-X main.GitCommit=$(GIT_HASH) \
-X 'main.BuildDate=$(BUILD_TIME)'
.PHONY: all build web-dist install run clean clean-dist help latest build-all dump-info latest-yaml build-linux build-linux-arm64 build-darwin build-darwin-arm64 build-windows
DIST_DIR := dist
WEB_DIR := web
# 注:值会经 `echo "description: $(DESCRIPTION)"` 写入 latest.yaml,避免 `;`/`:`/引号等
# shell/YAML 元字符(否则 recipe 展开后会被截断成多条命令)。
DESCRIPTION := small SSH helper CLI for managing hosts, running remote commands.
## all: build (default)
all: build
## build: build Go binary (current platform)
build: web-dist
@mkdir -p $(DIST_DIR)
@echo "🐹 Building Go binary with embedded web assets ($(VERSION) @ $(GIT_HASH))..."
go build -tags "$(GO_TAGS)" -ldflags "$(LDFLAGS)" -o $(DIST_DIR)/$(BINARY) $(MAIN_DIR)
@echo "📦 Compressing binary..."
@upx -6 --no-progress $(DIST_DIR)/$(BINARY)
@echo "✅ Binary: $(DIST_DIR)/$(BINARY) ($$(du -sh $(DIST_DIR)/$(BINARY) | cut -f1))"
## web-dist: build Web UI assets for embedding
web-dist:
npm --prefix $(WEB_DIR) run build
## install: install Go binary to $GOPATH/bin
install: build
@cp $(DIST_DIR)/$(BINARY) $(GOPATH)/bin/$(BINARY)
@echo "✅ Installed to $(GOPATH)/bin/$(BINARY)"
## run: build and run with current directory
run: build
./$(DIST_DIR)/$(BINARY)
# ─── Cross Compilation ────────────────────────────────────────────────────────
## build-all: cross-compile for all platforms
build-all: clean-dist dump-info web-dist build-linux build-linux-arm64 build-darwin build-darwin-arm64 build-windows latest-yaml
ls -lh $(DIST_DIR)
## 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:
@mkdir -p $(DIST_DIR)
@{ \
echo "name: $(APP)"; \
echo "version: $(VERSION)"; \
echo "released_at: $(BUILD_TIME)"; \
echo "description: $(DESCRIPTION)"; \
} > $(DIST_DIR)/latest.yaml
@echo " → $(DIST_DIR)/latest.yaml"
## build-linux: compile for Linux amd64
build-linux: web-dist
@echo "🐧 linux/amd64..."
@mkdir -p $(DIST_DIR)
@GOOS=linux GOARCH=amd64 go build -tags "$(GO_TAGS)" -ldflags "$(LDFLAGS)" -o $(DIST_DIR)/$(APP)-linux-amd64 $(MAIN_DIR)
upx -6 --no-progress $(DIST_DIR)/$(APP)-linux-amd64
chmod +x $(DIST_DIR)/$(APP)-linux-amd64
@echo " → $(DIST_DIR)/$(APP)-linux-amd64"
## build-linux-arm64: compile for Linux arm64
build-linux-arm64: web-dist
@echo "🐧 linux/arm64..."
@mkdir -p $(DIST_DIR)
@GOOS=linux GOARCH=arm64 go build -tags "$(GO_TAGS)" -ldflags "$(LDFLAGS)" -o $(DIST_DIR)/$(APP)-linux-arm64 $(MAIN_DIR)
upx -6 --no-progress $(DIST_DIR)/$(APP)-linux-arm64
chmod +x $(DIST_DIR)/$(APP)-linux-arm64
@echo " → $(DIST_DIR)/$(APP)-linux-arm64"
## build-darwin: compile for macOS amd64
build-darwin: web-dist
@echo "🍎 darwin/amd64..."
@mkdir -p $(DIST_DIR)
@GOOS=darwin GOARCH=amd64 go build -tags "$(GO_TAGS)" -ldflags "$(LDFLAGS)" -o $(DIST_DIR)/$(APP)-darwin-amd64 $(MAIN_DIR)
@echo " → $(DIST_DIR)/$(APP)-darwin-amd64"
## build-darwin-arm64: compile for macOS Apple Silicon
build-darwin-arm64: web-dist
@echo "🍎 darwin/arm64..."
@mkdir -p $(DIST_DIR)
@GOOS=darwin GOARCH=arm64 go build -tags "$(GO_TAGS)" -ldflags "$(LDFLAGS)" -o $(DIST_DIR)/$(APP)-darwin-arm64 $(MAIN_DIR)
# upx -6 --no-progress $(DIST_DIR)/$(APP)-darwin-arm64 # 压缩有问题在 macos 12+
@echo " → $(DIST_DIR)/$(APP)-darwin-arm64"
## build-windows: compile for Windows amd64
build-windows: web-dist
@echo "🪟 windows/amd64..."
@mkdir -p $(DIST_DIR)
@GOOS=windows GOARCH=amd64 go build -tags "$(GO_TAGS)" -ldflags "$(LDFLAGS)" -o $(DIST_DIR)/$(APP)-windows-amd64.exe $(MAIN_DIR)
upx -6 --no-progress $(DIST_DIR)/$(APP)-windows-amd64.exe
@echo " → $(DIST_DIR)/$(APP)-windows-amd64.exe"
.PHONY: release
release: build-all ## Create release archives for all platforms TODO 还未启用的
@echo "Creating release archives..."
@mkdir -p release
@cd $(DIST_DIR) && \
tar -czf ../release/$(APP)-linux-amd64.tar.gz $(APP)-linux-amd64; \
tar -czf ../release/$(APP)-linux-arm64.tar.gz $(APP)-linux-arm64; \
tar -czf ../release/$(APP)-darwin-amd64.tar.gz $(APP)-darwin-amd64; \
tar -czf ../release/$(APP)-darwin-arm64.tar.gz $(APP)-darwin-arm64; \
zip ../release/$(APP)-windows-amd64.zip $(APP)-windows-amd64.exe;
@echo "Release archives created in release/"
## clean: remove build artifacts
clean:
@rm -f $(BINARY)
@rm -rf $(DIST_DIR)
@echo "🧹 Cleaned"
## clean-dist: remove old dist files
clean-dist:
@rm -rf $(DIST_DIR)
@mkdir -p $(DIST_DIR)
@echo "🧹 Cleaned $(DIST_DIR)"
## help: show this help
help:
@echo "gofer Build System"
@echo ""
@grep -E '^## ' $(MAKEFILE_LIST) | sed 's/## / /'