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.
2.3 KiB
2.3 KiB
编译模式
TypePHP 通过 -m / --mode 支持三种产物形态,由 Translator::setBuildMode() 归一化。三种模式共享同一套 PHP→C++ 翻译,区别只在于入口约定与链接目标。
Sources: src/Translator.php · docs/COMPILATION_MODES.md
三种模式对比
| 模式 | 参数 | 产物 | 入口要求 | 典型用途 |
|---|---|---|---|---|
| 二进制 | bin(默认) |
独立可执行文件 | 必须定义 main() |
CLI 工具、独立服务 |
| 共享库 | lib |
.so / .dll / .dylib |
不需要 main() |
供其它程序调用的逻辑库 |
| 扩展 | ext |
PHP 扩展 .so |
不需要 main() |
嵌入现有 PHP 应用(php-fpm 等) |
bin 是 binary/cli 的别名;lib 涵盖 library/shared/dll/dylib/so;ext 等价于 extension。未知值会报错退出。
Sources: src/Translator.php
二进制模式(embed)
bin 模式在 genExtension() 中嵌入 cli_set_process_title / cli_get_process_title 内置函数,并在 RINIT 阶段用 php::eval 直接执行 main()(带 $argc/$argv 或空参数两种签名)。
flowchart LR
A["main()"] --> B["RINIT: php_app_init()"]
B --> C["php::eval('main($argc,$argv)')"]
C --> D["RSHUTDOWN: php_app_clean()"]
Sources: src/Translator.php · src/Translator.php
共享库模式
库模式下,编译器会:
- 对
exported函数加上TYPEPHP_<NAME>_API导出宏; - 生成
<target>.stub.php导入桩,供其它 TypePHP 工程按库调用; - 在 Linux 上自动给目标名加
lib前缀。
Sources: src/Translator.php · [src/Translator.php#L1736-L1738) · src/Translator.php
扩展模式
ext 模式会跳过 main(),使用 ZEND_GET_MODULE() 注册 Zend 模块,并在 RSHUTDOWN 清空函数/类/属性表以支持重复加载。
Sources: src/Translator.php · src/Translator.php