-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
84 lines (71 loc) · 2.02 KB
/
Makefile
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
# 变量定义
APP_NAME = starter
VERSION ?= $(shell git describe --tags --always)
BUILD_DIR = build
# 默认架构
ARCH ?= amd64
# 编译参数
LDFLAGS = -s -w \
-X main.Version=$(VERSION) \
-X main.BuildTime=$(shell date -u +%Y-%m-%d_%H:%M:%S)
# 默认目标
.PHONY: all
all: linux
# 创建构建目录
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
# 构建 Linux 版本
.PHONY: linux
linux: $(BUILD_DIR)
@echo "Building for Linux ($(ARCH))..."
GOOS=linux GOARCH=$(ARCH) go build -ldflags="$(LDFLAGS)" \
-o $(BUILD_DIR)/$(APP_NAME)-linux-$(ARCH)
# 构建 Windows 版本
.PHONY: windows
windows: $(BUILD_DIR)
@echo "Building for Windows ($(ARCH))..."
GOOS=windows GOARCH=$(ARCH) go build -ldflags="$(LDFLAGS)" \
-o $(BUILD_DIR)/$(APP_NAME)-windows-$(ARCH).exe
# 构建 MacOS 版本
.PHONY: darwin
darwin: $(BUILD_DIR)
@echo "Building for MacOS ($(ARCH))..."
GOOS=darwin GOARCH=$(ARCH) go build -ldflags="$(LDFLAGS)" \
-o $(BUILD_DIR)/$(APP_NAME)-darwin-$(ARCH)
# 构建所有平台 amd64 版本
.PHONY: all-arch
all-arch: linux windows darwin
# 构建所有平台 arm64 版本
.PHONY: all-arm64
all-arm64: ARCH=arm64
all-arm64: linux windows darwin
# 清理构建产物
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)
# 运行测试
.PHONY: test
test:
go test -v ./...
# 运行程序
.PHONY: run
run:
go run main.go
# 帮助信息
.PHONY: help
help:
@echo "Make targets:"
@echo " linux - Build for Linux (ARCH=amd64/arm64)"
@echo " windows - Build for Windows (ARCH=amd64/arm64)"
@echo " darwin - Build for MacOS (ARCH=amd64/arm64)"
@echo " all-arch - Build for all platforms (amd64)"
@echo " all-arm64 - Build for all platforms (arm64)"
@echo " clean - Clean build directory"
@echo " test - Run tests"
@echo " run - Run the application"
@echo ""
@echo "Examples:"
@echo " make linux ARCH=arm64 - Build Linux arm64 version"
@echo " make darwin ARCH=arm64 - Build MacOS arm64 version"
@echo " make all-arch - Build all platforms in amd64"
@echo " make all-arm64 - Build all platforms in arm64"