Files
SiteProxy/build.sh
2025-12-15 01:37:53 +08:00

87 lines
2.6 KiB
Bash

#!/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"