From 5fe8f46841ac8ec5302a51926c71343968fe2818 Mon Sep 17 00:00:00 2001 From: XOF Date: Mon, 15 Dec 2025 01:37:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20build.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.sh | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 build.sh diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..f8a405c --- /dev/null +++ b/build.sh @@ -0,0 +1,87 @@ +#!/bin/bash +# build.sh - Build script for SiteProxy +# Usage: ./build.sh [platform] +# Example: ./build.sh linux +set -e +echo "================================" +echo " SiteProxy Build Script" +echo "================================" +echo "" +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color +# 版本信息 +VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev") +BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S') +GO_VERSION=$(go version | awk '{print $3}') +echo -e "${YELLOW}Version:${NC} $VERSION" +echo -e "${YELLOW}Build Time:${NC} $BUILD_TIME" +echo -e "${YELLOW}Go Version:${NC} $GO_VERSION" +echo "" +# 清理旧文件 +echo -e "${YELLOW}Cleaning old builds...${NC}" +rm -f siteproxy siteproxy-* +echo -e "${GREEN}✓ Cleaned${NC}" +echo "" +# 构建标志 +LDFLAGS="-s -w -X main.Version=$VERSION -X main.BuildTime=$BUILD_TIME" +# 检查平台参数 +PLATFORM=${1:-"current"} +build_binary() { + local GOOS=$1 + local GOARCH=$2 + local OUTPUT=$3 + + echo -e "${YELLOW}Building for $GOOS/$GOARCH...${NC}" + + if CGO_ENABLED=0 GOOS=$GOOS GOARCH=$GOARCH go build -ldflags="$LDFLAGS" -o $OUTPUT .; then + local SIZE=$(du -h $OUTPUT | cut -f1) + echo -e "${GREEN}✓ Built: $OUTPUT ($SIZE)${NC}" + return 0 + else + echo -e "${RED}✗ Build failed for $GOOS/$GOARCH${NC}" + return 1 + fi +} +case $PLATFORM in + "current") + echo "Building for current platform..." + build_binary $(go env GOOS) $(go env GOARCH) "siteproxy" + ;; + + "linux") + build_binary "linux" "amd64" "siteproxy-linux-amd64" + ;; + + "darwin") + build_binary "darwin" "amd64" "siteproxy-darwin-amd64" + build_binary "darwin" "arm64" "siteproxy-darwin-arm64" + ;; + + "windows") + build_binary "windows" "amd64" "siteproxy-windows-amd64.exe" + ;; + + "all") + echo "Building for all platforms..." + build_binary "linux" "amd64" "siteproxy-linux-amd64" + build_binary "linux" "arm64" "siteproxy-linux-arm64" + build_binary "darwin" "amd64" "siteproxy-darwin-amd64" + build_binary "darwin" "arm64" "siteproxy-darwin-arm64" + build_binary "windows" "amd64" "siteproxy-windows-amd64.exe" + ;; + + *) + echo -e "${RED}Unknown platform: $PLATFORM${NC}" + echo "Usage: ./build.sh [current|linux|darwin|windows|all]" + exit 1 + ;; +esac +echo "" +echo -e "${GREEN}================================${NC}" +echo -e "${GREEN} Build Complete!${NC}" +echo -e "${GREEN}================================${NC}" +echo "" +echo "Run with: ./siteproxy" \ No newline at end of file