-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
58 lines (50 loc) · 1.39 KB
/
Copy pathMakefile
File metadata and controls
58 lines (50 loc) · 1.39 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
# 设置变量
BINARY=tscan
VERSION=1.0.0
BUILD_TIME=$(shell date +%F-%H-%M-%S)
BUILD_DIR=build
LDFLAGS=-ldflags "-s -w -X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME}"
# 支持的平台
PLATFORMS=linux-amd64 linux-arm64 darwin-amd64 darwin-arm64 windows-amd64
# 默认目标
.PHONY: all
all: clean build-all
# 清理构建目录
.PHONY: clean
clean:
@rm -rf ${BUILD_DIR}
@mkdir -p ${BUILD_DIR}
# 构建所有平台
.PHONY: build-all
build-all: $(PLATFORMS)
# 构建单个平台
.PHONY: $(PLATFORMS)
$(PLATFORMS):
$(eval GOOS := $(word 1,$(subst -, ,$@)))
$(eval GOARCH := $(word 2,$(subst -, ,$@)))
@echo "Building for ${GOOS}-${GOARCH}..."
@if [ "${GOOS}" = "windows" ]; then \
GOOS=${GOOS} GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BUILD_DIR}/${BINARY}-${GOOS}-${GOARCH}.exe .; \
else \
GOOS=${GOOS} GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BUILD_DIR}/${BINARY}-${GOOS}-${GOARCH} .; \
fi
# 仅构建当前平台
.PHONY: build
build:
@go build ${LDFLAGS} -o ${BUILD_DIR}/${BINARY} .
# 运行测试
.PHONY: test
test:
@go test -v ./...
# 帮助信息
.PHONY: help
help:
@echo "支持的命令:"
@echo "make - 清理并构建所有平台"
@echo "make clean - 清理构建目录"
@echo "make build - 构建当前平台"
@echo "make test - 运行测试"
@echo "make build-all- 构建所有平台"
@echo ""
@echo "支持的平台:"
@echo $(PLATFORMS) | tr ' ' '\n'