Files
2026-06-30 15:14:37 +08:00

149 lines
4.1 KiB
Bash
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
# MeshRay 跨平台构建脚本(go-winres
# ============================================
echo ""
echo "========================================"
echo " MeshRay 构建工具"
echo " 版本:2.0.0"
echo "========================================"
echo ""
# 检测操作系统
OS=$(uname -s)
echo "[信息] 检测到操作系统:$OS"
case "$OS" in
MINGW*|MSYS*|CYGWIN*)
echo "[信息] Windows 环境,需要 go-winres 工具"
RSRC_NEEDED=true
;;
Darwin)
echo "[信息] macOS 环境"
RSRC_NEEDED=false
;;
Linux)
echo "[信息] Linux 环境"
RSRC_NEEDED=false
;;
*)
echo "[错误] 不支持的操作系统:$OS"
exit 1
;;
esac
# 1. 检查 go-winres(仅 Windows 需要)
if [ "$RSRC_NEEDED" = true ]; then
echo "[1/7] 检查 go-winres 工具..."
if ! command -v go-winres &> /dev/null; then
echo "[警告] go-winres 未安装,正在安装..."
go install github.com/tc-hib/go-winres@latest || {
echo "[错误] go-winres 安装失败!"
exit 1
}
fi
echo "[✓] go-winres 已安装
# 2. 检查配置文件
echo "[2/7] 检查资源配置..."
if [ ! -f "build/winres.json" ]; then
echo "[错误] 配置文件不存在:build/winres.json"
exit 1
fi
echo "[] 配置文件已检查"
else
echo "[1/3] 跳过 Windows 资源文件生成(非 Windows 平台)"
fi
# 3. 生成资源文件(仅 Windows
if [ "$RSRC_NEEDED" = true ]; then
echo "[3/7] 生成 Windows 资源文件..."
go-winres make --in build/winres.json --arch amd64 || {
echo "[错误] 资源文件生成失败!"
exit 1
}
echo "[] 资源文件生成成功"
# 4. 复制 syso到 cmd/meshray 目录(仅 Windows
echo "[4/7] 复制资源文件到正确位置..."
cp rsrc_windows_amd64.syso cmd/meshray/meshray.syso || {
echo "[错误] 复制失败!"
exit 1
}
echo "[] 资源文件已放置"
fi
# 5. 编译程序(所有平台)
echo "[$([ "$RSRC_NEEDED" = true ] && echo '5' || echo '3')/$( [ "$RSRC_NEEDED" = true ] && echo '7' || echo '3' )] 编译 MeshRay..."
if [ "$RSRC_NEEDED" = true ]; then
# Windows 平台:隐藏控制台窗口
go build -ldflags="-s -w -H windowsgui" -o meshray ./cmd/meshray
else
# macOS/Linux: 保持控制台
go build -ldflags="-s -w" -o meshray ./cmd/meshray
fi
if [ $? -ne 0 ]; then
echo "[错误] 编译失败!"
if [ "$RSRC_NEEDED" = true ]; then
rm -f rsrc_*.syso
rm -f cmd/meshray/meshray.syso
fi
exit 1
fi
echo "[] 编译成功"
# 6. 清理临时文件(仅 Windows
if [ "$RSRC_NEEDED" = true ]; then
echo "[6/7] 清理临时文件..."
rm -f rsrc_*.syso
rm -f cmd/meshray/meshray.syso
echo "[] 清理完成"
fi
# 7. 验证文件(仅 Windows
if [ "$RSRC_NEEDED" = true ]; then
echo "[7/7] 验证可执行文件..."
if [ -f "meshray.exe" ]; then
echo "[] 验证通过"
else
echo "[错误] 可执行文件未生成!"
exit 1
fi
else
echo "[3/3] 验证可执行文件..."
if [ -f "meshray" ] || [ -f "meshray.exe" ]; then
if [ "$OS" = "Darwin" ] || [ "$OS" = "Linux" ]; then
chmod +x meshray
fi
FILE_SIZE=$(ls -lh meshray* | awk '{print $5}')
echo "[] 文件大小:$FILE_SIZE"
echo "[] 验证通过"
else
echo "[错误] 可执行文件未生成!"
exit 1
fi
fi
echo ""
echo "========================================"
echo " 构建完成!"
echo ""
echo " 输出文件:meshray$([ "$RSRC_NEEDED" = true ] && echo '.exe')"
echo " 版本信息:2.0.0.0"
if [ "$RSRC_NEEDED" = true ]; then
echo " 包含:图标 + Manifest + 版本信息"
fi
echo "========================================"
echo ""
# 显示版本信息(仅 Windows)
if [ "$RSRC_NEEDED" = true ]; then
echo "查看版本信息:"
powershell -Command "(Get-Item meshray.exe).VersionInfo.FileDescription"
echo ""
fi
echo "按任意键退出..."
read -n 1