# 🔨 构建脚本使用说明 ## 问题修复 ### ❌ 错误的运行方式 ```bash sh build-all-platforms.sh # 会报错! ``` **错误信息:** ``` Syntax error: "(" unexpected (expecting "then") ``` ### ✅ 正确的运行方式 ```bash # 方式1: 使用 bash(推荐) bash build-all-platforms.sh # 方式2: 直接执行 chmod +x build-all-platforms.sh ./build-all-platforms.sh ``` --- ## 交互式编译 运行脚本后会看到菜单: ``` ╔════════════════════════════════════════════╗ ║ MosDNS 多平台构建工具 (带 Web UI) ║ ╚════════════════════════════════════════════╝ 请选择要编译的平台: [1] Linux AMD64 (x86_64 服务器) [2] Linux ARM64 (树莓派、ARM 服务器) [3] Windows AMD64 (Windows 64位) [4] macOS AMD64 (Intel Mac) [5] macOS ARM64 (Apple Silicon M1/M2/M3) [6] 编译所有 Linux 版本 (AMD64 + ARM64) [7] 编译所有 macOS 版本 (AMD64 + ARM64) [8] 编译所有 Windows 版本 (仅 AMD64) [A] 编译全部平台 (推荐用于发布) [0] 退出 ``` **选择示例:** - 输入 `1` - 只编译 Linux AMD64 - 输入 `A` - 编译所有平台 - 输入 `6` - 编译所有 Linux 版本 --- ## 非交互式编译 ### 单个平台 ```bash # Linux AMD64 bash build-all-platforms.sh <<< "1" # Windows AMD64 bash build-all-platforms.sh <<< "3" # macOS ARM64 (Apple Silicon) bash build-all-platforms.sh <<< "5" ``` ### 批量编译 ```bash # 所有 Linux 版本 bash build-all-platforms.sh <<< "6" # 所有平台 bash build-all-platforms.sh <<< "A" ``` --- ## 直接使用 go build ### 当前平台 ```bash go build -o dist/mosdns-linux-amd64 . ``` ### 指定平台 ```bash # Linux AMD64 GOOS=linux GOARCH=amd64 go build -o dist/mosdns-linux-amd64 . # Linux ARM64 GOOS=linux GOARCH=arm64 go build -o dist/mosdns-linux-arm64 . # Windows AMD64 GOOS=windows GOARCH=amd64 go build -o dist/mosdns-windows-amd64.exe . # macOS AMD64 (Intel) GOOS=darwin GOARCH=amd64 go build -o dist/mosdns-darwin-amd64 . # macOS ARM64 (Apple Silicon) GOOS=darwin GOARCH=arm64 go build -o dist/mosdns-darwin-arm64 . ``` --- ## 前端构建 ### 自动构建 脚本会自动检查并构建 Vue 前端(如果需要) ### 手动构建 ```bash cd web-ui npm install npm run build cd .. ``` --- ## 输出文件 编译产物在 `dist/` 目录: ``` dist/ ├── mosdns-linux-amd64 # Linux x86_64 ├── mosdns-linux-arm64 # Linux ARM64 ├── mosdns-windows-amd64.exe # Windows 64位 ├── mosdns-darwin-amd64 # macOS Intel └── mosdns-darwin-arm64 # macOS Apple Silicon ``` --- ## 常见问题 ### Q: 为什么 `sh build-all-platforms.sh` 会报错? **A:** 脚本使用了 Bash 特性,必须用 `bash` 运行。 ### Q: 如何跳过前端构建? **A:** 确保 `web-ui/dist/index.html` 已存在,脚本会自动跳过。 ### Q: 编译失败怎么办? **A:** 检查: 1. Go 版本 >= 1.20 2. Node.js 和 npm 已安装(如需构建前端) 3. 网络连接正常(下载依赖) ### Q: 如何编译特定平台? **A:** 运行脚本后选择对应编号,或使用 `go build` 直接编译。 --- ## 快速命令 ```bash # 开发:编译当前平台 go build -o dist/mosdns . # 测试:编译 Linux AMD64 bash build-all-platforms.sh <<< "1" # 发布:编译所有平台 bash build-all-platforms.sh <<< "A" ``` --- **💡 提示:** 首次编译会下载依赖和构建前端,需要几分钟时间。后续编译会快很多。