mosdns/构建脚本使用说明.md

451 lines
11 KiB
Markdown
Raw 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.

# 🔨 MosDNS 多平台构建脚本使用说明
> 支持 Linux、Windows、macOS 全平台编译,自动构建 Web UI
---
## 📋 脚本对比
| 特性 | Windows 版本 | Linux 版本 |
|------|-------------|-----------|
| 文件名 | `build-all-platforms.bat` | `build-all-platforms.sh` |
| 运行环境 | Windows CMD/PowerShell | Linux/macOS Bash |
| 语法 | 批处理 (.bat) | Shell Script (.sh) |
| 颜色支持 | ✅ 有限支持 | ✅ 完整 ANSI 色彩 |
| 交互菜单 | ✅ | ✅ |
| 自动构建 Vue | ✅ | ✅ |
---
## 🚀 快速开始
### Linux/macOS 使用方法
```bash
# 1. 赋予执行权限
chmod +x build-all-platforms.sh
# 2. 运行脚本
./build-all-platforms.sh
```
### Windows 使用方法
```cmd
# 直接运行批处理文件
build-all-platforms.bat
```
---
## 📝 功能特性
### 1. 自动环境检查
**检查项目**:
- ✅ Go 环境是否安装
- ✅ Node.js 环境(用于构建 Vue
- ✅ npm 包管理器
- ✅ Vue 前端是否已构建
**自动处理**:
```bash
# 如果 web-ui/dist 不存在,自动执行:
cd web-ui
npm install # 安装依赖
npm run build # 构建前端
cd ..
```
---
### 2. 交互式菜单
```
╔════════════════════════════════════════════╗
║ 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] 退出
```
---
### 3. 支持的平台
| 平台 | 架构 | 输出文件 | 用途 |
|------|------|---------|------|
| **Linux** | AMD64 | `mosdns-linux-amd64` | x86_64 服务器 |
| **Linux** | ARM64 | `mosdns-linux-arm64` | 树莓派、ARM 服务器 |
| **Windows** | AMD64 | `mosdns-windows-amd64.exe` | Windows 64位 |
| **macOS** | AMD64 | `mosdns-darwin-amd64` | Intel Mac |
| **macOS** | ARM64 | `mosdns-darwin-arm64` | Apple Silicon (M1/M2/M3) |
---
### 4. 构建参数
**自动设置**:
```bash
VERSION="v5.0.0-webui"
BUILD_TIME=$(date '+%Y-%m-%d %H:%M:%S')
CGO_ENABLED=0
LDFLAGS="-s -w -X 'main.version=$VERSION' -X 'main.buildTime=$BUILD_TIME'"
```
**编译优化**:
- `-s`: 去除符号表(减小体积)
- `-w`: 去除 DWARF 调试信息(减小体积)
- `CGO_ENABLED=0`: 静态编译,无需外部依赖
---
## 📊 构建流程
### 完整流程示意图
```
┌─────────────────────────────────────────┐
│ 1. 检查 Go 环境 │
└─────────────┬───────────────────────────┘
┌─────────────────────────────────────────┐
│ 2. 检查/构建 Vue 前端 │
│ - 检查 web-ui/dist/index.html │
│ - 如不存在npm install && npm build │
└─────────────┬───────────────────────────┘
┌─────────────────────────────────────────┐
│ 3. 显示平台选择菜单 │
└─────────────┬───────────────────────────┘
┌─────────────────────────────────────────┐
│ 4. 初始化构建参数 │
│ - 设置版本号、构建时间 │
│ - 创建 dist/ 目录 │
│ - 设置 LDFLAGS │
└─────────────┬───────────────────────────┘
┌─────────────────────────────────────────┐
│ 5. 执行跨平台编译 │
│ - 设置 GOOS, GOARCH │
│ - go build -ldflags="..." │
└─────────────┬───────────────────────────┘
┌─────────────────────────────────────────┐
│ 6. 显示构建结果 │
│ - 列出构建产物 │
│ - 显示文件大小 │
│ - 提供使用说明 │
└─────────────┬───────────────────────────┘
┌─────────────────────────────────────────┐
│ 7. 询问是否继续 (循环) │
└─────────────────────────────────────────┘
```
---
## 🎨 输出示例
### 成功构建
```bash
════════════════════════════════════════════
🎉 构建完成!
📦 构建产物列表:
mosdns-darwin-amd64
mosdns-darwin-arm64
mosdns-linux-amd64
mosdns-linux-arm64
mosdns-windows-amd64.exe
📊 文件大小详情:
mosdns-linux-amd64 - 24M
mosdns-linux-arm64 - 23M
mosdns-windows-amd64.exe - 24M
mosdns-darwin-amd64 - 24M
mosdns-darwin-arm64 - 23M
════════════════════════════════════════════
📝 使用方法:
Linux:
chmod +x dist/mosdns-linux-amd64
./dist/mosdns-linux-amd64 start -c config.yaml
Windows:
dist\mosdns-windows-amd64.exe start -c config.yaml
macOS:
chmod +x dist/mosdns-darwin-amd64
./dist/mosdns-darwin-amd64 start -c config.yaml
🌐 Web 管理界面: http://localhost:5555
💡 提示: 所有可执行文件已内嵌 Web 资源,可独立运行
```
---
## 🔧 高级用法
### 1. 仅构建 Vue 前端
```bash
cd web-ui
npm install
npm run build
```
### 2. 手动编译单个平台
```bash
# Linux AMD64
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -ldflags="-s -w" -o dist/mosdns-linux-amd64 .
# macOS ARM64
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 \
go build -ldflags="-s -w" -o dist/mosdns-darwin-arm64 .
# Windows AMD64
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 \
go build -ldflags="-s -w" -o dist/mosdns-windows-amd64.exe .
```
### 3. 自定义版本号
**编辑脚本**:
```bash
# Linux 脚本 (build-all-platforms.sh)
VERSION="v5.0.0-webui" # 修改此行
# Windows 脚本 (build-all-platforms.bat)
set "VERSION=v5.0.0-webui" # 修改此行
```
### 4. 自定义输出目录
```bash
# Linux 脚本
OUTPUT_DIR="dist" # 修改为 "release" 或其他目录
# Windows 脚本
set "OUTPUT_DIR=dist" # 修改为 "release" 或其他目录
```
---
## 🐛 故障排查
### 问题 1: Go 环境未找到
**错误信息**:
```
❌ Go 未安装或不在 PATH 中
```
**解决方案**:
```bash
# 检查 Go 是否安装
which go
go version
# 如未安装,请访问 https://golang.org/dl/
# 或使用包管理器安装
sudo apt install golang-go # Debian/Ubuntu
brew install go # macOS
```
---
### 问题 2: Node.js 环境未找到
**错误信息**:
```
❌ Node.js 未安装,无法构建 Vue 前端
```
**解决方案**:
```bash
# 检查 Node.js
node --version
npm --version
# 如未安装
sudo apt install nodejs npm # Debian/Ubuntu
brew install node # macOS
# 或访问 https://nodejs.org/
```
---
### 问题 3: Vue 构建失败
**错误信息**:
```
❌ Vue 构建失败
ERROR: "type-check" exited with 1.
```
**解决方案**:
```bash
# 已修复 TypeScript 类型错误
# 确保使用最新代码
cd web-ui
npm install
npm run build
```
---
### 问题 4: 权限不足 (Linux/macOS)
**错误信息**:
```
bash: ./build-all-platforms.sh: Permission denied
```
**解决方案**:
```bash
# 赋予执行权限
chmod +x build-all-platforms.sh
# 或使用 bash 执行
bash build-all-platforms.sh
```
---
## 📦 构建产物说明
### 文件大小
| 平台 | 预期大小 | 说明 |
|------|---------|------|
| Linux AMD64 | ~24MB | 包含完整 Web UI |
| Linux ARM64 | ~23MB | ARM 架构稍小 |
| Windows AMD64 | ~24MB | .exe 可执行文件 |
| macOS AMD64 | ~24MB | Intel Mac |
| macOS ARM64 | ~23MB | Apple Silicon |
### 内嵌资源
每个可执行文件都包含:
- ✅ MosDNS 核心引擎
- ✅ 所有插件(包括 smart_fallback, mikrotik_addresslist 等)
- ✅ Vue 3 Web 管理界面(完整 SPA
- ✅ 热加载功能
- ✅ RESTful API
**优势**:
- 🚀 单文件部署,无需额外依赖
- 📦 开箱即用,无需安装 Node.js
- 🔒 资源嵌入二进制,安全可靠
---
## 🎯 最佳实践
### 1. 发布版本构建
```bash
# 选择 [A] 编译全部平台
./build-all-platforms.sh
# 输入: A
# 构建产物
dist/
├── mosdns-darwin-amd64
├── mosdns-darwin-arm64
├── mosdns-linux-amd64
├── mosdns-linux-arm64
└── mosdns-windows-amd64.exe
```
### 2. 测试单平台
```bash
# 选择 [1] 仅编译 Linux AMD64
./build-all-platforms.sh
# 输入: 1
# 立即测试
chmod +x dist/mosdns-linux-amd64
./dist/mosdns-linux-amd64 start -c config.yaml
```
### 3. CI/CD 集成
```yaml
# GitHub Actions 示例
- name: Build all platforms
run: |
chmod +x build-all-platforms.sh
echo "A" | ./build-all-platforms.sh
```
---
## 📚 相关文档
- [Go 跨平台编译文档](https://golang.org/doc/install/source#environment)
- [MosDNS 项目文档](./README.md)
- [Vue 3 构建配置](./web-ui/README.md)
- [热加载功能说明](./热加载功能实现总结.md)
- [TypeScript 类型修复](./TypeScript类型错误修复.md)
---
## 🎉 总结
### Linux 版本脚本特性
**完整功能对等** - 与 Windows 版本功能完全一致
**彩色输出** - 使用 ANSI 色彩增强可读性
**交互式菜单** - 友好的用户界面
**错误处理** - 完善的错误检测和提示
**跨平台编译** - 支持 5 种平台架构
**自动化流程** - Vue 前端自动构建
### 使用场景
- 🖥️ **Linux 服务器**: 直接在服务器上构建
- 🍎 **macOS 开发机**: 本地开发和测试
- 🐳 **Docker 容器**: 容器化构建环境
- 🔄 **CI/CD 管道**: 自动化构建和发布
---
**🔨 构建脚本已准备就绪,开始构建您的 MosDNS**
*更新时间: 2025-10-16*
*版本: v1.0*