TypePHP 编译器 https://swoole.com/aot/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

45 lines
2.0 KiB

<blog>
# 编译器后端
TypePHP 不直接调 `g++`/`cl`,而是抽象出 **平台(Platform)+ 编译器后端(Backend)** 两层,由工厂在启动时自动探测。这样同一份生成代码能在 GCC / Clang / MSVC 下编译。
Sources: [src/Translator.php](src/Translator.php#L164-L190) · [src/Backend/](src/Backend/)
## 抽象结构
| 类 | 角色 |
|---|---|
| `PlatformFactory` | 按 OS 创建 `Windows` / `Linux` / `macOS` 平台对象 |
| `CompilerFactory` | `detectCompilerName()` / `createByName()` / `autoDetect()` 选择后端 |
| `CompilerBackend`(接口) | 定义编译/链接命令抽象 |
| `GccLikeBackend` | GCC / Clang 共用逻辑 |
| `Gcc` / `Clang` | 具体后端 |
| `Msvc` | MSVC(Windows)后端 |
`detectPlatform()` 先建平台,再探测编译器名,最后 `CompilerFactory::createByName()` 生成后端;若失败则告警并回退到旧逻辑(`initializeNewArchitecture()`)。
Sources: [src/Translator.php](src/Translator.php#L164-L190) · [src/Translator.php](src/Translator.php#L538-L560)
## 命令生成
后端只负责「给定源/目标/选项 → 返回命令字符串」。真实的进程执行在 [构建流水线](build-pipeline) 的 `NativeBuilder` 中。例如:
- `compileCommand()` / `linkCommand()` 产出可复现的命令行;
- 命令行本身被写进**缓存键**,确保编译参数变化能触发重编译。
Sources: [src/Translator.php](src/Translator.php#L1408-L1417) · [src/Translator.php](src/Translator.php#L1644-L1647)
## 预编译头
后端是否支持 PCH 由 `supportsPrecompiledHeaders()` 决定;支持的会在构建期预编译 `phpx.h` 等全局头,显著加速后续编译单元。
Sources: [src/Translator.php](src/Translator.php#L1449-L1487) · [src/Build/PrecompiledHeaderManager.php](src/Build/PrecompiledHeaderManager.php)
## 相关阅读
- 命令如何被执行与排队 → [构建流水线](build-pipeline)
- 缓存如何依赖编译命令 → [增量编译缓存](incremental-cache)
- 平台差异(Windows SDK / PHP lib) → [跨平台支持](platform)
</blog>