Files
yoyo/build.sh
titor 95338962cb
All checks were successful
Release / build (push) Successful in 6m28s
fix: Windows 交叉编译添加 CGO_ENABLED=0
2026-04-08 03:14:30 +08:00

97 lines
2.6 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
cd "$(dirname "$0")"
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
show_help() {
echo "YOYO 编译脚本"
echo ""
echo "用法:"
echo " ./build.sh 构建当前平台,输出 yoyo"
echo " ./build.sh <平台> 交叉编译指定平台"
echo " ./build.sh -h 显示帮助"
echo ""
echo "参数:"
echo " -h 显示帮助信息"
echo " -o <文件名> 指定输出文件名"
echo ""
echo "支持的平台:"
echo " linux/amd64, linux/arm64"
echo " darwin/amd64, darwin/arm64"
echo " windows/amd64"
echo ""
echo "示例:"
echo " ./build.sh # 构建当前平台"
echo " ./build.sh linux/amd64 # 编译 Linux x64"
echo " ./build.sh darwin/arm64 # 编译 macOS ARM"
echo " ./build.sh windows/amd64 -o app.exe # 编译 Windows x64自定义输出名"
echo ""
echo "输出文件名格式: yoo-<os>-<arch>[.exe]"
echo " yoo-linux-amd64"
echo " yoo-darwin-arm64"
echo " yoo-windows-amd64.exe"
}
OUTPUT_NAME="yoyo"
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-o|--output)
OUTPUT_NAME="$2"
shift 2
;;
linux/amd64)
GOOS=linux GOARCH=amd64
PLATFORM="linux-amd64"
shift
;;
linux/arm64)
GOOS=linux GOARCH=arm64
PLATFORM="linux-arm64"
shift
;;
darwin/amd64)
GOOS=darwin GOARCH=amd64
PLATFORM="darwin-amd64"
shift
;;
darwin/arm64)
GOOS=darwin GOARCH=arm64
PLATFORM="darwin-arm64"
shift
;;
windows/amd64)
GOOS=windows GOARCH=amd64
CGO_ENABLED=0
PLATFORM="windows-amd64"
if [[ "$OUTPUT_NAME" == "yoyo" ]]; then
OUTPUT_NAME="yoo-windows-amd64.exe"
fi
shift
;;
*)
echo "未知平台: $1"
show_help
exit 1
;;
esac
done
echo "Building yoyo version: $VERSION"
BUILD_CMD="go build -buildvcs=false -ldflags \"-s -w -X github.com/titor/fanyi/internal/logo.version=${VERSION}\" -o \"$OUTPUT_NAME\" ./cmd/yoyo"
if [ -n "$GOOS" ]; then
echo "Target: $PLATFORM"
eval CGO_ENABLED=${CGO_ENABLED:-0} $BUILD_CMD
else
eval CGO_ENABLED=${CGO_ENABLED:-0} $BUILD_CMD
fi
echo "Build complete: ./$OUTPUT_NAME"