GO = go APP_NAME = wormhole-server VERSION ?= v1.1.0 BUILD_TIME = $(shell date -u '+%Y-%m-%d_%H:%M:%S') GIT_COMMIT = $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown") LDFLAGS = -ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME) -X main.gitCommit=$(GIT_COMMIT) -s -w" # 支持的平台和架构 PLATFORMS = linux/amd64 linux/arm64 linux/386 linux/arm \ darwin/amd64 darwin/arm64 \ windows/amd64 windows/386 windows/arm64 \ freebsd/amd64 freebsd/arm64 # 构建输出目录 BUILD_DIR = bin DIST_DIR = dist .PHONY: all build clean deps test run cross-compile release help install docker-build docker-build-multi version platforms all: clean deps build deps: $(GO) mod download $(GO) mod tidy # 本地构建 (当前平台) build: @echo "Building $(APP_NAME) for current platform..." $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME) cmd/wormhole-server/main.go # 交叉编译所有平台 cross-compile: clean deps @echo "Cross-compiling for all supported platforms..." @mkdir -p $(DIST_DIR) @for platform in $(PLATFORMS); do \ OS=$$(echo $$platform | cut -d'/' -f1); \ ARCH=$$(echo $$platform | cut -d'/' -f2); \ echo "Building for $$OS/$$ARCH..."; \ if [ "$$OS" = "windows" ]; then \ GOOS=$$OS GOARCH=$$ARCH $(GO) build $(LDFLAGS) -o $(DIST_DIR)/$(APP_NAME)-$(VERSION)-$$OS-$$ARCH.exe cmd/wormhole-server/main.go; \ else \ GOOS=$$OS GOARCH=$$ARCH $(GO) build $(LDFLAGS) -o $(DIST_DIR)/$(APP_NAME)-$(VERSION)-$$OS-$$ARCH cmd/wormhole-server/main.go; \ fi; \ done @echo "Cross-compilation completed. Binaries are in $(DIST_DIR)/" # 创建发布包 release: cross-compile @echo "Creating release packages..." @cd $(DIST_DIR) && \ for file in $(APP_NAME)-$(VERSION)-*; do \ if [ "$${file##*.}" = "exe" ]; then \ platform=$$(echo $$file | sed 's/$(APP_NAME)-$(VERSION)-//; s/.exe$$//'); \ zip -q $(APP_NAME)-$(VERSION)-$$platform.zip $$file ../configs/server.yaml ../README.md; \ else \ platform=$$(echo $$file | sed 's/$(APP_NAME)-$(VERSION)-//'); \ tar -czf $(APP_NAME)-$(VERSION)-$$platform.tar.gz $$file -C .. configs/server.yaml README.md; \ fi; \ done @echo "Release packages created in $(DIST_DIR)/" # 特定平台构建 build-linux: @echo "Building for Linux AMD64..." GOOS=linux GOARCH=amd64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME)-linux-amd64 cmd/wormhole-server/main.go build-darwin: @echo "Building for macOS AMD64..." GOOS=darwin GOARCH=amd64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME)-darwin-amd64 cmd/wormhole-server/main.go build-windows: @echo "Building for Windows AMD64..." GOOS=windows GOARCH=amd64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME)-windows-amd64.exe cmd/wormhole-server/main.go # ARM平台构建 build-arm64: @echo "Building for ARM64..." GOOS=linux GOARCH=arm64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME)-linux-arm64 cmd/wormhole-server/main.go build-darwin-arm64: @echo "Building for macOS ARM64 (Apple Silicon)..." GOOS=darwin GOARCH=arm64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME)-darwin-arm64 cmd/wormhole-server/main.go # 运行和测试 run: build ./$(BUILD_DIR)/$(APP_NAME) -config configs/server.yaml test: $(GO) test -v ./... test-race: $(GO) test -race -v ./... bench: $(GO) test -bench=. -benchmem ./... # 清理 clean: rm -rf $(BUILD_DIR)/ $(DIST_DIR)/ # 安装到系统 install: build sudo cp $(BUILD_DIR)/$(APP_NAME) /usr/local/bin/ # Docker构建 docker-build: docker build -t $(APP_NAME):$(VERSION) . docker-build-multi: docker buildx build --platform linux/amd64,linux/arm64 -t $(APP_NAME):$(VERSION) . # 版本信息 version: @echo "Version: $(VERSION)" @echo "Build Time: $(BUILD_TIME)" @echo "Git Commit: $(GIT_COMMIT)" # 显示支持的平台 platforms: @echo "Supported platforms:" @for platform in $(PLATFORMS); do \ echo " $$platform"; \ done # 帮助信息 help: @echo "Wormhole Server Build System" @echo "" @echo "Available targets:" @echo " build - Build for current platform" @echo " cross-compile - Build for all supported platforms" @echo " release - Create release packages for all platforms" @echo " build-linux - Build for Linux AMD64" @echo " build-darwin - Build for macOS AMD64" @echo " build-windows - Build for Windows AMD64" @echo " build-arm64 - Build for Linux ARM64" @echo " build-darwin-arm64 - Build for macOS ARM64" @echo " run - Build and run the server" @echo " test - Run tests" @echo " test-race - Run tests with race detection" @echo " bench - Run benchmarks" @echo " clean - Clean build artifacts" @echo " deps - Download dependencies" @echo " install - Install to /usr/local/bin" @echo " docker-build - Build Docker image" @echo " docker-build-multi - Build multi-arch Docker image" @echo " version - Show version information" @echo " platforms - Show supported platforms" @echo "" @echo "Environment variables:" @echo " VERSION - Set build version (default: $(VERSION))" @echo "" @echo "Examples:" @echo " make build # Build for current platform" @echo " make cross-compile # Build for all platforms" @echo " make release # Create release packages" @echo " VERSION=v1.2.0 make release # Create release with custom version"