From 8ecb48e2463ce35f880837d8260d69c435ea5a84 Mon Sep 17 00:00:00 2001 From: xfy Date: Thu, 9 Apr 2026 14:54:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(build):=20=E4=B8=BA=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E5=91=BD=E4=BB=A4=E5=90=AF=E7=94=A8=E9=9D=99?= =?UTF-8?q?=E6=80=81=E9=93=BE=E6=8E=A5=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 CGO_DISABLE 变量(CGO_ENABLED=0) - 所有构建目标(build、build-prod、build-perf、build-pgo)均使用静态链接 - 跨平台构建(linux、darwin、windows)同样启用静态链接 - 更新 help 文案反映静态构建特性 静态链接确保二进制文件无外部依赖,便于容器化部署。 Co-Authored-By: Claude Opus 4.6 --- Makefile | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 9b1b638..4d66d01 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,9 @@ GO_VERSION := $(shell go version | awk '{print $$3}') BUILD_PLATFORM := $(shell go env GOOS)/$(shell go env GOARCH) BUILD_DIR := bin +# 静态构建(禁用 CGO) +CGO_DISABLE := CGO_ENABLED=0 + # 生产构建标志(体积优化) LDFLAGS := -ldflags "-s -w \ -X 'rua.plus/lolly/internal/app.Version=$(VERSION)' \ @@ -33,36 +36,36 @@ MAIN_PATH := main.go # 构建命令 # ============================================ -# 本地构建 +# 本地构建(静态链接) build: - @echo "Building $(APP_NAME)..." + @echo "Building $(APP_NAME) (static)..." @mkdir -p $(BUILD_DIR) - go build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH) + $(CGO_DISABLE) go build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH) @echo "Built: $(BUILD_DIR)/$(APP_NAME)" @echo "Version: $(VERSION) | Commit: $(GIT_COMMIT) | Platform: $(BUILD_PLATFORM)" -# 生产构建(体积优化) +# 生产构建(体积优化,静态链接) build-prod: - @echo "Building $(APP_NAME) for production..." + @echo "Building $(APP_NAME) for production (static)..." @mkdir -p $(BUILD_DIR) - go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH) + $(CGO_DISABLE) go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH) @echo "Production build complete: $(BUILD_DIR)/$(APP_NAME)" -# 生产构建(最大运行时性能) +# 生产构建(最大运行时性能,静态链接) build-perf: - @echo "Building $(APP_NAME) with max runtime performance..." + @echo "Building $(APP_NAME) with max runtime performance (static)..." @mkdir -p $(BUILD_DIR) - go build $(LDFLAGS) $(PERF_GCFLAGS) $(PERF_ASMFLAGS) -trimpath \ + $(CGO_DISABLE) go build $(LDFLAGS) $(PERF_GCFLAGS) $(PERF_ASMFLAGS) -trimpath \ -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH) @echo "Performance build complete: $(BUILD_DIR)/$(APP_NAME)" -# PGO 构建(需先收集 profile) +# PGO 构建(需先收集 profile,静态链接) PGO_PROFILE ?= default.pgo build-pgo: - @echo "Building $(APP_NAME) with PGO optimization..." + @echo "Building $(APP_NAME) with PGO optimization (static)..." @mkdir -p $(BUILD_DIR) if [ -f $(PGO_PROFILE) ]; then \ - go build $(LDFLAGS) $(PERF_GCFLAGS) $(PERF_ASMFLAGS) -trimpath \ + $(CGO_DISABLE) go build $(LDFLAGS) $(PERF_GCFLAGS) $(PERF_ASMFLAGS) -trimpath \ -pgo=$(PGO_PROFILE) -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH); \ echo "PGO build complete using: $(PGO_PROFILE)"; \ else \ @@ -99,24 +102,24 @@ pgo-collect: @echo "" @echo "Tip: Profile during real workload for best PGO results" -# 跨平台构建 +# 跨平台构建(静态链接) build-linux: - @echo "Building for Linux..." + @echo "Building for Linux (static)..." @mkdir -p $(BUILD_DIR) - GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME)-linux-amd64 $(MAIN_PATH) + $(CGO_DISABLE) GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME)-linux-amd64 $(MAIN_PATH) @echo "Built: $(BUILD_DIR)/$(APP_NAME)-linux-amd64" build-darwin: - @echo "Building for macOS..." + @echo "Building for macOS (static)..." @mkdir -p $(BUILD_DIR) - GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME)-darwin-amd64 $(MAIN_PATH) - GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME)-darwin-arm64 $(MAIN_PATH) + $(CGO_DISABLE) GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME)-darwin-amd64 $(MAIN_PATH) + $(CGO_DISABLE) GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME)-darwin-arm64 $(MAIN_PATH) @echo "Built: $(BUILD_DIR)/$(APP_NAME)-darwin-{amd64,arm64}" build-windows: - @echo "Building for Windows..." + @echo "Building for Windows (static)..." @mkdir -p $(BUILD_DIR) - GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME)-windows-amd64.exe $(MAIN_PATH) + $(CGO_DISABLE) GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -trimpath -o $(BUILD_DIR)/$(APP_NAME)-windows-amd64.exe $(MAIN_PATH) @echo "Built: $(BUILD_DIR)/$(APP_NAME)-windows-amd64.exe" # 构建所有平台 @@ -278,7 +281,7 @@ clean: help: @echo "$(APP_NAME) Makefile Commands" @echo "" - @echo "Build:" + @echo "Build (static linked):" @echo " make build - Build for current platform" @echo " make build-prod - Production build (size optimized)" @echo " make build-perf - Production build (max runtime performance)"