From 15a6d4196f2d41e4f3eab550d49e0f1d2da6772c Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 3 Jun 2026 18:23:13 +0800 Subject: [PATCH] =?UTF-8?q?docs(guide):=20=E7=A7=BB=E9=99=A4=20Windows=20C?= =?UTF-8?q?lang=20=E6=94=AF=E6=8C=81=E6=B5=8B=E8=AF=95=E6=8C=87=E5=8D=97?= =?UTF-8?q?=E5=92=8C=E9=87=8D=E6=9E=84=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除 CLANG_TEST_GUIDE.md 测试指南文档 - 移除 REFACTORING_README.md 重构快速指南 - 删除 REFACTORING_SUMMARY.md 重构总结报告 - 移除 svg-to-ico.js SVG 转 ICO 工具脚本 - 清理 mv.php 文件重命名工具脚本 --- CLANG_TEST_GUIDE.md | 335 ------------------------------------- REFACTORING_README.md | 235 -------------------------- REFACTORING_SUMMARY.md | 366 ----------------------------------------- mv.php | 18 -- patent.md | 1 - smaller | 0 svg-to-ico.js | 147 ----------------- 7 files changed, 1102 deletions(-) delete mode 100644 CLANG_TEST_GUIDE.md delete mode 100644 REFACTORING_README.md delete mode 100644 REFACTORING_SUMMARY.md delete mode 100755 mv.php delete mode 100644 patent.md delete mode 100644 smaller delete mode 100644 svg-to-ico.js diff --git a/CLANG_TEST_GUIDE.md b/CLANG_TEST_GUIDE.md deleted file mode 100644 index e08f3b2b..00000000 --- a/CLANG_TEST_GUIDE.md +++ /dev/null @@ -1,335 +0,0 @@ -# Windows Clang 支持测试指南 - -## ✅ 已完成的修改 - -### 1. 代码实现 - -#### CompilerBase.php 修改 - -**文件位置:** `D:\workspace\compiler\src\Php\CompilerBase.php` - -**主要修改:** - -1. **detectPlatform() 方法** (第 298-327 行) - - 添加智能编译器检测 - - 优先级:PHPX_CC 环境变量 > Clang > MSVC - - 显示当前使用的编译器信息 - -2. **isClangAvailable() 方法** (第 329-370 行) - - 检测 PATH 中的 clang++ - - 自动搜索 Visual Studio LLVM 路径 - - 支持 VS 2019 和 VS 2022 所有版本 - - 找到后自动添加到 PATH - -3. **addCompilationOption() 方法** (第 2410-2425 行) - - 根据编译器类型分发到不同的处理方法 - - 支持 MSVC 和 Clang - -4. **addWindowsClangCompilationOption() 方法** (第 2543-2647 行) - - 新增方法,处理 Windows Clang 编译选项 - - 使用 GCC 风格的编译选项(-std, -O, -Wall) - - 链接时使用 MSVC 链接器选项 - - 完整的 Sanitizer 支持 - ---- - -## 🧪 测试步骤 - -### 前置条件 - -您需要安装以下之一: - -#### 选项 1:Visual Studio LLVM 组件(推荐) - -1. 打开 **Visual Studio Installer** -2. 点击 **修改** 您的 Visual Studio 安装 -3. 切换到 **单个组件** 标签 -4. 搜索并勾选: - - ✅ **Clang compiler for Windows** - - ✅ **C++ Clang tools for Windows** -5. 点击 **修改** 开始安装 - -#### 选项 2:独立 LLVM 安装 - -1. 访问 https://releases.llvm.org/download.html -2. 下载 **LLVM-{version}-win64.exe** -3. 运行安装程序 -4. **重要:** 勾选 "Add LLVM to the system PATH" - ---- - -### 测试 1:检查 Clang 安装 - -运行检查脚本: - -```cmd -cd D:\workspace\compiler -check-clang.bat -``` - -或使用 PowerShell: - -```powershell -cd D:\workspace\compiler -.\check-clang.ps1 -``` - -**预期输出:** - -如果 Clang 已安装: -``` -=== Checking Clang Installation === - -1. Checking PATH for clang++: -Found in PATH -clang version 17.0.6 - -2. Checking Visual Studio paths: -Found: VS 2022 Community -clang version 17.0.6 - -=== Check Complete === -``` - -如果未安装: -``` -Not found in PATH -Not found in Visual Studio directories - -To install LLVM for Visual Studio: -1. Open Visual Studio Installer -2. Modify your installation -... -``` - ---- - -### 测试 2:编译器检测 - -运行 PHP 测试脚本: - -```cmd -php test-compiler-detection.php -``` - -**预期输出:** - -如果使用 Clang: -``` -=== Testing Compiler Detection === - -Platform Detection: - isWindows: true - cppCompiler: clang++ - cxxStd: c++17 - -Clang Detection Test: - isClangAvailable: true - -✓ Clang is available and will be used by default - -=== Test Complete === -``` - -如果使用 MSVC: -``` -Platform Detection: - isWindows: true - cppCompiler: cl - cxxStd: c++17 - -Clang Detection Test: - isClangAvailable: false - -✗ Clang not found, will use MSVC - -=== Test Complete === -``` - ---- - -### 测试 3:实际编译测试 - -#### 测试 3a:使用 Clang 编译 - -```cmd -# 确保 Clang 可用 -php test-compiler-detection.php - -# 编译测试文件 -php bin/compiler.php test-clang-simple.php - -# 运行生成的可执行文件 -test-clang-simple.exe -``` - -**预期输出:** -``` -Hello from PHPX! -Testing Clang compiler support... -PHP Version: 8.x.x -Sum of [1,2,3,4,5] = 15 -Test completed successfully! -``` - ---- - -#### 测试 3b:强制使用 MSVC - -```cmd -# 设置环境变量强制使用 MSVC -set PHPX_CC=cl - -# 编译 -php bin/compiler.php test-clang-simple.php - -# 运行 -test-clang-simple.exe -``` - ---- - -#### 测试 3c:强制使用 Clang - -```cmd -# 设置环境变量强制使用 Clang -set PHPX_CC=clang++ - -# 编译 -php bin/compiler.php test-clang-simple.php - -# 运行 -test-clang-simple.exe -``` - ---- - -### 测试 4:Sanitizer 测试(仅 Clang) - -```cmd -# 使用 AddressSanitizer 编译 -php bin/compiler.php test-clang-simple.php --sanitize=address - -# 运行(如果有内存错误会报告) -test-clang-simple.exe -``` - ---- - -## 📊 验证清单 - -完成后,请确认以下功能: - -- [ ] Clang 正确安装在系统中 -- [ ] `isClangAvailable()` 能检测到 Clang -- [ ] 默认情况下优先使用 Clang(如果可用) -- [ ] 可以通过 `PHPX_CC` 环境变量切换编译器 -- [ ] Clang 编译的程序可以正常运行 -- [ ] MSVC 仍然可以正常工作 -- [ ] Sanitizer 选项在 Clang 下工作 -- [ ] 编译时显示正确的编译器信息 - ---- - -## 🐛 故障排除 - -### 问题 1:Clang 未被检测到 - -**症状:** `isClangAvailable()` 返回 false - -**解决:** -1. 确认 Clang 已安装 -2. 检查 PATH 环境变量 -3. 重启命令行窗口 -4. 手动添加 Clang 到 PATH: - ```cmd - set PATH=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\x64\bin;%PATH% - ``` - ---- - -### 问题 2:编译失败 - -**症状:** 编译时出现错误 - -**检查:** -1. 确认使用的是正确的编译器 -2. 检查 Visual Studio Developer Command Prompt -3. 查看详细的错误信息 - -**尝试:** -```cmd -# 清除缓存后重新编译 -del /s /q *.o 2>nul -del /s /q *.obj 2>nul -php bin/compiler.php test.php -``` - ---- - -### 问题 3:链接错误 - -**症状:** 编译成功但链接失败 - -**原因:** Clang on Windows 需要 MSVC 的链接器 - -**解决:** -1. 确保 Visual Studio Build Tools 已安装 -2. 使用 Developer Command Prompt -3. 检查 Windows SDK 是否安装 - ---- - -## 📝 下一步 - -如果测试成功,您可以: - -1. ✅ 开始在项目中使用 Clang -2. ✅ 利用 Clang 的 Sanitizers 进行调试 -3. ✅ 享受更好的错误信息和诊断 -4. ✅ 为跨平台开发做准备 - ---- - -## 🔗 相关文档 - -- [WINDOWS_CLANG_GUIDE.md](./examples/win32-hello/WINDOWS_CLANG_GUIDE.md) - 完整的使用指南 -- [YAML_CONFIG_NAMING_CONVENTION.md](./examples/win32-hello/YAML_CONFIG_NAMING_CONVENTION.md) - YAML 配置规范 -- [CONFIG_PRIORITY_RULES.md](./examples/win32-hello/CONFIG_PRIORITY_RULES.md) - 配置优先级规则 - ---- - -## 💡 提示 - -**快速切换编译器:** - -```cmd -# 使用 Clang -set PHPX_CC=clang++ -php bin/compiler.php app.php - -# 使用 MSVC -set PHPX_CC=cl -php bin/compiler.php app.php - -# 自动选择(默认) -set PHPX_CC= -php bin/compiler.php app.php -``` - -**永久设置(系统环境变量):** - -```cmd -# 设置为 Clang -setx PHPX_CC "clang++" - -# 设置为 MSVC -setx PHPX_CC "cl" - -# 删除设置(自动选择) -setx PHPX_CC "" -``` - ---- - -祝测试顺利!如有问题,请查看详细文档或报告 bug。 diff --git a/REFACTORING_README.md b/REFACTORING_README.md deleted file mode 100644 index d373b836..00000000 --- a/REFACTORING_README.md +++ /dev/null @@ -1,235 +0,0 @@ -# 编译器架构重构 - 快速指南 - -## 🎯 重构目标 - -将编译器的平台和编译器相关逻辑从 `CompilerBase` 中解耦,建立清晰的 **Platform + Backend** 双层抽象架构。 - ---- - -## ✅ 完成情况 - -**核心架构重构:80% 完成** - -- ✅ Platform 层(100%) -- ✅ Backend 层(100%) -- ✅ 适配器层(60%) -- ✅ Translator 重构(50%) -- ✅ Bug 修复(100%) -- ✅ 测试覆盖(100%) - ---- - -## 📁 核心文件 - -### Platform 层(平台抽象) -- `src/Php/Platform/Windows.php` - Windows 平台支持 -- `src/Php/Platform/Linux.php` - Linux 平台支持 -- `src/Php/Platform/Macos.php` - macOS 平台支持 - -### Backend 层(编译器抽象) -- `src/Php/Backend/Msvc.php` - MSVC 编译器 -- `src/Php/Backend/Gcc.php` - GCC 编译器 -- `src/Php/Backend/Clang.php` - Clang 编译器 -- `src/Php/Backend/CompilerBackend.php` - 抽象基类 - -### 适配器层 -- `src/Php/CompilerBase.php` - 协调 Platform 和 Backend - -### 测试 -- `phpunit/src/Backend/WindowsCompilationFixTest.php` - 8 个测试用例 - -### 工具脚本 -- `init_msvc_simple.ps1` - MSVC 环境初始化(PowerShell) -- `init_msvc_env.bat` - MSVC 环境初始化(Batch) - ---- - -## 🚀 快速开始 - -### Windows 环境 - -```powershell -# 1. 初始化 MSVC 环境 -.\init_msvc_simple.ps1 - -# 2. 编译示例 -D:\workspace\php-8.4.20\php.exe bin/compiler.php examples/hello.php - -# 3. 运行生成的可执行文件 -.\hello.exe -``` - -### 运行测试 - -```bash -# 单元测试 -D:\workspace\php-8.4.20\php.exe vendor/bin/phpunit phpunit/src/Backend/WindowsCompilationFixTest.php - -# 输出: OK (8 tests, 26 assertions) -``` - ---- - -## 🏗️ 架构设计 - -### 重构前 -``` -CompilerBase.php (6000+ 行) -├── 平台检测逻辑 -├── 平台特定方法 -├── 编译器特定方法 -└── ... 所有逻辑混在一起 -``` - -### 重构后 -``` -CompilerBase.php (协调者) -├── parseIncludes() → Platform -├── parseLdflags() → Platform -├── parseLibs() → Platform -└── compileFile() → Backend - -Platform/ (平台抽象层) -├── Windows.php -├── Linux.php -└── Macos.php - -Backend/ (编译器抽象层) -├── Msvc.php -├── Gcc.php -└── Clang.php -``` - ---- - -## 📊 关键改进 - -### 1. 职责分离 -- **Platform 层**:处理平台差异(路径分隔符、库文件格式等) -- **Backend 层**:处理编译器差异(命令行参数、编译选项等) -- **CompilerBase**:只负责协调,不包含具体实现 - -### 2. C/C++ 文件区分 -- `buildCompileCommand()` - C++ 文件(包含 `/EHsc`, `/std:c++17` 等) -- `buildCCompileCommand()` - C 文件(纯 C 编译选项) - -### 3. ZTS/NTS 支持 -- Platform 对象创建时传递正确的 ZTS 状态 -- 自动生成 `/DZTS` 或 `-DZTS` 宏定义 - -### 4. 库文件链接顺序 -- bin 模式:`phpx.lib` → `php8ts.lib` → `php8embed.lib` → Windows API 库 -- ext 模式:`phpx.lib` → `php8ts.lib` - ---- - -## 🔧 使用示例 - -### 使用 Platform 层 - -```php -// 创建 Windows Platform 对象 -$platform = new \PhpAot\Php\Platform\Windows( - phpLibs: [], - isZts: true, - phpSdkPath: 'C:\PHP\SDK' -); - -// 获取包含路径 -$includeFlags = $platform->getIncludeFlags([ - 'C:\PHP\SDK\include', - 'C:\PHP\SDK\include\main' -]); - -// 获取库文件标志 -$libFlags = $platform->getLibraryFlags([ - 'C:\PHP\SDK\lib\php8ts.lib', - 'user32.lib' -]); -``` - -### 使用 Backend 层 - -```php -// 创建 MSVC Backend -$compiler = new \PhpAot\Php\Backend\Msvc($platform); - -// 编译 C++ 文件 -$cmd = $compiler->buildCompileCommand( - 'hello.cc', - 'hello.obj', - [ - 'optimize' => 2, - 'cpp_std' => 'c++17', - 'is_zts' => true, - ] -); - -// 编译 C 文件 -$cmd = $compiler->buildCCompileCommand( - 'main.c', - 'main.obj', - [ - 'optimize' => 0, - 'suppressed_warnings' => ['4244', '4146'], - ] -); - -// 链接对象文件 -$linkCmd = $compiler->buildLinkCommand( - ['hello.obj', 'main.obj'], - 'hello.exe', - [ - 'debug' => false, - 'no_console' => false, - ] -); -``` - ---- - -## 📚 详细文档 - -- **[REFACTORING_SUMMARY.md](REFACTORING_SUMMARY.md)** - 完整的重构总结报告 -- **[REFACTORING_PROGRESS.md](src/Php/Backend/REFACTORING_PROGRESS.md)** - 详细进度记录 -- **[MSVC_ENV_SETUP.md](MSVC_ENV_SETUP.md)** - MSVC 环境配置指南 - ---- - -## ✨ 主要成就 - -1. ✅ **架构清晰** - Platform + Backend 双层抽象 -2. ✅ **代码简洁** - 删除 171 行 Legacy 代码 -3. ✅ **测试完善** - 8 个单元测试,26 个断言 -4. ✅ **文档齐全** - 详细的使用说明和重构报告 -5. ✅ **工具实用** - MSVC 环境自动化配置 - ---- - -## 🔮 下一步 - -### 短期优化(可选) -- [ ] 为 GCC/Clang Backend 添加单元测试 -- [ ] 测试更多示例项目 - -### 中期完善(可选) -- [ ] 完全重构 `addCompilationOption()` 方法 -- [ ] 完善错误处理和日志记录 - -### 长期演进(可选) -- [ ] 移除 Legacy 回退代码 -- [ ] 性能优化 -- [ ] 更多编译器/平台支持 - ---- - -## 🎉 总结 - -本次重构成功建立了现代化的编译器架构,通过 **Platform + Backend** 双层抽象,实现了: - -- 🎯 **清晰的职责分离** -- 🔧 **优秀的可维护性** -- 🚀 **强大的可扩展性** -- ✅ **可靠的代码质量** - -**核心目标已达成!** 🎊 diff --git a/REFACTORING_SUMMARY.md b/REFACTORING_SUMMARY.md deleted file mode 100644 index 9b2f6ad3..00000000 --- a/REFACTORING_SUMMARY.md +++ /dev/null @@ -1,366 +0,0 @@ -# 编译器架构重构总结报告 - -## 📅 完成时间 -2026-05-07 - -## 🎯 重构目标 - -将编译器的平台和编译器相关逻辑从 `CompilerBase` 中解耦,建立清晰的 **Platform + Backend** 双层抽象架构。 - ---- - -## ✅ 已完成的工作 - -### Phase 1: Platform 层(100%) - -创建了三个平台类,提供统一的平台抽象 API: - -#### Windows Platform (`src/Php/Platform/Windows.php`) -- ✅ `buildPhpSdkIncludePaths()` - 构建 PHP SDK 包含路径 -- ✅ `buildPhpSdkLibPaths()` - 构建 PHP SDK 库路径 -- ✅ `detectPhpLibs()` - 检测 PHP lib 文件(php8ts.lib / php8.lib) -- ✅ `getLibraryFlags()` - 格式化库文件参数 -- ✅ `getIncludeFlags()` - 格式化包含路径参数 -- ✅ ZTS/NTS 模式支持 - -#### Linux Platform (`src/Php/Platform/Linux.php`) -- ✅ `buildPhpIncludePaths()` - 构建 PHP 包含路径 -- ✅ `buildPhpLibPaths()` - 构建 PHP 库路径 -- ✅ `detectPhpLibs()` - 检测 PHP 库文件 -- ✅ `getRpathOptions()` - RPATH 选项 -- ✅ `getPicFlag()` - PIC 标志 -- ✅ `getSharedLinkFlag()` - 共享库链接标志 - -#### macOS Platform (`src/Php/Platform/Macos.php`) -- ✅ `buildPhpIncludePaths()` - 构建 PHP 包含路径 -- ✅ `buildPhpLibPaths()` - 构建 PHP 库路径 -- ✅ `detectPhpLibs()` - 检测 PHP 库文件 -- ✅ `getRpathOptions()` - RPATH 选项 -- ✅ `getCurrentInstallNameOption()` - install_name 选项 -- ✅ `getSharedLinkFlag()` - 动态库链接标志 - ---- - -### Phase 2: Backend 层(100%) - -创建了三个编译器后端类,提供统一的编译器抽象 API: - -#### MSVC Backend (`src/Php/Backend/Msvc.php`) -- ✅ `buildCompileCommand()` - C++ 文件编译命令 -- ✅ `buildCCompileCommand()` - C 文件编译命令(无 C++ 特定选项) -- ✅ `buildLinkCommand()` - 链接命令 -- ✅ `buildCompileOptions()` - 编译选项字符串 -- ✅ `buildLinkOptions()` - 链接选项字符串 - -#### GCC Backend (`src/Php/Backend/Gcc.php`) -- ✅ `buildCompileCommand()` - C++ 文件编译命令 -- ✅ `buildCCompileCommand()` - C 文件编译命令 -- ✅ `buildLinkCommand()` - 链接命令 -- ✅ `buildCompileOptions()` - 编译选项字符串 -- ✅ `buildLinkOptions()` - 链接选项字符串 - -#### Clang Backend (`src/Php/Backend/Clang.php`) -- ✅ `buildCompileCommand()` - C++ 文件编译命令(支持 Windows MSVC 兼容模式) -- ✅ `buildCCompileCommand()` - C 文件编译命令 -- ✅ `buildLinkCommand()` - 链接命令(支持 Windows/Unix 双模式) -- ✅ `buildCompileOptions()` - 编译选项字符串 -- ✅ `buildLinkOptions()` - 链接选项字符串 - ---- - -### Phase 3: CompilerBase 适配器层(部分完成) - -在 `CompilerBase.php` 中添加了适配器方法,桥接旧代码和新架构: - -#### 已完成的适配器方法 -- ✅ `parseIncludes()` - 使用 Platform 层解析包含路径 - - 新实现:`parseIncludesNew()` - - 回退机制:`parseIncludesLegacy()` - -- ✅ `parseLdflags()` - 使用 Platform 层解析库路径 - - 新实现:`parseLdflagsNew()` - - 回退机制:`parseLdflagsLegacy()` - -- ✅ `parseLibs()` - 使用 Platform 层解析库文件 - - 新实现:`parseLibsNew()` - - 回退机制:`parseLibsLegacy()` - -#### 待完成的适配器方法 -- ⏳ `addCompilationOption()` - 编译和链接选项 -- ⏳ `compileFile()` - 编译单个文件 -- ⏳ `linkObjects()` - 链接目标文件 - ---- - -### Phase 4: Translator 编译逻辑(部分重构) - -在 `Translator.php` 中部分使用了新的 Backend 层: - -#### compileFile() 方法 -- ✅ C++ 文件使用 `$this->compilerBackend->buildCompileCommand()` -- ✅ C 文件使用 `$this->compilerBackend->buildCCompileCommand()` -- ✅ 消除了硬编码的平台/编译器判断 -- ✅ 封装了 `isCppFile()` 方法判断文件类型 - -#### build() 方法(链接逻辑) -- ⏳ 仍使用旧的链接命令构建方式 -- ⏳ 待迁移到 `$this->compilerBackend->buildLinkCommand()` - ---- - -### Phase 5: Bug 修复(100%) - -修复了重构过程中发现的关键问题: - -#### 1. ZTS 宏缺失问题 -**问题:** Platform 对象创建时没有传递 ZTS 状态,导致编译命令缺少 `/DZTS` 宏 - -**修复:** 在 `initializeNewArchitecture()` 中重新创建 Windows Platform,传递正确的 `isZts` 参数 - -```php -$this->platform = new \PhpAot\Php\Platform\Windows( - phpLibs: [], - isZts: $this->isPhpZts, // ✅ 传递检测到的 ZTS 状态 - phpSdkPath: $phpSdkPath -); -``` - -#### 2. 库文件链接顺序问题 -**问题:** bin 模式需要同时链接 `php8ts.lib` 和 `php8embed.lib`,但顺序很重要 - -**修复:** 调整顺序为 `php8ts.lib` → `php8embed.lib`(被依赖的库在前) - -#### 3. 双引号嵌套问题 -**问题:** `parseLibs()` 添加引号,`getLibraryFlags()` 又添加引号,导致 `""path""` - -**修复:** 统一由 `getLibraryFlags()` 处理引号,`parseLibs()` 不添加引号 - -#### 4. MSVC 环境问题 -**问题:** 编译时找不到 `malloc.h` 等标准库头文件 - -**修复:** 创建 MSVC 环境初始化脚本(`init_msvc_simple.ps1`),自动设置环境变量 - ---- - -## 📊 代码统计 - -### 新增代码 -| 文件 | 行数 | 说明 | -|------|------|------| -| `Platform/Windows.php` | +119 | 增强 Windows 功能 | -| `Platform/Linux.php` | +59 | 添加 Linux 功能 | -| `Platform/Macos.php` | +59 | 添加 macOS 功能 | -| `Backend/Msvc.php` | +39 | 添加 C 文件编译方法 | -| `Backend/Gcc.php` | +27 | 添加 C 文件编译方法 | -| `Backend/Clang.php` | +37 | 添加 C 文件编译方法 | -| `Backend/CompilerBackend.php` | +9 | 添加抽象方法声明 | -| `CompilerBase.php` | +132 | 添加适配器方法 | -| `Translator.php` | +9 | 添加 isCppFile() 方法 | -| **总计** | **+490行** | **核心重构代码** | - -### 删除代码 -- ❌ 约 171 行 Legacy 回退逻辑(已从 CompilerBase 中移除) - -### 测试代码 -- ✅ `phpunit/src/Backend/WindowsCompilationFixTest.php` - 224 行,8 个测试 - ---- - -## 🧪 测试结果 - -### 单元测试 -```bash -PHPUnit 10.5.63 by Sebastian Bergmann and contributors. - -OK (8 tests, 26 assertions) -``` - -测试覆盖: -- ✅ ZTS 宏在编译命令中的存在性 -- ✅ NTS 模式下不包含 ZTS 宏 -- ✅ buildCompileOptions 尊重 is_zts 配置 -- ✅ 库文件路径格式(无双重引号) -- ✅ 链接命令中库的顺序 -- ✅ 完整编译流程模拟 -- ✅ Windows API 库的添加 -- ✅ 扩展模式的库配置 - -### 集成测试 -```bash -# 编译 hello.php -D:\workspace\php-8.4.20\php.exe bin/compiler.php examples/hello.php -Build successful: hello.exe - -# 运行生成的可执行文件 -.\hello.exe -Hello World! -string(6) "8.4.20" -string(51) "Windows NT BUILDER 6.2 build 9200 (Windows 8) AMD64" -``` - ---- - -## 🏗️ 架构改进 - -### 重构前 -``` -CompilerBase.php (6000+ 行) -├── 平台检测逻辑 -├── 平台特定方法(Windows/Unix) -├── 编译器特定方法(MSVC/GCC/Clang) -├── 路径处理 -├── 库文件检测 -└── ... 所有逻辑混在一起 -``` - -### 重构后 -``` -CompilerBase.php (协调者) -├── parseIncludes() → 委托给 Platform -├── parseLdflags() → 委托给 Platform -├── parseLibs() → 委托给 Platform -└── ... 简洁的协调逻辑 - -Platform/ (平台抽象层) -├── Windows.php (Windows 平台逻辑) -├── Linux.php (Linux 平台逻辑) -└── Macos.php (macOS 平台逻辑) - -Backend/ (编译器抽象层) -├── Msvc.php (MSVC 编译器逻辑) -├── Gcc.php (GCC 编译器逻辑) -└── Clang.php (Clang 编译器逻辑) -``` - ---- - -## 🎁 关键成果 - -### 1. 完全解耦 -- ✅ Platform 层独立于编译器 -- ✅ Backend 层独立于平台 -- ✅ CompilerBase 只负责协调 - -### 2. 向后兼容 -- ✅ 所有新方法都有回退机制 -- ✅ 旧代码继续工作 -- ✅ 零破坏性变更 - -### 3. 易于扩展 -- ✅ 添加新平台:创建新的 Platform 类 -- ✅ 添加新编译器:创建新的 Backend 类 -- ✅ 无需修改 CompilerBase - -### 4. 代码质量 -- ✅ 职责单一 -- ✅ 易于测试 -- ✅ 易于维护 - ---- - -## 📁 新增文件清单 - -### 核心代码 -- `src/Php/Platform/Windows.php` - 增强版 -- `src/Php/Platform/Linux.php` - 增强版 -- `src/Php/Platform/Macos.php` - 增强版 -- `src/Php/Backend/Msvc.php` - 增强版 -- `src/Php/Backend/Gcc.php` - 增强版 -- `src/Php/Backend/Clang.php` - 增强版 -- `src/Php/Backend/CompilerBackend.php` - 抽象基类 - -### 测试文件 -- `phpunit/src/Backend/WindowsCompilationFixTest.php` - 8 个测试 - -### 工具脚本 -- `init_msvc_env.bat` - Batch 版本的 MSVC 环境初始化 -- `init_msvc_simple.ps1` - PowerShell 简化版本 -- `MSVC_ENV_SETUP.md` - MSVC 环境配置指南 - -### 文档 -- `REFACTORING_SUMMARY.md` - 本文档(最终总结) -- `REFACTORING_PROGRESS.md` - 详细进度记录(保留历史) -- `REFACTORING_COMPLETION_REPORT.md` - 阶段性完成报告 - ---- - -## 🚀 下一步建议 - -### 短期(可选优化) -1. 测试更多示例项目(如 win32-hello) -2. 为 GCC/Clang Backend 添加单元测试 -3. 完善错误处理和日志记录 - -### 中期(功能完善) -1. 完成 `addCompilationOption()` 方法的迁移 -2. 完成 `compileFile()` 和 `linkObjects()` 的完全重构 -3. 移除旧的 Legacy 回退代码 - -### 长期(架构演进) -1. 性能优化(并行编译、缓存机制) -2. 更多编译器支持(Intel ICC、MinGW) -3. 更多平台支持(FreeBSD、ARM Windows) -4. 高级功能(交叉编译、远程编译) - ---- - -## 💡 经验教训 - -### 1. 渐进式重构的优势 -- ✅ 保持向后兼容 -- ✅ 可以随时回退 -- ✅ 降低风险 - -### 2. 测试驱动开发 -- ✅ 先写测试,再重构 -- ✅ 确保每次修改都有测试覆盖 -- ✅ 快速发现问题 - -### 3. 职责分离的重要性 -- ✅ Platform 层处理平台差异 -- ✅ Backend 层处理编译器差异 -- ✅ 清晰的边界,易于维护 - -### 4. 环境变量管理 -- ✅ Windows 下 MSVC 需要正确的环境变量 -- ✅ 提供自动化脚本简化配置 -- ✅ 文档化常见问题和解决方案 - ---- - -## 📈 总体进度评估 - -### 核心架构重构:**80% 完成** ✅ - -- ✅ Phase 1: Platform 层 - 100% -- ✅ Phase 2: Backend 层 - 100% -- ⚠️ Phase 3: CompilerBase 适配器层 - 60% -- ⚠️ Phase 4: Translator 编译逻辑 - 50% -- ✅ Phase 5: Bug 修复 - 100% - -### 主要成就 -1. ✅ 建立了清晰的 **Platform + Backend** 双层抽象架构 -2. ✅ 消除了大部分硬编码的平台/编译器判断 -3. ✅ 修复了所有关键的编译链接问题 -4. ✅ 提供了完整的测试覆盖 -5. ✅ 创建了实用的工具脚本和文档 - -### 剩余工作 -- ⏳ 完成剩余的适配器方法迁移 -- ⏳ 完全消除 Translator 中的旧逻辑 -- ⏳ 清理 Legacy 回退代码 - ---- - -## ✨ 总结 - -本次重构成功建立了现代化的编译器架构,通过 **Platform + Backend** 双层抽象,实现了: - -- 🎯 **清晰的职责分离** - 平台逻辑与编译器逻辑完全解耦 -- 🔧 **优秀的可维护性** - 模块化设计,易于理解和修改 -- 🚀 **强大的可扩展性** - 轻松添加新平台和新编译器 -- ✅ **可靠的代码质量** - 完整的测试覆盖和文档 - -虽然仍有部分工作待完成,但**核心架构已经稳固**,为未来的发展奠定了坚实的基础。 - -**重构目标基本达成!** 🎉 diff --git a/mv.php b/mv.php deleted file mode 100755 index 0742a658..00000000 --- a/mv.php +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env php -\n"; - exit(1); -} -$file = $argv[1]; -if (!file_exists($file)) { - echo "File not found: " . $file . "\n"; - exit(1); -} -$newFile = str_replace('-raw', '', $file); -$dir = dirname($newFile); -if (!is_dir($dir)) { - mkdir($dir, 0755, true); -} -rename($file, $newFile); -shell_exec('git add ' . $newFile); diff --git a/patent.md b/patent.md deleted file mode 100644 index 37be888a..00000000 --- a/patent.md +++ /dev/null @@ -1 +0,0 @@ -技术交底书该技术应用产品:1、详细介绍技术背景,并描述已有的与本发明最相近似的实现方案(包括两部分:背景技术及现有技术方案[大的技术背景和小的技术背景],应详细介绍,以不需再去看文献即可领会该技术内容为准,如果现有技术出自专利、期刊、书籍,则提供出处)2、现有技术的缺点是什么?针对这些缺点,说明本发明的目的。(客观评价,现有技术的缺点是针对于本发明的优点来说的,本发明不能解决的缺点不必写;基于本发明能解决的问题写出发明的目的)3、本发明技术方案的详细阐述,应该结合示意图进行说明(越详细越好,至少要提供2页;发明中每一功能的实现都要有相应的技术实现方案;所有英文缩写都应有中文注释;所有附图都应该有详细的文字描述,以别人不看附图即可明白技术方案为准;同时附图中的关键词或方框图中的注释都尽量用中文;方法专利都应该提供流程图,并提供相关的系统装置图;附图中各相关部件都要提供名称)。4、本发明的关键点和欲保护点是什么?(发明内容部分提供的是为完成一定功能的完整技术方案,在本部分是提炼出技术方案的关键创新点,列出1、2、3…,以提醒代理人注意,便于代理人撰写权利要求书5、与第1部分最好的现有技术相比,本发明有何优点(结合发明内容简单介绍,一两个自然段即可)注意:1.代理人并不是技术专家,交底书要使代理人能看懂,尤其是背景技术和详细技术方案,一定要写的全面、清楚。2.英文缩写有中文译文,避免使用英文单词,最好在术语解释部分给出。3.全文对同一事物的叫法应统一,避免出现一种东西多种叫法。4.认为需要保密的地方可在交底书中注明,对代理人不必保密。5.专利法规定:1)专利必须是一个技术方案,应该阐述发明目的是通过什么技术方案来实现的,不能只有原理,也 不能只做功能介绍; 2)专利必须充分公开,以本领域技术人员不需付出创造性劳动即可实现为准。 diff --git a/smaller b/smaller deleted file mode 100644 index e69de29b..00000000 diff --git a/svg-to-ico.js b/svg-to-ico.js deleted file mode 100644 index 4dfa28b0..00000000 --- a/svg-to-ico.js +++ /dev/null @@ -1,147 +0,0 @@ -/** - * SVG → ICO 转换脚本 (Node.js) - * - * 使用 sharp 库将 SVG 渲染为多尺寸 PNG,再合并为 ICO - * - * 用法: node svg-to-ico.js [output.ico] - */ - -const sharp = require('sharp'); -const fs = require('fs'); -const path = require('path'); - -const sizes = [16, 32, 48, 64, 128, 256]; - -async function main() { - const args = process.argv.slice(2); - if (args.length < 1) { - console.log('用法: node svg-to-ico.js [output.ico]'); - process.exit(1); - } - - const inputSvg = path.resolve(args[0]); - const outputIco = args.length >= 2 - ? path.resolve(args[1]) - : path.join(path.dirname(inputSvg), path.basename(inputSvg, '.svg') + '.ico'); - - if (!fs.existsSync(inputSvg)) { - console.log(`错误: SVG 文件不存在: ${inputSvg}`); - process.exit(1); - } - - console.log('=== SVG → ICO 转换工具 ===\n'); - console.log(`输入: ${inputSvg}`); - console.log(`输出: ${outputIco}\n`); - - const svgBuffer = fs.readFileSync(inputSvg); - - // 步骤1: 渲染 SVG 为多个尺寸的 PNG - const pngBuffers = {}; - for (const size of sizes) { - console.log(`[1/2] 渲染 SVG → PNG (${size}x${size})...`); - try { - const pngBuffer = await sharp(svgBuffer, { density: 300 }) - .resize(size, size, { fit: 'contain', background: { r: 255, g: 255, b: 255, alpha: 1 } }) - .png() - .toBuffer(); - pngBuffers[size] = pngBuffer; - console.log(` 完成: ${size}x${size} (${pngBuffer.length} bytes)`); - } catch (err) { - console.log(` 警告: ${size}x${size} PNG 生成失败: ${err.message},跳过`); - } - } - - const validSizes = Object.keys(pngBuffers).map(Number).sort((a, b) => a - b); - if (validSizes.length === 0) { - console.log('\n错误: 所有尺寸的 PNG 都生成失败'); - process.exit(1); - } - - // 步骤2: 合并为 ICO - console.log('\n[2/2] 合并 PNG → ICO...'); - const icoBuffer = createIcoFromPngs(pngBuffers, validSizes); - fs.writeFileSync(outputIco, icoBuffer); - - console.log(`\n成功! ICO 文件已生成: ${outputIco}`); - console.log(`包含尺寸: ${validSizes.join(', ')}`); - console.log(`文件大小: ${icoBuffer.length} bytes`); -} - -/** - * 将多个 PNG buffer 合并为 ICO 格式的 Buffer - */ -function createIcoFromPngs(pngBuffers, sizes) { - const imageCount = sizes.length; - - // 计算各部分大小 - const headerSize = 6; // ICO 头部 - const dirEntrySize = 16; // 每个目录条目 - const dirSize = dirEntrySize * imageCount; - - let dataOffset = headerSize + dirSize; - - // 构建目录条目 - const dirEntries = []; - for (const size of sizes) { - const pngData = pngBuffers[size]; - const dataSize = pngData.length; - - // Width/Height: 0 表示 256 - const w = size >= 256 ? 0 : size; - const h = size >= 256 ? 0 : size; - - dirEntries.push({ - width: w, - height: h, - colorCount: 0, - reserved: 0, - planes: 1, - bitCount: 32, - dataSize: dataSize, - offset: dataOffset - }); - - dataOffset += dataSize; - } - - // 计算总大小 - let totalSize = headerSize + dirSize; - for (const size of sizes) { - totalSize += pngBuffers[size].length; - } - - // 构建 ICO 二进制数据 - const buffer = Buffer.alloc(totalSize); - let pos = 0; - - // ICO 头部 (6 bytes) - buffer.writeUInt16LE(0, pos); pos += 2; // Reserved - buffer.writeUInt16LE(1, pos); pos += 2; // Type: 1 = ICO - buffer.writeUInt16LE(imageCount, pos); pos += 2; // Image count - - // 目录条目 - for (const entry of dirEntries) { - buffer.writeUInt8(entry.width, pos); pos += 1; - buffer.writeUInt8(entry.height, pos); pos += 1; - buffer.writeUInt8(entry.colorCount, pos); pos += 1; - buffer.writeUInt8(entry.reserved, pos); pos += 1; - buffer.writeUInt16LE(entry.planes, pos); pos += 2; - buffer.writeUInt16LE(entry.bitCount, pos); pos += 2; - buffer.writeUInt32LE(entry.dataSize, pos); pos += 4; - buffer.writeUInt32LE(entry.offset, pos); pos += 4; - } - - // 图像数据 - for (const size of sizes) { - const pngData = pngBuffers[size]; - pngData.copy(buffer, pos); - pos += pngData.length; - } - - return buffer; -} - -main().catch(err => { - console.error('错误:', err); - process.exit(1); -});