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.
 
 

56 lines
2.5 KiB

<blog>
# 构建流水线
`Translator::compile()` 把生成的 C++ 与 phpx 运行时源文件编译为对象文件,`build()` 再链接成最终产物。两者都委托给 `src/Build/NativeBuilder`,它负责真正派发编译/链接进程。
Sources: [src/Translator.php](src/Translator.php#L1419-L1447) · [src/Translator.php](src/Translator.php#L1649-L1680)
## compile() 步骤
```mermaid
flowchart TD
A["compile(sourceFiles)"] --> B["追加 phpx misc 源\nfiber_generator / helper / main / cli_title"]
B --> C["preparePhpXPrecompiledHeader()"]
C --> D["compileResourceFile() (Windows)"]
D --> E{"支持 pcntl 且 -j>1?"}
E -- "否" --> F["compileSourceFile() 串行"]
E -- "是" --> G["compileWithPcntl() 并行"]
```
phpx misc 源(`typephp_fiber_generator.cc`、`typephp_helper.cc`、`typephp_main.cc`、`php_cli_process_title.c`、`ps_title.c`)按模式追加:embed 加 `typephp_main.cc`,bin 加 cli 标题源。
Sources: [src/Translator.php](src/Translator.php#L1423-L1434)
## 并行编译
`compileWithPcntl()` 在 Unix/Linux/macOS 用 `pcntl_fork` 多进程编译,进度条实时刷新;无 `pcntl` 扩展时自动降级为串行。每个 `compileFile()` 调用前都会先查 [增量缓存](incremental-cache),命中则跳过。
Sources: [src/Translator.php](src/Translator.php#L1584-L1637) · [src/Translator.php](src/Translator.php#L1349-L1392)
## NativeBuilder 与命令选项
| 组件 | 职责 |
|---|---|
| `NativeBuilder` | 真正执行 `compile` / `link`,返回 `command`/`status`/`output` |
| `NativeCommandOptionsTrait` | 汇总编译/链接选项 |
| `CompileOptions` / `LinkOptions` / `CommandOptions` | 选项数据对象 |
| `FileScanner` | 识别原生源文件(C/C++/汇编/ObjC) |
| `SourceCompileQueue` / `SourcePipelineTrait` | 编译队列与流水线调度 |
| `ResourceCompilationTrait` | Windows `.rc``.res` |
Sources: [src/Build/NativeBuilder.php](src/Build/NativeBuilder.php) · [src/Build/](src/Build/)
## build() 链接
`build()``NativeBuilder::link()`,Windows 模式下把 `.res` 资源文件并入目标;链接失败或产物未生成则 `error()` 退出。
Sources: [src/Translator.php](src/Translator.php#L1649-L1680) · [src/Translator.php](src/Translator.php#L1653-L1659)
## 相关阅读
- 后端如何产出命令 → [编译器后端](backend)
- 缓存如何避免重复编译 → [增量编译缓存](incremental-cache)
- 平台相关的链接差异 → [跨平台支持](platform)
</blog>